Codebase list libgclib / HEAD GBitVec.h
HEAD

Tree @HEAD (Download .tar.gz)

GBitVec.h @HEADraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#ifndef __GBITVEC_H__
#define __GBITVEC_H__
#include "GBase.h"
//this code is lifted from LLVM (llvm.org, BitVector.h)

/// bitCount_32 - this function counts the number of set bits in a value.
/// Ex. CountPopulation(0xF000F000) = 8
/// Returns 0 if the word is zero.
inline uint bitCount_32(uint32_t Value) {
#if __GNUC__ >= 4
    return __builtin_popcount(Value);
#else
    uint32_t v = Value - ((Value >> 1) & 0x55555555);
    v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
    return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
#endif
  }

/// bitCount_64 - this function counts the number of set bits in a value,
/// (64 bit edition.)
inline uint bitCount_64(uint64_t Value) {
#if __GNUC__ >= 4
    return __builtin_popcountll(Value);
#else
    uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
    v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
    v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
    return uint((uint64_t)(v * 0x0101010101010101ULL) >> 56);
#endif
  }

/// CountTrailingZeros_32 - this function performs the platform optimal form of
/// counting the number of zeros from the least significant bit to the first one
/// bit.  Ex. CountTrailingZeros_32(0xFF00FF00) == 8.
/// Returns 32 if the word is zero.
inline unsigned bitCountTrailingZeros_32(uint32_t Value) {
#if __GNUC__ >= 4
  return Value ? __builtin_ctz(Value) : 32;
#else
  static const unsigned Mod37BitPosition[] = {
    32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13,
    4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9,
    5, 20, 8, 19, 18
  };
  return Mod37BitPosition[(-Value & Value) % 37];
#endif
}

// CountTrailingZeros_64 - This function performs the platform optimal form
/// of counting the number of zeros from the least significant bit to the first
/// one bit (64 bit edition.)
/// Returns 64 if the word is zero.
inline unsigned bitCountTrailingZeros_64(uint64_t Value) {
#if __GNUC__ >= 4
  return Value ? __builtin_ctzll(Value) : 64;
#else
  static const unsigned Mod67Position[] = {
    64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54,
    4, 64, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55,
    47, 5, 32, 65, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27,
    29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56,
    7, 48, 35, 6, 34, 33, 0
  };
  return Mod67Position[(-Value & Value) % 67];
#endif
}

class GBitVec {
  typedef unsigned long BitWord;

  enum { BITWORD_SIZE = (uint)sizeof(BitWord) * CHAR_BIT };

  BitWord  *fBits;        // Actual bits.
  uint Size;         // Size of GBitVec in bits.
  uint Capacity;     // Size of allocated memory in BitWord.

public:
  // Encapsulation of a single bit.
  class GBitRef {
    friend class GBitVec;
    BitWord *WordRef;
    uint BitPos;
    GBitRef();  // Undefined
  public:
    GBitRef(GBitVec &b, uint Idx) {
      WordRef = &b.fBits[Idx / BITWORD_SIZE];
      BitPos = Idx % BITWORD_SIZE;
    }

    ~GBitRef() {}

    GBitRef &operator=(GBitRef t) {
      *this = bool(t);
      return *this;
    }

    GBitRef& operator=(bool t) {
      if (t)
        *WordRef |= 1L << BitPos;
      else
        *WordRef &= ~(1L << BitPos);
      return *this;
    }

    operator bool() const {
      return ((*WordRef) & (1L << BitPos)) ? true : false;
    }
  };


  /// GBitVec default ctor - Creates an empty GBitVec.
  GBitVec() : Size(0), Capacity(0) {
    fBits = 0;
  }

