souffle  2.0.2-371-g6315b36
Public Member Functions | Data Fields | Static Public Attributes
souffle::RandomInsertPiggyList< T > Class Template Reference

A PiggyList that allows insertAt functionality. More...

#include <PiggyList.h>

Collaboration diagram for souffle::RandomInsertPiggyList< T >:
Collaboration graph

Public Member Functions

void clear ()
 
void freeList ()
 Free the arrays allocated within the linked list nodes. More...
 
T & get (size_t index) const
 
T * getBlock (size_t blockNum) const
 
void insertAt (size_t index, T value)
 
RandomInsertPiggyListoperator= (RandomInsertPiggyList &&other)=delete
 
RandomInsertPiggyListoperator= (RandomInsertPiggyList &other)=delete
 
 RandomInsertPiggyList ()=default
 
 RandomInsertPiggyList (const RandomInsertPiggyList &other)
 copy constructor More...
 
 RandomInsertPiggyList (RandomInsertPiggyList &&other)=delete
 
 RandomInsertPiggyList (size_t initialbitsize)
 
size_t size () const
 
 ~RandomInsertPiggyList ()
 

Data Fields

const size_t BLOCKBITS = 16ul
 
std::array< std::atomic< T * >, maxContainersblockLookupTable = {}
 
const size_t INITIALBLOCKSIZE = (1ul << BLOCKBITS)
 
std::atomic< size_t > numElements {0}
 
SpinLock slock
 

Static Public Attributes

static constexpr size_t maxContainers = 64
 

Detailed Description

template<class T>
class souffle::RandomInsertPiggyList< T >

A PiggyList that allows insertAt functionality.

This means we can't append, as we don't know the next available element. insertAt is dangerous. You must be careful not to call it for the same index twice!

Definition at line 37 of file PiggyList.h.

Constructor & Destructor Documentation

◆ RandomInsertPiggyList() [1/4]

template<class T >
souffle::RandomInsertPiggyList< T >::RandomInsertPiggyList ( )
default

◆ RandomInsertPiggyList() [2/4]

template<class T >
souffle::RandomInsertPiggyList< T >::RandomInsertPiggyList ( size_t  initialbitsize)
inline

Definition at line 42 of file PiggyList.h.

42 : BLOCKBITS(initialbitsize) {}

◆ RandomInsertPiggyList() [3/4]

template<class T >
souffle::RandomInsertPiggyList< T >::RandomInsertPiggyList ( const RandomInsertPiggyList< T > &  other)
inline

copy constructor

Definition at line 45 of file PiggyList.h.

45  : BLOCKBITS(other.BLOCKBITS) {
46  this->numElements.store(other.numElements.load());
47 
48  // copy blocks from the old lookup table to this one
49  for (size_t i = 0; i < maxContainers; ++i) {
50  if (other.blockLookupTable[i].load() != nullptr) {
51  // calculate the size of that block
52  const size_t blockSize = INITIALBLOCKSIZE << i;
53 
54  // allocate that in the new container
55  this->blockLookupTable[i].store(new T[blockSize]);
56 
57  // then copy the stuff over
58  std::memcpy(this->blockLookupTable[i].load(), other.blockLookupTable[i].load(),
59  blockSize * sizeof(T));
60  }
61  }
62  }

◆ RandomInsertPiggyList() [4/4]

template<class T >
souffle::RandomInsertPiggyList< T >::RandomInsertPiggyList ( RandomInsertPiggyList< T > &&  other)
delete

◆ ~RandomInsertPiggyList()

template<class T >
souffle::RandomInsertPiggyList< T >::~RandomInsertPiggyList ( )
inline

Definition at line 71 of file PiggyList.h.

71  {
72  freeList();
73  }

Member Function Documentation

◆ clear()

template<class T >
void souffle::RandomInsertPiggyList< T >::clear ( )
inline

Definition at line 110 of file PiggyList.h.

110  {
111  freeList();
112  numElements.store(0);
113  }

Referenced by souffle::SparseDisjointSet< value_type >::size(), and souffle::test::TEST().

◆ freeList()

template<class T >
void souffle::RandomInsertPiggyList< T >::freeList ( )
inline

Free the arrays allocated within the linked list nodes.

Definition at line 130 of file PiggyList.h.

130  {
131  slock.lock();
132  // delete all - deleting a nullptr is a no-op
133  for (size_t i = 0; i < maxContainers; ++i) {
134  delete[] blockLookupTable[i].load();
135  // reset the container within to be empty.
136  blockLookupTable[i].store(nullptr);
137  }
138  slock.unlock();
139  }

Referenced by souffle::RandomInsertPiggyList< SparseDomain >::clear(), and souffle::RandomInsertPiggyList< SparseDomain >::~RandomInsertPiggyList().

◆ get()

