This is Unofficial EPICS BASE Doxygen Site
epics::pvData::BitSet Class Reference

A vector of bits. More...

#include "bitSet.h"

+ Inheritance diagram for epics::pvData::BitSet:
+ Collaboration diagram for epics::pvData::BitSet:

Public Member Functions

 POINTER_DEFINITIONS (BitSet)
 
 BitSet ()
 
 BitSet (uint32 nbits)
 
virtual ~BitSet ()
 
BitSetflip (uint32 bitIndex)
 
BitSetset (uint32 bitIndex)
 
BitSetclear (uint32 bitIndex)
 
void set (uint32 bitIndex, bool value)
 
bool get (uint32 bitIndex) const
 
void clear ()
 
int32 nextSetBit (uint32 fromIndex) const
 
int32 nextClearBit (uint32 fromIndex) const
 
bool isEmpty () const
 
uint32 cardinality () const
 
uint32 size () const
 
bool logical_and (const BitSet &other) const
 Returns true if any bit is set in both *this and other. More...
 
bool logical_or (const BitSet &other) const
 Returns true if any bit is set in both *this or other. More...
 
BitSetoperator&= (const BitSet &set)
 
BitSetoperator|= (const BitSet &set)
 
BitSetoperator^= (const BitSet &set)
 
BitSetoperator= (const BitSet &set)
 
void swap (BitSet &set)
 Swap contents. More...
 
void or_and (const BitSet &set1, const BitSet &set2)
 
bool operator== (const BitSet &set) const
 
bool operator!= (const BitSet &set) const
 
virtual void serialize (ByteBuffer *buffer, SerializableControl *flusher) const
 
virtual void deserialize (ByteBuffer *buffer, DeserializableControl *flusher)
 
- Public Member Functions inherited from epics::pvData::Serializable
virtual ~Serializable ()
 

Static Public Member Functions

static BitSetPtr create (uint32 nbits)
 

Detailed Description

A vector of bits.

This class implements a vector of bits that grows as needed. Each component of the bit set has a bool value. The bits of a BitSet are indexed by nonnegative integers. Individual indexed bits can be examined, set, or cleared. One BitSet may be used to modify the contents of another BitSet through logical AND, logical inclusive OR, and logical exclusive OR operations.

By default, all bits in the set initially have the value false.

Every bit set has a current size, which is the number of bits of space currently in use by the bit set. Note that the size is related to the implementation of a bit set, so it may change with implementation. The length of a bit set relates to logical length of a bit set and is defined independently of implementation.

A BitSet is not safe for multithreaded use without external synchronization.

Based on Java implementation.

Since
7.0.0 Many methods return BitSet& to facilite method chaining.

Definition at line 56 of file bitSet.h.

Constructor & Destructor Documentation

epics::pvData::BitSet::BitSet ( )

Creates a new bit set. All bits are initially false.

Definition at line 51 of file bitSet.cpp.

51 {}
epics::pvData::BitSet::BitSet ( uint32  nbits)

Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1. All bits are initially false.

Parameters
nbitsthe initial size of the bit set

Definition at line 53 of file bitSet.cpp.

54  {
55  words.reserve((nbits == 0) ? 1 : WORD_INDEX(nbits-1) + 1);
56  }
#define WORD_INDEX(bitn)
Definition: bitSet.cpp:36
epics::pvData::BitSet::~BitSet ( )
virtual

Destructor.

Definition at line 71 of file bitSet.cpp.

71 {}

Member Function Documentation

uint32 epics::pvData::BitSet::cardinality ( ) const

Returns the number of bits set to true in this BitSet.

Returns
the number of bits set to true in this BitSet

Definition at line 203 of file bitSet.cpp.

203  {
204  uint32 sum = 0;
205  for (uint32 i = 0; i < words.size(); i++)
206  sum += bitCount(words[i]);
207  return sum;
208  }
int i
Definition: scan.c:967
uint32_t uint32
Definition: pvType.h:99
BitSet & epics::pvData::BitSet::clear ( uint32  bitIndex)

Sets the bit specified by the index to false.

Parameters
bitIndexthe index of the bit to be cleared

Definition at line 112 of file bitSet.cpp.

112  {
113 
114  uint32 wordIdx = WORD_INDEX(bitIndex);
115  if (wordIdx < words.size()) {
116  words[wordIdx] &= ~(((uint64)1) << WORD_OFFSET(bitIndex));
117 
118  recalculateWordsInUse();
119  }
120  return *this;
121  }
#define WORD_OFFSET(bitn)
Definition: bitSet.cpp:38
#define WORD_INDEX(bitn)
Definition: bitSet.cpp:36
uint64_t uint64
Definition: pvType.h:103
uint32_t uint32
Definition: pvType.h:99
void epics::pvData::BitSet::clear ( )

