Static Value-Flow Analysis
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
SVF::SparseBitVectorElement< ElementSize > Struct Template Reference

#include <SparseBitVector.h>

Public Types

enum  { BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT , BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE , BITS_PER_ELEMENT = ElementSize }
 
using BitWord = unsigned long
 
using size_type = unsigned
 

Public Member Functions

 SparseBitVectorElement (unsigned Idx)
 
bool operator== (const SparseBitVectorElement &RHS) const
 
bool operator!= (const SparseBitVectorElement &RHS) const
 
BitWord word (unsigned Idx) const
 
unsigned index () const
 
bool empty () const
 
void set (unsigned Idx)
 
bool test_and_set (unsigned Idx)
 
void reset (unsigned Idx)
 
bool test (unsigned Idx) const
 
size_type count () const
 
int find_first () const
 find_first - Returns the index of the first set bit.
 
int find_last () const
 find_last - Returns the index of the last set bit.
 
int find_next (unsigned Curr) const
 
bool unionWith (const SparseBitVectorElement &RHS)
 
bool intersects (const SparseBitVectorElement &RHS) const
 
bool intersectWith (const SparseBitVectorElement &RHS, bool &BecameZero)
 
bool intersectWithComplement (const SparseBitVectorElement &RHS, bool &BecameZero)
 
void intersectWithComplement (const SparseBitVectorElement &RHS1, const SparseBitVectorElement &RHS2, bool &BecameZero)
 

Private Member Functions

 SparseBitVectorElement ()=default
 

Private Attributes

unsigned ElementIndex = 0
 
BitWord Bits [BITWORDS_PER_ELEMENT] = {0}
 

Detailed Description

template<unsigned ElementSize = 128>
struct SVF::SparseBitVectorElement< ElementSize >

SparseBitVector is an implementation of a bitvector that is sparse by only storing the elements that have non-zero bits set. In order to make this fast for the most common cases, SparseBitVector is implemented as a linked list of SparseBitVectorElements. We maintain a pointer to the last SparseBitVectorElement accessed (in the form of a list iterator), in order to make multiple in-order test/set constant time after the first one is executed. Note that using vectors to store SparseBitVectorElement's does not work out very well because it causes insertion in the middle to take enormous amounts of time with a large amount of bits. Other structures that have better worst cases for insertion in the middle (various balanced trees, etc) do not perform as well in practice as a linked list with this iterator kept up to date. They are also significantly more memory intensive.

Definition at line 259 of file SparseBitVector.h.

Member Typedef Documentation

◆ BitWord

template<unsigned ElementSize = 128>
using SVF::SparseBitVectorElement< ElementSize >::BitWord = unsigned long

Definition at line 263 of file SparseBitVector.h.

◆ size_type

template<unsigned ElementSize = 128>
using SVF::SparseBitVectorElement< ElementSize >::size_type = unsigned

Definition at line 264 of file SparseBitVector.h.

Member Enumeration Documentation

◆ anonymous enum

template<unsigned ElementSize = 128>
anonymous enum
Enumerator
BITWORD_SIZE 
BITWORDS_PER_ELEMENT 
BITS_PER_ELEMENT 

Definition at line 265 of file SparseBitVector.h.

266 {
267 BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT,
268 // N.B. (+ BITWORD_SIZE - 1) is to round up, to ensure we can have
269 // sufficient bits to represent *at least* ElementSize bits.
272 };
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74

Constructor & Destructor Documentation

◆ SparseBitVectorElement() [1/2]

template<unsigned ElementSize = 128>
SVF::SparseBitVectorElement< ElementSize >::SparseBitVectorElement ( )
privatedefault

◆ SparseBitVectorElement() [2/2]

template<unsigned ElementSize = 128>
SVF::SparseBitVectorElement< ElementSize >::SparseBitVectorElement ( unsigned  Idx)
inlineexplicit

Definition at line 282 of file SparseBitVector.h.

Member Function Documentation

◆ count()

template<unsigned ElementSize = 128>
size_type SVF::SparseBitVectorElement< ElementSize >::count ( ) const
inline

Definition at line 346 of file SparseBitVector.h.

