Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
file_bplus_map_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
9#include <filesystem>
10#include <iostream>
11#include <string>
12#include <utility>
13
14#include <tpl_file_bplus_map.H>
15
16using Aleph::Array;
18
19namespace
20{
21 template <typename K, typename V>
22 void print_items(const Array<std::pair<K, V>> & items)
23 {
24 std::cout << "[";
25 for (size_t i = 0; i < items.size(); ++i)
26 {
27 if (i != 0)
28 std::cout << ", ";
29 std::cout << "(" << items[i].first << " -> " << items[i].second << ")";
30 }
31 std::cout << "]";
32 }
33
34 struct Cleanup
35 {
36 std::filesystem::path path;
37
38 ~Cleanup()
39 {
40 std::error_code ec;
41 std::filesystem::remove(path, ec);
42 std::filesystem::remove(path.string() + ".lock", ec);
43 std::filesystem::remove(path.string() + ".wal", ec);
44 std::filesystem::remove(path.string() + ".wal.tmp", ec);
45 std::filesystem::remove(path.string() + ".journal", ec);
46 std::filesystem::remove(path.string() + ".journal.tmp", ec);
47 }
48 };
49
50 template <typename Map>
51 void print_iter_items(const Map & map, const int first, const int last)
52 {
53 std::cout << "[";
54 bool first_item = true;
55 for (auto it = map.get_range_it(first, last); it.has_curr(); it.next_ne())
56 {
57 if (not first_item)
58 std::cout << ", ";
59 const auto item = it.get_curr();
60 std::cout << "(" << item.first << " -> " << item.second << ")";
61 first_item = false;
62 }
63 std::cout << "]";
64 }
65} // namespace
66
67int main()
68{
69 const auto file_path =
70 std::filesystem::temp_directory_path() / "aleph_file_bplus_map_example.idx";
71 Cleanup cleanup{file_path};
72
73 std::cout << "Persistent B+ Tree map example\n";
74 std::cout << "==============================\n";
75 std::cout << "file path : " << file_path.string() << "\n";
76
77 {
78 File_BPlus_Map<int, int> prices(file_path.string(), false);
79 prices.insert(101, 750);
80 prices.insert(105, 920);
81 prices.insert(110, 860);
82 prices.insert(120, 990);
83 prices.insert_or_assign(110, 875);
84 prices.sync();
85
86 std::cout << "first session\n";
87 std::cout << "-------------\n";
88 std::cout << "all items : ";
89 print_items(prices.items());
90 std::cout << "\n";
91 std::cout << "range 104..115: ";
92 print_items(prices.range(104, 115));
93 std::cout << "\n";
94 std::cout << "iter 104..115 : ";
95 print_iter_items(prices, 104, 115);
96 std::cout << "\n";
97 }
98
99 File_BPlus_Map<int, int> reopened(file_path.string(),
101 std::cout << "\nafter reopen\n";
102 std::cout << "------------\n";
103 std::cout << "read only : " << std::boolalpha << reopened.is_read_only() << "\n";
104 std::cout << "all items : ";
105 print_items(reopened.items());
106 std::cout << "\n";
107 std::cout << "iter 104..115 : ";
108 print_iter_items(reopened, 104, 115);
109 std::cout << "\n";
110 std::cout << "upper_bound(110): ";
111 const auto ub = reopened.upper_bound(110);
112 if (ub.has_value())
113 std::cout << "(" << ub->first << " -> " << ub->second << ")";
114 else
115 std::cout << "(none)";
116 std::cout << "\n";
117 return 0;
118}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
Persistent ordered map backed by a paged File_BPlus_Tree.
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.
Persistent key/value map built on top of Aleph::File_BPlus_Tree.