688 |
khor |
1 |
/* ----------------------------------------------------------------------- *
|
|
|
2 |
* This file is part of GEL, http://www.imm.dtu.dk/GEL
|
|
|
3 |
* Copyright (C) the authors and DTU Informatics
|
|
|
4 |
* For license and list of authors, see ../../doc/intro.pdf
|
|
|
5 |
* ----------------------------------------------------------------------- */
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* @file HashKey.h
|
|
|
9 |
* @brief Kehy class for hash tables.
|
|
|
10 |
*/
|
|
|
11 |
#ifndef __UTIL_HASHKEY_H
|
|
|
12 |
#define __UTIL_HASHKEY_H
|
|
|
13 |
|
|
|
14 |
#include <stdlib.h>
|
|
|
15 |
#include <limits.h>
|
|
|
16 |
#include "../CGLA/Vec3uc.h"
|
|
|
17 |
#include "../CGLA/Vec3usi.h"
|
|
|
18 |
|
|
|
19 |
namespace Util
|
|
|
20 |
{
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
extern int randoms1[UCHAR_MAX];
|
|
|
24 |
extern int randoms2[UCHAR_MAX];
|
|
|
25 |
extern int randoms3[UCHAR_MAX];
|
|
|
26 |
|
|
|
27 |
bool init_randoms();
|
|
|
28 |
void do_init_randoms();
|
|
|
29 |
|
|
|
30 |
struct HashKey3uc
|
|
|
31 |
{
|
|
|
32 |
CGLA::Vec3uc key;
|
|
|
33 |
|
|
|
34 |
HashKey3uc() {do_init_randoms();}
|
|
|
35 |
HashKey3uc(CGLA::Vec3uc _key): key(_key) {do_init_randoms();}
|
|
|
36 |
HashKey3uc(CGLA::Vec3i _key): key(_key) {do_init_randoms();}
|
|
|
37 |
|
|
|
38 |
int hash(int use_size) const
|
|
|
39 |
{
|
|
|
40 |
return int((randoms1[key[0]] >> (key[1]&0x0f)) +
|
|
|
41 |
(randoms2[key[1]] >> (key[2]&0x0f)) +
|
|
|
42 |
(randoms3[key[2]] >> (key[0]&0x0f))) & (use_size-1);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
bool operator==(const HashKey3uc& k2) const {return key==k2.key;}
|
|
|
46 |
bool operator!=(const HashKey3uc& k2) const {return !(key==k2.key);}
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
struct HashKey3usi
|
|
|
50 |
{
|
|
|
51 |
CGLA::Vec3usi key;
|
|
|
52 |
|
|
|
53 |
HashKey3usi() {do_init_randoms();}
|
|
|
54 |
HashKey3usi(CGLA::Vec3usi _key): key(_key) {do_init_randoms();}
|
|
|
55 |
HashKey3usi(CGLA::Vec3i _key): key(_key) {do_init_randoms();}
|
|
|
56 |
|
|
|
57 |
int hash(int use_size) const
|
|
|
58 |
{
|
|
|
59 |
return int(
|
|
|
60 |
((randoms1[key[0]&0xff00>>8] * randoms2[key[1]&0xff] >> (key[2]&0x0f))
|
|
|
61 |
+ (randoms2[key[1]&0xff00>>8] * randoms1[key[2]&0xff] >> (key[0]&0x0f))
|
|
|
62 |
+ (randoms3[key[2]&0xff00>>8] * randoms3[key[0]&0xff] >> (key[1]&0x0f)))
|
|
|
63 |
& (use_size-1));
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
bool operator==(const HashKey3usi& k2) const {return key==k2.key;}
|
|
|
67 |
bool operator!=(const HashKey3usi& k2) const {return !(key==k2.key);}
|
|
|
68 |
};
|
|
|
69 |
|
|
|
70 |
struct HashKey1c
|
|
|
71 |
{
|
|
|
72 |
unsigned char key;
|
|
|
73 |
|
|
|
74 |
HashKey1c() {do_init_randoms();}
|
|
|
75 |
HashKey1c(unsigned char _key): key(_key) {do_init_randoms();}
|
|
|
76 |
|
|
|
77 |
int hash(int use_size) const
|
|
|
78 |
{
|
|
|
79 |
return int(randoms1[key] & (use_size-1));
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
bool operator==(const HashKey1c& k2) const {return key==k2.key;}
|
|
|
83 |
bool operator!=(const HashKey1c& k2) const {return !(key==k2.key);}
|
|
|
84 |
};
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
#endif
|