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 = ODhashTable,
61 class Cmp = Aleph::equal_to<Key>>
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 };
88
89 template
90 <typename Key, typename Data,
91 template <typename, typename, class> class HashMapTable = MapODhash,
93 struct HashMap : public HashMapTable<Key, Data, Cmp>
94 {
96 using Base::Base;
97
98 void add(std::initializer_list<Key> lk, std::initializer_list<Data> ld)
99 {
100 ah_range_error_if(lk.size() != ld.size())
101 << "size mismatch between domain and range";
102
103 auto itk = lk.begin();
104 auto itd = ld.begin();
105 for (; itk != lk.end(); ++itk, ++itd)
106 this->insert(*itk, *itd);
107 }
108
109 HashMap() : Base() {}
110
111 HashMap(std::initializer_list<Key> lk, std::initializer_list<Data> ld)
112 : Base()
113 {
114 add(lk, ld);
115 }
116
117 template <template <typename T> class Container = DynList>
118 HashMap(const Container<Key> & c, std::initializer_list<Data> ld) : Base()
119 {
120 ah_range_error_if(c.size() != ld.size())
121 << "size mismatch between domain and range";
122
123 auto itd = ld.begin();
124 c.for_each(/* Lambda */ [this, &itd](const Key & key)
125 {
126 set_entry(key, *itd++);
127 });
128 }
129
131 };
132} // end namespace Aleph
133
134
135# 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:1423
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:180
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:685
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
HashMapTable< Key, Data, Cmp > Base
Definition tpl_hash.H:95
void add(std::initializer_list< Key > lk, std::initializer_list< Data > ld)
Definition tpl_hash.H:98
HashMap(const Container< Key > &c, std::initializer_list< Data > ld)
Definition tpl_hash.H:118
HashMap(std::initializer_list< Key > lk, std::initializer_list< Data > ld)
Definition tpl_hash.H:111
Key & get_first() const
Definition tpl_hash.H:79
Key & get_last() const
Definition tpl_hash.H:81
void add(const DynList< Key > &l)
Definition tpl_hash.H:67
HashSetTable< Key, Cmp > Base
Definition tpl_hash.H:64
Open addressing hash map using double hashing.
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
DynList< int > l