2 // This file is part of the aMule Project.
4 // Copyright (c) 2009-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2009-2011 Stu Redman ( sturedman@amule.org )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
42 ~BitVector() { clear(); }
45 uint32
size() const { return m_bits
; }
48 bool empty() const { return m_bits
== 0; }
51 bool get(uint32 idx
) const
57 return (m_vector
[idx
/ 8] & s_posMask
[idx
& 7]) != 0;
61 void set(uint32 idx
, bool value
)
67 uint32 bidx
= idx
/ 8;
69 m_vector
[bidx
] = m_vector
[bidx
] | s_posMask
[idx
& 7];
70 // If it was not all true before, then we don't know now.
75 m_vector
[bidx
] = m_vector
[bidx
] & s_negMask
[idx
& 7];
80 // set number of bits to zero and free memory
90 // set number of bits and initialize them with value
91 void setsize(uint32 newsize
, bool value
)
98 m_bytes
= newsize
/ 8;
103 m_vector
= new uint8
[m_bytes
];
104 memset(m_vector
, value
? 0xFF : 0, m_bytes
);
105 m_allTrue
= value
? 1 : 0;
108 // are all bits true ?
111 if (m_allTrue
== 2) {
112 // don't know, have to check
113 bool foundFalse
= false;
114 uint32 lastByte
= m_bytes
;
116 // uneven: check bits of last byte individually
118 for (uint32 i
= m_bits
& 0xfffffff8; !foundFalse
&& i
< m_bits
; i
++) {
119 foundFalse
= !get(i
);
123 for (uint32 i
= 0; !foundFalse
&& i
< lastByte
; i
++) {
124 foundFalse
= m_vector
[i
] != 0xff;
126 // This is really just a caching of information,
127 // so m_allTrue is mutable and AllTrue() still const.
128 m_allTrue
= foundFalse
? 0 : 1;
130 return m_allTrue
== 1;
133 // set all bits to true
134 void SetAllTrue() { if (m_bytes
) { memset(m_vector
, 0xFF, m_bytes
); } }
136 // handling of the internal buffer (for EC)
138 uint32
SizeBuffer() const { return m_bytes
; }
140 const void* GetBuffer() const { return m_vector
; }
142 void SetBuffer(const void* src
) { memcpy(m_vector
, src
, m_bytes
); m_allTrue
= 2; }
145 uint32 m_bits
; // number of bits
146 uint32 m_bytes
; // number of bytes in the vector
147 uint8
* m_vector
; // the storage
148 mutable uint8 m_allTrue
;// All true ? 0: no 1: yes 2: don't know
149 static const uint8 s_posMask
[]; // = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; implemented in OtherFunctions.cpp
150 static const uint8 s_negMask
[]; // = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};