svf-lib 1.0.1108 → 1.0.1111

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -209,6 +209,9 @@ public:
209
209
  }
210
210
 
211
211
  private:
212
+ std::vector<const Function *> getLLVMGlobalFunctions(
213
+ const GlobalVariable *global);
214
+
212
215
  void loadModules(const std::vector<std::string> &moduleNameVec);
213
216
  void addSVFMain();
214
217
  void initialize();
Binary file
@@ -17,6 +17,7 @@
17
17
  #include "Util/SVFBasicTypes.h"
18
18
  #include "Util/BitVector.h"
19
19
  #include "Util/CoreBitVector.h"
20
+ #include "Util/SparseBitVector.h"
20
21
 
21
22
  namespace SVF
22
23
  {
@@ -162,7 +163,7 @@ private:
162
163
  union
163
164
  {
164
165
  /// Sparse bit vector backing.
165
- SparseBitVector sbv;
166
+ SparseBitVector<> sbv;
166
167
  /// Core bit vector backing.
167
168
  CoreBitVector cbv;
168
169
  /// Bit vector backing.
@@ -223,7 +224,7 @@ public:
223
224
  /// TODO: std::variant when we move to C++17.
224
225
  union
225
226
  {
226
- SparseBitVector::iterator sbvIt;
227
+ SparseBitVector<>::iterator sbvIt;
227
228
  CoreBitVector::iterator cbvIt;
228
229
  BitVector::iterator bvIt;
229
230
  };
@@ -209,6 +209,9 @@ public:
209
209
  }
210
210
 
211
211
  private:
212
+ std::vector<const Function *> getLLVMGlobalFunctions(
213
+ const GlobalVariable *global);
214
+
212
215
  void loadModules(const std::vector<std::string> &moduleNameVec);
213
216
  void addSVFMain();
214
217
  void initialize();
@@ -35,9 +35,10 @@
35
35
  #include <llvm/IR/BasicBlock.h>
36
36
  #include <llvm/ADT/SmallVector.h>
37
37
 
38
- #include <llvm/ADT/SparseBitVector.h> // for points-to
39
38
  #include <llvm/Support/CommandLine.h> // for command line options
40
39
 
40
+ #include <Util/SparseBitVector.h>
41
+
41
42
  #include <iostream>
42
43
  #include <vector>
43
44
  #include <list>
@@ -92,8 +93,8 @@ typedef unsigned SymID;
92
93
  typedef unsigned CallSiteID;
93
94
  typedef unsigned ThreadID;
94
95
 
95
- typedef llvm::SparseBitVector<> NodeBS;
96
- typedef llvm::SparseBitVector<> SparseBitVector;
96
+ typedef SparseBitVector<> NodeBS;
97
+ //typedef SparseBitVector<> SparseBitVector;
97
98
  //typedef NodeBS PointsTo;
98
99
  class PointsTo;
99
100
  typedef PointsTo AliasSet;
@@ -297,9 +298,9 @@ template <> struct std::hash<SVF::NodePair>
297
298
 
298
299
  /// Specialise hash for SparseBitVectors.
299
300
  template <unsigned N>
300
- struct std::hash<llvm::SparseBitVector<N>>
301
+ struct std::hash<SVF::SparseBitVector<N>>
301
302
  {
302
- size_t operator()(const llvm::SparseBitVector<N> &sbv) const
303
+ size_t operator()(const SVF::SparseBitVector<N> &sbv) const
303
304
  {
304
305
  SVF::Hash<std::pair<std::pair<size_t, size_t>, size_t>> h;
305
306
  return h(std::make_pair(std::make_pair(sbv.count(), sbv.find_first()), sbv.find_last()));
@@ -0,0 +1,1208 @@
1
+ //===- SparseBitVector.h - Efficient Sparse BitVector --*- C++ -*-===//
2
+ //
3
+ // From the LLVM Project with some modifications, under the Apache License v2.0
4
+ // with LLVM Exceptions. See https://llvm.org/LICENSE.txt for license information.
5
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
+ //
7
+
8
+ #ifndef SPARSEBITVECTOR_H
9
+ #define SPARSEBITVECTOR_H
10
+
11
+ #include <ostream>
12
+ #include <cassert>
13
+ #include <cstring>
14
+ #include <climits>
15
+ #include <limits>
16
+ #include <iterator>
17
+ #include <list>
18
+
19
+ // Appease GCC?
20
+ #ifdef __has_builtin
21
+ # define HAS_CLZ __has_builtin(__builtin_clz)
22
+ # define HAS_CLZLL __has_builtin(__builtin_clzll)
23
+ #else
24
+ # define HAS_CLZ 0
25
+ # define HAS_CLZLL 0
26
+ #endif
27
+
28
+ namespace SVF
29
+ {
30
+
31
+ /// The behavior an operation has on an input of 0.
32
+ enum ZeroBehavior
33
+ {
34
+ /// The returned value is undefined.
35
+ ZB_Undefined,
36
+ /// The returned value is numeric_limits<T>::max()
37
+ ZB_Max,
38
+ /// The returned value is numeric_limits<T>::digits
39
+ ZB_Width
40
+ };
41
+
42
+ template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter
43
+ {
44
+ static unsigned count(T Val, ZeroBehavior)
45
+ {
46
+ if (!Val)
47
+ return std::numeric_limits<T>::digits;
48
+ if (Val & 0x1)
49
+ return 0;
50
+
51
+ // Bisection method.
52
+ unsigned ZeroBits = 0;
53
+ T Shift = std::numeric_limits<T>::digits >> 1;
54
+ T Mask = std::numeric_limits<T>::max() >> Shift;
55
+ while (Shift)
56
+ {
57
+ if ((Val & Mask) == 0)
58
+ {
59
+ Val >>= Shift;
60
+ ZeroBits |= Shift;
61
+ }
62
+ Shift >>= 1;
63
+ Mask >>= Shift;
64
+ }
65
+ return ZeroBits;
66
+ }
67
+ };
68
+
69
+ /// Count number of 0's from the least significant bit to the most
70
+ /// stopping at the first 1.
71
+ ///
72
+ /// Only unsigned integral types are allowed.
73
+ ///
74
+ /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
75
+ /// valid arguments.
76
+ template <typename T>
77
+ unsigned countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width)
78
+ {
79
+ static_assert(std::numeric_limits<T>::is_integer &&
80
+ !std::numeric_limits<T>::is_signed,
81
+ "Only unsigned integral types are allowed.");
82
+ return TrailingZerosCounter<T, sizeof(T)>::count(Val, ZB);
83
+ }
84
+
85
+ template <typename T, std::size_t SizeOfT> struct PopulationCounter
86
+ {
87
+ static unsigned count(T Value)
88
+ {
89
+ // Generic version, forward to 32 bits.
90
+ static_assert(SizeOfT <= 4, "Not implemented!");
91
+ #if defined(__GNUC__)
92
+ return __builtin_popcount(Value);
93
+ #else
94
+ uint32_t v = Value;
95
+ v = v - ((v >> 1) & 0x55555555);
96
+ v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
97
+ return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
98
+ #endif
99
+ }
100
+ };
101
+
102
+ template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter
103
+ {
104
+ static unsigned count(T Val, ZeroBehavior)
105
+ {
106
+ if (!Val)
107
+ return std::numeric_limits<T>::digits;
108
+
109
+ // Bisection method.
110
+ unsigned ZeroBits = 0;
111
+ for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1)
112
+ {
113
+ T Tmp = Val >> Shift;
114
+ if (Tmp)
115
+ Val = Tmp;
116
+ else
117
+ ZeroBits |= Shift;
118
+ }
119
+ return ZeroBits;
120
+ }
121
+ };
122
+
123
+ #if defined(__GNUC__) || defined(_MSC_VER)
124
+ template <typename T> struct LeadingZerosCounter<T, 4>
125
+ {
126
+ static unsigned count(T Val, ZeroBehavior ZB)
127
+ {
128
+ if (ZB != ZB_Undefined && Val == 0)
129
+ return 32;
130
+
131
+ #if defined(__GNUC__) || HAS_CLZ
132
+ return __builtin_clz(Val);
133
+ #elif defined(_MSC_VER)
134
+ unsigned long Index;
135
+ _BitScanReverse(&Index, Val);
136
+ return Index ^ 31;
137
+ #endif
138
+ }
139
+ };
140
+
141
+ #if !defined(_MSC_VER) || defined(_M_X64)
142
+ template <typename T> struct LeadingZerosCounter<T, 8>
143
+ {
144
+ static unsigned count(T Val, ZeroBehavior ZB)
145
+ {
146
+ if (ZB != ZB_Undefined && Val == 0)
147
+ return 64;
148
+
149
+ #if defined(__GNUC__) || HAS_CLZLL
150
+ return __builtin_clzll(Val);
151
+ #elif defined(_MSC_VER)
152
+ unsigned long Index;
153
+ _BitScanReverse64(&Index, Val);
154
+ return Index ^ 63;
155
+ #endif
156
+ }
157
+ };
158
+ #endif
159
+ #endif
160
+
161
+ /// Count number of 0's from the most significant bit to the least
162
+ /// stopping at the first 1.
163
+ ///
164
+ /// Only unsigned integral types are allowed.
165
+ ///
166
+ /// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
167
+ /// valid arguments.
168
+ template <typename T>
169
+ unsigned countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width)
170
+ {
171
+ static_assert(std::numeric_limits<T>::is_integer &&
172
+ !std::numeric_limits<T>::is_signed,
173
+ "Only unsigned integral types are allowed.");
174
+ return LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB);
175
+ }
176
+
177
+ template <typename T> struct PopulationCounter<T, 8>
178
+ {
179
+ static unsigned count(T Value)
180
+ {
181
+ #if defined(__GNUC__)
182
+ return __builtin_popcountll(Value);
183
+ #else
184
+ uint64_t v = Value;
185
+ v = v - ((v >> 1) & 0x5555555555555555ULL);
186
+ v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
187
+ v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
188
+ return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
189
+ #endif
190
+ }
191
+ };
192
+
193
+ /// Count the number of set bits in a value.
194
+ /// Ex. countPopulation(0xF000F000) = 8
195
+ /// Returns 0 if the word is zero.
196
+ template <typename T>
197
+ inline unsigned countPopulation(T Value)
198
+ {
199
+ static_assert(std::numeric_limits<T>::is_integer &&
200
+ !std::numeric_limits<T>::is_signed,
201
+ "Only unsigned integral types are allowed.");
202
+ return PopulationCounter<T, sizeof(T)>::count(Value);
203
+ }
204
+
205
+ /// SparseBitVector is an implementation of a bitvector that is sparse by only
206
+ /// storing the elements that have non-zero bits set. In order to make this
207
+ /// fast for the most common cases, SparseBitVector is implemented as a linked
208
+ /// list of SparseBitVectorElements. We maintain a pointer to the last
209
+ /// SparseBitVectorElement accessed (in the form of a list iterator), in order
210
+ /// to make multiple in-order test/set constant time after the first one is
211
+ /// executed. Note that using vectors to store SparseBitVectorElement's does
212
+ /// not work out very well because it causes insertion in the middle to take
213
+ /// enormous amounts of time with a large amount of bits. Other structures that
214
+ /// have better worst cases for insertion in the middle (various balanced trees,
215
+ /// etc) do not perform as well in practice as a linked list with this iterator
216
+ /// kept up to date. They are also significantly more memory intensive.
217
+ template <unsigned ElementSize = 128> struct SparseBitVectorElement
218
+ {
219
+ public:
220
+ using BitWord = unsigned long;
221
+ using size_type = unsigned;
222
+ enum
223
+ {
224
+ BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT,
225
+ BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
226
+ BITS_PER_ELEMENT = ElementSize
227
+ };
228
+
229
+ private:
230
+ // Index of Element in terms of where first bit starts.
231
+ unsigned ElementIndex;
232
+ BitWord Bits[BITWORDS_PER_ELEMENT];
233
+
234
+ SparseBitVectorElement()
235
+ {
236
+ ElementIndex = ~0U;
237
+ memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
238
+ }
239
+
240
+ public:
241
+ explicit SparseBitVectorElement(unsigned Idx)
242
+ {
243
+ ElementIndex = Idx;
244
+ memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
245
+ }
246
+
247
+ // Comparison.
248
+ bool operator==(const SparseBitVectorElement &RHS) const
249
+ {
250
+ if (ElementIndex != RHS.ElementIndex)
251
+ return false;
252
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
253
+ if (Bits[i] != RHS.Bits[i])
254
+ return false;
255
+ return true;
256
+ }
257
+
258
+ bool operator!=(const SparseBitVectorElement &RHS) const
259
+ {
260
+ return !(*this == RHS);
261
+ }
262
+
263
+ // Return the bits that make up word Idx in our element.
264
+ BitWord word(unsigned Idx) const
265
+ {
266
+ assert(Idx < BITWORDS_PER_ELEMENT);
267
+ return Bits[Idx];
268
+ }
269
+
270
+ unsigned index() const
271
+ {
272
+ return ElementIndex;
273
+ }
274
+
275
+ bool empty() const
276
+ {
277
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
278
+ if (Bits[i])
279
+ return false;
280
+ return true;
281
+ }
282
+
283
+ void set(unsigned Idx)
284
+ {
285
+ Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
286
+ }
287
+
288
+ bool test_and_set(unsigned Idx)
289
+ {
290
+ bool old = test(Idx);
291
+ if (!old)
292
+ {
293
+ set(Idx);
294
+ return true;
295
+ }
296
+ return false;
297
+ }
298
+
299
+ void reset(unsigned Idx)
300
+ {
301
+ Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
302
+ }
303
+
304
+ bool test(unsigned Idx) const
305
+ {
306
+ return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
307
+ }
308
+
309
+ size_type count() const
310
+ {
311
+ unsigned NumBits = 0;
312
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
313
+ NumBits += countPopulation(Bits[i]);
314
+ return NumBits;
315
+ }
316
+
317
+ /// find_first - Returns the index of the first set bit.
318
+ int find_first() const
319
+ {
320
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
321
+ if (Bits[i] != 0)
322
+ return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
323
+ assert(false && "SBV: find_first: SBV cannot be empty");
324
+ }
325
+
326
+ /// find_last - Returns the index of the last set bit.
327
+ int find_last() const
328
+ {
329
+ for (unsigned I = 0; I < BITWORDS_PER_ELEMENT; ++I)
330
+ {
331
+ unsigned Idx = BITWORDS_PER_ELEMENT - I - 1;
332
+ if (Bits[Idx] != 0)
333
+ return Idx * BITWORD_SIZE + BITWORD_SIZE -
334
+ countLeadingZeros(Bits[Idx]) - 1;
335
+ }
336
+ assert(false && "SBV: find_last: SBV cannot be empty");
337
+ }
338
+
339
+ /// find_next - Returns the index of the next set bit starting from the
340
+ /// "Curr" bit. Returns -1 if the next set bit is not found.
341
+ int find_next(unsigned Curr) const
342
+ {
343
+ if (Curr >= BITS_PER_ELEMENT)
344
+ return -1;
345
+
346
+ unsigned WordPos = Curr / BITWORD_SIZE;
347
+ unsigned BitPos = Curr % BITWORD_SIZE;
348
+ BitWord Copy = Bits[WordPos];
349
+ assert(WordPos <= BITWORDS_PER_ELEMENT
350
+ && "Word Position outside of element");
351
+
352
+ // Mask off previous bits.
353
+ Copy &= ~0UL << BitPos;
354
+
355
+ if (Copy != 0)
356
+ return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
357
+
358
+ // Check subsequent words.
359
+ for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
360
+ if (Bits[i] != 0)
361
+ return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
362
+ return -1;
363
+ }
364
+
365
+ // Union this element with RHS and return true if this one changed.
366
+ bool unionWith(const SparseBitVectorElement &RHS)
367
+ {
368
+ bool changed = false;
369
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
370
+ {
371
+ BitWord old = changed ? 0 : Bits[i];
372
+
373
+ Bits[i] |= RHS.Bits[i];
374
+ if (!changed && old != Bits[i])
375
+ changed = true;
376
+ }
377
+ return changed;
378
+ }
379
+
380
+ // Return true if we have any bits in common with RHS
381
+ bool intersects(const SparseBitVectorElement &RHS) const
382
+ {
383
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
384
+ {
385
+ if (RHS.Bits[i] & Bits[i])
386
+ return true;
387
+ }
388
+ return false;
389
+ }
390
+
391
+ // Intersect this Element with RHS and return true if this one changed.
392
+ // BecameZero is set to true if this element became all-zero bits.
393
+ bool intersectWith(const SparseBitVectorElement &RHS,
394
+ bool &BecameZero)
395
+ {
396
+ bool changed = false;
397
+ bool allzero = true;
398
+
399
+ BecameZero = false;
400
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
401
+ {
402
+ BitWord old = changed ? 0 : Bits[i];
403
+
404
+ Bits[i] &= RHS.Bits[i];
405
+ if (Bits[i] != 0)
406
+ allzero = false;
407
+
408
+ if (!changed && old != Bits[i])
409
+ changed = true;
410
+ }
411
+ BecameZero = allzero;
412
+ return changed;
413
+ }
414
+
415
+ // Intersect this Element with the complement of RHS and return true if this
416
+ // one changed. BecameZero is set to true if this element became all-zero
417
+ // bits.
418
+ bool intersectWithComplement(const SparseBitVectorElement &RHS,
419
+ bool &BecameZero)
420
+ {
421
+ bool changed = false;
422
+ bool allzero = true;
423
+
424
+ BecameZero = false;
425
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
426
+ {
427
+ BitWord old = changed ? 0 : Bits[i];
428
+
429
+ Bits[i] &= ~RHS.Bits[i];
430
+ if (Bits[i] != 0)
431
+ allzero = false;
432
+
433
+ if (!changed && old != Bits[i])
434
+ changed = true;
435
+ }
436
+ BecameZero = allzero;
437
+ return changed;
438
+ }
439
+
440
+ // Three argument version of intersectWithComplement that intersects
441
+ // RHS1 & ~RHS2 into this element
442
+ void intersectWithComplement(const SparseBitVectorElement &RHS1,
443
+ const SparseBitVectorElement &RHS2,
444
+ bool &BecameZero)
445
+ {
446
+ bool allzero = true;
447
+
448
+ BecameZero = false;
449
+ for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
450
+ {
451
+ Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
452
+ if (Bits[i] != 0)
453
+ allzero = false;
454
+ }
455
+ BecameZero = allzero;
456
+ }
457
+ };
458
+
459
+ template <unsigned ElementSize = 128>
460
+ class SparseBitVector
461
+ {
462
+ using ElementList = std::list<SparseBitVectorElement<ElementSize>>;
463
+ using ElementListIter = typename ElementList::iterator;
464
+ using ElementListConstIter = typename ElementList::const_iterator;
465
+ enum
466
+ {
467
+ BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
468
+ };
469
+
470
+ ElementList Elements;
471
+ // Pointer to our current Element. This has no visible effect on the external
472
+ // state of a SparseBitVector, it's just used to improve performance in the
473
+ // common case of testing/modifying bits with similar indices.
474
+ mutable ElementListIter CurrElementIter;
475
+
476
+ // This is like std::lower_bound, except we do linear searching from the
477
+ // current position.
478
+ ElementListIter FindLowerBoundImpl(unsigned ElementIndex) const
479
+ {
480
+
481
+ // We cache a non-const iterator so we're forced to resort to const_cast to
482
+ // get the begin/end in the case where 'this' is const. To avoid duplication
483
+ // of code with the only difference being whether the const cast is present
484
+ // 'this' is always const in this particular function and we sort out the
485
+ // difference in FindLowerBound and FindLowerBoundConst.
486
+ ElementListIter Begin =
487
+ const_cast<SparseBitVector<ElementSize> *>(this)->Elements.begin();
488
+ ElementListIter End =
489
+ const_cast<SparseBitVector<ElementSize> *>(this)->Elements.end();
490
+
491
+ if (Elements.empty())
492
+ {
493
+ CurrElementIter = Begin;
494
+ return CurrElementIter;
495
+ }
496
+
497
+ // Make sure our current iterator is valid.
498
+ if (CurrElementIter == End)
499
+ --CurrElementIter;
500
+
501
+ // Search from our current iterator, either backwards or forwards,
502
+ // depending on what element we are looking for.
503
+ ElementListIter ElementIter = CurrElementIter;
504
+ if (CurrElementIter->index() == ElementIndex)
505
+ {
506
+ return ElementIter;
507
+ }
508
+ else if (CurrElementIter->index() > ElementIndex)
509
+ {
510
+ while (ElementIter != Begin
511
+ && ElementIter->index() > ElementIndex)
512
+ --ElementIter;
513
+ }
514
+ else
515
+ {
516
+ while (ElementIter != End &&
517
+ ElementIter->index() < ElementIndex)
518
+ ++ElementIter;
519
+ }
520
+ CurrElementIter = ElementIter;
521
+ return ElementIter;
522
+ }
523
+ ElementListConstIter FindLowerBoundConst(unsigned ElementIndex) const
524
+ {
525
+ return FindLowerBoundImpl(ElementIndex);
526
+ }
527
+ ElementListIter FindLowerBound(unsigned ElementIndex)
528
+ {
529
+ return FindLowerBoundImpl(ElementIndex);
530
+ }
531
+
532
+ // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
533
+ // than it would be, in order to be efficient.
534
+ class SparseBitVectorIterator
535
+ {
536
+ private:
537
+ bool AtEnd;
538
+
539
+ const SparseBitVector<ElementSize> *BitVector = nullptr;
540
+
541
+ // Current element inside of bitmap.
542
+ ElementListConstIter Iter;
543
+
544
+ // Current bit number inside of our bitmap.
545
+ unsigned BitNumber;
546
+
547
+ // Current word number inside of our element.
548
+ unsigned WordNumber;
549
+
550
+ // Current bits from the element.
551
+ typename SparseBitVectorElement<ElementSize>::BitWord Bits;
552
+
553
+ // Move our iterator to the first non-zero bit in the bitmap.
554
+ void AdvanceToFirstNonZero()
555
+ {
556
+ if (AtEnd)
557
+ return;
558
+ if (BitVector->Elements.empty())
559
+ {
560
+ AtEnd = true;
561
+ return;
562
+ }
563
+ Iter = BitVector->Elements.begin();
564
+ BitNumber = Iter->index() * ElementSize;
565
+ unsigned BitPos = Iter->find_first();
566
+ BitNumber += BitPos;
567
+ WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
568
+ Bits = Iter->word(WordNumber);
569
+ Bits >>= BitPos % BITWORD_SIZE;
570
+ }
571
+
572
+ // Move our iterator to the next non-zero bit.
573
+ void AdvanceToNextNonZero()
574
+ {
575
+ if (AtEnd)
576
+ return;
577
+
578
+ while (Bits && !(Bits & 1))
579
+ {
580
+ Bits >>= 1;
581
+ BitNumber += 1;
582
+ }
583
+
584
+ // See if we ran out of Bits in this word.
585
+ if (!Bits)
586
+ {
587
+ int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
588
+ // If we ran out of set bits in this element, move to next element.
589
+ if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0))
590
+ {
591
+ ++Iter;
592
+ WordNumber = 0;
593
+
594
+ // We may run out of elements in the bitmap.
595
+ if (Iter == BitVector->Elements.end())
596
+ {
597
+ AtEnd = true;
598
+ return;
599
+ }
600
+ // Set up for next non-zero word in bitmap.
601
+ BitNumber = Iter->index() * ElementSize;
602
+ NextSetBitNumber = Iter->find_first();
603
+ BitNumber += NextSetBitNumber;
604
+ WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
605
+ Bits = Iter->word(WordNumber);
606
+ Bits >>= NextSetBitNumber % BITWORD_SIZE;
607
+ }
608
+ else
609
+ {
610
+ WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
611
+ Bits = Iter->word(WordNumber);
612
+ Bits >>= NextSetBitNumber % BITWORD_SIZE;
613
+ BitNumber = Iter->index() * ElementSize;
614
+ BitNumber += NextSetBitNumber;
615
+ }
616
+ }
617
+ }
618
+
619
+ public:
620
+ SparseBitVectorIterator() = default;
621
+
622
+ SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
623
+ bool end = false):BitVector(RHS)
624
+ {
625
+ Iter = BitVector->Elements.begin();
626
+ BitNumber = 0;
627
+ Bits = 0;
628
+ WordNumber = ~0;
629
+ AtEnd = end;
630
+ AdvanceToFirstNonZero();
631
+ }
632
+
633
+ // Preincrement.
634
+ inline SparseBitVectorIterator& operator++()
635
+ {
636
+ ++BitNumber;
637
+ Bits >>= 1;
638
+ AdvanceToNextNonZero();
639
+ return *this;
640
+ }
641
+
642
+ // Postincrement.
643
+ inline SparseBitVectorIterator operator++(int)
644
+ {
645
+ SparseBitVectorIterator tmp = *this;
646
+ ++*this;
647
+ return tmp;
648
+ }
649
+
650
+ // Return the current set bit number.
651
+ unsigned operator*() const
652
+ {
653
+ return BitNumber;
654
+ }
655
+
656
+ bool operator==(const SparseBitVectorIterator &RHS) const
657
+ {
658
+ // If they are both at the end, ignore the rest of the fields.
659
+ if (AtEnd && RHS.AtEnd)
660
+ return true;
661
+ // Otherwise they are the same if they have the same bit number and
662
+ // bitmap.
663
+ return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
664
+ }
665
+
666
+ bool operator!=(const SparseBitVectorIterator &RHS) const
667
+ {
668
+ return !(*this == RHS);
669
+ }
670
+ };
671
+
672
+ public:
673
+ using iterator = SparseBitVectorIterator;
674
+
675
+ SparseBitVector() : Elements(), CurrElementIter(Elements.begin()) {}
676
+
677
+ SparseBitVector(const SparseBitVector &RHS)
678
+ : Elements(RHS.Elements), CurrElementIter(Elements.begin()) {}
679
+ SparseBitVector(SparseBitVector &&RHS)
680
+ : Elements(std::move(RHS.Elements)), CurrElementIter(Elements.begin()) {}
681
+
682
+ // Clear.
683
+ void clear()
684
+ {
685
+ Elements.clear();
686
+ }
687
+
688
+ // Assignment
689
+ SparseBitVector& operator=(const SparseBitVector& RHS)
690
+ {
691
+ if (this == &RHS)
692
+ return *this;
693
+
694
+ Elements = RHS.Elements;
695
+ CurrElementIter = Elements.begin();
696
+ return *this;
697
+ }
698
+ SparseBitVector &operator=(SparseBitVector &&RHS)
699
+ {
700
+ Elements = std::move(RHS.Elements);
701
+ CurrElementIter = Elements.begin();
702
+ return *this;
703
+ }
704
+
705
+ // Test, Reset, and Set a bit in the bitmap.
706
+ bool test(unsigned Idx) const
707
+ {
708
+ if (Elements.empty())
709
+ return false;
710
+
711
+ unsigned ElementIndex = Idx / ElementSize;
712
+ ElementListConstIter ElementIter = FindLowerBoundConst(ElementIndex);
713
+
714
+ // If we can't find an element that is supposed to contain this bit, there
715
+ // is nothing more to do.
716
+ if (ElementIter == Elements.end() ||
717
+ ElementIter->index() != ElementIndex)
718
+ return false;
719
+ return ElementIter->test(Idx % ElementSize);
720
+ }
721
+
722
+ void reset(unsigned Idx)
723
+ {
724
+ if (Elements.empty())
725
+ return;
726
+
727
+ unsigned ElementIndex = Idx / ElementSize;
728
+ ElementListIter ElementIter = FindLowerBound(ElementIndex);
729
+
730
+ // If we can't find an element that is supposed to contain this bit, there
731
+ // is nothing more to do.
732
+ if (ElementIter == Elements.end() ||
733
+ ElementIter->index() != ElementIndex)
734
+ return;
735
+ ElementIter->reset(Idx % ElementSize);
736
+
737
+ // When the element is zeroed out, delete it.
738
+ if (ElementIter->empty())
739
+ {
740
+ ++CurrElementIter;
741
+ Elements.erase(ElementIter);
742
+ }
743
+ }
744
+
745
+ void set(unsigned Idx)
746
+ {
747
+ unsigned ElementIndex = Idx / ElementSize;
748
+ ElementListIter ElementIter;
749
+ if (Elements.empty())
750
+ {
751
+ ElementIter = Elements.emplace(Elements.end(), ElementIndex);
752
+ }
753
+ else
754
+ {
755
+ ElementIter = FindLowerBound(ElementIndex);
756
+
757
+ if (ElementIter == Elements.end() ||
758
+ ElementIter->index() != ElementIndex)
759
+ {
760
+ // We may have hit the beginning of our SparseBitVector, in which case,
761
+ // we may need to insert right after this element, which requires moving
762
+ // the current iterator forward one, because insert does insert before.
763
+ if (ElementIter != Elements.end() &&
764
+ ElementIter->index() < ElementIndex)
765
+ ++ElementIter;
766
+ ElementIter = Elements.emplace(ElementIter, ElementIndex);
767
+ }
768
+ }
769
+ CurrElementIter = ElementIter;
770
+
771
+ ElementIter->set(Idx % ElementSize);
772
+ }
773
+
774
+ bool test_and_set(unsigned Idx)
775
+ {
776
+ bool old = test(Idx);
777
+ if (!old)
778
+ {
779
+ set(Idx);
780
+ return true;
781
+ }
782
+ return false;
783
+ }
784
+
785
+ bool operator!=(const SparseBitVector &RHS) const
786
+ {
787
+ return !(*this == RHS);
788
+ }
789
+
790
+ bool operator==(const SparseBitVector &RHS) const
791
+ {
792
+ ElementListConstIter Iter1 = Elements.begin();
793
+ ElementListConstIter Iter2 = RHS.Elements.begin();
794
+
795
+ for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
796
+ ++Iter1, ++Iter2)
797
+ {
798
+ if (*Iter1 != *Iter2)
799
+ return false;
800
+ }
801
+ return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
802
+ }
803
+
804
+ // Union our bitmap with the RHS and return true if we changed.
805
+ bool operator|=(const SparseBitVector &RHS)
806
+ {
807
+ if (this == &RHS)
808
+ return false;
809
+
810
+ bool changed = false;
811
+ ElementListIter Iter1 = Elements.begin();
812
+ ElementListConstIter Iter2 = RHS.Elements.begin();
813
+
814
+ // If RHS is empty, we are done
815
+ if (RHS.Elements.empty())
816
+ return false;
817
+
818
+ while (Iter2 != RHS.Elements.end())
819
+ {
820
+ if (Iter1 == Elements.end() || Iter1->index() > Iter2->index())
821
+ {
822
+ Elements.insert(Iter1, *Iter2);
823
+ ++Iter2;
824
+ changed = true;
825
+ }
826
+ else if (Iter1->index() == Iter2->index())
827
+ {
828
+ changed |= Iter1->unionWith(*Iter2);
829
+ ++Iter1;
830
+ ++Iter2;
831
+ }
832
+ else
833
+ {
834
+ ++Iter1;
835
+ }
836
+ }
837
+ CurrElementIter = Elements.begin();
838
+ return changed;
839
+ }
840
+
841
+ // Intersect our bitmap with the RHS and return true if ours changed.
842
+ bool operator&=(const SparseBitVector &RHS)
843
+ {
844
+ if (this == &RHS)
845
+ return false;
846
+
847
+ bool changed = false;
848
+ ElementListIter Iter1 = Elements.begin();
849
+ ElementListConstIter Iter2 = RHS.Elements.begin();
850
+
851
+ // Check if both bitmaps are empty.
852
+ if (Elements.empty() && RHS.Elements.empty())
853
+ return false;
854
+
855
+ // Loop through, intersecting as we go, erasing elements when necessary.
856
+ while (Iter2 != RHS.Elements.end())
857
+ {
858
+ if (Iter1 == Elements.end())
859
+ {
860
+ CurrElementIter = Elements.begin();
861
+ return changed;
862
+ }
863
+
864
+ if (Iter1->index() > Iter2->index())
865
+ {
866
+ ++Iter2;
867
+ }
868
+ else if (Iter1->index() == Iter2->index())
869
+ {
870
+ bool BecameZero;
871
+ changed |= Iter1->intersectWith(*Iter2, BecameZero);
872
+ if (BecameZero)
873
+ {
874
+ ElementListIter IterTmp = Iter1;
875
+ ++Iter1;
876
+ Elements.erase(IterTmp);
877
+ }
878
+ else
879
+ {
880
+ ++Iter1;
881
+ }
882
+ ++Iter2;
883
+ }
884
+ else
885
+ {
886
+ ElementListIter IterTmp = Iter1;
887
+ ++Iter1;
888
+ Elements.erase(IterTmp);
889
+ changed = true;
890
+ }
891
+ }
892
+ if (Iter1 != Elements.end())
893
+ {
894
+ Elements.erase(Iter1, Elements.end());
895
+ changed = true;
896
+ }
897
+ CurrElementIter = Elements.begin();
898
+ return changed;
899
+ }
900
+
901
+ // Intersect our bitmap with the complement of the RHS and return true
902
+ // if ours changed.
903
+ bool intersectWithComplement(const SparseBitVector &RHS)
904
+ {
905
+ if (this == &RHS)
906
+ {
907
+ if (!empty())
908
+ {
909
+ clear();
910
+ return true;
911
+ }
912
+ return false;
913
+ }
914
+
915
+ bool changed = false;
916
+ ElementListIter Iter1 = Elements.begin();
917
+ ElementListConstIter Iter2 = RHS.Elements.begin();
918
+
919
+ // If either our bitmap or RHS is empty, we are done
920
+ if (Elements.empty() || RHS.Elements.empty())
921
+ return false;
922
+
923
+ // Loop through, intersecting as we go, erasing elements when necessary.
924
+ while (Iter2 != RHS.Elements.end())
925
+ {
926
+ if (Iter1 == Elements.end())
927
+ {
928
+ CurrElementIter = Elements.begin();
929
+ return changed;
930
+ }
931
+
932
+ if (Iter1->index() > Iter2->index())
933
+ {
934
+ ++Iter2;
935
+ }
936
+ else if (Iter1->index() == Iter2->index())
937
+ {
938
+ bool BecameZero;
939
+ changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
940
+ if (BecameZero)
941
+ {
942
+ ElementListIter IterTmp = Iter1;
943
+ ++Iter1;
944
+ Elements.erase(IterTmp);
945
+ }
946
+ else
947
+ {
948
+ ++Iter1;
949
+ }
950
+ ++Iter2;
951
+ }
952
+ else
953
+ {
954
+ ++Iter1;
955
+ }
956
+ }
957
+ CurrElementIter = Elements.begin();
958
+ return changed;
959
+ }
960
+
961
+ bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const
962
+ {
963
+ return intersectWithComplement(*RHS);
964
+ }
965
+
966
+ // Three argument version of intersectWithComplement.
967
+ // Result of RHS1 & ~RHS2 is stored into this bitmap.
968
+ void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
969
+ const SparseBitVector<ElementSize> &RHS2)
970
+ {
971
+ if (this == &RHS1)
972
+ {
973
+ intersectWithComplement(RHS2);
974
+ return;
975
+ }
976
+ else if (this == &RHS2)
977
+ {
978
+ SparseBitVector RHS2Copy(RHS2);
979
+ intersectWithComplement(RHS1, RHS2Copy);
980
+ return;
981
+ }
982
+
983
+ Elements.clear();
984
+ CurrElementIter = Elements.begin();
985
+ ElementListConstIter Iter1 = RHS1.Elements.begin();
986
+ ElementListConstIter Iter2 = RHS2.Elements.begin();
987
+
988
+ // If RHS1 is empty, we are done
989
+ // If RHS2 is empty, we still have to copy RHS1
990
+ if (RHS1.Elements.empty())
991
+ return;
992
+
993
+ // Loop through, intersecting as we go, erasing elements when necessary.
994
+ while (Iter2 != RHS2.Elements.end())
995
+ {
996
+ if (Iter1 == RHS1.Elements.end())
997
+ return;
998
+
999
+ if (Iter1->index() > Iter2->index())
1000
+ {
1001
+ ++Iter2;
1002
+ }
1003
+ else if (Iter1->index() == Iter2->index())
1004
+ {
1005
+ bool BecameZero = false;
1006
+ Elements.emplace_back(Iter1->index());
1007
+ Elements.back().intersectWithComplement(*Iter1, *Iter2, BecameZero);
1008
+ if (BecameZero)
1009
+ Elements.pop_back();
1010
+ ++Iter1;
1011
+ ++Iter2;
1012
+ }
1013
+ else
1014
+ {
1015
+ Elements.push_back(*Iter1++);
1016
+ }
1017
+ }
1018
+
1019
+ // copy the remaining elements
1020
+ std::copy(Iter1, RHS1.Elements.end(), std::back_inserter(Elements));
1021
+ }
1022
+
1023
+ void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
1024
+ const SparseBitVector<ElementSize> *RHS2)
1025
+ {
1026
+ intersectWithComplement(*RHS1, *RHS2);
1027
+ }
1028
+
1029
+ bool intersects(const SparseBitVector<ElementSize> *RHS) const
1030
+ {
1031
+ return intersects(*RHS);
1032
+ }
1033
+
1034
+ // Return true if we share any bits in common with RHS
1035
+ bool intersects(const SparseBitVector<ElementSize> &RHS) const
1036
+ {
1037
+ ElementListConstIter Iter1 = Elements.begin();
1038
+ ElementListConstIter Iter2 = RHS.Elements.begin();
1039
+
1040
+ // Check if both bitmaps are empty.
1041
+ if (Elements.empty() && RHS.Elements.empty())
1042
+ return false;
1043
+
1044
+ // Loop through, intersecting stopping when we hit bits in common.
1045
+ while (Iter2 != RHS.Elements.end())
1046
+ {
1047
+ if (Iter1 == Elements.end())
1048
+ return false;
1049
+
1050
+ if (Iter1->index() > Iter2->index())
1051
+ {
1052
+ ++Iter2;
1053
+ }
1054
+ else if (Iter1->index() == Iter2->index())
1055
+ {
1056
+ if (Iter1->intersects(*Iter2))
1057
+ return true;
1058
+ ++Iter1;
1059
+ ++Iter2;
1060
+ }
1061
+ else
1062
+ {
1063
+ ++Iter1;
1064
+ }
1065
+ }
1066
+ return false;
1067
+ }
1068
+
1069
+ // Return true iff all bits set in this SparseBitVector are
1070
+ // also set in RHS.
1071
+ bool contains(const SparseBitVector<ElementSize> &RHS) const
1072
+ {
1073
+ SparseBitVector<ElementSize> Result(*this);
1074
+ Result &= RHS;
1075
+ return (Result == RHS);
1076
+ }
1077
+
1078
+ // Return the first set bit in the bitmap. Return -1 if no bits are set.
1079
+ int find_first() const
1080
+ {
1081
+ if (Elements.empty())
1082
+ return -1;
1083
+ const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
1084
+ return (First.index() * ElementSize) + First.find_first();
1085
+ }
1086
+
1087
+ // Return the last set bit in the bitmap. Return -1 if no bits are set.
1088
+ int find_last() const
1089
+ {
1090
+ if (Elements.empty())
1091
+ return -1;
1092
+ const SparseBitVectorElement<ElementSize> &Last = *(Elements.rbegin());
1093
+ return (Last.index() * ElementSize) + Last.find_last();
1094
+ }
1095
+
1096
+ // Return true if the SparseBitVector is empty
1097
+ bool empty() const
1098
+ {
1099
+ return Elements.empty();
1100
+ }
1101
+
1102
+ unsigned count() const
1103
+ {
1104
+ unsigned BitCount = 0;
1105
+ for (ElementListConstIter Iter = Elements.begin();
1106
+ Iter != Elements.end();
1107
+ ++Iter)
1108
+ BitCount += Iter->count();
1109
+
1110
+ return BitCount;
1111
+ }
1112
+
1113
+ iterator begin() const
1114
+ {
1115
+ return iterator(this);
1116
+ }
1117
+
1118
+ iterator end() const
1119
+ {
1120
+ return iterator(this, true);
1121
+ }
1122
+ };
1123
+
1124
+ // Convenience functions to allow Or and And without dereferencing in the user
1125
+ // code.
1126
+
1127
+ template <unsigned ElementSize>
1128
+ inline bool operator |=(SparseBitVector<ElementSize> &LHS,
1129
+ const SparseBitVector<ElementSize> *RHS)
1130
+ {
1131
+ return LHS |= *RHS;
1132
+ }
1133
+
1134
+ template <unsigned ElementSize>
1135
+ inline bool operator |=(SparseBitVector<ElementSize> *LHS,
1136
+ const SparseBitVector<ElementSize> &RHS)
1137
+ {
1138
+ return LHS->operator|=(RHS);
1139
+ }
1140
+
1141
+ template <unsigned ElementSize>
1142
+ inline bool operator &=(SparseBitVector<ElementSize> *LHS,
1143
+ const SparseBitVector<ElementSize> &RHS)
1144
+ {
1145
+ return LHS->operator&=(RHS);
1146
+ }
1147
+
1148
+ template <unsigned ElementSize>
1149
+ inline bool operator &=(SparseBitVector<ElementSize> &LHS,
1150
+ const SparseBitVector<ElementSize> *RHS)
1151
+ {
1152
+ return LHS &= *RHS;
1153
+ }
1154
+
1155
+ // Convenience functions for infix union, intersection, difference operators.
1156
+
1157
+ template <unsigned ElementSize>
1158
+ inline SparseBitVector<ElementSize>
1159
+ operator|(const SparseBitVector<ElementSize> &LHS,
1160
+ const SparseBitVector<ElementSize> &RHS)
1161
+ {
1162
+ SparseBitVector<ElementSize> Result(LHS);
1163
+ Result |= RHS;
1164
+ return Result;
1165
+ }
1166
+
1167
+ template <unsigned ElementSize>
1168
+ inline SparseBitVector<ElementSize>
1169
+ operator&(const SparseBitVector<ElementSize> &LHS,
1170
+ const SparseBitVector<ElementSize> &RHS)
1171
+ {
1172
+ SparseBitVector<ElementSize> Result(LHS);
1173
+ Result &= RHS;
1174
+ return Result;
1175
+ }
1176
+
1177
+ template <unsigned ElementSize>
1178
+ inline SparseBitVector<ElementSize>
1179
+ operator-(const SparseBitVector<ElementSize> &LHS,
1180
+ const SparseBitVector<ElementSize> &RHS)
1181
+ {
1182
+ SparseBitVector<ElementSize> Result;
1183
+ Result.intersectWithComplement(LHS, RHS);
1184
+ return Result;
1185
+ }
1186
+
1187
+ // Dump a SparseBitVector to a stream
1188
+ template <unsigned ElementSize>
1189
+ void dump(const SparseBitVector<ElementSize> &LHS, std::ostream &out)
1190
+ {
1191
+ out << "[";
1192
+
1193
+ typename SparseBitVector<ElementSize>::iterator bi = LHS.begin(),
1194
+ be = LHS.end();
1195
+ if (bi != be)
1196
+ {
1197
+ out << *bi;
1198
+ for (++bi; bi != be; ++bi)
1199
+ {
1200
+ out << " " << *bi;
1201
+ }
1202
+ }
1203
+ out << "]\n";
1204
+ }
1205
+
1206
+ }
1207
+
1208
+ #endif // SPARSEBITVECTOR_H
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svf-lib",
3
- "version": "1.0.1108",
3
+ "version": "1.0.1111",
4
4
  "description": "SVF's npm support",
5
5
  "main": "index.js",
6
6
  "scripts": {