casacore
Loading...
Searching...
No Matches
BitVector.h
Go to the documentation of this file.
1// # BitVector.h: Bit vectors of any size
2// # Copyright (C) 1993,1994,1995,1999,2000,2001
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_BITVECTOR_H
27#define CASA_BITVECTOR_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Containers/Block.h>
32#include <casacore/casa/Utilities/Assert.h>
33#include <casacore/casa/iosfwd.h>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// # Forward Declarations
38class BitVectorHelper;
39
40// The size of a unsigned Integer ( assumes 8-bit char )
41const uInt WORDSIZE = sizeof(uInt) * 8;
42
43// <summary>
44// Bit vectors of any size
45// </summary>
46
47// <use visibility=export>
48
49// <reviewed reviewer="Friso Olnon" date="1995/03/13" tests="tBitVector" demos="">
50
51// <etymology>
52// A variable utilized as a discrete collection of bits is referred
53// to as a bit vector.
54// </etymology>
55
56// <synopsis>
57// Bit vectors are an efficent method of keeping <em>True/False</em>
58// information on a set of items or conditions. Class BitVector
59// provides functions to manipulate individual bits in the vector and
60// to perform logical operations on whole bit vectors.
61// </synopsis>
62
63// <example>
64// <srcblock>
65// // Create a bit vector with 20 bits (and set them all to False).
66// BitVector bv (20, False);
67//
68// // Change some individual bits:
69// // Turn On (make True) bit 19.
70// bv.setBit (19);
71// // Turn Off (make False) bit 12 (superfluous here).
72// bv.clearBit (12);
73// // Toggle bit 5 (here: change value from 0 (False) to 1 (True)).
74// bv.toggleBit (5)
75// // Another way of setting a bit using the index operator.
76// bv[0] = True;
77// // Assign the value of bit 0 to bit 1 (in three ways).
78// bv[1] = bv.getBit(0);
79// bv[1] = bv[0];
80// bv.putBit (1, bv.getBit(0));
81//
82// // Show the bit vector size and its value on standard output.
83// cout << "Size of bit vector: "<< b.nbits() <<"\n";
84// cout << "Value of bit vector: "<< bv <<"\n";
85//
86// // Perform logical operations on bit vectors.
87// // Create two more bit vectors.
88// BitVector bv2 (40, False);
89// BitVector bv3 (40, True);
90// // bitwise OR
91// bv = bv2 | bv3;
92// // bitwise AND
93// bv = bv2 & bv3;
94// // bitwise XOR
95// bv = bv2 ^ bv3;
96// // bitwise NOT
97// bv = ~bv2;
98//
99// // Reset all bits to False, and then to True
100// bv = False;
101// bv.set (True);
102// // Change the vector's size to 10 (and copy the old values).
103// bv.resize (10);
104// // Change back to original size and set all bits to True.
105// void bv.resize (size, True, False);
106// </srcblock>
107// </example>
108
110 public:
111 // BitVectorHelper is a helper class.
112 friend class BitVectorHelper;
113
114 // Create a bit vector of length 0.
116
117 // Create a bit vector with <src>length</src> bits
118 // and set all bits to to the specified state.
120
121 // Copy constructor (copy semantics).
122 BitVector(const BitVector& that);
123
124 // Delete the bit vector.
126
127 // Assignment (copy semantics).
129
130 // Set all bits to the given state.
132
133 // Return the number of bits in the bitvector.
134 uInt nbits() const;
135
136 // Set a bit at the given position (0-relative).
137 // In debug-mode an exception is thrown when the position is invalid.
138 void setBit(uInt pos);
139
140 // Clear a bit at the given position (0-relative).
141 // In debug-mode an exception is thrown when the position is invalid.
142 void clearBit(uInt pos);
143
144 // Toggle a bit at the given position (0-relative).
145 // It returns the original state.
146 // In debug-mode an exception is thrown when the position is invalid.
148
149 // Get a bit at the given position (0-relative).
150 // In debug-mode an exception is thrown when the position is invalid.
151 Bool getBit(uInt pos) const;
152
153 // Set a bit at the given position (0-relative) to the given state.
154 // In debug-mode an exception is thrown when the position is invalid.
155 void putBit(uInt pos, Bool state);
156
157 // Index operator to access the specified bit.
158 // In debug-mode an exception is thrown when the position is invalid.
159 // <group>
160 Bool operator[](uInt pos) const;
162 // </group>
163
164 // Logical operations on whole bit vectors.
165 // The binary operators <src>&</src> (bitwise
166 // AND), <src>|</src> (bitwise OR) and <src>^</src> (bitwise XOR),
167 // and the unary operator <src>~</src> (bitwise NOT) are provided.
168 // An exception is thrown if the lengths of the vectors differ.
169 // <group>
170 BitVector operator&(const BitVector& that) const;
171 BitVector operator|(const BitVector& that) const;
172 BitVector operator^(const BitVector& that) const;
174 // </group>
175
176 // Logical in-place operations on whole bit vectors.
177 // The binary operators <src>&</src> (bitwise
178 // AND), <src>|</src> (bitwise OR) and <src>^</src> (bitwise XOR),
179 // and the unary operator <src>reverse</src> (bitwise NOT) are provided.
180 // An exception is thrown if the lengths of the vectors differ.
181 // <group>
182 void operator&=(const BitVector& that);
183 void operator|=(const BitVector& that);
184 void operator^=(const BitVector& that);
185 void reverse();
186 // </group>
187
188 // Returns True if all bits are equal.
189 // An exception is thrown if the lengths of the vectors differ.
190 Bool operator==(const BitVector& that) const;
191
192 // Returns True if a bit differs.
193 // An exception is thrown if the lengths of the vectors differ.
194 Bool operator!=(const BitVector& that) const;
195
196 // Resize the bit vector to the new length.
197 // By default the original bits are copied.
198 // The remaining bits (or all bits in case of no copy) are
199 // set the the given state.
201
202 // Set all bits of the bit vector to the specified state.
203 void set(Bool state);
204
205 // Set <src>length</src> bits starting at the start position
206 // (0-relative) to the given state.
207 // An exception is thrown if start+length exceeds the length
208 // of the vector.
209 void set(uInt start, uInt length, Bool state);
210
211 // Copy <src>length</src> bits starting at thatStart in the
212 // other BitVector to this BitVector starting at thisStart.
213 void copy(uInt thisStart, uInt length, const BitVector& that, uInt thatStart);
214
215 // Write a representation of the bit vector (a list of
216 // <em>zeros</em> and <em>ones</em> enclosed in square
217 // parentheses) to ostream.
218 friend ostream& operator<<(ostream&, const BitVector& vector);
219
220 private:
221 // Number of bits in the BitVector object.
223
224 // Pointer to the actual bit vector, stored as a contiguous
225 // sequence of one or more unsigned integers.
227};
228
229// <summary> Helper class for BitVector </summary>
230// <use visibility=local>
231// <reviewed reviewer="Friso Olnon" date="1995/03/13" tests="tBitVector" demos="">
232
233// <prerequisite>
234// <li> class <linkto class=BitVector>BitVector</linkto>
235// </prerequisite>
236
237// <synopsis>
238// Helper class for class <linkto class=BitVector>BitVector</linkto>.
239// For all practical purposes a BitVectorHelper object is the individual bit in
240// a bit vector. It is the object returned by the index operator of
241// BitVector.
242// </synopsis>
243
245 friend class BitVector;
246
247 public:
248 // Copy constructor has to be public.
249 BitVectorHelper(const BitVectorHelper& that);
250
251 // Set the bit to the state of the bit in the other BitVector.
252 // Thus assignment has not the usual copy semantics, but affects
253 // the underlying BitVector bit.
254 const BitVectorHelper& operator=(const BitVectorHelper& that) const;
255
256 // Set to a state.
257 const BitVectorHelper& operator=(Bool state) const;
258
259 // Defines the conversion from <src>BitVectorHelper</src> to
260 // <src>Bool</src>.
261 operator Bool() const;
262
263 private:
265
266 // Pointer back to the original vector.
268
269 // The constructor we actually use.
270 BitVectorHelper(uInt bitNumber, BitVector* vector);
271};
272
273inline void BitVector::setBit(uInt pos) {
275 uInt index = pos / WORDSIZE;
276 bits_p[index] |= (1 << (pos - index * WORDSIZE));
277}
278
279inline void BitVector::clearBit(uInt pos) {
281 uInt index = pos / WORDSIZE;
282 bits_p[index] &= (~(1 << (pos - index * WORDSIZE)));
283}
284
285inline Bool BitVector::operator[](uInt pos) const { return getBit(pos); }
286
287inline uInt BitVector::nbits() const { return size_p; }
288
290 : bitNumber_p(bitNumber), vecPtr_p(vector) {}
291
293
296
298 vecPtr_p->putBit(bitNumber_p, state);
299 return *this;
300}
301
302inline BitVectorHelper::operator Bool() const { return vecPtr_p->getBit(bitNumber_p); }
303
305 vecPtr_p->putBit(bitNumber_p, that.vecPtr_p->getBit(that.bitNumber_p));
306 return *this;
307}
308
309} // namespace casacore
310
311#endif
#define DebugAssert(expr, exception)
Definition Assert.h:161
Helper class for BitVector.
Definition BitVector.h:244
BitVector * vecPtr_p
Pointer back to the original vector.
Definition BitVector.h:267
BitVectorHelper(const BitVectorHelper &that)
Copy constructor has to be public.
Definition BitVector.h:294
const BitVectorHelper & operator=(const BitVectorHelper &that) const
Set the bit to the state of the bit in the other BitVector.
Definition BitVector.h:304
void operator^=(const BitVector &that)
BitVector operator~() const
Bool operator!=(const BitVector &that) const
Returns True if a bit differs.
Bool toggleBit(uInt pos)
Toggle a bit at the given position (0-relative).
void set(Bool state)
Set all bits of the bit vector to the specified state.
BitVector(uInt length, Bool state)
Create a bit vector with length bits and set all bits to to the specified state.
BitVector & operator=(const BitVector &that)
Assignment (copy semantics).
void setBit(uInt pos)
Set a bit at the given position (0-relative).
Definition BitVector.h:273
uInt nbits() const
Return the number of bits in the bitvector.
Definition BitVector.h:287
uInt size_p
Number of bits in the BitVector object.
Definition BitVector.h:222
void clearBit(uInt pos)
Clear a bit at the given position (0-relative).
Definition BitVector.h:279
BitVector(const BitVector &that)
Copy constructor (copy semantics).
void putBit(uInt pos, Bool state)
Set a bit at the given position (0-relative) to the given state.
BitVector & operator=(Bool state)
Set all bits to the given state.
void operator|=(const BitVector &that)
BitVector operator&(const BitVector &that) const
Logical operations on whole bit vectors.
void resize(uInt length, Bool state=False, Bool copy=True)
Resize the bit vector to the new length.
friend ostream & operator<<(ostream &, const BitVector &vector)
Write a representation of the bit vector (a list of zeros and ones enclosed in square parentheses) to...
BitVector operator^(const BitVector &that) const
Bool getBit(uInt pos) const
Get a bit at the given position (0-relative).
void copy(uInt thisStart, uInt length, const BitVector &that, uInt thatStart)
Copy length bits starting at thatStart in the other BitVector to this BitVector starting at thisStart...
BitVector operator|(const BitVector &that) const
void operator&=(const BitVector &that)
Logical in-place operations on whole bit vectors.
Bool operator[](uInt pos) const
Index operator to access the specified bit.
Definition BitVector.h:285
Block< uInt > bits_p
Pointer to the actual bit vector, stored as a contiguous sequence of one or more unsigned integers.
Definition BitVector.h:226
void set(uInt start, uInt length, Bool state)
Set length bits starting at the start position (0-relative) to the given state.
Bool operator==(const BitVector &that) const
Returns True if all bits are equal.
~BitVector()
Delete the bit vector.
friend class BitVectorHelper
BitVectorHelper is a helper class.
Definition BitVector.h:112
BitVector()
Create a bit vector of length 0.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
const uInt WORDSIZE
The size of a unsigned Integer (assumes 8-bit char).
Definition BitVector.h:41
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41