The class SumTree is a class which represents the Sum-Tree which is used in proportional prioritization. It implements all the methods necessary to create the Sum-Tree and sample from it. More...
#include <SumTree.h>
Public Member Functions | |
void | create_tree (std::deque< float_t > &priorities, std::optional< std::vector< SumTreeNode * > > &children) |
float_t | get_cumulative_sum () |
int64_t | get_tree_height () |
void | reset (int64_t parallelismSizeThreshold=4096) |
int64_t | sample (float_t seedValue, int64_t currentSize) |
SumTree () | |
SumTree (int32_t bufferSize) | |
void | update (int64_t index, float_t value) |
~SumTree () | |
Private Member Functions | |
void | propagate_changes_upwards (SumTreeNode *node, float_t change) |
SumTreeNode * | traverse (SumTreeNode *node, float_t value) |
Private Attributes | |
int64_t | bufferSize_ = 32768 |
Attribute to store the buffer size. If not initialised, when using SumTree::SumTree(), will be set to 32768. More... | |
std::vector< SumTreeNode * > | leaves_ |
A vector to store the pointers to dynamically allocated SumTreeNode leaves. More... | |
std::vector< SumTreeNode * > | sumTree_ |
A vector to store the pointers to dynamically allocated SumTreeNode nodes. More... | |
int64_t | treeHeight_ = 0 |
Attribute to store the tree height. More... | |
The class SumTree is a class which represents the Sum-Tree which is used in proportional prioritization. It implements all the methods necessary to create the Sum-Tree and sample from it.
|
explicit |
The class constructor will bufferSize. This constructor will reserve memory according the buffer size passed.
bufferSize | : Buffer Size to be elements to be used to create the sum tree. |
|
default |
Sum Tree default constructor.
|
default |
Sum Tree default destructor.
void SumTree::create_tree | ( | std::deque< float_t > & | priorities, |
std::optional< std::vector< SumTreeNode * > > & | children | ||
) |
Builds the sum-tree data structure on the fly.
priorities | : The priorities which are to be used to create the sum-tree. Priorities are used to create the leaves for the sum-tree. |
children | The children at a tree level. This is an optional argument and is only used for recursion. |
float_t SumTree::get_cumulative_sum | ( | ) |
Returns the cumulative sum of the sum tree. This is the value of the root node.
int64_t SumTree::get_tree_height | ( | ) |
Returns the height of the tree that is created. Height calculation includes the leaves level.
|
private |
Propagates changes upwards by making the change to each node recursively until root node.
*node | : The node pointer on which the change is to be applied. |
change | The change of value which is to be applied on the given node. |
void SumTree::reset | ( | int64_t | parallelismSizeThreshold = 4096 | ) |
Resets the sum tree by de-allocating all the SumTreeNode objects and clearing the relevant vectors.
parallelismSizeThreshold | : The length threshold for sumTree_ after which the de-allocation is done parallely with OpenMP |
int64_t SumTree::sample | ( | float_t | seedValue, |
int64_t | currentSize | ||
) |
Performs sampling by traversing the sum tree with seed value. The argument currentSize
is requested since this helps perform safety check for final sampled index to ensure that it is valid.
seedValue | : The seed value which is used to traverse the sum tree. |
currentSize | : The current size of the priorities from which the sum tree was created. |
|
private |
The traversal of the tree from the root node given a value. The value must be between 0 and root node's value, i.e. cumulative sum. A valid random value will select an index based on weighted uniform distribution.
*node | : The node pointer on which the change is to be applied. |
value | : A valid random value between 0 and root node's value. |
void SumTree::update | ( | int64_t | index, |
float_t | value | ||
) |
Updates the change sum tree by for a given leaf index by the given value. The changes are propagated upwards till the root.
index | : The leaf index to be updated. |
value | : The new value for leaf node at the specified index. |
|
private |
Attribute to store the buffer size. If not initialised, when using SumTree::SumTree(), will be set to 32768.
|
private |
A vector to store the pointers to dynamically allocated SumTreeNode leaves.
|
private |
A vector to store the pointers to dynamically allocated SumTreeNode nodes.
|
private |
Attribute to store the tree height.