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