Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hash-dry.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
43# ifndef HASH_DRY_H
44# define HASH_DRY_H
45
46# include <type_traits>
47
48# include <ahFunction.H>
49
50# define OHASH_COMMON(class_name) \
51 public: \
52 \
53 Hash_Fct get_hash_fct() const noexcept { return hash_fct; } \
54 \
55private: \
56 \
57 /* Rebuilds the table if the key just stored in curr_bucket took its */ \
58 /* buckets in use over the upper threshold (see */ \
59 /* capacity_after_insertion()), and returns where the key is. */ \
60 Key * test_resize(Bucket * curr_bucket, const Key & key) \
61 { \
62 const size_t new_len = this->capacity_after_insertion(); \
63 if (new_len == 0) \
64 return &curr_bucket->key; \
65 \
66 return rebuild_after_insertion(curr_bucket, key, new_len); \
67 } \
68 \
69 /* The rebuild itself, apart from the test that every insertion runs. */ \
70 /* If the table cannot be rebuilt, the key is taken out again and the */ \
71 /* exception goes on: an insertion that throws leaves the table as */ \
72 /* it was. */ \
73 Key * rebuild_after_insertion(Bucket * curr_bucket, const Key & key, \
74 const size_t new_len) \
75 { \
76 try \
77 { \
78 Key key_copy = key; \
79 this->rebuild(new_len); \
80 \
81 Key * key_ptr = search(key_copy); \
82 assert(key_ptr != nullptr); \
83 assert(cmp(*key_ptr, key_copy)); \
84 \
85 return key_ptr; \
86 } \
87 catch (...) \
88 { \
89 /* rebuild() leaves the buckets as they were when it fails */ \
90 this->undo_allocate_bucket(curr_bucket, curr_bucket->key); \
91 throw; \
92 } \
93 } \
94 \
95 static void update_stat_len(DynArray<size_t> & lens, size_t i) \
96 { \
97 if (lens.exist(i)) \
98 lens(i) += 1; \
99 else \
100 lens.touch(i) = 1; \
101 } \
102 \
103public: \
104 \
105 /* Sets the hash function. On a table that is not empty, it places */ \
106 /* the keys again with it, as rehash() does; if that throws, the */ \
107 /* table keeps its function and its keys. */ \
108 void set_hash_fct(Hash_Fct fct) \
109 { \
110 Hash_Fct old = std::move(hash_fct); \
111 hash_fct = std::move(fct); \
112 if (N == 0) \
113 return; \
114 \
115 try \
116 { \
117 this->rehash(); \
118 } \
119 catch (...) \
120 { \
121 hash_fct = std::move(old); \
122 throw; \
123 } \
124 } \
125 \
126 void set_hash_fct(Hash_Fct_Ptr fct) \
127 { \
128 set_hash_fct(Hash_Fct(fct)); \
129 }
130
131namespace Aleph
132{
133
134 extern const float hash_default_lower_alpha;
135
136 extern const float hash_default_upper_alpha;
137
138 namespace hash_table_detail
139 {
152 template <typename H, typename Ptr>
154 std::is_class_v<std::remove_cvref_t<H>> and std::is_convertible_v<H, Ptr>;
155 }
156
157}
158
159
160# endif // HASH_DRY_H
Standard functor implementations and comparison objects.
A class type that converts to the function pointer type Ptr.
Definition hash-dry.H:153
Main namespace for Aleph-w library functions.
const float hash_default_upper_alpha
Definition hash-dry.C:40
and
Check uniqueness with explicit hash + equality functors.
const float hash_default_lower_alpha
Definition hash-dry.C:38