ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
KnuthHashing.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File KnuthHashing.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
44/*--------------------------------------------------------------------------*/
45/*----------------------------- DEFINITIONS --------------------------------*/
46/*--------------------------------------------------------------------------*/
47
48#ifndef __ADS_KnuthHashing
49 #define __ADS_KnuthHashing
50/* self-identification: #endif constant */
51
52/*--------------------------------------------------------------------------*/
53/*------------------------------ INCLUDES ----------------------------------*/
54/*--------------------------------------------------------------------------*/
55
56#include "ADS/Contract.h"
57#include "ADS/Iterators.h"
58
59#include <cstddef>
60#include <functional>
61#include <optional>
62#include <utility>
63#include <vector>
64
65/*--------------------------------------------------------------------------*/
66/*--------------------------- NAMESPACE ------------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69namespace ADS
70{
71
72/*--------------------------------------------------------------------------*/
73/*------------------------ CLASS CoalescedHashTable ------------------------*/
74/*--------------------------------------------------------------------------*/
75
77template< class K , class V , class H = std::hash< K > >
79{
80
81/*--------------------------------------------------------------------------*/
82 public:
85 explicit CoalescedHashTable( std::size_t m , std::size_t cellar = 0 ,
86 H h = H() );
87
90 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
91
117 void insert( const K & k , const V & v );
118
120 [[nodiscard]] std::size_t size( void ) const;
121
123 [[nodiscard]] std::size_t probes( void ) const;
124
125/*------------------------- THE STANDARD INTERFACE -------------------------*/
126
127 // a constant view of the pairs ( key , value ), in the order of the
128 // places (the cellar last), bidirectional
129
131 struct Places
132 {
134 static std::size_t size( const auto & o ) { return( o.f_T.size() ); }
136 static bool occupied( const auto & o , std::size_t i ) {
137 return( o.f_T[ i ].used );
138 }
139
140 static auto read( const auto & o , std::size_t i ) {
141 return(
142 PairRef< const K & , const V & >( o.f_T[ i ].key , o.f_T[ i ].value ) );
143 }
144 };
145
147 using value_type = std::pair< K , V >;
149 using key_type = K;
151 using mapped_type = V;
153 using size_type = std::size_t;
155 using difference_type = std::ptrdiff_t;
164
166 [[nodiscard]] const_iterator begin( void ) const {
167 return( const_iterator( this , 0 ) );
168 }
169
170 [[nodiscard]] const_iterator end( void ) const {
171 return( const_iterator( this , Places::size( *this ) ) );
172 }
173
174 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
176 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
177
178/*--------------------------------------------------------------------------*/
179 private:
180 struct Slot
181 {
182 bool used = false;
183 K key{};
184 V value{};
185 long link = -1;
186 };
187
189 [[nodiscard]] bool listed( void ) const;
190
191 std::vector< Slot > f_T;
192 std::size_t f_m;
193 long f_free;
194 std::size_t f_size = 0;
195 mutable std::size_t f_probes = 0;
196 H f_h;
197
198 }; // end( class CoalescedHashTable )
199
200/*--------------------------------------------------------------------------*/
201/*-------------------------- CLASS BrentHashTable --------------------------*/
202/*--------------------------------------------------------------------------*/
203
205template< class K , class V , class H = std::hash< K > >
207{
208
209/*--------------------------------------------------------------------------*/
210 public:
213 explicit BrentHashTable( std::size_t m , H h = H() );
214
218 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
219
247 void insert( const K & k , const V & v );
248
250 [[nodiscard]] std::size_t size( void ) const;
251
253 [[nodiscard]] std::size_t probes( void ) const;
254
255/*------------------------- THE STANDARD INTERFACE -------------------------*/
256
257 // a constant view of the pairs ( key , value ), in the order of the
258 // places, bidirectional; an insertion may move the pairs
259
261 struct Places
262 {
264 static std::size_t size( const auto & o ) { return( o.f_T.size() ); }
266 static bool occupied( const auto & o , std::size_t i ) {
267 return( o.f_T[ i ].has_value() );
268 }
269
270 static const std::pair< K , V > & read( const auto & o , std::size_t i ) {
271 return( *o.f_T[ i ] );
272 }
273 };
274
276 using value_type = std::pair< K , V >;
278 using key_type = K;
280 using mapped_type = V;
282 using size_type = std::size_t;
284 using difference_type = std::ptrdiff_t;
286 using reference = const std::pair< K , V > &;
288 using const_reference = const std::pair< K , V > &;
293
295 [[nodiscard]] const_iterator begin( void ) const {
296 return( const_iterator( this , 0 ) );
297 }
298
299 [[nodiscard]] const_iterator end( void ) const {
300 return( const_iterator( this , Places::size( *this ) ) );
301 }
302
303 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
305 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
306
307/*--------------------------------------------------------------------------*/
308 private:
310 [[nodiscard]] std::size_t at( const K & k , std::size_t i ) const;
311
313 std::vector< std::optional< std::pair< K , V > > > f_T;
314 std::size_t f_size = 0;
315 mutable std::size_t f_probes = 0;
316 H f_h;
317
318 }; // end( class BrentHashTable )
319
320/*--------------------------------------------------------------------------*/
321
322} // end( namespace ADS )
323
324/*--------------------------------------------------------------------------*/
325/*-------------------------- IMPLEMENTATION --------------------------------*/
326/*--------------------------------------------------------------------------*/
327
328#include "ADS/dictionary/impl/KnuthHashing.tpp"
329
330/*--------------------------------------------------------------------------*/
331
332#endif /* KnuthHashing.h included */
333
334/*--------------------------------------------------------------------------*/
335/*------------------------ End File KnuthHashing.h -------------------------*/
336/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
std::size_t size_type
the type of the sizes
Definition KnuthHashing.h:282
const std::pair< K, V > & reference
a reference to an item
Definition KnuthHashing.h:286
void insert(const K &k, const V &v)
associates to
const_iterator cbegin(void) const
the constant iterator on the first item
Definition KnuthHashing.h:303
std::size_t probes(void) const
the places examined by the last lookup()
BrentHashTable(std::size_t m, H h=H())
a table of places
const_iterator iterator
the iterator on the items
Definition KnuthHashing.h:292
K key_type
the type of the keys
Definition KnuthHashing.h:278
const_iterator cend(void) const
the constant iterator past the last item
Definition KnuthHashing.h:305
std::optional< V > lookup(const K &k) const
the value of the key , or nothing, along the places of its inspection up to an empty one
std::pair< K, V > value_type
the type of the items, for the standard library
Definition KnuthHashing.h:276
SlotIterator< BrentHashTable, Places > const_iterator
the iterator on the items, constant
Definition KnuthHashing.h:290
V mapped_type
the type of the values
Definition KnuthHashing.h:280
const std::pair< K, V > & const_reference
a constant reference to an item
Definition KnuthHashing.h:288
const_iterator end(void) const
the iterator past the last item
Definition KnuthHashing.h:299
std::size_t size(void) const
the number of keys
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition KnuthHashing.h:284
const_iterator begin(void) const
the iterator on the first item
Definition KnuthHashing.h:295
const_iterator begin(void) const
the iterator on the first item
Definition KnuthHashing.h:166
std::size_t size_type
the type of the sizes
Definition KnuthHashing.h:153
void insert(const K &k, const V &v)
associates to
std::pair< K, V > value_type
the type of the items, for the standard library
Definition KnuthHashing.h:147
PairRef< const K &, const V & > const_reference
a constant reference to an item
Definition KnuthHashing.h:159
const_iterator cend(void) const
the constant iterator past the last item
Definition KnuthHashing.h:176
const_iterator cbegin(void) const
the constant iterator on the first item
Definition KnuthHashing.h:174
const_iterator iterator
the iterator on the items
Definition KnuthHashing.h:163
SlotIterator< CoalescedHashTable, Places > const_iterator
the iterator on the items, constant
Definition KnuthHashing.h:161
std::size_t probes(void) const
the places examined by the last lookup() or insert()
V mapped_type
the type of the values
Definition KnuthHashing.h:151
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition KnuthHashing.h:155
CoalescedHashTable(std::size_t m, std::size_t cellar=0, H h=H())
a table of places addressed by the hash and of cellar more
std::size_t size(void) const
the number of keys
const_iterator end(void) const
the iterator past the last item
Definition KnuthHashing.h:170
PairRef< const K &, const V & > reference
a reference to an item
Definition KnuthHashing.h:157
std::optional< V > lookup(const K &k) const
the value of the key , or nothing, along the list of the place
K key_type
the type of the keys
Definition KnuthHashing.h:149
a bidirectional iterator on the occupied places of the table O, by the policy P with the static funct...
Definition Iterators.h:691
std::conditional_t< detail::plainPair< A, B, VA, VB >, std::pair< A, B >, detail::PairProxy< A, B, VA, VB > > PairRef
the pair that an iterator reads by value, in general two references into the container,...
Definition Iterators.h:648
Definition ADT.h:96
the places of the table, for SlotIterator
Definition KnuthHashing.h:262
static bool occupied(const auto &o, std::size_t i)
whether the place holds a key
Definition KnuthHashing.h:266
static std::size_t size(const auto &o)
the number of places
Definition KnuthHashing.h:264
static const std::pair< K, V > & read(const auto &o, std::size_t i)
the pair of the place
Definition KnuthHashing.h:270
the places of the table, for SlotIterator
Definition KnuthHashing.h:132
static auto read(const auto &o, std::size_t i)
the pair of the place
Definition KnuthHashing.h:140
static bool occupied(const auto &o, std::size_t i)
whether the place holds a key
Definition KnuthHashing.h:136
static std::size_t size(const auto &o)
the number of places
Definition KnuthHashing.h:134