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::ChunkBPlusTree< T > Class Template Reference

A B+ tree implementation for chunk indexing. More...

#include <advanced_structures.hpp>

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

Classes

struct  Node
 

Public Member Functions

 ChunkBPlusTree ()
 Constructs a new ChunkBPlusTree object.
 
void insert (const T &key)
 Inserts a key into the B+ tree.
 
bool search (const T &key) const
 Searches for a key in the B+ tree.
 

Private Member Functions

void insert_non_full (std::shared_ptr< Node > node, const T &key)
 
bool search_node (const std::shared_ptr< Node > &node, const T &key) const
 
void split_child (std::shared_ptr< Node > parent, int index)
 

Private Attributes

std::shared_ptr< Noderoot
 

Static Private Attributes

static constexpr int ORDER = 4
 

Detailed Description

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

A B+ tree implementation for chunk indexing.

Template Parameters
TThe type of elements stored in the tree

Definition at line 239 of file advanced_structures.hpp.

Constructor & Destructor Documentation

◆ ChunkBPlusTree()

template<typename T >
advanced_structures::ChunkBPlusTree< T >::ChunkBPlusTree ( )
inline

Constructs a new ChunkBPlusTree object.

Definition at line 257 of file advanced_structures.hpp.

257: root(std::make_shared<Node>()) {}

Member Function Documentation

◆ insert()

template<typename T >
void advanced_structures::ChunkBPlusTree< T >::insert ( const T &  key)
inline

Inserts a key into the B+ tree.

Parameters
keyThe key to insert

Definition at line 263 of file advanced_structures.hpp.

263 {
264 if (root->keys.empty()) {
265 root->keys.push_back(key);
266 return;
267 }
268
269 if (root->keys.size() == ORDER - 1) {
270 auto new_root = std::make_shared<Node>(false);
271 new_root->children.push_back(root);
272 split_child(new_root, 0);
273 root = new_root;
274 }
275 insert_non_full(root, key);
276 }
void insert_non_full(std::shared_ptr< Node > node, const T &key)
void split_child(std::shared_ptr< Node > parent, int index)

References advanced_structures::ChunkBPlusTree< T >::insert_non_full(), advanced_structures::ChunkBPlusTree< T >::ORDER, advanced_structures::ChunkBPlusTree< T >::root, and advanced_structures::ChunkBPlusTree< T >::split_child().

Referenced by TEST_F(), and TEST_F().

◆ insert_non_full()

template<typename T >
void advanced_structures::ChunkBPlusTree< T >::insert_non_full ( std::shared_ptr< Node node,
const T &  key 
)
inlineprivate

Definition at line 309 of file advanced_structures.hpp.

309 {
310 int i = node->keys.size() - 1;
311
312 if (node->is_leaf) {
313 node->keys.push_back(T());
314 while (i >= 0 && key < node->keys[i]) {
315 node->keys[i + 1] = node->keys[i];
316 i--;
317 }
318 node->keys[i + 1] = key;
319 } else {
320 while (i >= 0 && key < node->keys[i]) {
321 i--;
322 }
323 i++;
324
325 if (node->children[i]->keys.size() == ORDER - 1) {
326 split_child(node, i);
327 if (key > node->keys[i]) {
328 i++;
329 }
330 }
331 insert_non_full(node->children[i], key);
332 }
333 }

References advanced_structures::ChunkBPlusTree< T >::insert_non_full(), advanced_structures::ChunkBPlusTree< T >::ORDER, and advanced_structures::ChunkBPlusTree< T >::split_child().

Referenced by advanced_structures::ChunkBPlusTree< T >::insert(), and advanced_structures::ChunkBPlusTree< T >::insert_non_full().

◆ search()

template<typename T >
bool advanced_structures::ChunkBPlusTree< T >::search ( const T &  key) const
inline

Searches for a key in the B+ tree.

Parameters
keyThe key to search for
Returns
True if the key is found, false otherwise

Definition at line 283 of file advanced_structures.hpp.

283 {
284 if (root == nullptr)
285 return false;
286
287 return search_node(root, key);
288 }
bool search_node(const std::shared_ptr< Node > &node, const T &key) const

References advanced_structures::ChunkBPlusTree< T >::root, and advanced_structures::ChunkBPlusTree< T >::search_node().

Referenced by TEST_F(), and TEST_F().

◆ search_node()

template<typename T >
bool advanced_structures::ChunkBPlusTree< T >::search_node ( const std::shared_ptr< Node > &  node,
const T &  key 
) const
inlineprivate

Definition at line 335 of file advanced_structures.hpp.

335 {
336 int i = 0;
337 while (i < node->keys.size() && key > node->keys[i]) {
338 i++;
339 }
340
341 if (i < node->keys.size() && key == node->keys[i]) {
342 return true;
343 }
344
345 if (node->is_leaf) {
346 return false;
347 }
348
349 return search_node(node->children[i], key);
350 }

References advanced_structures::ChunkBPlusTree< T >::search_node().

Referenced by advanced_structures::ChunkBPlusTree< T >::search(), and advanced_structures::ChunkBPlusTree< T >::search_node().

◆ split_child()

template<typename T >
void advanced_structures::ChunkBPlusTree< T >::split_child ( std::shared_ptr< Node parent,
int  index 
)
inlineprivate

Definition at line 291 of file advanced_structures.hpp.

291 {
292 auto child = parent->children[index];
293 auto new_child = std::make_shared<Node>(child->is_leaf);
294
295 parent->keys.insert(parent->keys.begin() + index, child->keys[ORDER / 2 - 1]);
296
297 parent->children.insert(parent->children.begin() + index + 1, new_child);
298
299 new_child->keys.assign(child->keys.begin() + ORDER / 2, child->keys.end());
300
301 child->keys.resize(ORDER / 2 - 1);
302
303 if (!child->is_leaf) {
304 new_child->children.assign(child->children.begin() + ORDER / 2, child->children.end());
305 child->children.resize(ORDER / 2);
306 }
307 }

References advanced_structures::ChunkBPlusTree< T >::ORDER.

Referenced by advanced_structures::ChunkBPlusTree< T >::insert(), and advanced_structures::ChunkBPlusTree< T >::insert_non_full().

Member Data Documentation

◆ ORDER

◆ root

template<typename T >
std::shared_ptr<Node> advanced_structures::ChunkBPlusTree< T >::root
private

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