  /// GBitVec ctor - Creates a GBitVec of specified number of bits. All
  /// bits are initialized to the specified value.
  explicit GBitVec(uint bitsize, bool value = false) : Size(bitsize) {
	if (bitsize==0) {
		Capacity=0;
		fBits=0;
		return;
	}
    Capacity = NumBitWords(bitsize);
    //fBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
    GMALLOC(fBits, Capacity * sizeof(BitWord));
    init_words(fBits, Capacity, value);
    if (value)
      clear_unused_bits();
  }
  unsigned long getMemorySize() const {
	   unsigned long r = ((unsigned long) Capacity) * sizeof(BitWord);
	   return r;
  }

  GBitVec(const GBitVec* RHS) {
    if (RHS==NULL) {
      Size = 0;
      fBits = 0;
      Capacity = 0;
      return;
    }
    Capacity = NumBitWords(RHS->size());
    GMALLOC(fBits, Capacity * sizeof(BitWord));
    memcpy(fBits, RHS->fBits, Capacity * sizeof(BitWord));
  }


  /// GBitVec copy ctor.
  GBitVec(const GBitVec &RHS) : Size(RHS.size()) {
    if (Size == 0) {
      fBits = 0;
      Capacity = 0;
      return;
    }

    Capacity = NumBitWords(RHS.size());
    GMALLOC(fBits, Capacity * sizeof(BitWord));
    memcpy(fBits, RHS.fBits, Capacity * sizeof(BitWord));
  }

  ~GBitVec() {
    GFREE(fBits);
  }

  /// empty - Tests whether there are no bits in this GBitVec.
  bool empty() const { return Size == 0; }

  /// size - Returns the number of bits in this GBitVec.
  uint size() const { return Size; }


  void bitSizeError() {
    GError("Error at GBitVec: unsupported BitWord size (%d)!\n",
        sizeof(BitWord));
    }
  /// count - Returns the number of bits which are set.
  uint count() {
    uint NumBits = 0;
    for (uint i = 0; i < NumBitWords(size()); ++i)
      if (sizeof(BitWord) == 4)
        NumBits += bitCount_32((uint32_t)fBits[i]);
      else if (sizeof(BitWord) == 8)
        NumBits += bitCount_64(fBits[i]);
      else
        bitSizeError();
    return NumBits;
  }

  /// any - Returns true if any bit is set.
  bool any() {
    for (uint i = 0; i < NumBitWords(size()); ++i)
      if (fBits[i] != 0)
        return true;
    return false;
  }

  /// all - Returns true if all bits are set.
  bool all() {
    // TODO: Optimize this.
    return count() == size();
  }

  /// none - Returns true if none of the bits are set.
  bool none() {
    return !any();
  }

  /// find_first - Returns the index of the first set bit, -1 if none
  /// of the bits are set.
  int find_first() {
    for (uint i = 0; i < NumBitWords(size()); ++i)
      if (fBits[i] != 0) {
        if (sizeof(BitWord) == 4)
          return i * BITWORD_SIZE + bitCountTrailingZeros_32((uint32_t)fBits[i]);
        else if (sizeof(BitWord) == 8)
          return i * BITWORD_SIZE + bitCountTrailingZeros_64(fBits[i]);
        else
          bitSizeError();
      }
    return -1;
  }

  /// find_next - Returns the index of the next set bit following the
  /// "Prev" bit. Returns -1 if the next set bit is not found.
  int find_next(uint Prev) {
    ++Prev;
    if (Prev >= Size)
      return -1;

    uint WordPos = Prev / BITWORD_SIZE;
    uint BitPos = Prev % BITWORD_SIZE;
    BitWord Copy = fBits[WordPos];
    // Mask off previous bits.
    Copy &= ~0UL << BitPos;

    if (Copy != 0) {
      if (sizeof(BitWord) == 4)
        return WordPos * BITWORD_SIZE + bitCountTrailingZeros_32((uint32_t)Copy);
      else if (sizeof(BitWord) == 8)
        return WordPos * BITWORD_SIZE + bitCountTrailingZeros_64(Copy);
      else
        bitSizeError();
    }

    // Check subsequent words.
    for (uint i = WordPos+1; i < NumBitWords(size()); ++i)
      if (fBits[i] != 0) {
        if (sizeof(BitWord) == 4)
          return i * BITWORD_SIZE + bitCountTrailingZeros_32((uint32_t)fBits[i]);
        else if (sizeof(BitWord) == 8)
          return i * BITWORD_SIZE + bitCountTrailingZeros_64(fBits[i]);
        else
          bitSizeError();
      }
    return -1;
  }

