Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
hash.h
1 /*
2  * hash classes
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2006, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_HASH_H_
22 #define ELM_HASH_H_
23 
24 
25 #include <elm/types.h>
26 #include <elm/string.h>
27 #include <elm/util/Option.h>
28 #include <elm/util/Pair.h>
29 #include <elm/equiv.h>
30 
31 namespace elm {
32 
33 namespace sys { class Path; }
34 namespace t { typedef t::intptr hash; }
35 
36 // Useful hash functions
37 t::hash hash_string(const char *chars, int length);
38 t::hash hash_cstring(const char *chars);
39 t::hash hash_jenkins(const void *block, int size);
40 inline t::hash hash_ptr(const void *p) {
41 # ifdef ELM_32
42  return t::hash(p) >> 2;
43 # else
44  return t::hash(p) >> 3;
45 # endif
46 }
47 bool hash_equals(const void *p1, const void *p2, int size);
48 
49 // HashKey class
50 template <class T> class HashKey {
51 public:
52  static t::hash hash(const T& key) { return hash_jenkins(&key, sizeof(T)); };
53  static inline bool equals(const T& key1, const T& key2) { return &key1 == &key2 || Equiv<T>::equals(key1, key2); }
54  inline t::hash computeHash(const T& key) const { return hash(key); }
55  inline bool isEqual(const T& key1, const T& key2) const { return equals(key1, key2); }
56 };
57 
58 // Predefined hash keys
59 template <> class HashKey<int> {
60 public:
61  static inline t::hash hash(int key) { return t::hash(key); }
62  static inline bool equals(int key1, int key2) { return key1 == key2; }
63  inline t::hash computeHash(int key) const { return hash(key); }
64  inline bool isEqual(int key1, int key2) const { return equals(key1, key2); }
65 };
66 
67 template <class T>
68 class HashKey<T *> {
69 public:
70  static inline t::hash hash(T *key) { return hash_ptr(key); }
71  static inline bool equals(T *key1, T *key2) { return key1 == key2; }
72  inline t::hash computeHash(T *key) const { return hash(key); }
73  inline bool isEqual(T *key1, T *key2) const { return equals(key1, key2); }
74 };
75 
76 template <class T>
77 class HashKey<const T *> {
78 public:
79  static inline t::hash hash(const T *key) { return hash_ptr(key); }
80  static inline bool equals(const T *key1, const void *key2)
81  { return key1 == key2; }
82  inline t::hash computeHash(const T *key) const { return hash(key); }
83  inline bool isEqual(const T *key1, const T *key2) const { return equals(key1, key2); }
84 };
85 
86 template <> class HashKey<CString> {
87 public:
88  static t::hash hash(CString key) { return hash_cstring(key.chars()); }
89  static inline bool equals(CString key1, CString key2) { return key1 == key2; }
90  inline t::hash computeHash(cstring key) const { return hash(key); }
91  inline bool isEqual(cstring key1, cstring key2) const { return equals(key1, key2); }
92 };
93 
94 template <> class HashKey<String> {
95 public:
96  static t::hash hash(const String& key) { return hash_string(key.chars(), key.length()); };
97  static inline bool equals(const String& key1, const String& key2) { return key1 == key2; };
98  inline t::hash computeHash(string key) const { return hash(key); }
99  inline bool isEqual(string key1, string key2) const { return equals(key1, key2); }
100 };
101 
102 template <class T1, class T2> class HashKey<Pair<T1, T2> > {
103 public:
104  typedef Pair<T1, T2> T;
105  static t::hash hash(const T& p) { return HashKey<T1>::hash(p.fst) + HashKey<T2>::hash(p.snd); };
106  static inline bool equals(const T& p1, const T& p2) { return p1 == p2; };
107  inline t::hash computeHash(const T& key) const { return hash(key); }
108  inline bool isEqual(const T& key1, const T& key2) const { return equals(key1, key2); }
109 };
110 
111 
112 // Hasher class
113 class Hasher {
114 public:
115  inline Hasher(void): h(0) { }
116  template <class T> void add(const T& value) { h = h ^ HashKey<T>::hash(value); }
117  template <class T> Hasher& operator+=(const T& value) { add<T>(value); return *this; }
118  template <class T> Hasher& operator<<(const T& value) { add<T>(value); return *this; }
119  inline t::hash hash(void) const { return h; }
120  inline operator t::hash(void) const { return h; }
121 private:
122  t::hash h;
123 };
124 
125 // SelfHashKey class
126 template <class T>
127 class SelfHashKey {
128 public:
129  static t::hash hash(const T& v) { return v.hash(); }
130  static bool equals(const T& v1, const T& v2) { return v1 == v2; }
131  inline t::hash computeHash(const T& key) const { return hash(key); }
132  inline bool isEqual(const T& key1, const T& key2) const { return equals(key1, key2); }
133 };
134 
135 
136 template <class K, class T, class H = HashKey<K> >
137 class AssocHashKey: public H {
138 public:
139  typedef Pair<K, T> t;
140  const H& keyHash() const { return *this; }
141  H& keyHash() { return *this; }
142 
143  inline elm::t::hash computeHash(const t& k) const { return H::computeHash(k.fst); }
144  inline bool isEqual(const t& k1, const t& k2) const { return H::isEqual(k1.fst, k2.fst); }
145 };
146 
147 template <> class HashKey<sys::Path> {
148 public:
149  static t::hash hash(const sys::Path& key);
150  static bool equals(const sys::Path& key1, const sys::Path& key2);
151  inline t::hash computeHash(const sys::Path& key) const { return hash(key); }
152  inline bool isEqual(const sys::Path& key1, const sys::Path& key2) const { return equals(key1, key2); }
153 };
154 
155 template <class T> inline t::hash hash(const T& x) { return HashKey<T>::hash(x); }
156 
157 }; // elm
158 
159 #endif /* ELM_HASH_H_ */
elm::HashKey< T * >::computeHash
t::hash computeHash(T *key) const
Definition: hash.h:72
elm::HashKey< String >::equals
static bool equals(const String &key1, const String &key2)
Definition: hash.h:97
elm::HashKey< Pair< T1, T2 > >::equals
static bool equals(const T &p1, const T &p2)
Definition: hash.h:106
elm::HashKey< CString >::isEqual
bool isEqual(cstring key1, cstring key2) const
Definition: hash.h:91
elm::String::chars
const char * chars(void) const
Definition: String.h:76
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::sys::Path
Definition: Path.h:33
elm::HashKey< int >::equals
static bool equals(int key1, int key2)
Definition: hash.h:62
elm::HashKey< sys::Path >::computeHash
t::hash computeHash(const sys::Path &key) const
Definition: hash.h:151
elm::Hasher::hash
t::hash hash(void) const
Definition: hash.h:119
elm::AssocHashKey::keyHash
H & keyHash()
Definition: hash.h:141
elm::AssocHashKey::keyHash
const H & keyHash() const
Definition: hash.h:140
elm::equals
bool equals(const C1 &c1, const C2 &c2)
Definition: util.h:107
elm::Hasher::operator<<
Hasher & operator<<(const T &value)
Definition: hash.h:118
elm::HashKey::equals
static bool equals(const T &key1, const T &key2)
Definition: hash.h:53
elm::HashKey< CString >::equals
static bool equals(CString key1, CString key2)
Definition: hash.h:89
elm::SelfHashKey::computeHash
t::hash computeHash(const T &key) const
Definition: hash.h:131
elm::HashKey< const T * >::isEqual
bool isEqual(const T *key1, const T *key2) const
Definition: hash.h:83
elm::hash_ptr
t::hash hash_ptr(const void *p)
Definition: hash.h:40
elm::Pair
Definition: Pair.h:33
value
elm::AssocHashKey
Definition: hash.h:137
elm::CString
Definition: CString.h:17
elm::Hasher::add
void add(const T &value)
Definition: hash.h:116
elm::HashKey< int >::isEqual
bool isEqual(int key1, int key2) const
Definition: hash.h:64
elm::SelfHashKey::hash
static t::hash hash(const T &v)
Definition: hash.h:129
elm::HashKey< T * >::isEqual
bool isEqual(T *key1, T *key2) const
Definition: hash.h:73
elm::HashKey< CString >::hash
static t::hash hash(CString key)
Definition: hash.h:88
elm::Hasher::Hasher
Hasher(void)
Definition: hash.h:115
elm::String::length
int length(void) const
Definition: String.h:75
elm::HashKey< T * >::hash
static t::hash hash(T *key)
Definition: hash.h:70
elm::HashKey< Pair< T1, T2 > >::isEqual
bool isEqual(const T &key1, const T &key2) const
Definition: hash.h:108
elm
Definition: adapter.h:26
elm::HashKey< const T * >::equals
static bool equals(const T *key1, const void *key2)
Definition: hash.h:80
elm::CString::chars
const char * chars(void) const
Definition: CString.h:27
elm::SelfHashKey::isEqual
bool isEqual(const T &key1, const T &key2) const
Definition: hash.h:132
elm::HashKey< Pair< T1, T2 > >::T
Pair< T1, T2 > T
Definition: hash.h:104
elm::HashKey< const T * >::computeHash
t::hash computeHash(const T *key) const
Definition: hash.h:82
elm::HashKey< String >::hash
static t::hash hash(const String &key)
Definition: hash.h:96
elm::HashKey::isEqual
bool isEqual(const T &key1, const T &key2) const
Definition: hash.h:55
elm::HashKey< Pair< T1, T2 > >::computeHash
t::hash computeHash(const T &key) const
Definition: hash.h:107
elm::AssocHashKey::isEqual
bool isEqual(const t &k1, const t &k2) const
Definition: hash.h:144
elm::t::size
uint64 size
Definition: arch.h:35
elm::hash_jenkins
t::hash hash_jenkins(const void *block, int size)
Definition: util_HashKey.cpp:92
elm::SelfHashKey::equals
static bool equals(const T &v1, const T &v2)
Definition: hash.h:130
elm::AssocHashKey::t
Pair< K, T > t
Definition: hash.h:139
elm::HashKey< CString >::computeHash
t::hash computeHash(cstring key) const
Definition: hash.h:90
elm::HashKey< T * >::equals
static bool equals(T *key1, T *key2)
Definition: hash.h:71
elm::HashKey< int >::computeHash
t::hash computeHash(int key) const
Definition: hash.h:63
elm::HashKey< sys::Path >::isEqual
bool isEqual(const sys::Path &key1, const sys::Path &key2) const
Definition: hash.h:152
elm::String
Definition: String.h:30
elm::Hasher::operator+=
Hasher & operator+=(const T &value)
Definition: hash.h:117
elm::HashKey
Definition: hash.h:50
elm::hash
t::hash hash(const T &x)
Definition: hash.h:155
elm::HashKey< const T * >::hash
static t::hash hash(const T *key)
Definition: hash.h:79
elm::HashKey< Pair< T1, T2 > >::hash
static t::hash hash(const T &p)
Definition: hash.h:105
elm::t::hash
t::intptr hash
Definition: hash.h:34
elm::HashKey< String >::computeHash
t::hash computeHash(string key) const
Definition: hash.h:98
elm::HashKey::computeHash
t::hash computeHash(const T &key) const
Definition: hash.h:54
elm::SelfHashKey
Definition: hash.h:127
elm::hash_equals
bool hash_equals(const void *p1, const void *p2, int size)
Definition: util_HashKey.cpp:160
elm::HashKey< int >::hash
static t::hash hash(int key)
Definition: hash.h:61
elm::Hasher
Definition: hash.h:113
elm::HashKey< String >::isEqual
bool isEqual(string key1, string key2) const
Definition: hash.h:99
elm::hash_string
t::hash hash_string(const char *chars, int length)
Definition: util_HashKey.cpp:121
elm::HashKey::hash
static t::hash hash(const T &key)
Definition: hash.h:52
elm::hash_cstring
t::hash hash_cstring(const char *chars)
Definition: util_HashKey.cpp:139
elm::t::intptr
uint64 intptr
Definition: arch.h:38
elm::Equiv::equals
static bool equals(const T &v1, const T &v2)
Definition: equiv.h:35
elm::AssocHashKey::computeHash
elm::t::hash computeHash(const t &k) const
Definition: hash.h:143