Aleph-w 7.10.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 {
64 typedef HashSetTable<Key, Cmp> Base;
65 using Base::Base;
67
68 void add(const DynList<Key> & l)
69 {
70 l.for_each([this](const Key & k)
71 {
72 this->insert(k);
73 });
74 }
75
77
78 HashSet() : Base() {}
79
80 const Key &get_first() const { return this->get_it().get_curr(); }
81
82 const Key &get_last() const
83 {
84 auto it = this->get_it();
85 it.reset_last();
86 return it.get_curr();
87 }
88
94 void clear() { this->empty(); }
95
96 using Base::contains;
97
103 bool is_empty() const noexcept { return this->Base::is_empty(); }
104 };
105
106 template
107 <typename Key, typename Data,
108 template <typename, typename, class> class HashMapTable = MapOLhash,
110 struct HashMap : public HashMapTable<Key, Data, Cmp>
111 {
112 typedef HashMapTable<Key, Data, Cmp> Base;
113 using Base::Base;
115
116 void add(std::initializer_list<Key> lk, std::initializer_list<Data> ld)
117 {
118 ah_range_error_if(lk.size() != ld.size())
119 << "size mismatch between domain and range";
120
121 auto itk = lk.begin();
122 auto itd = ld.begin();
123 for (; itk != lk.end(); ++itk, ++itd)
124 this->insert(*itk, *itd);
125 }
126
127 HashMap() : Base() {}
128
129 HashMap(std::initializer_list<Key> lk, std::initializer_list<Data> ld)
130 : Base()
131 {
132 add(lk, ld);
133 }
134
135 template <template <typename T> class Container = DynList>
136 HashMap(const Container<Key> & c, std::initializer_list<Data> ld) : Base()
137 {
138 ah_range_error_if(c.size() != ld.size())
139 << "size mismatch between domain and range";
140
141 auto itd = ld.begin();
142 c.for_each(/* Lambda */ [this, &itd](const Key & key)
143 {
144 set_entry(key, *itd++);
145 });
146 }
147
149
155 void clear() { this->empty(); }
156
157 using Base::contains;
158
164 bool is_empty() const noexcept { return this->Base::is_empty(); }
165 };
166
171 template <typename Key,
172 template <typename, class> class HashSetTable = OLhashTable,
175
180 template <typename Key, typename Data,
181 template <typename, typename, class> class HashMapTable = MapOLhash,
184
185} // end namespace Aleph
186
187
188# endif // TPL_HASH_H
#define Derived_Build(Name)
Declare build() in a class derived from a container.
Definition ah-dry.H:2881
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:356
#define Generate_Proxy_Operator(Class_Name)
Generates operator[] for map-like containers.
Definition ahDry.H:163
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:175
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
Main namespace for Aleph-w library functions.
HashMapTable< Key, Data, Cmp > Base
Definition tpl_hash.H:112
void add(std::initializer_list< Key > lk, std::initializer_list< Data > ld)
Definition tpl_hash.H:116
bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_hash.H:164
void clear()
Empties the container.
Definition tpl_hash.H:155
HashMap(const Container< Key > &c, std::initializer_list< Data > ld)
Definition tpl_hash.H:136
HashMap(std::initializer_list< Key > lk, std::initializer_list< Data > ld)
Definition tpl_hash.H:129
void clear()
Empties the container.
Definition tpl_hash.H:94
void add(const DynList< Key > &l)
Definition tpl_hash.H:68
const Key & get_last() const
Definition tpl_hash.H:82
bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_hash.H:103
HashSetTable< Key, Cmp > Base
Definition tpl_hash.H:64
const Key & get_first() const
Definition tpl_hash.H:80
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