Sets all of the bits in this BitSet to false.

Definition at line 136 of file bitSet.cpp.

136  {
137  words.clear();
138  }
BitSet::shared_pointer epics::pvData::BitSet::create ( uint32  nbits)
static

Definition at line 46 of file bitSet.cpp.

47  {
48  return BitSet::shared_pointer(new BitSet(nbits));
49  }
void epics::pvData::BitSet::deserialize ( ByteBuffer buffer,
DeserializableControl flusher 
)
virtual

Deserialize buffer.

Parameters
bufferserialization buffer.
flusherdeserialization control.

Implements epics::pvData::Serializable.

Definition at line 339 of file bitSet.cpp.

339  {
340 
341  uint32 bytes = static_cast<uint32>(SerializeHelper::readSize(buffer, control)); // in bytes
342 
343  size_t wordsInUse = (bytes + 7) / BYTES_PER_WORD;
344  words.resize(wordsInUse);
345 
346  if (wordsInUse == 0)
347  return;
348 
349  control->ensureData(bytes);
350 
351  uint32 i = 0;
352  uint32 longs = bytes / 8;
353  while (i < longs)
354  words[i++] = buffer->getLong();
355 
356  for (uint32 j = i; j < wordsInUse; j++)
357  words[j] = 0;
358 
359  for (uint32 remaining = (bytes - longs * 8), j = 0; j < remaining; j++)
360  words[i] |= (buffer->getByte() & 0xffLL) << (8 * j);
361 
362  recalculateWordsInUse(); // Sender shouldn't add extra zero bytes, but don't fail it it does
363  }
#define BYTES_PER_WORD
Definition: bitSet.cpp:29
int i
Definition: scan.c:967
static std::size_t readSize(ByteBuffer *buffer, DeserializableControl *control)
uint32_t uint32
Definition: pvType.h:99
BitSet & epics::pvData::BitSet::flip ( uint32  bitIndex)

Sets the bit at the specified index to the complement of its current value.

Parameters
bitIndexthe index of the bit to flip

Definition at line 92 of file bitSet.cpp.

92  {
93 
94  uint32 wordIdx = WORD_INDEX(bitIndex);
95  expandTo(wordIdx);
96 
97  words[wordIdx] ^= (((uint64)1) << WORD_OFFSET(bitIndex));
98 
99  recalculateWordsInUse();
100  return *this;
101  }
#define WORD_OFFSET(bitn)
Definition: bitSet.cpp:38
#define WORD_INDEX(bitn)
Definition: bitSet.cpp:36
uint64_t uint64
Definition: pvType.h:103
uint32_t uint32
Definition: pvType.h:99
bool epics::pvData::BitSet::get ( uint32  bitIndex) const

Returns the value of the bit with the specified index. The value is true if the bit with the index bitIndex is currently set in this BitSet; otherwise, the result is false.

Parameters
bitIndexthe bit index
Returns
the value of the bit with the specified index

Definition at line 130 of file bitSet.cpp.

130  {
131  uint32 wordIdx = WORD_INDEX(bitIndex);
132  return ((wordIdx < words.size())
133  && ((words[wordIdx] & (((uint64)1) << WORD_OFFSET(bitIndex))) != 0));
134  }
#define WORD_OFFSET(bitn)
Definition: bitSet.cpp:38
#define WORD_INDEX(bitn)
Definition: bitSet.cpp:36
uint64_t uint64
Definition: pvType.h:103
uint32_t uint32
Definition: pvType.h:99
bool epics::pvData::BitSet::isEmpty ( ) const

Returns true if this BitSet contains no bits that are set to true.

Returns
indicating whether this BitSet is empty

Definition at line 199 of file bitSet.cpp.

199  {
200  return words.empty();
201  }
bool epics::pvData::BitSet::logical_and ( const BitSet other) const

Returns true if any bit is set in both *this and other.

Definition at line 214 of file bitSet.cpp.

215  {
216  size_t nwords = std::min(words.size(), set.words.size());
217  for(size_t i=0; i<nwords; i++) {
218  if(words[i] & set.words[i])
219  return true;
220  }
221  return false;
222  }
int i
Definition: scan.c:967
#define min(x, y)
Definition: flexdef.h:78
bool epics::pvData::BitSet::logical_or ( const BitSet other) const

Returns true if any bit is set in both *this or other.

Definition at line 223 of file bitSet.cpp.

224  {
225  return !words.empty() || !set.words.empty();
226  }
int32 epics::pvData::BitSet::nextClearBit ( uint32  fromIndex) const

Returns the index of the first bit that is set to false that occurs on or after the specified starting index.

Parameters
fromIndexthe index to start checking from (inclusive)
Returns
the index of the next clear bit

Definition at line 181 of file bitSet.cpp.

