Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Transposition_Table.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
48# ifndef TRANSPOSITION_TABLE_H
49# define TRANSPOSITION_TABLE_H
50
51#include <concepts>
52#include <utility>
53
54#include <state_search_common.H>
55
56namespace Aleph {
57
60{
61 Exact,
64};
65
68{
69 Inserted,
70 Replaced,
72};
73
76{
77 size_t probes = 0;
78 size_t hits = 0;
79 size_t misses = 0;
80 size_t cutoffs = 0;
81 size_t stores = 0;
82 size_t replacements = 0;
83 size_t rejected_updates = 0;
84};
85
95template <typename Policy, typename Entry>
97 = requires(const Policy &policy, const Entry &candidate, const Entry &current) {
98 { policy(candidate, current) } -> std::convertible_to<bool>;
99 };
100
107template <typename Table, typename Key, typename Entry>
108concept MemoTable = requires(Table &table, const Key &key, const Entry &entry) {
109 { table.probe(key) } -> std::same_as<Entry *>;
110 { table.store(key, entry) } -> std::same_as<TranspositionStoreStatus>;
111};
112
117template <typename Entry>
119{
123 bool operator()(const Entry &, const Entry &) const noexcept
124 {
125 return true;
126 }
127};
128
133template <typename Entry>
134 requires requires(const Entry &entry) {
135 { entry.depth } -> std::convertible_to<size_t>;
136 }
138{
144 bool operator()(const Entry &candidate, const Entry &current) const noexcept
145 {
146 return candidate.depth > current.depth;
147 }
148};
149
163template <typename Key,
164 typename Entry,
165 template <typename, typename, class> class HashMapTable = MapOLhash,
170{
171public:
172 static constexpr bool enabled = true;
173
174 using Key_Type = Key;
178
183
189 {
190 // empty
191 }
192
197 {
198 table_.empty();
199 }
200
205 {
206 stats_ = {};
207 }
208
213 {
214 return table_.size();
215 }
216
221 {
222 return table_.is_empty();
223 }
224
229 {
230 return stats_;
231 }
232
238 [[nodiscard]] Entry *probe(const Key &key)
239 {
240 return probe_impl(key);
241 }
242
248 [[nodiscard]] Entry *probe(Key &&key)
249 {
250 return probe_impl(std::move(key));
251 }
252
264 [[nodiscard]] TranspositionStoreStatus store(const Key &key, const Entry &entry)
265 {
266 return store_impl(key, entry);
267 }
268
276 {
277 return store_impl(std::move(key), std::move(entry));
278 }
279
280private:
281 template <typename KeyLike>
283 {
284 ++stats_.probes;
285
286 auto *pair = table_.search(std::forward<KeyLike>(key));
287 if (pair == nullptr)
288 {
289 ++stats_.misses;
290 return nullptr;
291 }
292
293 ++stats_.hits;
294 return &pair->second;
295 }
296
297 template <typename KeyLike, typename EntryLike>
299 {
300 if (auto *current = table_.search(key); current != nullptr)
301 {
302 if (not replace_(entry, current->second))
303 {
306 }
307
308 current->second = std::forward<EntryLike>(entry);
309 ++stats_.stores;
312 }
313
314 (void) table_.insert(std::forward<KeyLike>(key), std::forward<EntryLike>(entry));
315 ++stats_.stores;
317 }
318
322};
323
324} // end namespace Aleph
325
326# endif // TRANSPOSITION_TABLE_H
Generic linear hash table.
Transposition-table container built on top of Aleph hash maps.
Entry * probe(const Key &key)
Probe one key and return the stored entry if present.
bool is_empty() const noexcept
Return true if the table contains no entries.
size_t size() const noexcept
Number of currently stored entries.
const TranspositionStats & stats() const noexcept
Read-only access to accumulated storage statistics.
TranspositionStoreStatus store(const Key &key, const Entry &entry)
Store a candidate entry for key.
Transposition_Table()=default
Build an empty table with the default replacement policy.
void reset_stats() noexcept
Reset only the accumulated storage statistics.
Entry * probe(Key &&key)
Probe one key using move semantics.
static constexpr bool enabled
Compile-time marker for search adapters.
Entry * probe_impl(KeyLike &&key)
TranspositionStoreStatus store_impl(KeyLike &&key, EntryLike &&entry)
void clear() noexcept
Remove all stored entries.
Transposition_Table(ReplacePolicy replace)
Build a table with a custom replacement policy instance.
TranspositionStoreStatus store(Key &&key, Entry entry)
Store a candidate entry for key using move semantics.
Minimal protocol for memo tables used by search engines.
Minimal protocol for replacement policies.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
TranspositionBound
Stored bound classification for memoized search entries.
@ Upper_Bound
Value is an upper bound (fail-low style).
@ Exact
Exact value for the stored search configuration.
@ Lower_Bound
Value is a lower bound (fail-high style).
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.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
TranspositionStoreStatus
Outcome of one store/update attempt in a transposition table.
@ Inserted
A new key was inserted.
@ Replaced
An existing key was updated according to replacement policy.
@ Rejected
Existing entry was kept and candidate was discarded.
void replace(Itor beg, const Itor &end, const T &old_value, const T &new_value)
Replace elements equal to a value.
Definition ahAlgo.H:815
STL namespace.
Common infrastructure for implicit state-space search.
bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_hash.H:162
Open addressing hash map using linear probing.
Replacement policy that prefers entries searched to greater depth.
bool operator()(const Entry &candidate, const Entry &current) const noexcept
Decide which entry to keep according to remaining depth.
Minimal replacement policy: always replace the current entry.
bool operator()(const Entry &, const Entry &) const noexcept
Decide whether to overwrite a stored entry.
Statistics collected by memo/transposition storage.
size_t replacements
Number of accepted overwrites of existing entries.
size_t probes
Number of probe attempts.
size_t misses
Number of failed probes.
size_t hits
Number of successful probes.
size_t cutoffs
Number of search cutoffs enabled by cached data.
size_t stores
Number of accepted stores (insertions or replacements).
size_t rejected_updates
Number of refused overwrites.