75 #ifndef __OSL_HASH_TABLE_C
76 #define __OSL_HASH_TABLE_C
104 #ifndef __OSL_HASH_TABLE_CPP
105 #define __OSL_HASH_TABLE_CPP
112 typedef unsigned int CkHashCode;
115 inline CkHashCode circleShift(CkHashCode h,
unsigned int by)
117 const unsigned int intBits=8*
sizeof(CkHashCode);
119 return (h<<by)|(h>>(intBits-
by));
123 typedef CkHashCode (*CkHashFunction)(
const void *keyData,
size_t keyLen);
126 inline CkHashCode CkHashFunction_int(
const void *keyData,
size_t )
127 {
return *(
int *)keyData;}
130 typedef int (*CkHashCompare)(
const void *
key1,
const void *
key2,
size_t keyLen);
133 inline int CkHashCompare_int(
const void *k1,
const void *k2,
size_t )
134 {
return *(
int *)k1 == *(
int *)k2;}
138 class CkHashtableIterator;
157 class CkHashtableLayout {
163 CkHashtableLayout(
int keySize,
int emptyOffset,
164 int objectOffset,
int objectSize,
int entryLength)
168 po=emptyOffset; ps=1;
169 oo=objectOffset; os=objectSize;
172 inline int entrySize(
void)
const {
return size;}
173 inline int keySize(
void)
const {
return ks;}
174 inline int objectSize(
void)
const {
return os;}
178 inline char *getKey(
char *entry)
const {
return entry+ko;}
180 inline char *getObject(
char *entry)
const {
return entry+oo;}
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;}
190 inline char *nextEntry(
char *entry)
const {
return entry+size;}
193 inline char *entryFromKey(
char *
key)
const {
return key-ko;}
203 CkHashtable(
const CkHashtable &);
204 void operator=(
const CkHashtable &);
207 CkHashtableLayout layout;
219 int inc(
int &
i)
const {i++;
if (i>=len) i=0;
return i;}
222 char *entry(
int i)
const {
return (
char *)(table+i*layout.entrySize());}
225 char *findKey(
const void *
key)
const;
227 char *findEntry(
const void *
key)
const;
230 void buildTable(
int newLen);
232 void rehash(
int newLen);
235 CkHashtable(
const CkHashtableLayout &layout_,
236 int initLen=5,
float NloadFactor=0.5,
245 void *put(
const void *
key);
248 void *
get(
const void *
key)
const {
249 char *ent=findKey(
key);
250 if (ent==NULL)
return NULL;
251 else return layout.getObject(ent);
255 void remove(
const void *
key);
260 int numObjects(
void)
const {
return nObj; }
263 CkHashtableIterator *iterator(
void);
268 class CkHashtableIterator {
271 CkHashtableLayout layout;
275 char *entry(
int i)
const {
return table+i*layout.entrySize();}
277 CkHashtableIterator(
char *table_,
int len_,
const CkHashtableLayout &lo)
278 :len(len_),layout(lo),table(table_)
282 void seekStart(
void);
291 void *next(
void **retKey=NULL);
305 template <
class KEY,
class OBJ>
306 class CkHashtableTslow:
public CkHashtable {
308 static inline CkHashtableLayout getLayout(
void) {
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));
328 int initLen=5,
float NloadFactor=0.5,
331 :CkHashtable(getLayout(),initLen,NloadFactor,Nhash,Ncompare)
334 OBJ &put(
const KEY &
key) {
335 void *obj = CkHashtable::put((
const void *)&key);
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;
344 OBJ &getRef(
const KEY &key) {
345 return *(OBJ *)CkHashtable::get((
const void *)&
key);
347 void remove(
const KEY &
key) {CkHashtable::remove((
const void *)&key);}
354 template <
class KEY,
class OBJ>
355 class CkHashtableT:
public CkHashtableTslow<KEY,OBJ> {
358 CkHashtableT(
int initLen=5,
float NloadFactor=0.5)
359 :CkHashtableTslow<KEY,OBJ>(initLen,NloadFactor,
360 KEY::staticHash,KEY::staticCompare)
363 int initLen,
float NloadFactor,
364 CkHashFunction Nhash,CkHashCompare Ncompare)
365 :CkHashtableTslow<KEY,OBJ>(initLen,NloadFactor,Nhash,Ncompare)
369 OBJ
get(
const KEY &
key)
const {
370 int i=key.hash()%this->len;
372 char *cur=this->entry(i);
374 if (this->layout.isEmpty(cur)){
378 if (key.compare(*(KEY *)this->layout.getKey(cur)))
379 return *(OBJ *)this->layout.getObject(cur);
385 OBJ &getRef(
const KEY &key) {
386 int i=key.hash()%this->len;
388 char *cur=this->entry(i);
390 if (key.compare(*(KEY *)this->layout.getKey(cur)))
391 return *(OBJ *)this->layout.getObject(cur);
408 template <
class T>
class CkHashtableAdaptorT {
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
422 static int staticCompare(
const void *a,
const void *b,
size_t)
424 return ((CkHashtableAdaptorT<T> *)a)->
425 compare(*(CkHashtableAdaptorT<T> *)b);
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed by
CDECL CkHashtable_c CkCreateHashtable_string(int objBytes, int initSize)
CDECL void * CkHashtablePut(CkHashtable_c h, const void *atKey)
CDECL CkHashtable_c CkCreateHashtable_int(int objBytes, int initSize)
boolean empty(T_VertexSet s)
int CkHashCompare_string(const void *key1, const void *key2, size_t keyLen)
*********************************************************************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
CkHashCode CkHashFunction_string(const void *keyData, size_t keyLen)
int CkHashCompare_default(const void *key1, const void *key2, size_t keyLen)
CkHashCode CkHashFunction_default(const void *keyData, size_t keyLen)
CDECL void CkDeleteHashtable(CkHashtable_c h)
CGAL_KERNEL_INLINE Comparison_result compare(const NT &n1, const NT &n2)
CDECL void CkHashtableRemove(CkHashtable_c h, const void *doomedKey)
CDECL void * CkHashtableGet(CkHashtable_c h, const void *fromKey)