Advanced Chunk Processing Library 0.2.0
A comprehensive C++ library for advanced data chunking strategies and processing operations
Loading...
Searching...
No Matches
advanced_structures::ChunkSkipList< T > Class Template Reference

A skip list implementation for efficient chunk searching. More...

#include <advanced_structures.hpp>

+ Collaboration diagram for advanced_structures::ChunkSkipList< T >:

Classes

struct  Node
 

Public Member Functions

 ChunkSkipList (int max_lvl=16, float prob=0.5)
 Constructs a new ChunkSkipList object.
 
void insert (const T &value)
 Inserts a value into the skip list.
 
bool search (const T &value) const
 Searches for a value in the skip list.
 

Private Member Functions

int random_level ()
 Generates a random level for node insertion.
 

Private Attributes

int current_level
 
std::shared_ptr< Nodehead
 
int max_level
 
float p
 

Detailed Description

template<typename T>
class advanced_structures::ChunkSkipList< T >

A skip list implementation for efficient chunk searching.

Template Parameters
TThe type of elements stored in the skip list

Definition at line 151 of file advanced_structures.hpp.

Constructor & Destructor Documentation

◆ ChunkSkipList()

template<typename T >
advanced_structures::ChunkSkipList< T >::ChunkSkipList ( int  max_lvl = 16,
float  prob = 0.5 
)
inline

Constructs a new ChunkSkipList object.

Parameters
max_lvlMaximum level of the skip list
probProbability factor for level generation

Definition at line 182 of file advanced_structures.hpp.

References advanced_structures::ChunkSkipList< T >::head, and advanced_structures::ChunkSkipList< T >::max_level.

Member Function Documentation

◆ insert()

template<typename T >
void advanced_structures::ChunkSkipList< T >::insert ( const T &  value)
inline

Inserts a value into the skip list.

Parameters
valueThe value to insert

Definition at line 191 of file advanced_structures.hpp.

191 {
192 std::vector<std::shared_ptr<Node>> update(max_level);
193 auto current = head;
194
195 for (int i = current_level - 1; i >= 0; i--) {
196 while (current->forward[i] && current->forward[i]->value < value) {
197 current = current->forward[i];
198 }
199 update[i] = current;
200 }
201
202 int new_level = random_level();
203 if (new_level > current_level) {
204 for (int i = current_level; i < new_level; i++) {
205 update[i] = head;
206 }
207 current_level = new_level;
208 }
209
210 auto new_node = std::make_shared<Node>(value, new_level);
211 for (int i = 0; i < new_level; i++) {
212 new_node->forward[i] = update[i]->forward[i];
213 update[i]->forward[i] = new_node;
214 }
215 }
int random_level()
Generates a random level for node insertion.

References advanced_structures::ChunkSkipList< T >::current_level, advanced_structures::ChunkSkipList< T >::head, advanced_structures::ChunkSkipList< T >::max_level, and advanced_structures::ChunkSkipList< T >::random_level().

Referenced by TEST_F(), and TEST_F().

◆ random_level()

template<typename T >
int advanced_structures::ChunkSkipList< T >::random_level ( )
inlineprivate

Generates a random level for node insertion.

Returns
The random level

Definition at line 168 of file advanced_structures.hpp.

168 {
169 int lvl = 1;
170 while ((static_cast<float>(rand()) / RAND_MAX) < p && lvl < max_level) {
171 lvl++;
172 }
173 return lvl;
174 }

References advanced_structures::ChunkSkipList< T >::max_level, and advanced_structures::ChunkSkipList< T >::p.

Referenced by advanced_structures::ChunkSkipList< T >::insert().

◆ search()

template<typename T >
bool advanced_structures::ChunkSkipList< T >::search ( const T &  value) const
inline

Searches for a value in the skip list.

Parameters
valueThe value to search for
Returns
True if the value is found, false otherwise

Definition at line 222 of file advanced_structures.hpp.

222 {
223 auto current = head;
224 for (int i = current_level - 1; i >= 0; i--) {
225 while (current->forward[i] && current->forward[i]->value < value) {
226 current = current->forward[i];
227 }
228 }
229 current = current->forward[0];
230 return current && current->value == value;
231 }

References advanced_structures::ChunkSkipList< T >::current_level, and advanced_structures::ChunkSkipList< T >::head.

Referenced by TEST_F(), TEST_F(), and TEST_F().

Member Data Documentation

◆ current_level

template<typename T >
int advanced_structures::ChunkSkipList< T >::current_level
private

◆ head

◆ max_level

◆ p

template<typename T >
float advanced_structures::ChunkSkipList< T >::p
private

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