Subversion Repositories gelsvn

Rev

Rev 448 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 448 Rev 595
-
 
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
 */
1
#ifndef __UTIL_HASHKEY_H
11
#ifndef __UTIL_HASHKEY_H
2
#define __UTIL_HASHKEY_H
12
#define __UTIL_HASHKEY_H
3
 
13
 
4
#include <stdlib.h>
14
#include <stdlib.h>
5
#include <limits.h>
15
#include <limits.h>
6
#include "CGLA/Vec3uc.h"
16
#include "CGLA/Vec3uc.h"
7
#include "CGLA/Vec3usi.h"
17
#include "CGLA/Vec3usi.h"
8
 
18
 
9
namespace Util
19
namespace Util
10
{
20
{
11
 
21
 
12
	
22
	
13
	extern int randoms1[UCHAR_MAX];
23
	extern int randoms1[UCHAR_MAX];
14
	extern int randoms2[UCHAR_MAX];
24
	extern int randoms2[UCHAR_MAX];
15
	extern int randoms3[UCHAR_MAX];
25
	extern int randoms3[UCHAR_MAX];
16
 
26
 
17
	bool init_randoms();
27
	bool init_randoms();
18
	void do_init_randoms();
28
	void do_init_randoms();
19
	
29
	
20
	struct HashKey3uc
30
	struct HashKey3uc
21
	{
31
	{
22
		CGLA::Vec3uc key;
32
		CGLA::Vec3uc key;
23
 
33
 
24
		HashKey3uc() {do_init_randoms();}
34
		HashKey3uc() {do_init_randoms();}
25
		HashKey3uc(CGLA::Vec3uc _key): key(_key) {do_init_randoms();}
35
		HashKey3uc(CGLA::Vec3uc _key): key(_key) {do_init_randoms();}
26
		HashKey3uc(CGLA::Vec3i _key): key(_key) {do_init_randoms();}
36
		HashKey3uc(CGLA::Vec3i _key): key(_key) {do_init_randoms();}
27
		
37
		
28
		int hash(int use_size) const
38
		int hash(int use_size) const
29
		{
39
		{
30
			return int((randoms1[key[0]] >> (key[1]&0x0f)) +
40
			return int((randoms1[key[0]] >> (key[1]&0x0f)) +
31
								 (randoms2[key[1]] >> (key[2]&0x0f)) +
41
								 (randoms2[key[1]] >> (key[2]&0x0f)) +
32
								 (randoms3[key[2]] >> (key[0]&0x0f))) & (use_size-1);
42
								 (randoms3[key[2]] >> (key[0]&0x0f))) & (use_size-1);
33
		}
43
		}
34
		
44
		
35
		bool operator==(const HashKey3uc& k2) const {return key==k2.key;}
45
		bool operator==(const HashKey3uc& k2) const {return key==k2.key;}
36
		bool operator!=(const HashKey3uc& k2) const {return !(key==k2.key);}
46
		bool operator!=(const HashKey3uc& k2) const {return !(key==k2.key);}
37
	};
47
	};
38
 
48
 
39
	struct HashKey3usi
49
	struct HashKey3usi
40
	{
50
	{
41
		CGLA::Vec3usi key;
51
		CGLA::Vec3usi key;
42
 
52
 
43
		HashKey3usi() {do_init_randoms();}
53
		HashKey3usi() {do_init_randoms();}
44
		HashKey3usi(CGLA::Vec3usi _key): key(_key) {do_init_randoms();}
54
		HashKey3usi(CGLA::Vec3usi _key): key(_key) {do_init_randoms();}
45
		HashKey3usi(CGLA::Vec3i _key): key(_key) {do_init_randoms();}
55
		HashKey3usi(CGLA::Vec3i _key): key(_key) {do_init_randoms();}
46
 
56
 
47
		int hash(int use_size) const
57
		int hash(int use_size) const
48
		{
58
		{
49
			return int(
59
			return int(
50
								 ((randoms1[key[0]&0xff00>>8] * randoms2[key[1]&0xff] >> (key[2]&0x0f))
60
								 ((randoms1[key[0]&0xff00>>8] * randoms2[key[1]&0xff] >> (key[2]&0x0f))
51
									+ (randoms2[key[1]&0xff00>>8] * randoms1[key[2]&0xff] >> (key[0]&0x0f))
61
									+ (randoms2[key[1]&0xff00>>8] * randoms1[key[2]&0xff] >> (key[0]&0x0f))
52
									+ (randoms3[key[2]&0xff00>>8] * randoms3[key[0]&0xff] >> (key[1]&0x0f)))
62
									+ (randoms3[key[2]&0xff00>>8] * randoms3[key[0]&0xff] >> (key[1]&0x0f)))
53
								 & (use_size-1));
63
								 & (use_size-1));
54
		}
64
		}
55
	
65
	
56
		bool operator==(const HashKey3usi& k2) const {return key==k2.key;}
66
		bool operator==(const HashKey3usi& k2) const {return key==k2.key;}
57
		bool operator!=(const HashKey3usi& k2) const {return !(key==k2.key);}
67
		bool operator!=(const HashKey3usi& k2) const {return !(key==k2.key);}
58
	};
68
	};
59
 
69
 
60
	struct HashKey1c
70
	struct HashKey1c
61
	{
71
	{
62
		unsigned char key;
72
		unsigned char key;
63
 
73
 
64
		HashKey1c() {do_init_randoms();}
74
		HashKey1c() {do_init_randoms();}
65
		HashKey1c(unsigned char _key): key(_key) {do_init_randoms();}
75
		HashKey1c(unsigned char _key): key(_key) {do_init_randoms();}
66
 
76
 
67
		int hash(int use_size) const
77
		int hash(int use_size) const
68
		{
78
		{
69
			return int(randoms1[key] & (use_size-1));
79
			return int(randoms1[key] & (use_size-1));
70
		}
80
		}
71
	
81
	
72
		bool operator==(const HashKey1c& k2) const {return key==k2.key;}
82
		bool operator==(const HashKey1c& k2) const {return key==k2.key;}
73
		bool operator!=(const HashKey1c& k2) const {return !(key==k2.key);}
83
		bool operator!=(const HashKey1c& k2) const {return !(key==k2.key);}
74
	};
84
	};
75
}
85
}
76
 
86
 
77
 
87
 
78
#endif
88
#endif
79
 
89