aGrUM  0.14.2
hashFunc_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #include <string>
26 #include <utility>
27 
28 #include <agrum/agrum.h>
29 
30 // to ease parsing in IDE
31 #include <agrum/core/hashFunc.h>
32 
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 
35 namespace gum {
36 
37  /* in aGrUM, the sizes of hash tables (number of slots) are powers of 2. This
38  * is
39  * not actually compulsory for the hash function we use. However, as it
40  * speeds up the computations of hashed values, we chose to impose
41  * this restriction. Function __hashTableLog2 thus returns the size in
42  * bits - 1 necessary to store the smallest power of 2 greater than or
43  * equal nb. */
44  INLINE unsigned int __hashTableLog2(const Size nb) {
45  unsigned int i = 0;
46 
47  for (Size nbb = nb; nbb > Size(1); ++i, nbb >>= 1) {};
48 
49  return ((Size(1) << i) < nb ? i + Size(1) : i);
50  }
51 
52  // ===========================================================================
53 
55  INLINE Size HashFunc< std::string >::castToSize(const std::string& key) {
56  Size h = 0;
57  Size size = Size(key.size());
58  const char* char_ptr = key.c_str();
59  const Size* int_ptr = (const Size*)char_ptr;
60 
61  for (; size >= sizeof(Size); size -= sizeof(Size), ++int_ptr) {
62  h = h * HashFuncConst::gold + *int_ptr;
63  }
64 
65  for (char_ptr = (const char*)int_ptr; size != Size(0); --size, ++char_ptr) {
66  h = Size(19) * h + Size(*char_ptr);
67  }
68 
69  return h;
70  }
71 
72  // Returns the hashed value of a key.
73  INLINE Size HashFunc< std::string >::operator()(const std::string& key) const {
74  return castToSize(key) & this->_hash_mask;
75  }
76 
77  // ===========================================================================
78 
80  INLINE Size
81  HashFunc< std::vector< Idx > >::castToSize(const std::vector< Idx >& key) {
82  Size h = Size(0);
83  Size size = Size(key.size());
84  for (Size i = Size(0); i < size; ++i)
85  h += i * Size(key[i]);
86 
87  return h;
88  }
89 
90  // Returns the hashed value of a key.
91  INLINE Size HashFunc< std::vector< Idx > >::
92  operator()(const std::vector< Idx >& key) const {
93  return (castToSize(key) * HashFuncConst::gold) & this->_hash_mask;
94  }
95 
96  // ===========================================================================
97 
99  INLINE Size HashFunc< Debug >::castToSize(const Debug& key) {
100  Size h = Size(0);
101 
102  for (Size i = Size(0), j = Size(key.size()); i < j; ++i)
103  h = Size(19) * h + Size(key[i]);
104 
105  return h;
106  }
107 
108  // Returns the hashed value of a key.
109  INLINE Size HashFunc< Debug >::operator()(const Debug& key) const {
110  return (castToSize(key) * HashFuncConst::gold) & this->_hash_mask;
111  }
112 
113 
114 } /* namespace gum */
115 
116 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
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...
static constexpr Size gold
Definition: hashFunc.h:74
static Size castToSize(const std::string &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.
virtual Size operator()(const Debug &key) const override final
Computes the hashed value of a key.
Classes providing basic hash functions for hash tables.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
static Size castToSize(const Debug &key)
Returns the value of a key as a Size.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45