Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_hash.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
49# ifndef TPL_HASH_H
50# define TPL_HASH_H
51
52# include <initializer_list>
53# include <tpl_dynSetHash.H>
54# include <tpl_dynMapOhash.H>
55# include <ah-errors.H>
56
57namespace Aleph
58{
59 template <typename Key,
60 template <typename, class> class HashSetTable = OLhashTable,
62 struct HashSet : public HashSetTable<Key, Cmp>
63 {
65 using Base::Base;
66
67 void add(const DynList<Key> & l)
68 {
69 l.for_each([this](const Key & k)
70 {
71 this->insert(k);
72 });
73 }
74
76
77 HashSet() : Base() {}
78
79 Key &get_first() const { return this->get_it().get_curr(); }
80
81 Key &get_last() const
82 {
83 auto it = this->get_it();
84 it.reset_last();
85 return it.get_curr();
86 }
87
93 void clear() { this->empty(); }
94
95 using Base::contains;
96
102 bool is_empty() const noexcept { return this->Base::is_empty(); }
103 };
104
105 template
106 <typename Key, typename Data,
107 template <typename, typename, class> class HashMapTable = MapOLhash,
109 struct HashMap : public HashMapTable<Key, Data, Cmp>
110 {
112 using Base::Base;
113
114 void add(std::initializer_list<Key> lk, std::initializer_list<Data> ld)
115 {
116 ah_range_error_if(lk.size() != ld.size())
117 << "size mismatch between domain and range";
118
119 auto itk = lk.begin();
120 auto itd = ld.begin();
121 for (; itk != lk.end(); ++itk, ++itd)
122 this->insert(*itk, *itd);
123 }
124
125 HashMap() : Base() {}
126
127 HashMap(std::initializer_list<Key> lk, std::initializer_list<Data> ld)
128 : Base()
129 {
130 add(lk, ld);
131 }
132
133 template <template <typename T> class Container = DynList>
134 HashMap(const Container<Key> & c, std::initializer_list<Data> ld) : Base()
135 {
136 ah_range_error_if(c.size() != ld.size())
137 << "size mismatch between domain and range";
138
139 auto itd = ld.begin();
140 c.for_each(/* Lambda */ [this, &itd](const Key & key)
141 {
142 set_entry(key, *itd++);
143 });
144 }
145
147
153 void clear() { this->empty(); }
154
155 using Base::contains;
156
162 bool is_empty() const noexcept { return this->Base::is_empty(); }
163 };
164
169 template <typename Key,
170 template <typename, class> class HashSetTable = OLhashTable,
173
178 template <typename Key, typename Data,
179 template <typename, typename, class> class HashMapTable = MapOLhash,
182
183} // end namespace Aleph
184
185
186# endif // TPL_HASH_H
Exception handling system with formatted messages for Aleph-w.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:207
#define Generate_Proxy_Operator(Class_Name)
Generates operator[] for map-like containers.
Definition ahDry.H:151
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:113
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:170
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:779
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.
HashMapTable< Key, Data, Cmp > Base
Definition tpl_hash.H:111
void add(std::initializer_list< Key > lk, std::initializer_list< Data > ld)
Definition tpl_hash.H:114
bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_hash.H:162
void clear()
Empties the container.
Definition tpl_hash.H:153
HashMap(const Container< Key > &c, std::initializer_list< Data > ld)
Definition tpl_hash.H:134
HashMap(std::initializer_list< Key > lk, std::initializer_list< Data > ld)
Definition tpl_hash.H:127
Key & get_first() const
Definition tpl_hash.H:79
void clear()
Empties the container.
Definition tpl_hash.H:93
Key & get_last() const
Definition tpl_hash.H:81
void add(const DynList< Key > &l)
Definition tpl_hash.H:67
bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_hash.H:102
HashSetTable< Key, Cmp > Base
Definition tpl_hash.H:64
Open addressing hash map using linear probing.
static int * k
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
DynList< int > l