181  {
182  // Neither spec nor implementation handle bitsets of maximal length.
183 
184  uint32 u = WORD_INDEX(fromIndex);
185  if (u >= words.size())
186  return fromIndex;
187 
188  uint64 word = ~words[u] & (WORD_MASK << (fromIndex % BITS_PER_WORD));
189 
190  while (true) {
191  if (word != 0)
192  return (u * BITS_PER_WORD) + numberOfTrailingZeros(word);
193  if (++u == words.size())
194  return words.size() * BITS_PER_WORD;
195  word = ~words[u];
196  }
197  }
#define WORD_MASK
Definition: bitSet.cpp:33
#define WORD_INDEX(bitn)
Definition: bitSet.cpp:36
uint64_t uint64
Definition: pvType.h:103
#define BITS_PER_WORD
Definition: bitSet.cpp:28
uint32_t uint32
Definition: pvType.h:99
int32 epics::pvData::BitSet::nextSetBit ( uint32  fromIndex) const

Returns the index of the first bit that is set to true that occurs on or after the specified starting index. If no such bit exists then -1 is returned.

To iterate over the true bits in a BitSet, use the following loop:

for (int32 i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
// operate on index i here
}
Parameters
fromIndexthe index to start checking from (inclusive)
Returns
the index of the next set bit, or -1 if there is no such bit

Definition at line 164 of file bitSet.cpp.

164  {
165 
166  uint32 u = WORD_INDEX(fromIndex);
167  if (u >= words.size())
168  return -1;
169 
170  uint64 word = words[u] & (WORD_MASK << (fromIndex % BITS_PER_WORD));
171 
172  while (true) {
173  if (word != 0)
174  return (u * BITS_PER_WORD) + numberOfTrailingZeros(word);
175  if (++u == words.size())
176  return -1;
177  word = words[u];
178  }
179  }
#define WORD_MASK
Definition: bitSet.cpp:33
#define WORD_INDEX(bitn)
Definition: bitSet.cpp:36
uint64_t uint64
Definition: pvType.h:103
#define BITS_PER_WORD
Definition: bitSet.cpp:28
uint32_t uint32
Definition: pvType.h:99
bool epics::pvData::BitSet::operator!= ( const BitSet set) const

Definition at line 310 of file bitSet.cpp.

311  {
312  return !(*this == set);
313  }
BitSet & epics::pvData::BitSet::operator&= ( const BitSet set)

Performs a bitwise AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if it both initially had the value true and the corresponding bit in the bit set argument also had the value true.

Parameters
seta bit set

Definition at line 228 of file bitSet.cpp.

228  {
229  // Check for self-assignment!
230  if (this == &set) return *this;
231 
232  // the result length will be <= the shorter of the two inputs
233  words.resize(std::min(words.size(), set.words.size()), 0);
234 
235  for(size_t i=0, e=words.size(); i<e; i++)
236  words[i] &= set.words[i];
237 
238  recalculateWordsInUse();
239  return *this;
240  }
int i
Definition: scan.c:967
#define min(x, y)
Definition: flexdef.h:78
BitSet & epics::pvData::BitSet::operator= ( const BitSet set)

Assignment operator.

Definition at line 269 of file bitSet.cpp.

269  {
270  // Check for self-assignment!
271  if (this != &set) {
272  words = set.words;
273  }
274  return *this;
275  }
bool epics::pvData::BitSet::operator== ( const BitSet set) const

Comparison operator.

Definition at line 294 of file bitSet.cpp.

295  {
296  if (this == &set)
297  return true;
298 
299  if (words.size() != set.words.size())
300  return false;
301 
302  // Check words in use by both BitSets
303  for (uint32 i = 0; i < words.size(); i++)
304  if (words[i] != set.words[i])
305  return false;
306 
307  return true;
308  }
int i
Definition: scan.c:967
uint32_t uint32
Definition: pvType.h:99
BitSet & epics::pvData::BitSet::operator^= ( const BitSet set)

Performs a bitwise XOR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if one of the following statements holds:

  • The bit initially has the value true, and the corresponding bit in the argument has the value false.
  • The bit initially has the value false, and the corresponding bit in the argument has the value true.
Parameters
seta bit set

Definition at line 257 of file bitSet.cpp.

257  {
258  // result length will <= the longer of the two inputs
259  words.resize(std::max(words.size(), set.words.size()), 0);
260 
261  for(size_t i=0, e=set.words.size(); i<e; i++)
262  words[i] ^= set.words[i];
263 
264  recalculateWordsInUse();
265  return *this;
266  }
#define max(x, y)
Definition: flexdef.h:81
int i
Definition: scan.c:967
BitSet & epics::pvData::BitSet::operator|= ( const BitSet set)

Performs a bitwise OR of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value true if and only if it either already had the value true or the corresponding bit in the bit set argument has the value true.

