aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
hashFunc.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48#ifndef GUM_HASH_FUNC_H
49#define GUM_HASH_FUNC_H
50
51// utility provides the std::pair <> template
52#include <climits>
53#include <memory>
54#include <string>
55#include <utility>
56
57#include <agrum/agrum.h>
58
59#include <string_view>
60#include <type_traits>
61
62namespace gum {
63
81 unsigned int _hashTableLog2_(const Size nb);
82
100 static constexpr Size gold
101 = sizeof(Size) == 4 ? Size(2654435769UL) : Size(11400714819323198486UL);
102 static constexpr Size pi // pi / 4
103 = sizeof(Size) == 4 ? Size(3373259426UL) : Size(14488038916154245684UL);
104 static constexpr Size sqrt3 // sqrt(3) / 2
105 = sizeof(Size) == 4 ? Size(3719550786UL) : Size(15975348984942515101UL);
106 static constexpr Size mask
107 = sizeof(Size) == 4 ? Size(4294967295UL) : Size(18446744073709551615UL);
108 static constexpr Size offset = sizeof(Size) == 4 ? Size(32) : Size(64);
109 };
110
111 // ===========================================================================
112 // === BASE CLASS SHARED BY ALL THE HASH FUNCTIONS ===
113 // ===========================================================================
173 template < typename Key >
175 public:
191 void resize(const Size new_size);
192
198 Size size() const;
199
222 virtual Size operator()(const Key& key) const = 0;
223
224 protected:
227
229 unsigned int hash_log2_size_{0};
230
241
251 unsigned int right_shift_{0};
252 };
253
254 // ===========================================================================
255 // === GENERIC HASH FUNCTIONS FOR SIMPLE TYPES ===
256 // ===========================================================================
257
265 template < typename Key >
266 class HashFuncSmallKey: public HashFuncBase< Key > {
267 public:
272
278 static Size castToSize(const Key& key);
279
285
286 private: // best attempt to get rid of overloaded virtual warnings
287 using HashFuncBase< Key >::operator();
288
289 public:
290 Size operator()(const Key& key) const final;
291 };
292
301 template < typename Key >
302 class HashFuncSmallCastKey: public HashFuncBase< Key > {
303 public:
308
314 static Size castToSize(const Key& key);
315
321 Size operator()(const Key& key) const final;
322
323 protected:
328 static constexpr Size small_key_mask_{(Size(1) << (8 * sizeof(Key))) - Size(1)};
329 };
330
339 template < typename Key >
340 class HashFuncMediumCastKey: public HashFuncBase< Key > {
341 public:
346
352 static Size castToSize(const Key& key);
353
359 Size operator()(const Key& key) const final;
360 };
361
370 template < typename Key >
371 class HashFuncLargeCastKey: public HashFuncBase< Key > {
372 public:
377
383 static Size castToSize(const Key& key);
384
390 virtual Size operator()(const Key& key) const final;
391 };
392
405 template < typename Key >
408 using type = typename std::conditional<
409 sizeof(Key) <= sizeof(Size) && std::is_integral< Key >::value,
411 typename std::conditional<
412 sizeof(Key) < sizeof(Size),
414 typename std::conditional< sizeof(Key) == sizeof(Size),
416 typename std::conditional< sizeof(Key) == 2 * sizeof(Size),
418 void >::type >::type >::type >::
419 type;
420 };
421
422 // ===========================================================================
423 // === CLASSES FOR NOT DEFINING SEVERAL TIMES THE SAME HASH FUNCTIONS ===
424 // ===========================================================================
425
426 // a dummy hash type
427 template < typename Key >
428 class dummyHash {};
429
430 // the general type of the recursive type to select the appropriate hash function
431 template < typename... >
433
434 // base of the recursive type to select the appropriate hash function
435 template < typename Key >
437 using type = Key;
438 };
439
440 // base of the recursive type to select the appropriate hash function
441 template < typename KEY_TYPE, typename TYPE >
442 struct HashFuncConditionalType< KEY_TYPE, TYPE > {
443 using type = typename std::
444 conditional< std::is_same< KEY_TYPE, TYPE >::value, dummyHash< KEY_TYPE >, KEY_TYPE >::type;
445 };
446
468 template < typename KEY_TYPE, typename FIRST_TYPE, typename... OTHER_TYPES >
469 struct HashFuncConditionalType< KEY_TYPE, FIRST_TYPE, OTHER_TYPES... > {
470 using type = typename std::conditional<
471 std::is_same< KEY_TYPE, FIRST_TYPE >::value,
473 typename HashFuncConditionalType< KEY_TYPE, OTHER_TYPES... >::type >::type;
474 };
475
476 // ===========================================================================
477 // === HASH FUNCTIONS FOR PAIRS OF KEYS ===
478 // ===========================================================================
479
491 template < typename key >
492 class HashFunc {};
493
502 template < typename Key1, typename Key2 >
503 class HashFunc< std::pair< Key1, Key2 > >: public HashFuncBase< std::pair< Key1, Key2 > > {
504 public:
510 static Size castToSize(const std::pair< Key1, Key2 >& key);
511
517 Size operator()(const std::pair< Key1, Key2 >& key) const final;
518 };
519
520 // ===========================================================================
521 // === WIDELY USED HASH FUNCTIONS ===
522 // ===========================================================================
523
529 template <>
530 class HashFunc< bool >: public HashFuncSmallKey< bool > {};
531
537 template <>
538 class HashFunc< int >: public HashFuncSmallKey< int > {};
539
545 template <>
546 class HashFunc< unsigned int >: public HashFuncSmallKey< unsigned int > {};
547
553 template <>
554 class HashFunc< long >: public HashFuncSmallKey< long > {};
555
561 template <>
562 class HashFunc< unsigned long >: public HashFuncSmallKey< unsigned long > {};
563
569 template <>
570 class HashFunc<
571 typename HashFuncConditionalType< std::size_t, unsigned long, unsigned int, long, int >::
572 type >: public HashFuncCastKey< std::size_t >::type {};
573
579 template <>
580 class HashFunc< float >: public HashFuncCastKey< float >::type {};
581
587 template <>
588 class HashFunc< double >: public HashFuncCastKey< double >::type {};
589
595 template < typename Type >
596 class HashFunc< Type* >: public HashFuncCastKey< Type* >::type {};
597
603 template <>
604 class HashFunc< std::string >: public HashFuncBase< std::string > {
605 public:
611 static Size castToSize(const std::string& key);
612
614 static Size castToSize(std::string_view key);
615
621 Size operator()(const std::string& key) const final;
622
624 Size operator()(std::string_view key) const;
625 };
626
632 template <>
633 class HashFunc< std::vector< Idx > >: public HashFuncBase< std::vector< Idx > > {
634 public:
640 static Size castToSize(const std::vector< Idx >& key);
641
647 Size operator()(const std::vector< Idx >& key) const final;
648 };
649
655 template <>
656 class HashFunc< Debug >: public HashFuncBase< Debug > {
657 public:
663 static Size castToSize(const Debug& key);
664
670 Size operator()(const Debug& key) const final;
671
672 template < typename OTHER_KEY >
673 friend class HashFunc;
674 };
675
682 template < typename Type >
683 class HashFunc< std::shared_ptr< Type > >: public HashFuncBase< std::shared_ptr< Type > > {
684 public:
690 static Size castToSize(const std::shared_ptr< Type >& key);
691
697 Size operator()(const std::shared_ptr< Type >& key) const final;
698 };
699
700} /* namespace gum */
701
703#ifndef GUM_NO_INLINE
705#endif /* GUM_NO_INLINE */
706
709
710#endif /* GUM_HASHFUNC_H */
All hash functions should inherit from this class.
Definition hashFunc.h:174
virtual Size operator()(const Key &key) const =0
Computes the hashed value of a key.
unsigned int right_shift_
performing y = x >> right_shift_ guarantees that y is a slot index of the hash table
Definition hashFunc.h:251
Size size() const
Returns the hash table size as known by the hash function.
Size hash_size_
The size of the hash table.
Definition hashFunc.h:226
Size hash_mask_
performing y = x & hash_mask_ guarantees that y is a slot index of the hash table
Definition hashFunc.h:240
void resize(const Size new_size)
Update the hash function to take into account a resize of the hash table.
unsigned int hash_log2_size_
Log of the number of slots of the hash table in base 2.
Definition hashFunc.h:229
Generic hash functions for keys castable as Size and whose size is precisely twice that of Size.
Definition hashFunc.h:371
virtual Size operator()(const Key &key) const final
Computes the hashed value of a key.
HashFuncLargeCastKey()
Class constructor.
static Size castToSize(const Key &key)
Cast key to the expected type.
Generic hash functions for keys castable as Size and whose size is precisely that of Size.
Definition hashFunc.h:340
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
Size operator()(const Key &key) const final
Computes the hashed value of a key.
HashFuncMediumCastKey()
Class constructor.
Generic hash functions for keys castable as Size and whose size is strictly smaller than that of Size...
Definition hashFunc.h:302
HashFuncSmallCastKey()
Class constructor.
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
Size operator()(const Key &key) const final
Computes the hashed value of a key.
static constexpr Size small_key_mask_
An additional mask to ensure that keys with fewer bits than Size are cast correctly.
Definition hashFunc.h:328
Generic hash functions for numeric keys smaller than or equal to Size.
Definition hashFunc.h:266
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
Size operator()(const Key &key) const final
Computes the hashed value of a key.
HashFuncSmallKey()
Class constructor.
friend class HashFunc
Definition hashFunc.h:673
Size operator()(const Debug &key) const final
Computes the hashed value of a key.
static Size castToSize(const Debug &key)
Returns the value of a key as a Size.
Size operator()(const std::pair< Key1, Key2 > &key) const final
Computes the hashed value of a key.
static Size castToSize(const std::pair< Key1, Key2 > &key)
Returns the value of a key as a Size.
Size operator()(const std::shared_ptr< Type > &key) const final
Computes the hashed value of a key.
static Size castToSize(const std::shared_ptr< Type > &key)
Returns the value of a key as a Size.
static Size castToSize(const std::string &key)
Returns the value of a key as a Size.
Size operator()(const std::string &key) const final
Computes the hashed value of a key.
Size operator()(std::string_view key) const
Non-virtual overload for heterogeneous lookup with string_view.
static Size castToSize(std::string_view key)
Returns the value of a string_view key as a Size (heterogeneous lookup).
Size operator()(const std::vector< Idx > &key) const final
Computes the hashed value of a key.
static Size castToSize(const std::vector< Idx > &key)
Returns the value of a key as a Size.
This class should be useless as only its specializations should be used.
Definition hashFunc.h:492
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
unsigned int _hashTableLog2_(const Size nb)
Returns the size in bits - 1 necessary to store the smallest power of 2 greater than or equal to nb.
Inlined implementation of the basic hash functions.
Template implementation of the basic hash functions.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
Generic hash functions for keys castable as Size whose size is either smaller than Size,...
Definition hashFunc.h:406
typename std::conditional< sizeof(Key)<=sizeof(Size) &&std::is_integral< Key >::value, HashFuncSmallKey< Key >, typename std::conditional< sizeof(Key)< sizeof(Size), HashFuncSmallCastKey< Key >, typename std::conditional< sizeof(Key)==sizeof(Size), HashFuncMediumCastKey< Key >, typename std::conditional< sizeof(Key)==2 *sizeof(Size), HashFuncLargeCastKey< Key >, void >::type >::type >::type >:: type type
The type used by this class.
Definition hashFunc.h:408
typename std::conditional< std::is_same< KEY_TYPE, FIRST_TYPE >::value, dummyHash< KEY_TYPE >, typename HashFuncConditionalType< KEY_TYPE, OTHER_TYPES... >::type >::type type
Definition hashFunc.h:470
typename std:: conditional< std::is_same< KEY_TYPE, TYPE >::value, dummyHash< KEY_TYPE >, KEY_TYPE >::type type
Definition hashFunc.h:443
This class enables to safely define hash functions for types that may or may not already has defined ...
Definition hashFunc.h:432
Useful constants for hash functions.
Definition hashFunc.h:99
static constexpr Size mask
Definition hashFunc.h:107
static constexpr Size sqrt3
Definition hashFunc.h:105
static constexpr Size pi
Definition hashFunc.h:103
static constexpr Size offset
Definition hashFunc.h:108
static constexpr Size gold
Definition hashFunc.h:101