carl
24.04
Computer ARithmetic Library
|
This class packs a complete binary tree in a vector. More...
#include <CompactTree.h>
Data Structures | |
class | Node |
Public Member Functions | |
CompactTree (std::size_t initialCapacity=0) | |
CompactTree (const CompactTree &tree, std::size_t minCapacity=0) | |
~CompactTree () | |
Entry & | operator[] (Node n) |
const Entry & | operator[] (Node n) const |
bool | empty () const |
std::size_t | size () const |
std::size_t | capacity () const |
Node | lastLeaf () const |
void | pushBack (const Entry &value) |
void | pushBackWithCapacity (const Entry &value) |
void | popBack () |
bool | hasFreeCapacity (size_t extraCapacity) const |
void | increaseCapacity () |
void | swap (CompactTree &tree) |
void | print (std::ostream &out) const |
void | clear () |
std::size_t | getMemoryUse () const |
bool | isValid () const |
std::vector< Entry > | getCopy () const |
Private Member Functions | |
CompactTree & | operator= (const CompactTree &tree) const =delete |
Private Attributes | |
Entry * | _array |
Node | _lastLeaf |
Node | _capacityEnd |
This class packs a complete binary tree in a vector.
The idea is to have the root at index 1, and then the left child of node n will be at index 2n and the right child will be at index 2n + 1. The corresponding formulas when indexes start at 0 take more computation, so we need a 1-based array so we can't use std::vector.
Also, when sizeof(Entry) is a power of 2 it is faster to keep track of i * sizeof(Entry) than directly keeping track of an index i. This doesn't work well when sizeof(Entry) is not a power of two. So we need both possibilities. That is why this class never exposes indexes. Instead you interact with Node objects that serve the role of an index, but the precise value it stores is encapsulated. This way you can't do something like _array[i * sizeof(Entry)] by accident. Client code also does not need to (indeed, can't) be aware of how indexes are calculated, stored and looked up.
If FastIndex is false, then Nodes contain an index i. If FastIndex is true, then Nodes contain the byte offset i * sizeof(Entry). FastIndex must be false if sizeof(Entry) is not a power of two.
Definition at line 42 of file CompactTree.h.
|
explicit |
carl::CompactTree< Entry, FastIndex >::CompactTree | ( | const CompactTree< Entry, FastIndex > & | tree, |
std::size_t | minCapacity = 0 |
||
) |
|
inline |
Definition at line 48 of file CompactTree.h.
|
inline |
Definition at line 66 of file CompactTree.h.
void carl::CompactTree< E, FI >::clear |
Definition at line 194 of file CompactTree.h.
|
inline |
|
inline |
size_t carl::CompactTree< E, FI >::getMemoryUse |
Definition at line 200 of file CompactTree.h.
bool carl::CompactTree< E, FI >::hasFreeCapacity | ( | size_t | extraCapacity | ) | const |
void carl::CompactTree< E, FI >::increaseCapacity |
bool carl::CompactTree< E, FI >::isValid |
Definition at line 353 of file CompactTree.h.
|
inline |
|
privatedelete |
E & carl::CompactTree< E, FI >::operator[] | ( | Node | n | ) |
Definition at line 243 of file CompactTree.h.
const E & carl::CompactTree< E, FI >::operator[] | ( | Node | n | ) | const |
Definition at line 255 of file CompactTree.h.
void carl::CompactTree< E, FI >::popBack |
void carl::CompactTree< E, FI >::print | ( | std::ostream & | out | ) | const |
void carl::CompactTree< E, FI >::pushBack | ( | const Entry & | value | ) |
void carl::CompactTree< E, FI >::pushBackWithCapacity | ( | const Entry & | value | ) |
Definition at line 271 of file CompactTree.h.
|
inline |
void carl::CompactTree< E, FI >::swap | ( | CompactTree< Entry, FastIndex > & | tree | ) |
|
private |
Definition at line 188 of file CompactTree.h.
|
private |
Definition at line 190 of file CompactTree.h.
|
private |
Definition at line 189 of file CompactTree.h.