  /// clear - Clear all bits; does NOT release memory
  void clear() {
    Size = 0;
  }

  /// resize - Grow or shrink the GBitVec.
  void resize(uint N, bool value = false) {
    if (N > Capacity * BITWORD_SIZE) {
      uint OldCapacity = Capacity;
      grow(N);
      init_words(&fBits[OldCapacity], (Capacity-OldCapacity), value);
    }

    // Set any old unused bits that are now included in the GBitVec. This
    // may set bits that are not included in the new vector, but we will clear
    // them back out below.
    if (N > Size)
      set_unused_bits(value);

    // Update the size, and clear out any bits that are now unused
    uint OldSize = Size;
    Size = N;
    if (value || N < OldSize)
      clear_unused_bits();
  }

  void reserve(uint N) {
    if (N > Capacity * BITWORD_SIZE)
      grow(N);
  }

  // Set, reset, flip
  GBitVec &set() {
    init_words(fBits, Capacity, true);
    clear_unused_bits();
    return *this;
  }

  GBitVec &set(uint Idx) {
#ifndef NDEBUG
	  indexCheck(Idx, Size);
#endif
    fBits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
    return *this;
  }

  GBitVec &reset() {
    init_words(fBits, Capacity, false);
    return *this;
  }

  GBitVec &reset(uint Idx) {
#ifndef NDEBUG
	  indexCheck(Idx, Size);
#endif
    fBits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
    return *this;
  }

  GBitVec &flip() {
    for (uint i = 0; i < NumBitWords(size()); ++i)
      fBits[i] = ~fBits[i];
    clear_unused_bits();
    return *this;
  }

  GBitVec &flip(uint Idx) {
#ifndef NDEBUG
	  indexCheck(Idx, Size);
#endif
    fBits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
    return *this;
  }

  // No argument flip.
  GBitVec operator~() const {
    return GBitVec(*this).flip();
  }

  inline static void indexCheck(uint vIdx, uint vSize) {
    if (vIdx >= vSize)
      GError("Error at GBitVec: index %d out of bounds (size %d)\n",
        (int)vIdx, vSize);
   }

  // Indexing.
  GBitRef operator[](uint Idx) {
    //assert (Idx < Size && "Out-of-bounds Bit access.");
	#ifndef NDEBUG
	  indexCheck(Idx, Size);
	#endif
    return GBitRef(*this, Idx);
  }

  bool operator[](uint Idx) const {
   #ifndef NDEBUG
    indexCheck(Idx, Size);
   #endif
   BitWord Mask = 1L << (Idx % BITWORD_SIZE);
   return (fBits[Idx / BITWORD_SIZE] & Mask) != 0;
  }

  bool test(uint Idx) const {
    return (*this)[Idx];
  }

  // Comparison operators.
  bool operator==(const GBitVec &RHS) const {
    uint ThisWords = NumBitWords(size());
    uint RHSWords  = NumBitWords(RHS.size());
    uint i;
    uint imax=GMIN(ThisWords, RHSWords);
    for (i = 0; i != imax; ++i)
      if (fBits[i] != RHS.fBits[i])
        return false;

    // Verify that any extra words are all zeros.
    if (i != ThisWords) {
      for (; i != ThisWords; ++i)
        if (fBits[i])
          return false;
    } else if (i != RHSWords) {
      for (; i != RHSWords; ++i)
        if (RHS.fBits[i])
          return false;
    }
    return true;
  }

  bool operator!=(const GBitVec &RHS) const {
    return !(*this == RHS);
  }

