Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bplus_tree_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
36#include <exception>
37#include <iostream>
38
39#include <ah-errors.H>
40#include <tpl_bplus_tree.H>
41
42using namespace Aleph;
43
44namespace
45{
46 template <typename T>
47 void print_array(const char * label, const Array<T> & values)
48 {
49 std::cout << label << " [";
50 for (size_t i = 0; i < values.size(); ++i)
51 {
52 if (i != 0)
53 std::cout << ", ";
54 std::cout << values[i];
55 }
56 std::cout << "]\n";
57 }
58}
59
60int main()
61{
62 try
63 {
65
66 Ledger ledger = {
67 105, 110, 115, 120, 125, 130, 135,
68 140, 145, 150, 155, 160, 165, 170
69 };
70
71 std::cout << "B+ Tree example\n";
72 std::cout << "==============\n";
73 print_array("all keys :", ledger.keys());
74 print_array("range 123..158:", ledger.range(123, 158));
75
76 if (const auto lb = ledger.lower_bound(128); lb.has_value())
77 std::cout << "lower_bound(128) -> " << *lb << '\n';
78
79 if (const auto ub = ledger.upper_bound(145); ub.has_value())
80 std::cout << "upper_bound(145) -> " << *ub << '\n';
81
82 ledger.insert(172);
83 ledger.insert(175);
84 ledger.remove(125);
85 ledger.remove(130);
86
88 << "BPlus_Tree example produced an invalid tree";
89
90 std::cout << "\nafter updates\n";
91 std::cout << "-------------\n";
92 print_array("all keys :", ledger.keys());
93 print_array("range 140..176:", ledger.range(140, 176));
94 std::cout << "size : " << ledger.size() << '\n';
95 std::cout << "height : " << ledger.height() << '\n';
96
97 std::cout << "\nB+ Tree keeps user keys in the leaves, so range scans walk leaf links.\n";
98 }
99 catch (const std::exception & ex)
100 {
101 std::cerr << "bplus_tree_example failed: " << ex.what() << '\n';
102 return 1;
103 }
104
105 return 0;
106}
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Definition ah-errors.H:250
int main()
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
Generic B+ Tree with configurable minimum degree.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
void print_array(const char *label, const Container &a)
B+ Tree implementation with linked leaves and configurable degree.