Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
file_b_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_b_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} // namespace
50
51int main()
52{
53 const auto file_path =
54 std::filesystem::temp_directory_path() / "aleph_file_b_map_example.idx";
55 Cleanup cleanup{file_path};
56
57 std::cout << "Persistent B-Tree map example\n";
58 std::cout << "=============================\n";
59 std::cout << "file path : " << file_path.string() << "\n";
60
61 {
62 File_B_Map<int, int> inventory(file_path.string(), false);
63 inventory.insert(10, 3);
64 inventory.insert(20, 8);
65 inventory.insert(30, 5);
66 inventory.insert_or_assign(20, 11);
67 inventory.sync();
68
69 std::cout << "first session\n";
70 std::cout << "-------------\n";
71 std::cout << "inventory : ";
72 print_items(inventory.items());
73 std::cout << "\n";
74 std::cout << "page count : " << inventory.page_count() << "\n";
75 }
76
78 std::cout << "\nafter reopen\n";
79 std::cout << "------------\n";
80 std::cout << "read only : " << std::boolalpha << reopened.is_read_only() << "\n";
81 std::cout << "inventory : ";
82 print_items(reopened.items());
83 std::cout << "\n";
84 std::cout << "lower_bound(15): ";
85 const auto lb = reopened.lower_bound(15);
86 if (lb.has_value())
87 std::cout << "(" << lb->first << " -> " << lb->second << ")";
88 else
89 std::cout << "(none)";
90 std::cout << "\n";
91 return 0;
92}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
Persistent ordered map backed by a paged File_B_Tree.
int main()
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_B_Tree.