svcore  1.9
ResizeableBitset.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef _RESIZEABLE_BITMAP_H_
17 #define _RESIZEABLE_BITMAP_H_
18 
19 #include <vector>
20 #include <stdint.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 
25 
26 public:
28  }
29  ResizeableBitset(size_t size) : m_bits(new std::vector<uint8_t>), m_size(size) {
30  m_bits->assign((size >> 3) + 1, 0);
31  }
33  m_bits = new std::vector<uint8_t>(*b.m_bits);
34  }
36  if (&b != this) return *this;
37  delete m_bits;
38  m_bits = new std::vector<uint8_t>(*b.m_bits);
39  return *this;
40  }
42  delete m_bits;
43  }
44 
45  void resize(size_t size) { // retaining existing data; not thread safe
46  size_t bytes = (size >> 3) + 1;
47  if (m_bits && bytes == m_bits->size()) return;
48  std::vector<uint8_t> *newbits = new std::vector<uint8_t>(bytes);
49  newbits->assign(bytes, 0);
50  if (m_bits) {
51  for (size_t i = 0; i < bytes && i < m_bits->size(); ++i) {
52  (*newbits)[i] = (*m_bits)[i];
53  }
54  delete m_bits;
55  }
56  m_bits = newbits;
57  m_size = size;
58  }
59 
60  bool get(size_t column) const {
61  return ((*m_bits)[column >> 3]) & (1u << (column & 0x07));
62  }
63 
64  void set(size_t column) {
65  ((*m_bits)[column >> 3]) |= (uint8_t(1) << (column & 0x07));
66  }
67 
68  void reset(size_t column) {
69  ((*m_bits)[column >> 3]) &= ~(uint8_t(1) << (column & 0x07));
70  }
71 
72  void copy(size_t source, size_t dest) {
73  get(source) ? set(dest) : reset(dest);
74  }
75 
76  bool isAllOff() const {
77  for (size_t i = 0; i < m_bits->size(); ++i) {
78  if ((*m_bits)[i]) return false;
79  }
80  return true;
81  }
82 
83  bool isAllOn() const {
84  for (size_t i = 0; i + 1 < m_bits->size(); ++i) {
85  if ((*m_bits)[i] != 0xff) return false;
86  }
87  for (size_t i = (m_size / 8) * 8; i < m_size; ++i) {
88  if (!get(i)) return false;
89  }
90  return true;
91  }
92 
93  size_t size() const {
94  return m_size;
95  }
96 
97 private:
98  std::vector<uint8_t> *m_bits;
99  size_t m_size;
100 };
101 
102 
103 #endif
104 
void reset(size_t column)
size_t size() const
ResizeableBitset(size_t size)
void resize(size_t size)
bool get(size_t column) const
bool isAllOff() const
ResizeableBitset(const ResizeableBitset &b)
void set(size_t column)
void copy(size_t source, size_t dest)
std::vector< uint8_t > * m_bits
ResizeableBitset & operator=(const ResizeableBitset &b)
bool isAllOn() const