template<class T >
T& souffle::RandomInsertPiggyList< T >::get ( size_t  index) const
inline

Definition at line 83 of file PiggyList.h.

83  {
84  size_t nindex = index + INITIALBLOCKSIZE;
85  size_t blockNum = (63 - __builtin_clzll(nindex));
86  size_t blockInd = (nindex) & ((1 << blockNum) - 1);
87  return this->getBlock(blockNum - BLOCKBITS)[blockInd];
88  }

Referenced by souffle::RandomInsertPiggyList< SparseDomain >::insertAt().

◆ getBlock()

template<class T >
T* souffle::RandomInsertPiggyList< T >::getBlock ( size_t  blockNum) const
inline

Definition at line 79 of file PiggyList.h.

79  {
80  return blockLookupTable[blockNum];
81  }

Referenced by souffle::RandomInsertPiggyList< SparseDomain >::get().

◆ insertAt()

template<class T >
void souffle::RandomInsertPiggyList< T >::insertAt ( size_t  index,
value 
)
inline

Definition at line 90 of file PiggyList.h.

90  {
91  // starting with an initial blocksize requires some shifting to transform into a nice powers of two
92  // series
93  size_t blockNum = (63 - __builtin_clzll(index + INITIALBLOCKSIZE)) - BLOCKBITS;
94 
95  // allocate the block if not allocated
96  if (blockLookupTable[blockNum].load() == nullptr) {
97  slock.lock();
98  if (blockLookupTable[blockNum].load() == nullptr) {
99  blockLookupTable[blockNum].store(new T[INITIALBLOCKSIZE << blockNum]);
100  }
101  slock.unlock();
102  }
103 
104  this->get(index) = value;
105  // we ALWAYS increment size, even if there was something there before (its impossible to tell!)
106  // the onus is up to the user to not call this for an index twice
107  ++numElements;
108  }

Referenced by souffle::test::TEST().

◆ operator=() [1/2]

template<class T >
RandomInsertPiggyList& souffle::RandomInsertPiggyList< T >::operator= ( RandomInsertPiggyList< T > &&  other)
delete

◆ operator=() [2/2]

template<class T >
RandomInsertPiggyList& souffle::RandomInsertPiggyList< T >::operator= ( RandomInsertPiggyList< T > &  other)
delete

◆ size()

template<class T >
size_t souffle::RandomInsertPiggyList< T >::size ( ) const
inline

Definition at line 75 of file PiggyList.h.

75  {
76  return numElements.load();
77  }

Referenced by souffle::test::TEST().

Field Documentation

◆ BLOCKBITS

template<class T >
const size_t souffle::RandomInsertPiggyList< T >::BLOCKBITS = 16ul

◆ blockLookupTable

template<class T >
std::array<std::atomic<T*>, maxContainers> souffle::RandomInsertPiggyList< T >::blockLookupTable = {}

◆ INITIALBLOCKSIZE

template<class T >
const size_t souffle::RandomInsertPiggyList< T >::INITIALBLOCKSIZE = (1ul << BLOCKBITS)

◆ maxContainers

template<class T >
constexpr size_t souffle::RandomInsertPiggyList< T >::maxContainers = 64
staticconstexpr

◆ numElements

template<class T >
std::atomic<size_t> souffle::RandomInsertPiggyList< T >::numElements {0}

◆ slock

template<class T >
SpinLock souffle::RandomInsertPiggyList< T >::slock
mutable

The documentation for this class was generated from the following file:
souffle::RandomInsertPiggyList::slock
SpinLock slock
Definition: PiggyList.h:125
souffle::RandomInsertPiggyList::get
T & get(size_t index) const
Definition: PiggyList.h:83
souffle::RandomInsertPiggyList::INITIALBLOCKSIZE
const size_t INITIALBLOCKSIZE
Definition: PiggyList.h:115
souffle::RandomInsertPiggyList::blockLookupTable
std::array< std::atomic< T * >, maxContainers > blockLookupTable
Definition: PiggyList.h:122
souffle::RandomInsertPiggyList::numElements
std::atomic< size_t > numElements
Definition: PiggyList.h:118
i
size_t i
Definition: json11.h:663
souffle::RandomInsertPiggyList::BLOCKBITS
const size_t BLOCKBITS
Definition: PiggyList.h:114
souffle::RandomInsertPiggyList::getBlock
T * getBlock(size_t blockNum) const
Definition: PiggyList.h:79
souffle::RandomInsertPiggyList::freeList
void freeList()
Free the arrays allocated within the linked list nodes.
Definition: PiggyList.h:130
souffle::SpinLock::lock
void lock()
Definition: ParallelUtil.h:491
souffle::RandomInsertPiggyList::maxContainers
static constexpr size_t maxContainers
Definition: PiggyList.h:121
souffle::SpinLock::unlock
void unlock()
Definition: ParallelUtil.h:497