347 {
348 unsigned NumBits = 0;
349 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
351 return NumBits;
352 }
unsigned countPopulation(T Value)
BitWord Bits[BITWORDS_PER_ELEMENT]

◆ empty()

template<unsigned ElementSize = 128>
bool SVF::SparseBitVectorElement< ElementSize >::empty ( ) const
inline

Definition at line 312 of file SparseBitVector.h.

313 {
314 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
315 if (Bits[i])
316 return false;
317 return true;
318 }

◆ find_first()

template<unsigned ElementSize = 128>
int SVF::SparseBitVectorElement< ElementSize >::find_first ( ) const
inline

find_first - Returns the index of the first set bit.

Definition at line 355 of file SparseBitVector.h.

356 {
357 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
358 if (Bits[i] != 0)
360 assert(false && "SBV: find_first: SBV cannot be empty");
361 abort();
362 }
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)

◆ find_last()

template<unsigned ElementSize = 128>
int SVF::SparseBitVectorElement< ElementSize >::find_last ( ) const
inline

find_last - Returns the index of the last set bit.

Definition at line 365 of file SparseBitVector.h.

366 {
367 for (unsigned I = 0; I < BITWORDS_PER_ELEMENT; ++I)
368 {
369 unsigned Idx = BITWORDS_PER_ELEMENT - I - 1;
370 if (Bits[Idx] != 0)
371 return Idx * BITWORD_SIZE + BITWORD_SIZE -
373 }
374 assert(false && "SBV: find_last: SBV cannot be empty");
375 abort();
376 }
unsigned countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)

◆ find_next()

template<unsigned ElementSize = 128>
int SVF::SparseBitVectorElement< ElementSize >::find_next ( unsigned  Curr) const
inline

find_next - Returns the index of the next set bit starting from the "Curr" bit. Returns -1 if the next set bit is not found.

Definition at line 380 of file SparseBitVector.h.

381 {
382 if (Curr >= BITS_PER_ELEMENT)
383 return -1;
384
385 unsigned WordPos = Curr / BITWORD_SIZE;
386 unsigned BitPos = Curr % BITWORD_SIZE;
387 BitWord Copy = Bits[WordPos];
389 && "Word Position outside of element");
390
391 // Mask off previous bits.
392 Copy &= ~0UL << BitPos;
393
394 if (Copy != 0)
395 return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
396
397 // Check subsequent words.
398 for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
399 if (Bits[i] != 0)
401 return -1;
402 }

◆ index()

template<unsigned ElementSize = 128>
unsigned SVF::SparseBitVectorElement< ElementSize >::index ( ) const
inline

Definition at line 307 of file SparseBitVector.h.

308 {
309 return ElementIndex;
310 }

◆ intersects()

template<unsigned ElementSize = 128>
bool SVF::SparseBitVectorElement< ElementSize >::intersects ( const SparseBitVectorElement< ElementSize > &  RHS) const
inline

Definition at line 420 of file SparseBitVector.h.

421 {
422 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
423 {
424 if (RHS.Bits[i] & Bits[i])
425 return true;
426 }
427 return false;
428 }

◆ intersectWith()

template<unsigned ElementSize = 128>
bool SVF::SparseBitVectorElement< ElementSize >::intersectWith ( const SparseBitVectorElement< ElementSize > &  RHS,
bool BecameZero 
)
inline

Definition at line 432 of file SparseBitVector.h.

434 {
435 bool changed = false;
436 bool allzero = true;
437
438 BecameZero = false;
439 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
440 {
441 BitWord old = changed ? 0 : Bits[i];
442
443 Bits[i] &= RHS.Bits[i];
444 if (Bits[i] != 0)
445 allzero = false;
446
447 if (!changed && old != Bits[i])
448 changed = true;
449 }
451 return changed;
452 }

◆ intersectWithComplement() [1/2]

template<unsigned ElementSize = 128>
bool SVF::SparseBitVectorElement< ElementSize >::intersectWithComplement ( const SparseBitVectorElement< ElementSize > &  RHS,
bool BecameZero 
)
inline

Definition at line 457 of file SparseBitVector.h.