Parameters
seta bit set

Definition at line 242 of file bitSet.cpp.

242  {
243  // Check for self-assignment!
244  if (this == &set) return *this;
245 
246  // result length will be the same as the longer of the two inputs
247  words.resize(std::max(words.size(), set.words.size()), 0);
248 
249  // since we expand w/ zeros, then iterate using the size of the other vector
250  for(size_t i=0, e=set.words.size(); i<e; i++)
251  words[i] |= set.words[i];
252 
253  CHECK_POST();
254  return *this;
255  }
#define max(x, y)
Definition: flexdef.h:81
int i
Definition: scan.c:967
#define CHECK_POST()
Definition: bitSet.cpp:42
void epics::pvData::BitSet::or_and ( const BitSet set1,
const BitSet set2 
)

Perform AND operation on set1 and set2, and OR on result and this instance.

Parameters
set1
set2

Definition at line 282 of file bitSet.cpp.

282  {
283 
284  const size_t andlen = std::min(set1.words.size(), set2.words.size());
285  words.resize(std::max(words.size(), andlen), 0);
286 
287  // Perform logical AND on words in common
288  for (uint32 i = 0; i < andlen; i++)
289  words[i] |= (set1.words[i] & set2.words[i]);
290 
291  recalculateWordsInUse();
292  }
#define max(x, y)
Definition: flexdef.h:81
int i
Definition: scan.c:967
#define min(x, y)
Definition: flexdef.h:78
uint32_t uint32
Definition: pvType.h:99
epics::pvData::BitSet::POINTER_DEFINITIONS ( BitSet  )
void epics::pvData::BitSet::serialize ( ByteBuffer buffer,
SerializableControl flusher 
) const
virtual

Serialize field into given buffer.

Parameters
bufferserialization buffer.
flusherflush interface.

Implements epics::pvData::Serializable.

Definition at line 315 of file bitSet.cpp.

315  {
316 
317  uint32 n = words.size();
318  if (n == 0) {
319  SerializeHelper::writeSize(0, buffer, flusher);
320  return;
321  }
322  uint32 len = BYTES_PER_WORD * (n-1); // length excluding bits in the last word
323  // count non-zero bytes in the last word
324  for (uint64 x = words[n - 1]; x != 0; x >>= 8)
325  len++;
326 
327  SerializeHelper::writeSize(len, buffer, flusher);
328  flusher->ensureBuffer(len);
329 
330  n = len / 8;
331  for (uint32 i = 0; i < n; i++)
332  buffer->putLong(words[i]);
333 
334  if (n < words.size())
335  for (uint64 x = words[words.size() - 1]; x != 0; x >>= 8)
336  buffer->putByte((int8) (x & 0xff));
337  }
int8_t int8
Definition: pvType.h:75
#define BYTES_PER_WORD
Definition: bitSet.cpp:29
int i
Definition: scan.c:967
static void writeSize(std::size_t s, ByteBuffer *buffer, SerializableControl *flusher)
uint64_t uint64
Definition: pvType.h:103
uint32_t uint32
Definition: pvType.h:99
BitSet & epics::pvData::BitSet::set ( uint32  bitIndex)

Sets the bit at the specified index to true.

Parameters
bitIndexa bit index

Definition at line 103 of file bitSet.cpp.

103  {
104 
105  uint32 wordIdx = WORD_INDEX(bitIndex);
106  expandTo(wordIdx);
107 
108  words[wordIdx] |= (((uint64)1) << WORD_OFFSET(bitIndex));
109  return *this;
110  }
#define WORD_OFFSET(bitn)
Definition: bitSet.cpp:38
#define WORD_INDEX(bitn)
Definition: bitSet.cpp:36
uint64_t uint64
Definition: pvType.h:103
uint32_t uint32
Definition: pvType.h:99
void epics::pvData::BitSet::set ( uint32  bitIndex,
bool  value 
)

Sets the bit at the specified index to the specified value.

Parameters
bitIndexa bit index
valuea boolean value to set

Definition at line 123 of file bitSet.cpp.

123  {
124  if (value)
125  set(bitIndex);
126  else
127  clear(bitIndex);
128  }
Definition: link.h:174
uint32 epics::pvData::BitSet::size ( ) const

Returns the number of bits of space actually in use by this BitSet to represent bit values. The maximum element in the set is the size - 1st element.

Returns
the number of bits currently in this bit set

Definition at line 210 of file bitSet.cpp.

210  {
211  return words.size() * BITS_PER_WORD;
212  }
#define BITS_PER_WORD
Definition: bitSet.cpp:28
void epics::pvData::BitSet::swap ( BitSet set)

Swap contents.

Definition at line 277 of file bitSet.cpp.

278  {
279  words.swap(set.words);
280  }

The documentation for this class was generated from the following files: