aGrUM 2.3.2
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-2025 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-2025 *
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 <string>
54#include <utility>
55
56#include <agrum/agrum.h>
57
59
60#include <type_traits>
61
62namespace gum {
63
79 unsigned int _hashTableLog2_(const Size nb);
80
97 static constexpr Size gold
98 = sizeof(Size) == 4 ? Size(2654435769UL) : Size(11400714819323198486UL);
99 static constexpr Size pi
100 = sizeof(Size) == 4 ? Size(3373259426UL) : Size(14488038916154245684UL);
101 static constexpr Size mask
102 = sizeof(Size) == 4 ? Size(4294967295UL) : Size(18446744073709551615UL);
103 static constexpr Size offset = sizeof(Size) == 4 ? Size(32) : Size(64);
104 };
105
106 // ===========================================================================
107 // === BASE CLASS SHARED BY ALL THE HASH FUNCTIONS ===
108 // ===========================================================================
168 template < typename Key >
170 public:
186 void resize(const Size new_size);
187
193 Size size() const;
194
217 virtual Size operator()(const Key& key) const = 0;
218
219 protected:
222
224 unsigned int hash_log2_size_{0};
225
236
246 unsigned int right_shift_{0};
247 };
248
249 // ===========================================================================
250 // === GENERIC HASH FUNCTIONS FOR SIMPLE TYPES ===
251 // ===========================================================================
252
260 template < typename Key >
261 class HashFuncSmallKey: public HashFuncBase< Key > {
262 public:
267
273 static Size castToSize(const Key& key);
274
280
281 private: // best attempt to get rid of overloaded virtual warnings
282 using HashFuncBase< Key >::operator();
283
284 public:
285 virtual Size operator()(const Key& key) const override final;
286 };
287
296 template < typename Key >
297 class HashFuncSmallCastKey: public HashFuncBase< Key > {
298 public:
303
309 static Size castToSize(const Key& key);
310
316 virtual Size operator()(const Key& key) const override final;
317
318 protected:
323 static constexpr Size small_key_mask_{(Size(1) << (8 * sizeof(Key))) - Size(1)};
324 };
325
334 template < typename Key >
335 class HashFuncMediumCastKey: public HashFuncBase< Key > {
336 public:
341
347 static Size castToSize(const Key& key);
348
354 virtual Size operator()(const Key& key) const override final;
355 };
356
365 template < typename Key >
366 class HashFuncLargeCastKey: public HashFuncBase< Key > {
367 public:
372
378 static Size castToSize(const Key& key);
379
385 virtual Size operator()(const Key& key) const override final;
386 };
387
400 template < typename Key >
403 using type = typename std::conditional<
404 sizeof(Key) <= sizeof(Size) && std::is_integral< Key >::value,
406 typename std::conditional<
407 sizeof(Key) < sizeof(Size),
409 typename std::conditional< sizeof(Key) == sizeof(Size),
411 typename std::conditional< sizeof(Key) == 2 * sizeof(Size),
413 void >::type >::type >::type >::
414 type;
415 };
416
417 // ===========================================================================
418 // === CLASSES FOR NOT DEFINING SEVERAL TIMES THE SAME HASH FUNCTIONS ===
419 // ===========================================================================
420
421 // a dummy hash type
422 template < typename Key >
423 class dummyHash {};
424
425 // the general type of the recursive type to select the appropriate hash function
426 template < typename... >
428
429 // base of the recursive type to select the appropriate hash function
430 template < typename Key >
432 using type = Key;
433 };
434
435 // base of the recursive type to select the appropriate hash function
436 template < typename KEY_TYPE, typename TYPE >
437 struct HashFuncConditionalType< KEY_TYPE, TYPE > {
438 using type = typename std::
439 conditional< std::is_same< KEY_TYPE, TYPE >::value, dummyHash< KEY_TYPE >, KEY_TYPE >::type;
440 };
441
463 template < typename KEY_TYPE, typename FIRST_TYPE, typename... OTHER_TYPES >
464 struct HashFuncConditionalType< KEY_TYPE, FIRST_TYPE, OTHER_TYPES... > {
465 using type = typename std::conditional<
466 std::is_same< KEY_TYPE, FIRST_TYPE >::value,
468 typename HashFuncConditionalType< KEY_TYPE, OTHER_TYPES... >::type >::type;
469 };
470
471 // ===========================================================================
472 // === HASH FUNCTIONS FOR PAIRS OF KEYS ===
473 // ===========================================================================
474
486 template < typename key >
487 class HashFunc {};
488
497 template < typename Key1, typename Key2 >
498 class HashFunc< std::pair< Key1, Key2 > >: public HashFuncBase< std::pair< Key1, Key2 > > {
499 public:
505 static Size castToSize(const std::pair< Key1, Key2 >& key);
506
512 virtual Size operator()(const std::pair< Key1, Key2 >& key) const override final;
513 };
514
515 // ===========================================================================
516 // === WIDELY USED HASH FUNCTIONS ===
517 // ===========================================================================
518
524 template <>
525 class HashFunc< bool >: public HashFuncSmallKey< bool > {};
526
532 template <>
533 class HashFunc< int >: public HashFuncSmallKey< int > {};
534
540 template <>
541 class HashFunc< unsigned int >: public HashFuncSmallKey< unsigned int > {};
542
548 template <>
549 class HashFunc< long >: public HashFuncSmallKey< long > {};
550
556 template <>
557 class HashFunc< unsigned long >: public HashFuncSmallKey< unsigned long > {};
558
564 template <>
565 class HashFunc<
566 typename HashFuncConditionalType< std::size_t, unsigned long, unsigned int, long, int >::
567 type >: public HashFuncCastKey< std::size_t >::type {};
568
574 template <>
575 class HashFunc< float >: public HashFuncCastKey< float >::type {};
576
582 template <>
583 class HashFunc< double >: public HashFuncCastKey< double >::type {};
584
590 template < typename Type >
591 class HashFunc< Type* >: public HashFuncCastKey< Type* >::type {};
592
598 template <>
599 class HashFunc< std::string >: public HashFuncBase< std::string > {
600 public:
606 static Size castToSize(const std::string& key);
607
613 virtual Size operator()(const std::string& key) const override final;
614 };
615
621 template <>
622 class HashFunc< std::vector< Idx > >: public HashFuncBase< std::vector< Idx > > {
623 public:
629 static Size castToSize(const std::vector< Idx >& key);
630
636 virtual Size operator()(const std::vector< Idx >& key) const override final;
637 };
638
644 template <>
645 class HashFunc< Debug >: public HashFuncBase< Debug > {
646 public:
652 static Size castToSize(const Debug& key);
653
659 virtual Size operator()(const Debug& key) const override final;
660
661 template < typename OTHER_KEY >
662 friend class HashFunc;
663 };
664
671 template < typename Type >
672 class HashFunc< RefPtr< Type > >: public HashFuncBase< RefPtr< Type > > {
673 public:
679 static Size castToSize(const RefPtr< Type >& key);
680
686 virtual Size operator()(const RefPtr< Type >& key) const override final;
687 };
688
689} /* namespace gum */
690
692#ifndef GUM_NO_INLINE
694#endif /* GUM_NO_INLINE */
695
698
699#endif /* GUM_HASHFUNC_H */
All hash functions should inherit from this class.
Definition hashFunc.h:169
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:246
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:221
Size hash_mask_
performing y = x & hash_mask_ guarantees that y is a slot index of the hash table
Definition hashFunc.h:235
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:224
Generic hash functions for keys castable as Size and whose size is precisely twice that of Size.
Definition hashFunc.h:366
virtual Size operator()(const Key &key) const override 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:335
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
virtual Size operator()(const Key &key) const override 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:297
HashFuncSmallCastKey()
Class constructor.
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
virtual Size operator()(const Key &key) const override 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:323
Generic hash functions for numeric keys smaller than or equal to Size.
Definition hashFunc.h:261
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
virtual Size operator()(const Key &key) const override final
Computes the hashed value of a key.
HashFuncSmallKey()
Class constructor.
friend class HashFunc
Definition hashFunc.h:662
static Size castToSize(const Debug &key)
Returns the value of a key as a Size.
virtual Size operator()(const Debug &key) const override final
Computes the hashed value of a key.
virtual Size operator()(const RefPtr< Type > &key) const override final
Computes the hashed value of a key.
static Size castToSize(const RefPtr< Type > &key)
Returns the value of a key as a Size.
virtual Size operator()(const std::pair< Key1, Key2 > &key) const override 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.
virtual Size operator()(const std::string &key) const override final
Computes the hashed value of a key.
static Size castToSize(const std::string &key)
Returns the value of a key as a Size.
virtual Size operator()(const std::vector< Idx > &key) const override 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:487
Smart pointers.
Definition refPtr.h:136
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.
Class providing aGrUM's "smart" pointers.
Generic hash functions for keys castable as Size whose size is either smaller than Size,...
Definition hashFunc.h:401
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:403
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:465
typename std:: conditional< std::is_same< KEY_TYPE, TYPE >::value, dummyHash< KEY_TYPE >, KEY_TYPE >::type type
Definition hashFunc.h:438
This class enables to safely define hash functions for types that may or may not already has defined ...
Definition hashFunc.h:427
Useful constants for hash functions.
Definition hashFunc.h:96
static constexpr Size mask
Definition hashFunc.h:102
static constexpr Size pi
Definition hashFunc.h:100
static constexpr Size offset
Definition hashFunc.h:103
static constexpr Size gold
Definition hashFunc.h:98