ORIGINAL
|
Skip List container implementation. More...
#include <skipList.h>
Classes | |
class | Iterator |
Forward iterator for skipList. More... | |
class | skipListNode |
Internal node class for Skip List. More... | |
Public Types | |
using | rebind_alloc_node = ALLOC::template rebind_alloc< skipListNode > |
Rebound allocator for nodes. | |
using | rebind_alloc_pointer = ALLOC::template rebind_alloc< skipListNode * > |
Rebound allocator for pointers. | |
Public Member Functions | |
skipListNode * | createNode (const K_TYPE &key=K_TYPE{}, const V_TYPE &value=V_TYPE{}, u_integer levels=1, std::initializer_list< skipListNode * > next={}) const |
Creates a new node with given parameters. | |
void | destroyNode (skipListNode *node) const |
Destroys a node and deallocates memory. | |
bool | highPriority (skipListNode *cur, skipListNode *next) const |
Compares priority between two nodes. | |
bool | highPriority (const K_TYPE &key, skipListNode *next) const |
Compares priority between a key and a node. | |
u_integer | getRandomLevels () const |
Generates random number of levels for new node. | |
u_integer | getCurLevels () const |
Gets current number of levels in list. | |
void | expandCurLevels (u_integer new_levels) |
Expands list to more levels. | |
void | shrinkCurLevels (u_integer new_levels) |
Shrinks list to fewer levels. | |
skipListNode * | listCopy () const |
Creates a deep copy of the list. | |
skipListNode * | findLastNode () const |
Finds last node in list. | |
skipList (Compare compare=Compare{}) | |
Constructs skipList with given comparison function. | |
skipListNode * | find (const K_TYPE &key) const |
Finds node with given key. | |
bool | modify (const K_TYPE &key, const V_TYPE &value) |
Modifies value for existing key. | |
bool | insert (const K_TYPE &key, const V_TYPE &value) |
Inserts new key-value pair. | |
bool | erase (const K_TYPE &key) |
Erases node with given key. | |
void | listDestroy () noexcept |
Destroys entire list and deallocates all nodes. | |
virtual | ~skipList () |
Destructor. | |
Static Public Member Functions | |
static bool | equal (const K_TYPE &key, skipListNode *next) |
Checks if key matches node's key. | |
Public Attributes | |
u_integer | size_ |
Number of elements. | |
skipListNode * | head_ |
Head node pointer. | |
Compare | compare_ |
Comparison function. | |
rebind_alloc_node | rebind_alloc {} |
Node allocator. | |
std::mt19937 | gen_ {std::random_device{}()} |
Random number generator. | |
std::uniform_real_distribution | dis_ {0.0, 1.0} |
Uniform distribution for level generation. | |
friend | Iterator |
Skip List container implementation.
K_TYPE | Key type (must be comparable) |
V_TYPE | Value type |
ALLOC | Allocator type (default: allocator<K_TYPE>) |
Compare | Comparison function type (default: increaseComparator<K_TYPE>) |
This class provides a probabilistic alternative to balanced trees with:
|
explicit |
Constructs skipList with given comparison function.
compare | Comparison function to use |
|
virtual |
Destructor.
Cleans up all list nodes and allocated memory
original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::skipListNode * original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::createNode | ( | const K_TYPE & | key = K_TYPE{} , |
const V_TYPE & | value = V_TYPE{} , |
||
u_integer | levels = 1 , |
||
std::initializer_list< skipListNode * > | next = {} |
||
) | const |
Creates a new node with given parameters.
key | Key for new node |
value | Value for new node |
levels | Number of levels for new node |
next | Initializer list of next pointers |
Uses allocator to construct node in place
void original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::destroyNode | ( | skipListNode * | node | ) | const |
Destroys a node and deallocates memory.
node | Node to destroy |
Uses allocator to destroy and deallocate node
|
static |
Checks if key matches node's key.
key | Key to compare |
next | Node to compare |
Erases node with given key.
key | Key to erase |
Automatically adjusts list levels if needed
Expands list to more levels.
new_levels | New total number of levels |
original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::skipListNode * original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::find | ( | const K_TYPE & | key | ) | const |
Finds node with given key.
key | Key to search for |
Uses multi-level search for efficiency
original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::skipListNode * original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::findLastNode | ( | ) | const |
Finds last node in list.
original::u_integer original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::getCurLevels | ( | ) | const |
Gets current number of levels in list.
original::u_integer original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::getRandomLevels | ( | ) | const |
Generates random number of levels for new node.
bool original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::highPriority | ( | const K_TYPE & | key, |
skipListNode * | next | ||
) | const |
Compares priority between a key and a node.
key | Key to compare |
next | Node to compare |
Uses the list's comparison function
bool original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::highPriority | ( | skipListNode * | cur, |
skipListNode * | next | ||
) | const |
Compares priority between two nodes.
cur | First node to compare |
next | Second node to compare |
Uses the list's comparison function
bool original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::insert | ( | const K_TYPE & | key, |
const V_TYPE & | value | ||
) |
Inserts new key-value pair.
key | Key to insert |
value | Value to insert |
Automatically adjusts node levels probabilistically
original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::skipListNode * original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::listCopy | ( | ) | const |
Creates a deep copy of the list.
|
noexcept |
Destroys entire list and deallocates all nodes.
Uses sequential traversal to destroy all nodes
bool original::skipList< K_TYPE, V_TYPE, ALLOC, Compare >::modify | ( | const K_TYPE & | key, |
const V_TYPE & | value | ||
) |
Modifies value for existing key.
key | Key to modify |
value | New value to set |
Shrinks list to fewer levels.
new_levels | New total number of levels |