Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
persistent_hash_map_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
38#include <cassert>
39#include <iostream>
40#include <string>
41
43
44using namespace Aleph;
45using namespace std;
46
47int main()
48{
49 cout << "Aleph-w PersistentHashMap Example" << endl;
50 cout << "=================================" << endl;
51
52 // Start with an empty immutable map.
54
55 // Insert a few bindings.
56 auto m1 = m0.insert("apple", 10).insert("orange", 20).insert("banana", 30);
57
58 cout << "m1 has " << m1.size() << " bindings." << endl;
59
60 assert(m1.contains("apple"));
61 assert(*m1.find("orange") == 20);
62
63 // m0 remains intact.
64 assert(m0.is_empty());
65
66 // insert() leaves existing bindings unchanged.
67 auto duplicate = m1.insert("apple", 15);
68 assert(*duplicate.find("apple") == 10);
69
70 // insert_or_assign() updates a binding in the returned version.
71 auto m2 = m1.insert_or_assign("apple", 15).insert("grape", 40);
72
73 cout << "m2 has " << m2.size() << " bindings after update/insert." << endl;
74
75 assert(*m2.find("apple") == 15);
76
77 // m1 was not mutated.
78 assert(*m1.find("apple") == 10);
79 assert(!m1.contains("grape"));
80
81 auto m3 = m2.erase("orange").erase("banana");
82
83 cout << "m3 has " << m3.size() << " bindings after erasing." << endl;
84 assert(!m3.contains("orange"));
85 assert(m3.contains("apple"));
86
87 assert(m2.contains("orange"));
88 assert(m1.verify());
89 assert(m2.verify());
90 assert(m3.verify());
91
92 cout << "All persistent structural-sharing invariants are valid." << endl;
93
94 return 0;
95}
Immutable unordered map backed by a Hash Array Mapped Trie (HAMT).
PersistentHashMap insert(const Key &key, const T &value) const
Return a new version with key inserted if absent.
size_t size() const noexcept
Return the number of bindings stored in this version.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
STL namespace.
Immutable path-copying hash map backed by a HAMT.