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
109
111{
112public:
113 // BitVectorHelper is a helper class.
114 friend class BitVectorHelper;
115
116 // Create a bit vector of length 0.
118
119 // Create a bit vector with <src>length</src> bits
120 // and set all bits to to the specified state.
122
123 // Copy constructor (copy semantics).
124 BitVector (const BitVector& that);
125
126 // Delete the bit vector.
128
129 // Assignment (copy semantics).
131
132 // Set all bits to the given state.
134
135 // Return the number of bits in the bitvector.
136 uInt nbits() const;
137
138 // Set a bit at the given position (0-relative).
139 // In debug-mode an exception is thrown when the position is invalid.
140 void setBit (uInt pos);
141
142 // Clear a bit at the given position (0-relative).
143 // In debug-mode an exception is thrown when the position is invalid.
144 void clearBit (uInt pos);
145
146 // Toggle a bit at the given position (0-relative).
147 // It returns the original state.
148 // In debug-mode an exception is thrown when the position is invalid.
150
151 // Get a bit at the given position (0-relative).
152 // In debug-mode an exception is thrown when the position is invalid.
153 Bool getBit (uInt pos) const;
154
155 // Set a bit at the given position (0-relative) to the given state.
156 // In debug-mode an exception is thrown when the position is invalid.
157 void putBit (uInt pos, Bool state);
158
159 // Index operator to access the specified bit.
160 // In debug-mode an exception is thrown when the position is invalid.
161 // <group>
162 Bool operator[] (uInt pos) const;
164 // </group>
165
166 // Logical operations on whole bit vectors.
167 // The binary operators <src>&</src> (bitwise
168 // AND), <src>|</src> (bitwise OR) and <src>^</src> (bitwise XOR),
169 // and the unary operator <src>~</src> (bitwise NOT) are provided.
170 // An exception is thrown if the lengths of the vectors differ.
171 // <group>
172 BitVector operator& (const BitVector& that) const;
173 BitVector operator| (const BitVector& that) const;
174 BitVector operator^ (const BitVector& that) const;
176 // </group>
177
178 // Logical in-place operations on whole bit vectors.
179 // The binary operators <src>&</src> (bitwise
180 // AND), <src>|</src> (bitwise OR) and <src>^</src> (bitwise XOR),
181 // and the unary operator <src>reverse</src> (bitwise NOT) are provided.
182 // An exception is thrown if the lengths of the vectors differ.
183 // <group>
184 void operator&= (const BitVector& that);
185 void operator|= (const BitVector& that);
186 void operator^= (const BitVector& that);
187 void reverse ();
188 // </group>
189
190 // Returns True if all bits are equal.
191 // An exception is thrown if the lengths of the vectors differ.
192 Bool operator== (const BitVector& that) const;
193
194 // Returns True if a bit differs.
195 // An exception is thrown if the lengths of the vectors differ.
196 Bool operator!= (const BitVector& that) const;
197
198 // Resize the bit vector to the new length.
199 // By default the original bits are copied.
200 // The remaining bits (or all bits in case of no copy) are
201 // set the the given state.
203
204 // Set all bits of the bit vector to the specified state.
205 void set (Bool state);
206
207 // Set <src>length</src> bits starting at the start position
208 // (0-relative) to the given state.
209 // An exception is thrown if start+length exceeds the length
210 // of the vector.
211 void set (uInt start, uInt length, Bool state);
212
213 // Copy <src>length</src> bits starting at thatStart in the
214 // other BitVector to this BitVector starting at thisStart.
215 void copy (uInt thisStart, uInt length, const BitVector& that,
216 uInt thatStart);
217
218 // Write a representation of the bit vector (a list of
219 // <em>zeros</em> and <em>ones</em> enclosed in square
220 // parentheses) to ostream.
221 friend ostream& operator<< (ostream&, const BitVector& vector);
222
223private:
224 // Number of bits in the BitVector object.
226
227 // Pointer to the actual bit vector, stored as a contiguous
228 // sequence of one or more unsigned integers.
230};
231
232
233
234// <summary> Helper class for BitVector </summary>
235// <use visibility=local>
236// <reviewed reviewer="Friso Olnon" date="1995/03/13" tests="tBitVector" demos="">
237
238// <prerequisite>
239// <li> class <linkto class=BitVector>BitVector</linkto>
240// </prerequisite>
241
242// <synopsis>
243// Helper class for class <linkto class=BitVector>BitVector</linkto>.
244// For all practical purposes a BitVectorHelper object is the individual bit in
245// a bit vector. It is the object returned by the index operator of
246// BitVector.
247// </synopsis>
248
250{
251friend class BitVector;
252
253public:
254 // Copy constructor has to be public.
255 BitVectorHelper (const BitVectorHelper& that);
256
257 // Set the bit to the state of the bit in the other BitVector.
258 // Thus assignment has not the usual copy semantics, but affects
259 // the underlying BitVector bit.
260 const BitVectorHelper& operator= (const BitVectorHelper& that) const;
261
262 // Set to a state.
263 const BitVectorHelper& operator= (Bool state) const;
264
265 // Defines the conversion from <src>BitVectorHelper</src> to
266 // <src>Bool</src>.
267 operator Bool() const;
268
269private:
271
272 // Pointer back to the original vector.
274
275 // The constructor we actually use.
276 BitVectorHelper (uInt bitNumber, BitVector* vector);
277};
278
279
280
281inline void BitVector::setBit (uInt pos)
282{
284 uInt index = pos/WORDSIZE;
285 bits_p[index] |= (1 << (pos - index*WORDSIZE));
286}
287
288inline void BitVector::clearBit (uInt pos)
289{
291 uInt index = pos/WORDSIZE;
292 bits_p[index] &= (~ (1 << (pos - index*WORDSIZE)));
293}
294
296{
297 return getBit (pos);
298}
299
300inline uInt BitVector::nbits() const
301{
302 return size_p;
303}
304
305
307: bitNumber_p (bitNumber),
308 vecPtr_p (vector)
309{}
310
312{
313 return BitVectorHelper (pos, this);
314}
315
317: bitNumber_p (that.bitNumber_p),
318 vecPtr_p (that.vecPtr_p)
319{}
320
322{
323 vecPtr_p->putBit (bitNumber_p, state);
324 return *this;
325}
326
327inline BitVectorHelper::operator Bool() const
328{
329 return vecPtr_p->getBit (bitNumber_p);
330}
331
332inline const BitVectorHelper& BitVectorHelper::operator=
333 (const BitVectorHelper& that) const
334{
335 vecPtr_p->putBit (bitNumber_p, that.vecPtr_p->getBit (that.bitNumber_p));
336 return *this;
337}
338
339
340
341
342} //# NAMESPACE CASACORE - END
343
344#endif
345
#define DebugAssert(expr, exception)
Definition Assert.h:183
Helper class for BitVector.
Definition BitVector.h:250
BitVector * vecPtr_p
Pointer back to the original vector.
Definition BitVector.h:273
BitVectorHelper(const BitVectorHelper &that)
Copy constructor has to be public.
Definition BitVector.h:316
const BitVectorHelper & operator=(const BitVectorHelper &that) const
Set the bit to the state of the bit in the other BitVector.
Definition BitVector.h:333
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:281
uInt nbits() const
Return the number of bits in the bitvector.
Definition BitVector.h:300
uInt size_p
Number of bits in the BitVector object.
Definition BitVector.h:225
void clearBit(uInt pos)
Clear a bit at the given position (0-relative).
Definition BitVector.h:288
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.
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:295
Block< uInt > bits_p
Pointer to the actual bit vector, stored as a contiguous sequence of one or more unsigned integers.
Definition BitVector.h:229
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:114
BitVector()
Create a bit vector of length 0.
simple 1-D array
Definition Block.h:198
this file contains all the compiler specific defines
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