Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hash_table.h
Go to the documentation of this file.
1 /* *******************************************************************
2  * Illinois Open Source License *
3  * *
4  * University of Illinois/NCSA *
5  * Open Source License *
6  * *
7  * Copyright@2008, University of Illinois. All rights reserved. *
8  * *
9  * Developed by: *
10  * *
11  * Center for Simulation of Advanced Rockets *
12  * *
13  * University of Illinois *
14  * *
15  * www.csar.uiuc.edu *
16  * *
17  * Permission is hereby granted, free of charge, to any person *
18  * obtaining a copy of this software and associated documentation *
19  * files (the "Software"), to deal with the Software without *
20  * restriction, including without limitation the rights to use, *
21  * copy, modify, merge, publish, distribute, sublicense, and/or *
22  * sell copies of the Software, and to permit persons to whom the *
23  * Software is furnished to do so, subject to the following *
24  * conditions: *
25  * *
26  * *
27  * @ Redistributions of source code must retain the above copyright *
28  * notice, this list of conditions and the following disclaimers. *
29  * *
30  * @ Redistributions in binary form must reproduce the above *
31  * copyright notice, this list of conditions and the following *
32  * disclaimers in the documentation and/or other materials *
33  * provided with the distribution. *
34  * *
35  * @ Neither the names of the Center for Simulation of Advanced *
36  * Rockets, the University of Illinois, nor the names of its *
37  * contributors may be used to endorse or promote products derived *
38  * from this Software without specific prior written permission. *
39  * *
40  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
41  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
42  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
43  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
44  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
46  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
47  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
48  *********************************************************************
49  * Please acknowledge The University of Illinois Center for *
50  * Simulation of Advanced Rockets in works and publications *
51  * resulting from this software or its derivatives. *
52  *********************************************************************/
53 /* Hash Table routines
54 Orion Sky Lawlor, olawlor@acm.org, 11/21/1999
55 
56 This file defines the interface for the C++ Hashtable
57 class. A Hashtable stores key/object pairs
58 so that an arbitrary key can be accessed in
59 constant time.
60 
61 This is a complicated non-chaining Hashtable implementation.
62 It dynamically rehashes when the table gets too full.
63 Both the key and object are treated as arbitrary fixed-length
64 runs of bytes.
65 
66 Key hashing and comparison are handled by function pointers,
67 so the keys can be interpreted any way you like. The default
68 (and C interface way) is to treat them as runs of plain bytes.
69 */
70 
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74 
75 #ifndef __OSL_HASH_TABLE_C
76 #define __OSL_HASH_TABLE_C
77 
78 #include <stddef.h>
79 
80 /*C Version of Hashtable header file: */
81 typedef void *CkHashtable_c;
82 
83 /*Create hashtable with a single integer as the key*/
84 CkHashtable_c CkCreateHashtable_int(int objBytes,int initSize);
85 /*Create hashtable with a C string pointer as the key */
86 CkHashtable_c CkCreateHashtable_string(int objBytes,int initSize);
87 
88 void CkDeleteHashtable(CkHashtable_c h);
89 
90 /*Return object storage for this (possibly new) key*/
91 void *CkHashtablePut(CkHashtable_c h,const void *atKey);
92 
93 /*Return object storage for this (old) key-- */
94 /* returns NULL if key not found.*/
95 void *CkHashtableGet(CkHashtable_c h,const void *fromKey);
96 
97 /*Remove this key, rehashing as needed */
98 void CkHashtableRemove(CkHashtable_c h,const void *doomedKey);
99 
100 #endif /*__OSL_HASH_TABLE_C*/
101 #ifdef __cplusplus
102 };
103 
104 #ifndef __OSL_HASH_TABLE_CPP
105 #define __OSL_HASH_TABLE_CPP
106 
107 #include <stdio.h>
108 
109 //This data type is used to index into the hash table.
110 // For best results, all the bits of the hashCode should be
111 // meaningful (especially the high bits).
112 typedef unsigned int CkHashCode;
113 
114 //A circular-left-shift, useful for creating hash codes.
115 inline CkHashCode circleShift(CkHashCode h,unsigned int by)
116 {
117  const unsigned int intBits=8*sizeof(CkHashCode);
118  by%=intBits;
119  return (h<<by)|(h>>(intBits-by));
120 }
121 
122 //Functions to map keys to hash codes.
123 typedef CkHashCode (*CkHashFunction)(const void *keyData,size_t keyLen);
124 CkHashCode CkHashFunction_default(const void *keyData,size_t keyLen);
125 CkHashCode CkHashFunction_string(const void *keyData,size_t keyLen);
126 inline CkHashCode CkHashFunction_int(const void *keyData,size_t /*len*/)
127  {return *(int *)keyData;}
128 
129 //Functions return 1 if two keys are equal; 0 otherwise
130 typedef int (*CkHashCompare)(const void *key1,const void *key2,size_t keyLen);
131 int CkHashCompare_default(const void *key1,const void *key2,size_t keyLen);
132 int CkHashCompare_string(const void *key1,const void *key2,size_t keyLen);
133 inline int CkHashCompare_int(const void *k1,const void *k2,size_t /*len*/)
134  {return *(int *)k1 == *(int *)k2;}
135 
137 
138 class CkHashtableIterator;
139 
157 class CkHashtableLayout {
158  int size;
159  int ko,ks;
160  int po,ps;
161  int oo,os;
162  public:
163  CkHashtableLayout(int keySize,int emptyOffset,
164  int objectOffset,int objectSize,int entryLength)
165  {
166  size=entryLength;
167  ko=0; ks=keySize;
168  po=emptyOffset; ps=1;
169  oo=objectOffset; os=objectSize;
170  }
171 
172  inline int entrySize(void) const {return size;}
173  inline int keySize(void) const {return ks;}
174  inline int objectSize(void) const {return os;}
175 
176 //Utility functions:
178  inline char *getKey(char *entry) const {return entry+ko;}
180  inline char *getObject(char *entry) const {return entry+oo;}
181 
183  inline char isEmpty(char *entry) const {return *(entry+po);}
185  inline void empty(char *entry) const {*(entry+po)=1;}
187  inline void fill(char *entry) const {*(entry+po)=0;}
188 
190  inline char *nextEntry(char *entry) const {return entry+size;}
191 
193  inline char *entryFromKey(char *key) const {return key-ko;}
194 };
195 
201 class CkHashtable {
202 private:
203  CkHashtable(const CkHashtable &); //Don't use these
204  void operator=(const CkHashtable &);
205 protected:
206  int len;//Vertical dimension of below array (best if prime)
207  CkHashtableLayout layout; //Byte-wise storage layout for an entry
208  char *table;//len hashtable entries
209 
210  int nObj;//Number of objects actually stored (0..len)
211  int resizeAt;//Resize when nObj>=resizeAt
212  CkHashFunction hash; //Function pointer to compute key hash
213  CkHashCompare compare; //Function pointer to compare keys
214 
215  float loadFactor;//Maximum fraction of table to fill
216  // (0->always enlarge; 1->only when absolutely needed)
217 
218  //Increment i around the table
219  int inc(int &i) const {i++; if (i>=len) i=0; return i;}
220 
221  //Return the start of the i'th entry in the hash table
222  char *entry(int i) const {return (char *)(table+i*layout.entrySize());}
223 
224  //Find the given key in the table. If it's not there, return NULL
225  char *findKey(const void *key) const;
226  //Find a spot for the given key in the table. If there's not room, return NULL
227  char *findEntry(const void *key) const;
228 
229  //Build a new empty table of the given size
230  void buildTable(int newLen);
231  //Set the table to the given size, re-hashing everything.
232  void rehash(int newLen);
233 public:
234  //Constructor-- create an empty hash table of the given size
235  CkHashtable(const CkHashtableLayout &layout_,
236  int initLen=5,float NloadFactor=0.5,
237  CkHashFunction hash=CkHashFunction_default,
238  CkHashCompare compare=CkHashCompare_default);
239  //Destructor-- destroy table
240  ~CkHashtable();
241 
242  //Add the given object to this table under the given key
243  // Returns pointer to object storage.
244  // Table will be resized if needed.
245  void *put(const void *key);
246 
247  //Look up the given object in this table. Return NULL if not found.
248  void *get(const void *key) const {
249  char *ent=findKey(key);
250  if (ent==NULL) return NULL;
251  else return layout.getObject(ent);
252  }
253 
254  //Remove this object from the hashtable (re-hashing if needed)
255  void remove(const void *key);
256 
257  //Remove all objects and keys
258  void empty(void);
259 
260  int numObjects(void) const { return nObj; }
261 
262  //Return an iterator for the objects in this hash table
263  CkHashtableIterator *iterator(void);
264 };
265 
266 //A HashtableIterator lets you easily list all the objects
267 // in a hash table (without knowing all the keys).
268 class CkHashtableIterator {
269 protected:
270  int len;
271  CkHashtableLayout layout; //Byte-wise storage layout for an entry
272  char *table;
273  int curNo;//Table index of current object (to be returned next)
274  //Return the start of the i'th entry in the hash table
275  char *entry(int i) const {return table+i*layout.entrySize();}
276 public:
277  CkHashtableIterator(char *table_,int len_,const CkHashtableLayout &lo)
278  :len(len_),layout(lo),table(table_)
279  {curNo=0;}
280 
281  //Seek to start of hash table
282  void seekStart(void);
283 
284  //Seek forward (or back) n hash slots (*not* n objects!)
285  void seek(int n);
286 
287  //Return 1 if next will be non-NULL
288  int hasNext(void);
289  //Return the next object, or NULL if none.
290  // The corresponding object key will be returned in retKey.
291  void *next(void **retKey=NULL);
292 };
293 
294 
296 
305 template <class KEY, class OBJ>
306 class CkHashtableTslow:public CkHashtable {
307  //Return the layout for this hashtable:
308  static inline CkHashtableLayout getLayout(void) {
309  //This struct defines the in-memory layout that we will use.
310  // By including it in a struct rather than figuring it out ourselves,
311  // we let the compiler figure out what the (bizarre) alignment requirements are.
312  struct entry_t {
313  KEY k;
314  char empty;
315  OBJ o;
316  };
317  // HACK: All I want is the offset from entry_t to empty and o;
318  // but the compiler's "offsetof" keyword complains "non-POD type!".
319  entry_t *e=(entry_t *)0;
320  int emptyOffset=((char *)&e->empty)-(char *)e;
321  int oOffset=((char *)&e->o)-(char *)e;
322  return CkHashtableLayout(sizeof(KEY),emptyOffset,
323  oOffset,sizeof(OBJ),sizeof(entry_t));
324  }
325 public:
326  //Constructor-- create an empty hash table of at least the given size
327  CkHashtableTslow(
328  int initLen=5,float NloadFactor=0.5,
329  CkHashFunction Nhash=CkHashFunction_default,
330  CkHashCompare Ncompare=CkHashCompare_default)
331  :CkHashtable(getLayout(),initLen,NloadFactor,Nhash,Ncompare)
332  {}
333 
334  OBJ &put(const KEY &key) {
335  void *obj = CkHashtable::put((const void *)&key);
336  return *(OBJ *)obj;
337  }
338  OBJ get(const KEY &key) const {
339  void *r=CkHashtable::get((const void *)&key);
340  if (r==NULL) return OBJ(0);
341  else return *(OBJ *)r;
342  }
343  //Use this version when you're sure the entry exists
344  OBJ &getRef(const KEY &key) {
345  return *(OBJ *)CkHashtable::get((const void *)&key);
346  }
347  void remove(const KEY &key) {CkHashtable::remove((const void *)&key);}
348 };
349 
350 //Declare the KEY.hash & KEY.compare functions inline for a
351 // completely inlined (very fast) hashtable lookup.
352 // You MUST be SURE the hash and compare functions return the same
353 // values as the staticHash and staticCompare functions.
354 template <class KEY, class OBJ>
355 class CkHashtableT:public CkHashtableTslow<KEY,OBJ> {
356 public:
357  //Constructor-- create an empty hash table of at least the given size
358  CkHashtableT(int initLen=5,float NloadFactor=0.5)
359  :CkHashtableTslow<KEY,OBJ>(initLen,NloadFactor,
360  KEY::staticHash,KEY::staticCompare)
361  {}
362  CkHashtableT(
363  int initLen,float NloadFactor,
364  CkHashFunction Nhash,CkHashCompare Ncompare)
365  :CkHashtableTslow<KEY,OBJ>(initLen,NloadFactor,Nhash,Ncompare)
366  {}
367 
368  //Return the object, or "0" if it doesn't exist
369  OBJ get(const KEY &key) const {
370  int i=key.hash()%this->len;
371  while(1) {//Assumes key or empty slot will be found
372  char *cur=this->entry(i);
373  //An empty slot indicates the key is not here
374  if (this->layout.isEmpty(cur)){
375  return OBJ(0);
376  }
377  //Is this the key?
378  if (key.compare(*(KEY *)this->layout.getKey(cur)))
379  return *(OBJ *)this->layout.getObject(cur);
380  this->inc(i);
381  };
382  }
383  //Use this version when you're sure the entry exists--
384  // avoids the test for an empty entry
385  OBJ &getRef(const KEY &key) {
386  int i=key.hash()%this->len;
387  while(1) {//Assumes key or empty slot will be found
388  char *cur=this->entry(i);
389  //Is this the key?
390  if (key.compare(*(KEY *)this->layout.getKey(cur)))
391  return *(OBJ *)this->layout.getObject(cur);
392  this->inc(i);
393  };
394  }
395 
396 };
397 
408 template <class T> class CkHashtableAdaptorT {
409  T val;
410 public:
411  CkHashtableAdaptorT<T>(const T &v):val(v) {}
413  CkHashtableAdaptorT<T>(){}
414  operator T & () {return val;}
415  operator const T & () const {return val;}
416  inline CkHashCode hash(void) const
417  {return (CkHashCode)val;}
418  static CkHashCode staticHash(const void *k,size_t)
419  {return ((CkHashtableAdaptorT<T> *)k)->hash();}
420  inline int compare(const CkHashtableAdaptorT<T> &t) const
421  {return val==t.val;}
422  static int staticCompare(const void *a,const void *b,size_t)
423  {
424  return ((CkHashtableAdaptorT<T> *)a)->
425  compare(*(CkHashtableAdaptorT<T> *)b);
426  }
427 };
428 
429 #endif /*__OSL_HASH_TABLE_C++*/
430 
431 #endif /*C++*/
432 
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed by
Definition: roccomf90.h:7
void * CkHashtable_c
Definition: hash_table.h:81
CDECL CkHashtable_c CkCreateHashtable_string(int objBytes, int initSize)
Definition: hash_table.cpp:430
j indices k indices k
Definition: Indexing.h:6
CDECL void * CkHashtablePut(CkHashtable_c h, const void *atKey)
Definition: hash_table.cpp:443
CDECL CkHashtable_c CkCreateHashtable_int(int objBytes, int initSize)
Definition: hash_table.cpp:421
boolean empty(T_VertexSet s)
const unsigned int key2
Definition: CImg.h:2686
int CkHashCompare_string(const void *key1, const void *key2, size_t keyLen)
Definition: hash_table.cpp:112
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to and or **sell copies of the and to permit persons to whom the **Software is furnished to do subject to the following this list of conditions and the following disclaimers ****Redistributions in binary form must reproduce the above **copyright this list of conditions and the following **disclaimers in the documentation and or other materials **provided with the distribution ****Neither the names of the Center for Simulation of Advanced the University of nor the names of its **contributors may be used to endorse or promote products derived **from this Software without specific prior written permission ****THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE v
Definition: roccomf90.h:20
CkHashCode CkHashFunction_string(const void *keyData, size_t keyLen)
Definition: hash_table.cpp:88
blockLoc i
Definition: read.cpp:79
const NT & n
int CkHashCompare_default(const void *key1, const void *key2, size_t keyLen)
Definition: hash_table.cpp:102
CkHashCode CkHashFunction_default(const void *keyData, size_t keyLen)
Definition: hash_table.cpp:76
CDECL void CkDeleteHashtable(CkHashtable_c h)
Definition: hash_table.cpp:438
CGAL_KERNEL_INLINE Comparison_result compare(const NT &n1, const NT &n2)
Definition: number_utils.h:143
static T_Key key
Definition: vinci_lass.c:76
CDECL void CkHashtableRemove(CkHashtable_c h, const void *doomedKey)
Definition: hash_table.cpp:454
const unsigned int key1
Definition: CImg.h:2685
CDECL void * CkHashtableGet(CkHashtable_c h, const void *fromKey)
Definition: hash_table.cpp:449