casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: aips2-request@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 //# $Id$
27 
28 #ifndef CASA_BITVECTOR_H
29 #define CASA_BITVECTOR_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
35 #include <casacore/casa/iosfwd.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 //# Forward Declarations
40 class BitVectorHelper;
41 
42 // The size of a unsigned Integer ( assumes 8-bit char )
43 const uInt WORDSIZE = sizeof(uInt)*8;
44 
45 // <summary>
46 // Bit vectors of any size
47 // </summary>
48 
49 // <use visibility=export>
50 
51 // <reviewed reviewer="Friso Olnon" date="1995/03/13" tests="tBitVector" demos="">
52 
53 // <etymology>
54 // A variable utilized as a discrete collection of bits is referred
55 // to as a bit vector.
56 // </etymology>
57 
58 // <synopsis>
59 // Bit vectors are an efficent method of keeping <em>True/False</em>
60 // information on a set of items or conditions. Class BitVector
61 // provides functions to manipulate individual bits in the vector and
62 // to perform logical operations on whole bit vectors.
63 // </synopsis>
64 
65 // <example>
66 // <srcblock>
67 // // Create a bit vector with 20 bits (and set them all to False).
68 // BitVector bv (20, False);
69 //
70 // // Change some individual bits:
71 // // Turn On (make True) bit 19.
72 // bv.setBit (19);
73 // // Turn Off (make False) bit 12 (superfluous here).
74 // bv.clearBit (12);
75 // // Toggle bit 5 (here: change value from 0 (False) to 1 (True)).
76 // bv.toggleBit (5)
77 // // Another way of setting a bit using the index operator.
78 // bv[0] = True;
79 // // Assign the value of bit 0 to bit 1 (in three ways).
80 // bv[1] = bv.getBit(0);
81 // bv[1] = bv[0];
82 // bv.putBit (1, bv.getBit(0));
83 //
84 // // Show the bit vector size and its value on standard output.
85 // cout << "Size of bit vector: "<< b.nbits() <<"\n";
86 // cout << "Value of bit vector: "<< bv <<"\n";
87 //
88 // // Perform logical operations on bit vectors.
89 // // Create two more bit vectors.
90 // BitVector bv2 (40, False);
91 // BitVector bv3 (40, True);
92 // // bitwise OR
93 // bv = bv2 | bv3;
94 // // bitwise AND
95 // bv = bv2 & bv3;
96 // // bitwise XOR
97 // bv = bv2 ^ bv3;
98 // // bitwise NOT
99 // bv = ~bv2;
100 //
101 // // Reset all bits to False, and then to True
102 // bv = False;
103 // bv.set (True);
104 // // Change the vector's size to 10 (and copy the old values).
105 // bv.resize (10);
106 // // Change back to original size and set all bits to True.
107 // void bv.resize (size, True, False);
108 // </srcblock>
109 // </example>
110 
111 
113 {
114 public:
115  // BitVectorHelper is a helper class.
116  friend class BitVectorHelper;
117 
118  // Create a bit vector of length 0.
119  BitVector ();
120 
121  // Create a bit vector with <src>length</src> bits
122  // and set all bits to to the specified state.
123  BitVector (uInt length, Bool state);
124 
125  // Copy constructor (copy semantics).
126  BitVector (const BitVector& that);
127 
128  // Delete the bit vector.
129  ~BitVector ();
130 
131  // Assignment (copy semantics).
132  BitVector& operator= (const BitVector& that);
133 
134  // Set all bits to the given state.
135  BitVector& operator= (Bool state);
136 
137  // Return the number of bits in the bitvector.
138  uInt nbits() const;
139 
140  // Set a bit at the given position (0-relative).
141  // In debug-mode an exception is thrown when the position is invalid.
142  void setBit (uInt pos);
143 
144  // Clear a bit at the given position (0-relative).
145  // In debug-mode an exception is thrown when the position is invalid.
146  void clearBit (uInt pos);
147 
148  // Toggle a bit at the given position (0-relative).
149  // It returns the original state.
150  // In debug-mode an exception is thrown when the position is invalid.
151  Bool toggleBit (uInt pos);
152 
153  // Get a bit at the given position (0-relative).
154  // In debug-mode an exception is thrown when the position is invalid.
155  Bool getBit (uInt pos) const;
156 
157  // Set a bit at the given position (0-relative) to the given state.
158  // In debug-mode an exception is thrown when the position is invalid.
159  void putBit (uInt pos, Bool state);
160 
161  // Index operator to access the specified bit.
162  // In debug-mode an exception is thrown when the position is invalid.
163  // <group>
164  Bool operator[] (uInt pos) const;
166  // </group>
167 
168  // Logical operations on whole bit vectors.
169  // The binary operators <src>&</src> (bitwise
170  // AND), <src>|</src> (bitwise OR) and <src>^</src> (bitwise XOR),
171  // and the unary operator <src>~</src> (bitwise NOT) are provided.
172  // An exception is thrown if the lengths of the vectors differ.
173  // <group>
174  BitVector operator& (const BitVector& that) const;
175  BitVector operator| (const BitVector& that) const;
176  BitVector operator^ (const BitVector& that) const;
177  BitVector operator~ () const;
178  // </group>
179 
180  // Logical in-place operations on whole bit vectors.
181  // The binary operators <src>&</src> (bitwise
182  // AND), <src>|</src> (bitwise OR) and <src>^</src> (bitwise XOR),
183  // and the unary operator <src>reverse</src> (bitwise NOT) are provided.
184  // An exception is thrown if the lengths of the vectors differ.
185  // <group>
186  void operator&= (const BitVector& that);
187  void operator|= (const BitVector& that);
188  void operator^= (const BitVector& that);
189  void reverse ();
190  // </group>
191 
192  // Returns True if all bits are equal.
193  // An exception is thrown if the lengths of the vectors differ.
194  Bool operator== (const BitVector& that) const;
195 
196  // Returns True if a bit differs.
197  // An exception is thrown if the lengths of the vectors differ.
198  Bool operator!= (const BitVector& that) const;
199 
200  // Resize the bit vector to the new length.
201  // By default the original bits are copied.
202  // The remaining bits (or all bits in case of no copy) are
203  // set the the given state.
204  void resize (uInt length, Bool state=False, Bool copy=True);
205 
206  // Set all bits of the bit vector to the specified state.
207  void set (Bool state);
208 
209  // Set <src>length</src> bits starting at the start position
210  // (0-relative) to the given state.
211  // An exception is thrown if start+length exceeds the length
212  // of the vector.
213  void set (uInt start, uInt length, Bool state);
214 
215  // Copy <src>length</src> bits starting at thatStart in the
216  // other BitVector to this BitVector starting at thisStart.
217  void copy (uInt thisStart, uInt length, const BitVector& that,
218  uInt thatStart);
219 
220  // Write a representation of the bit vector (a list of
221  // <em>zeros</em> and <em>ones</em> enclosed in square
222  // parentheses) to ostream.
223  friend ostream& operator<< (ostream&, const BitVector& vector);
224 
225 private:
226  // Number of bits in the BitVector object.
228 
229  // Pointer to the actual bit vector, stored as a contiguous
230  // sequence of one or more unsigned integers.
232 };
233 
234 
235 
236 // <summary> Helper class for BitVector </summary>
237 // <use visibility=local>
238 // <reviewed reviewer="Friso Olnon" date="1995/03/13" tests="tBitVector" demos="">
239 
240 // <prerequisite>
241 // <li> class <linkto class=BitVector>BitVector</linkto>
242 // </prerequisite>
243 
244 // <synopsis>
245 // Helper class for class <linkto class=BitVector>BitVector</linkto>.
246 // For all practical purposes a BitVectorHelper object is the individual bit in
247 // a bit vector. It is the object returned by the index operator of
248 // BitVector.
249 // </synopsis>
250 
252 {
253 friend class BitVector;
254 
255 public:
256  // Copy constructor has to be public.
257  BitVectorHelper (const BitVectorHelper& that);
258 
259  // Set the bit to the state of the bit in the other BitVector.
260  // Thus assignment has not the usual copy semantics, but affects
261  // the underlying BitVector bit.
262  const BitVectorHelper& operator= (const BitVectorHelper& that) const;
263 
264  // Set to a state.
265  const BitVectorHelper& operator= (Bool state) const;
266 
267  // Defines the conversion from <src>BitVectorHelper</src> to
268  // <src>Bool</src>.
269  operator Bool() const;
270 
271 private:
273 
274  // Pointer back to the original vector.
276 
277  // The constructor we actually use.
278  BitVectorHelper (uInt bitNumber, BitVector* vector);
279 };
280 
281 
282 
283 inline void BitVector::setBit (uInt pos)
284 {
285  DebugAssert (pos < size_p, AipsError);
286  uInt index = pos/WORDSIZE;
287  bits_p[index] |= (1 << (pos - index*WORDSIZE));
288 }
289 
290 inline void BitVector::clearBit (uInt pos)
291 {
292  DebugAssert (pos < size_p, AipsError);
293  uInt index = pos/WORDSIZE;
294  bits_p[index] &= (~ (1 << (pos - index*WORDSIZE)));
295 }
296 
298 {
299  return getBit (pos);
300 }
301 
302 inline uInt BitVector::nbits() const
303 {
304  return size_p;
305 }
306 
307 
309 : bitNumber_p (bitNumber),
310  vecPtr_p (vector)
311 {}
312 
314 {
315  return BitVectorHelper (pos, this);
316 }
317 
319 : bitNumber_p (that.bitNumber_p),
320  vecPtr_p (that.vecPtr_p)
321 {}
322 
324 {
325  vecPtr_p->putBit (bitNumber_p, state);
326  return *this;
327 }
328 
329 inline BitVectorHelper::operator Bool() const
330 {
331  return vecPtr_p->getBit (bitNumber_p);
332 }
333 
334 inline const BitVectorHelper& BitVectorHelper::operator=
335  (const BitVectorHelper& that) const
336 {
337  vecPtr_p->putBit (bitNumber_p, that.vecPtr_p->getBit (that.bitNumber_p));
338  return *this;
339 }
340 
341 
342 
343 
344 } //# NAMESPACE CASACORE - END
345 
346 #endif
347 
BitVector operator^(const BitVector &that) const
~BitVector()
Delete the bit vector.
void operator^=(const BitVector &that)
uInt size_p
Number of bits in the BitVector object.
Definition: BitVector.h:227
Bool operator!=(const BitVector &that) const
Returns True if a bit differs.
void putBit(uInt pos, Bool state)
Set a bit at the given position (0-relative) to the given state.
BitVector operator&(const BitVector &that) const
Logical operations on whole bit vectors.
const uInt WORDSIZE
The size of a unsigned Integer (assumes 8-bit char)
Definition: BitVector.h:43
Bit vectors of any size.
Definition: BitVector.h:112
BitVector operator|(const BitVector &that) const
uInt nbits() const
Return the number of bits in the bitvector.
Definition: BitVector.h:302
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...
Bool toggleBit(uInt pos)
Toggle a bit at the given position (0-relative).
BitVector & operator=(const BitVector &that)
Assignment (copy semantics).
BitVector operator~() const
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...
void operator&=(const BitVector &that)
Logical in-place operations on whole bit vectors.
void setBit(uInt pos)
Set a bit at the given position (0-relative).
Definition: BitVector.h:283
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
#define DebugAssert(expr, exception)
Definition: Assert.h:185
BitVectorHelper(const BitVectorHelper &that)
Copy constructor has to be public.
Definition: BitVector.h:318
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
Bool operator==(const BitVector &that) const
Returns True if all bits are equal.
friend class BitVectorHelper
BitVectorHelper is a helper class.
Definition: BitVector.h:116
Bool operator[](uInt pos) const
Index operator to access the specified bit.
Definition: BitVector.h:297
const Bool False
Definition: aipstype.h:44
void set(Bool state)
Set all bits of the bit vector to the specified state.
const BitVectorHelper & operator=(const BitVectorHelper &that) const
Set the bit to the state of the bit in the other BitVector.
Definition: BitVector.h:335
Bool getBit(uInt pos) const
Get a bit at the given position (0-relative).
Base class for all Casacore library errors.
Definition: Error.h:134
Block< uInt > bits_p
Pointer to the actual bit vector, stored as a contiguous sequence of one or more unsigned integers...
Definition: BitVector.h:231
BitVector()
Create a bit vector of length 0.
void clearBit(uInt pos)
Clear a bit at the given position (0-relative).
Definition: BitVector.h:290
void resize(uInt length, Bool state=False, Bool copy=True)
Resize the bit vector to the new length.
Helper class for BitVector.
Definition: BitVector.h:251
BitVector * vecPtr_p
Pointer back to the original vector.
Definition: BitVector.h:275
const Bool True
Definition: aipstype.h:43
void operator|=(const BitVector &that)
unsigned int uInt
Definition: aipstype.h:51
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42