  // Intersection, union, disjoint union.
  GBitVec &operator&=(const GBitVec &RHS) {
    uint ThisWords = NumBitWords(size());
    uint RHSWords  = NumBitWords(RHS.size());
    uint i;
    uint imax=GMIN(ThisWords, RHSWords);
    for (i = 0; i != imax; ++i)
      fBits[i] &= RHS.fBits[i];

    // Any bits that are just in this GBitVec become zero, because they aren't
    // in the RHS bit vector.  Any words only in RHS are ignored because they
    // are already zero in the LHS.
    for (; i != ThisWords; ++i)
      fBits[i] = 0;

    return *this;
  }

  GBitVec &operator|=(const GBitVec &RHS) {
    if (size() < RHS.size())
      resize(RHS.size());
    for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
      fBits[i] |= RHS.fBits[i];
    return *this;
  }

  GBitVec &operator^=(const GBitVec &RHS) {
    if (size() < RHS.size())
      resize(RHS.size());
    for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
      fBits[i] ^= RHS.fBits[i];
    return *this;
  }

  // Assignment operator.
  const GBitVec &operator=(const GBitVec &RHS) {
    if (this == &RHS) return *this;

    Size = RHS.size();
    uint RHSWords = NumBitWords(Size);
    if (Size <= Capacity * BITWORD_SIZE) {
      if (Size)
        memcpy(fBits, RHS.fBits, RHSWords * sizeof(BitWord));
      clear_unused_bits();
      return *this;
    }

    // Grow the GBitVec to have enough elements.
    Capacity = RHSWords;
    //BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
    BitWord *NewBits = NULL;
    GMALLOC(NewBits, Capacity * sizeof(BitWord));
    memcpy(NewBits, RHS.fBits, Capacity * sizeof(BitWord));

    // Destroy the old bits.
    GFREE(fBits);
    fBits = NewBits;

    return *this;
  }

  void swap(GBitVec &RHS) {
    Gswap(fBits, RHS.fBits);
    Gswap(Size, RHS.Size);
    Gswap(Capacity, RHS.Capacity);
  }

private:
  uint NumBitWords(uint S) const {
    return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
  }

  // Set the unused bits in the high words.
  void set_unused_bits(bool value = true) {
    //  Set high words first.
    uint UsedWords = NumBitWords(Size);
    if (Capacity > UsedWords)
      init_words(&fBits[UsedWords], (Capacity-UsedWords), value);

    //  Then set any stray high bits of the last used word.
    uint ExtraBits = Size % BITWORD_SIZE;

    if (ExtraBits) {
      BitWord ExtraBitMask = ~0UL << ExtraBits;
      if (value)
        fBits[UsedWords-1] |= ExtraBitMask;
      else
        fBits[UsedWords-1] &= ~ExtraBitMask;
    }
  }

  // Clear the unused bits in the high words.
  void clear_unused_bits() {
    set_unused_bits(false);
  }

  void grow(uint NewSize) {
    Capacity = GMAX(NumBitWords(NewSize), Capacity * 2);
    //fBits = (BitWord *)std::realloc(fBits, Capacity * sizeof(BitWord));
    GREALLOC(fBits, Capacity * sizeof(BitWord));
    clear_unused_bits();
  }

  void init_words(BitWord *B, uint NumWords, bool value) {
    memset(B, 0 - (int)value, NumWords*sizeof(BitWord));
  }
};


inline GBitVec operator&(const GBitVec &LHS, const GBitVec &RHS) {
  GBitVec Result(LHS);
  Result &= RHS;
  return Result;
}

inline GBitVec operator|(const GBitVec &LHS, const GBitVec &RHS) {
  GBitVec Result(LHS);
  Result |= RHS;
  return Result;
}

inline GBitVec operator^(const GBitVec &LHS, const GBitVec &RHS) {
  GBitVec Result(LHS);
  Result ^= RHS;
  return Result;
}

inline void Gswap(GBitVec &LHS, GBitVec &RHS) {
  LHS.swap(RHS);
  }

#endif