LIBINT  2.1.0-stable
hashable.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
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, see http://www.gnu.org/licenses/.
17  *
18  */
19 
20 #ifndef _libint2_src_bin_libint_hashable_h_
21 #define _libint2_src_bin_libint_hashable_h_
22 
23 #include <libint2/intrinsic_types.h>
24 
25 namespace libint2 {
26 
28  typedef enum { CacheKey, ComputeKey, ReferToKey} KeyManagePolicy;
29 
31  template <KeyManagePolicy KeyManage> struct OwnKey { enum {result=false}; };
32  template<> struct OwnKey<CacheKey> { enum {result=true}; };
33 
35  template <class T, bool HasAKey>
36  struct KeyStore;
37  template <class T>
38  struct KeyStore<T,true> {
39  mutable T value;
40  };
41  template <class T>
42  struct KeyStore<T,false> {
43  };
44 
50  template <typename T>
51  struct KeyTraits
52  {
53  typedef T ReturnType;
54  };
56  template <>
57  struct KeyTraits<std::string>
58  {
59  typedef const std::string& ReturnType;
60  };
62  template <typename T, size_t Size>
63  struct KeyTraits<T[Size]>
64  {
65  typedef const T* const ReturnType;
66  };
67 
71  template <typename KeyType, KeyManagePolicy KeyMP>
72  class Hashable
73  {
74  public:
75  typedef typename KeyTraits<KeyType>::ReturnType KeyReturnType;
76  Hashable() {}
77  virtual ~Hashable() {}
78 
80  virtual KeyReturnType key() const =0;
81 
82  protected:
84  };
85 
87  class FNVStringHash {
88  public:
90  typedef LIBINT2_UINT_LEAST64 KeyType;
91  FNVStringHash():
92  hval_(hval_init) {}
93  ~FNVStringHash() {}
94 
96  inline LIBINT2_UINT_LEAST64 hash(const std::string& S);
97 
98  private:
99 #if __WORDSIZE == 64
100  static const LIBINT2_UINT_LEAST64 hval_init = 0xcbf29ce484222325UL;
101  static const LIBINT2_UINT_LEAST64 fnv_prime64 = 0x100000001b3UL;
102 #else
103  static const LIBINT2_UINT_LEAST64 hval_init = 0xcbf29ce484222325ULL;
104  static const LIBINT2_UINT_LEAST64 fnv_prime64 = 0x100000001b3ULL;
105 #endif
106  LIBINT2_UINT_LEAST64 hval_;
107  };
108 
109  LIBINT2_UINT_LEAST64
110  FNVStringHash::hash(const std::string& S) {
111  const unsigned char* cS = reinterpret_cast<const unsigned char*>(S.c_str());
112  const unsigned char* cptr = cS;
113  while (*cptr) {
114  hval_ ^= (LIBINT2_UINT_LEAST64)*cptr;
115  cptr++;
116  hval_ *= fnv_prime64;
117  }
118 
119  return hval_;
120  }
121 
122 };
123 
124 #endif
KeyTraits<T> describes following properties of type T: 1) how to return objects of type T...
Definition: hashable.h:51
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
Objects of Hashable<T> class provide hashing function key() which computes keys of type KeyType...
Definition: hashable.h:72
Definition: stdarray.h:18
KeyManagePolicy
KeyManagePolicy defines whether to compute+cache, compute, or key is just an object.
Definition: hashable.h:28
FNVStringHash uses Fowler/Noll/Vo algorithm to hash a char string to a 64-bit integer.
Definition: hashable.h:87
LIBINT2_UINT_LEAST64 KeyType
The type of key computed using this hash.
Definition: hashable.h:90
use OwnKey to figure out whether the key should be stored in Hashable
Definition: hashable.h:31
If OwnsKey is true then KeyStore<T> has the key of type T, otherwise it&#39;s empty.
Definition: hashable.h:36
LIBINT2_UINT_LEAST64 hash(const std::string &S)
Returns 64-bit integer hash of S.
Definition: hashable.h:110