459 {
460 bool changed = false;
461 bool allzero = true;
462
463 BecameZero = false;
464 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
465 {
466 BitWord old = changed ? 0 : Bits[i];
467
468 Bits[i] &= ~RHS.Bits[i];
469 if (Bits[i] != 0)
470 allzero = false;
471
472 if (!changed && old != Bits[i])
473 changed = true;
474 }
476 return changed;
477 }

◆ intersectWithComplement() [2/2]

template<unsigned ElementSize = 128>
void SVF::SparseBitVectorElement< ElementSize >::intersectWithComplement ( const SparseBitVectorElement< ElementSize > &  RHS1,
const SparseBitVectorElement< ElementSize > &  RHS2,
bool BecameZero 
)
inline

Definition at line 481 of file SparseBitVector.h.

484 {
485 bool allzero = true;
486
487 BecameZero = false;
488 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
489 {
490 Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
491 if (Bits[i] != 0)
492 allzero = false;
493 }
495 }

◆ operator!=()

template<unsigned ElementSize = 128>
bool SVF::SparseBitVectorElement< ElementSize >::operator!= ( const SparseBitVectorElement< ElementSize > &  RHS) const
inline

Definition at line 295 of file SparseBitVector.h.

296 {
297 return !(*this == RHS);
298 }

◆ operator==()

template<unsigned ElementSize = 128>
bool SVF::SparseBitVectorElement< ElementSize >::operator== ( const SparseBitVectorElement< ElementSize > &  RHS) const
inline

Definition at line 285 of file SparseBitVector.h.

286 {
287 if (ElementIndex != RHS.ElementIndex)
288 return false;
289 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
290 if (Bits[i] != RHS.Bits[i])
291 return false;
292 return true;
293 }

◆ reset()

template<unsigned ElementSize = 128>
void SVF::SparseBitVectorElement< ElementSize >::reset ( unsigned  Idx)
inline

Definition at line 336 of file SparseBitVector.h.

337 {
338 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
339 }

◆ set()

template<unsigned ElementSize = 128>
void SVF::SparseBitVectorElement< ElementSize >::set ( unsigned  Idx)
inline

Definition at line 320 of file SparseBitVector.h.

321 {
322 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
323 }

◆ test()

template<unsigned ElementSize = 128>
bool SVF::SparseBitVectorElement< ElementSize >::test ( unsigned  Idx) const
inline

Definition at line 341 of file SparseBitVector.h.

342 {
343 return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
344 }

◆ test_and_set()

template<unsigned ElementSize = 128>
bool SVF::SparseBitVectorElement< ElementSize >::test_and_set ( unsigned  Idx)
inline

Definition at line 325 of file SparseBitVector.h.

326 {
327 bool old = test(Idx);
328 if (!old)
329 {
330 set(Idx);
331 return true;
332 }
333 return false;
334 }
bool test(unsigned Idx) const

◆ unionWith()

template<unsigned ElementSize = 128>
bool SVF::SparseBitVectorElement< ElementSize >::unionWith ( const SparseBitVectorElement< ElementSize > &  RHS)
inline

Definition at line 405 of file SparseBitVector.h.

406 {
407 bool changed = false;
408 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
409 {
410 BitWord old = changed ? 0 : Bits[i];
411
412 Bits[i] |= RHS.Bits[i];
413 if (!changed && old != Bits[i])
414 changed = true;
415 }
416 return changed;
417 }

◆ word()

template<unsigned ElementSize = 128>
BitWord SVF::SparseBitVectorElement< ElementSize >::word ( unsigned  Idx) const
inline

Definition at line 301 of file SparseBitVector.h.

302 {
304 return Bits[Idx];
305 }

Member Data Documentation

◆ Bits

template<unsigned ElementSize = 128>
BitWord SVF::SparseBitVectorElement< ElementSize >::Bits[BITWORDS_PER_ELEMENT] = {0}
private

Definition at line 277 of file SparseBitVector.h.

277{0};

◆ ElementIndex

template<unsigned ElementSize = 128>
unsigned SVF::SparseBitVectorElement< ElementSize >::ElementIndex = 0
private

Definition at line 276 of file SparseBitVector.h.


The documentation for this struct was generated from the following file: