Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-dispatcher.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
104#ifndef AH_DISPATCHER_H
105#define AH_DISPATCHER_H
106
107# include <iostream>
108# include <tpl_dynMapTree.H>
109# include <tpl_odhash.H>
110
136template <typename Key, class Operation>
138{
140
141public:
142
144 AHDispatcher() = default;
145
163 template <typename ... Pairs>
164 AHDispatcher(const Key & key, Operation op, Pairs ... pairs)
165 : AHDispatcher(pairs...)
166 {
167 tbl.insert(key, op);
168 }
169
178 void insert(const Key & key, Operation op)
179 {
180 tbl.insert(key, op);
181 }
182
195 template <typename ... Args>
196 auto run(const Key & key, Args && ... args) const
197 {
198 auto ptr = tbl.search(key);
199 if (ptr == nullptr)
200 {
201 std::cerr << "Tried to run dispatcher with a key"
202 << " which has not been registered" << std::endl;
203 abort();
204 }
205 return (ptr->second)(std::forward<Args>(args)...);
206 }
207
213 void remove(const Key & key)
214 {
215 tbl.remove(key);
216 }
217
224 bool valid_key(const Key & key) { return tbl.has(key); }
225
231 DynList<Key> keys() const { return tbl.keys(); }
232
238 size_t size() const { return tbl.size(); }
239
245 bool is_empty() const { return tbl.is_empty(); }
246};
247
248
269template <typename Key, class Operation>
271{
272 using P = std::pair<Key, Operation>;
273
274 struct Equal
275 {
276 bool operator () (const P & p1, const P & p2) const
277 {
278 return p1.first == p2.first;
279 }
280 };
281
282 static size_t fst_hash(const P & p) { return dft_hash_fct(p.first); }
283 static size_t snd_hash(const P & p) { return snd_hash_fct(p.first); }
284
286
287public:
288
291 : tbl(Primes::DefaultPrime, fst_hash, snd_hash) {}
292
299 void insert(const Key & key, Operation op)
300 {
301 (void) tbl.insert(P(key, op));
302 }
303
314 template <typename ... Args>
315 auto run(const Key & key, Args ... args) const
316 {
317 P p; p.first = key;
318 return tbl.find(p).second(args...);
319 }
320
326 void remove(const Key & key)
327 {
328 P p; p.first = key;
329 tbl.remove(p);
330 }
331
338 bool has(const Key & key) const
339 {
340 P p; p.first = key;
341 return tbl.has(p);
342 }
343};
344
345#endif // AH_DISPATCHER_H
Tree-based command dispatcher.
bool is_empty() const
Check if the dispatcher is empty.
bool valid_key(const Key &key)
Check if a key is registered.
void insert(const Key &key, Operation op)
Register a key-operation pair.
size_t size() const
Get the number of registered operations.
auto run(const Key &key, Args &&... args) const
Execute the operation associated with a key.
DynList< Key > keys() const
Get all registered keys.
AHDispatcher()=default
Default constructor.
AHDispatcher(const Key &key, Operation op, Pairs ... pairs)
Variadic constructor for initializing with key-operation pairs.
DynMapTree< Key, Operation > tbl
void remove(const Key &key)
Remove a key-operation pair.
Hash-based command dispatcher.
bool has(const Key &key) const
Check if a key is registered.
auto run(const Key &key, Args ... args) const
Execute the operation associated with a key.
ODhashTable< P, Equal > tbl
static size_t fst_hash(const P &p)
void insert(const Key &key, Operation op)
Register a key-operation pair.
static size_t snd_hash(const P &p)
void remove(const Key &key)
Remove a key-operation pair.
AhHashDispatcher()
Default constructor.
std::pair< Key, Operation > P
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
Generic key-value map implemented on top of a binary search tree.
Data remove(const Key &key)
Deletes the pair key,data
Pair * search(const Key &key) const noexcept
Collect all keys.
bool has(const Key &key) const noexcept
DynList< Key > keys() const
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
const size_t & size() const
Returns the cardinality of the set.
bool is_empty() const
returns true if the set is empty
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:180
size_t snd_hash_fct(const Key &key) noexcept
Definition hash-fct.H:937
size_t dft_hash_fct(const Key &key) noexcept
Definition hash-fct.H:931
DynList< T > maps(const C &c, Op op)
Classic map operation.
bool operator()(const P &p1, const P &p2) const
Dynamic key-value map based on balanced binary search trees.
Open addressing hash table with double hashing.