LIBINT 2.9.0
hashable.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint compiler.
5 *
6 * Libint compiler 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 compiler 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 compiler. 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#include <string>
27
28namespace libint2 {
29
32typedef enum { CacheKey, ComputeKey, ReferToKey } KeyManagePolicy;
33
35template <KeyManagePolicy KeyManage>
36struct OwnKey {
37 enum { result = false };
38};
39template <>
40struct OwnKey<CacheKey> {
41 enum { result = true };
42};
43
46template <class T, bool HasAKey>
47struct KeyStore;
48template <class T>
49struct KeyStore<T, true> {
50 mutable T value;
51};
52template <class T>
53struct KeyStore<T, false> {};
54
60template <typename T>
61struct KeyTraits {
62 typedef T ReturnType;
63};
65template <>
66struct KeyTraits<std::string> {
67 typedef const std::string& ReturnType;
68};
70template <typename T, size_t Size>
71struct KeyTraits<T[Size]> {
72 typedef const T* const ReturnType;
73};
74
78template <typename KeyType, KeyManagePolicy KeyMP>
79class Hashable {
80 public:
81 typedef typename KeyTraits<KeyType>::ReturnType KeyReturnType;
82 Hashable() {}
83 virtual ~Hashable() {}
84
86 virtual KeyReturnType key() const = 0;
87
88 protected:
90};
91
95 public:
97 typedef LIBINT2_UINT_LEAST64 KeyType;
98 FNVStringHash() : hval_(hval_init) {}
100
102 inline LIBINT2_UINT_LEAST64 hash(const std::string& S);
103
104 private:
105#if __WORDSIZE == 64
106 static const LIBINT2_UINT_LEAST64 hval_init = 0xcbf29ce484222325UL;
107 static const LIBINT2_UINT_LEAST64 fnv_prime64 = 0x100000001b3UL;
108#else
109 static const LIBINT2_UINT_LEAST64 hval_init = 0xcbf29ce484222325ULL;
110 static const LIBINT2_UINT_LEAST64 fnv_prime64 = 0x100000001b3ULL;
111#endif
112 LIBINT2_UINT_LEAST64 hval_;
113};
114
115LIBINT2_UINT_LEAST64
116FNVStringHash::hash(const std::string& S) {
117 const unsigned char* cS = reinterpret_cast<const unsigned char*>(S.c_str());
118 const unsigned char* cptr = cS;
119 while (*cptr) {
120 hval_ ^= (LIBINT2_UINT_LEAST64)*cptr;
121 cptr++;
122 hval_ *= fnv_prime64;
123 }
124
125 return hval_;
126}
127
128}; // namespace libint2
129
130#endif
FNVStringHash uses Fowler/Noll/Vo algorithm to hash a char string to a 64-bit integer.
Definition hashable.h:94
LIBINT2_UINT_LEAST64 KeyType
The type of key computed using this hash.
Definition hashable.h:97
LIBINT2_UINT_LEAST64 hash(const std::string &S)
Returns 64-bit integer hash of S.
Definition hashable.h:116
Objects of Hashable<T> class provide hashing function key() which computes keys of type KeyType.
Definition hashable.h:79
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
KeyManagePolicy
KeyManagePolicy defines whether to compute+cache, compute, or key is just an object.
Definition hashable.h:32
If OwnsKey is true then KeyStore<T> has the key of type T, otherwise it's empty.
Definition hashable.h:47
KeyTraits<T> describes following properties of type T: 1) how to return objects of type T.
Definition hashable.h:61
use OwnKey to figure out whether the key should be stored in Hashable
Definition hashable.h:36