DDD  1.9.0.20240826145154
hash_support.hh
Go to the documentation of this file.
1 /****************************************************************************/
2 /* */
3 /* This file is part of libDDD, a library for manipulation of DDD and SDD. */
4 /* */
5 /* Copyright (C) 2001-2008 Alexandre Hamez, Yann Thierry-Mieg */
6 /* */
7 /* This program is free software; you can redistribute it and/or modify */
8 /* it under the terms of the GNU Lesser General Public License as */
9 /* published by the Free Software Foundation; either version 3 of the */
10 /* License, or (at your option) any later version. */
11 /* This program 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 LEsserGeneral Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with this program; if not, write to the Free Software */
18 /*Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19 /* */
20 /****************************************************************************/
21 #ifndef _HASH_SUPPORT_HH_
22 #define _HASH_SUPPORT_HH_
23 
24 #include <utility>
25 #include <set>
26 #include <vector>
27 #include <typeinfo>
28 #include <string>
29 
30 #include "ddd/hashfunc.hh"
31 
32 #include <unordered_map>
33 
34 
35 namespace d3 { namespace util {
36 
37 // Basic definition of a d3 hash function : define a member function : size_t hash() const , in hashable classes
38 template<typename T>
39 struct hash
40 {
41  size_t
42  operator()(const T &e) const
43  {
44  return e.hash();
45  }
46 };
47 
48 // Basic definition of a d3 equality test : define a member function : bool operator== (const T & )const , in comparable classes
49 template<typename T>
50 struct equal
51 {
52  bool
53  operator()(const T &e1,const T &e2) const
54  {
55  return e1==e2;
56  }
57 };
58 
59 // Specialized version for storage of pointers to hashable objects.
60 template <typename T>
61 struct hash<T*>
62 {
63  size_t
64  operator()(const T* e) const
65  {
66  if (e == NULL) return 0;
67  return e->hash();
68  }
69 };
70 
71 // Specialized version for storage of pointers to comparable objects. Perform deep comparison.
72 template<typename T>
73 struct equal<T*>
74 {
75  bool
76  operator()(const T* e1,const T* e2) const
77  {
78  if (e1 == NULL) return e2==NULL;
79  if (e2 == NULL) return false;
80  return (typeid(*e1)==typeid(*e2)?(*e1)==(*e2):false);
81  }
82 };
83 
84 // Specialized version for use of int as keys in hash_map
85 template<>
86 struct hash<int> {
87  size_t operator()(int i) const {
88  return ddd::wang32_hash(i);
89  };
90  };
91 
92 // Specialized version for std::pair of hashable objects.
93 template <typename T1,typename T2>
94 struct hash<std::pair<T1,T2> > {
95  size_t operator() (const std::pair<T1,T2> &p)const {
96  return hash<T1>()(p.first) ^ hash<T2>()(p.second);
97  };
98 };
99 
100 
101 // Specialized version for std::set of hashable objects.
102 template <typename T1>
103 struct hash<std::set<T1> > {
104  size_t operator() (const std::set<T1> &p)const {
105  size_t res = 11317;
106  typename std::set<T1>::const_iterator it;
107  for ( it = p.begin() ; it != p.end() ; ++it )
108  res ^= it->hash();
109  return res;
110  };
111 };
112 // Specialized version for std::vector<int>.
113 template <>
114 struct hash<const std::vector<int> > {
115  size_t operator() (const std::vector<int> &p)const {
116  size_t res = 2473;
117  std::vector<int>::const_iterator it;
118  for ( it = p.begin() ; it != p.end() ; ++it )
119  res ^= (ddd::wang32_hash(*it)* res);
120  return res;
121  };
122 };
123 // could this be removed somehow ??
124 template <>
125 struct hash<std::vector<int> > {
126  size_t operator() (const std::vector<int> &p)const {
127  return hash<const std::vector<int> > () (p);
128  };
129 };
130 
131 // Specialized version for std::vector<int>.
132 template <>
133 struct hash<const std::vector<short> > {
134  size_t operator() (const std::vector<short> &p)const {
135  size_t res = 2473;
136  std::vector<short>::const_iterator it;
137  for ( it = p.begin() ; it != p.end() ; ++it )
138  res ^= (ddd::wang32_hash(*it)* res);
139  return res;
140  };
141 };
142 // could this be removed somehow ??
143 template <>
144 struct hash<std::vector<short> > {
145  size_t operator() (const std::vector<short> &p)const {
146  return hash<const std::vector<short> > () (p);
147  };
148 };
149 
150 
151 template <>
152 struct hash<std::vector<int>* > {
153  size_t operator() (const std::vector<int> *p)const {
154  return hash<const std::vector<int> > () (*p);
155  };
156 };
157 template <>
158 struct hash<const std::vector<int>* > {
159  size_t operator() (const std::vector<int> *p)const {
160  return hash<const std::vector<int> > () (*p);
161  };
162 };
163 
164 // Specialized version for std::pair of comparable objects.
165 template<typename T1,typename T2>
166 struct equal<std::pair<T1,T2> >
167 {
168  bool
169  operator()(const std::pair<T1,T2> & e1,const std::pair<T1,T2>& e2) const
170  {
171  return equal<T1>()(e1.first,e2.first) && equal<T2>()(e1.second,e2.second);
172  }
173 };
174 
175 
176 // For use of strings as d3::util hash table keys
177  template<>
178  struct hash<std::string> {
179  size_t operator()(const std::string & string) const{
180  return std::hash<std::string>() (string);
181  }
182  };
183 
184  template<>
185  struct equal<std::string> {
186  bool operator()(const std::string & g1, const std::string & g2) const{
187  return g1==g2;
188  }
189  };
190 
191 #ifdef HASH_STAT
192  template<class T>
193  struct hash_stat_get_type
194  {
195  std::string
196  operator() (const T & t) const
197  {
198  return typeid (t).name ();
199  }
200  };
201 
202  template<class T>
203  struct hash_stat_get_type<T*>
204  {
205  std::string
206  operator() (const T * t) const
207  {
208  return typeid (*t).name ();
209  }
210  };
211 
212  template<class T1, class T2>
213  struct hash_stat_get_type<std::pair<T1,T2> >
214  {
215  std::string
216  operator() (const std::pair<T1, T2> & p) const
217  {
218  return std::string ("pair ").append (hash_stat_get_type<T1> () (p.first)).append (",").append (hash_stat_get_type<T2> () (p.second));
219  }
220  };
221 #endif // HASH_STAT
222 
223 }}
224 
225 // the clone contract
226 namespace unique {
227 
228 template<typename T>
229 struct clone
230 {
231  T*
232  operator()(const T& e1) const
233  {
234  return e1.clone();
235  }
236 };
237 
238 
239 template<>
240 struct clone<std::vector<int> >
241 {
242  std::vector<int>*
243  operator()(const std::vector<int>& e1) const
244  {
245  return new std::vector<int>(e1);
246  }
247 };
248 
249 }
250 
251 
252 
253 #ifdef HASH_STAT
254 #include <map>
255 #include <iostream>
256 // a function that prints statistics about caches
257 static
258 void
259 print_hash_stats(std::map<std::string, size_t> hits,
260  std::map<std::string, size_t> misses,
261  std::map<std::string, size_t> bounces)
262 {
263  size_t h,m,b;
264  h = m = b =0;
265  for (std::map<std::string, size_t>::const_iterator it = misses.begin() ; it != misses.end() ; ++it) {
266  std::cout << it->first << " : ";
267  std::cout << hits[it->first] << " hits, ";
268  std::cout << misses[it->first] << " misses, ";
269  std::cout << bounces[it->first] << " bounces, ";
270  std::cout << "hit ratio " << (hits[it->first]*100 / (hits[it->first] + misses[it->first] + 1)) << " %, ";
271  std::cout << (bounces[it->first]*100 / (hits[it->first] + misses[it->first] + 1)) << " b. per h.(%)";
272  std::cout << std::endl;
273  h += hits[it->first];
274  m += misses[it->first];
275  b += bounces[it->first];
276  }
277  std::cout << "Hits : " << h << " , Misses : " << m << " , Bounces : " << b << std::endl;
278  std::cout << std::endl;
279 }
280 #endif // HASH_STAT
281 
282 #endif
size_t wang32_hash(size_t key)
Thomas Wang's 32 bit hash function.
Definition: hashfunc.hh:42
Definition: Hom.cpp:41
Definition: DDD.h:340
Definition: hash_support.hh:226
Definition: set.hh:17
bool operator()(const T *e1, const T *e2) const
Definition: hash_support.hh:76
bool operator()(const std::pair< T1, T2 > &e1, const std::pair< T1, T2 > &e2) const
Definition: hash_support.hh:169
bool operator()(const std::string &g1, const std::string &g2) const
Definition: hash_support.hh:186
Definition: hash_support.hh:51
bool operator()(const T &e1, const T &e2) const
Definition: hash_support.hh:53
size_t operator()(const T *e) const
Definition: hash_support.hh:64
size_t operator()(int i) const
Definition: hash_support.hh:87
size_t operator()(const std::string &string) const
Definition: hash_support.hh:179
Definition: hash_support.hh:40
size_t operator()(const T &e) const
Definition: hash_support.hh:42
Definition: vector.hh:17
std::vector< int > * operator()(const std::vector< int > &e1) const
Definition: hash_support.hh:243
Definition: hash_support.hh:230
T * operator()(const T &e1) const
Definition: hash_support.hh:232

Please comment this page and report errors about it on the RefDocComments page.
Generated on Mon Aug 26 2024 14:54:00 for DDD by doxygen 1.9.1