LIBINT  2.6.0
hashable.h
1 /*
2  * Copyright (C) 2004-2019 Edward F. Valeev
3  *
4  * This file is part of Libint.
5  *
6  * Libint is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Libint is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Libint. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef _libint2_src_bin_libint_hashable_h_
22 #define _libint2_src_bin_libint_hashable_h_
23 
24 #include <libint2/util/intrinsic_types.h>
25 
26 namespace libint2 {
27 
29  typedef enum { CacheKey, ComputeKey, ReferToKey} KeyManagePolicy;
30 
32  template <KeyManagePolicy KeyManage> struct OwnKey { enum {result=false}; };
33  template<> struct OwnKey<CacheKey> { enum {result=true}; };
34 
36  template <class T, bool HasAKey>
37  struct KeyStore;
38  template <class T>
39  struct KeyStore<T,true> {
40  mutable T value;
41  };
42  template <class T>
43  struct KeyStore<T,false> {
44  };
45 
51  template <typename T>
52  struct KeyTraits
53  {
54  typedef T ReturnType;
55  };
57  template <>
58  struct KeyTraits<std::string>
59  {
60  typedef const std::string& ReturnType;
61  };
63  template <typename T, size_t Size>
64  struct KeyTraits<T[Size]>
65  {
66  typedef const T* const ReturnType;
67  };
68 
72  template <typename KeyType, KeyManagePolicy KeyMP>
73  class Hashable
74  {
75  public:
76  typedef typename KeyTraits<KeyType>::ReturnType KeyReturnType;
77  Hashable() {}
78  virtual ~Hashable() {}
79 
81  virtual KeyReturnType key() const =0;
82 
83  protected:
85  };
86 
88  class FNVStringHash {
89  public:
91  typedef LIBINT2_UINT_LEAST64 KeyType;
92  FNVStringHash():
93  hval_(hval_init) {}
94  ~FNVStringHash() {}
95 
97  inline LIBINT2_UINT_LEAST64 hash(const std::string& S);
98 
99  private:
100 #if __WORDSIZE == 64
101  static const LIBINT2_UINT_LEAST64 hval_init = 0xcbf29ce484222325UL;
102  static const LIBINT2_UINT_LEAST64 fnv_prime64 = 0x100000001b3UL;
103 #else
104  static const LIBINT2_UINT_LEAST64 hval_init = 0xcbf29ce484222325ULL;
105  static const LIBINT2_UINT_LEAST64 fnv_prime64 = 0x100000001b3ULL;
106 #endif
107  LIBINT2_UINT_LEAST64 hval_;
108  };
109 
110  LIBINT2_UINT_LEAST64
111  FNVStringHash::hash(const std::string& S) {
112  const unsigned char* cS = reinterpret_cast<const unsigned char*>(S.c_str());
113  const unsigned char* cptr = cS;
114  while (*cptr) {
115  hval_ ^= (LIBINT2_UINT_LEAST64)*cptr;
116  cptr++;
117  hval_ *= fnv_prime64;
118  }
119 
120  return hval_;
121  }
122 
123 };
124 
125 #endif
KeyTraits<T> describes following properties of type T: 1) how to return objects of type T.
Definition: hashable.h:52
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
Objects of Hashable<T> class provide hashing function key() which computes keys of type KeyType.
Definition: hashable.h:73
KeyManagePolicy
KeyManagePolicy defines whether to compute+cache, compute, or key is just an object.
Definition: hashable.h:29
FNVStringHash uses Fowler/Noll/Vo algorithm to hash a char string to a 64-bit integer.
Definition: hashable.h:88
LIBINT2_UINT_LEAST64 KeyType
The type of key computed using this hash.
Definition: hashable.h:91
use OwnKey to figure out whether the key should be stored in Hashable
Definition: hashable.h:32
If OwnsKey is true then KeyStore<T> has the key of type T, otherwise it's empty.
Definition: hashable.h:37
LIBINT2_UINT_LEAST64 hash(const std::string &S)
Returns 64-bit integer hash of S.
Definition: hashable.h:111