Mercurial > hg > Game > Cerium
annotate Renderer/Engine/TextureHash.cc @ 1134:80c1d74f2c7f draft
change the scope of id_count to member variables
author | kazz <kazz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 17 Feb 2011 09:13:27 +0900 |
parents | bb17a03bab60 |
children | 5abf0ce8c71c |
rev | line source |
---|---|
539 | 1 #include <string.h> |
2 #include <stdlib.h> | |
3 #include "TextureHash.h" | |
4 | |
5 TextureHash::TextureHash(void) | |
6 { | |
1134
80c1d74f2c7f
change the scope of id_count to member variables
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1133
diff
changeset
|
7 id_count = 0; |
1087 | 8 int size = sizeof(hashtable)*TABLE_SIZE; |
9 #if defined(__SPU__) || ! defined(HAS_POSIX_MEMALIGN) | |
10 table = (hashtable*)malloc(size); | |
11 #else | |
1133 | 12 posix_memalign((void**)&table, 16, size); |
1087 | 13 #endif |
1133 | 14 |
539 | 15 for (int i = 0; i < TABLE_SIZE; i++) { |
16 table[i].tx_id = -1; | |
17 table[i].key = NULL; | |
18 } | |
19 } | |
20 | |
21 TextureHash::~TextureHash(void) | |
22 { | |
23 free(table); | |
24 } | |
25 | |
26 int | |
27 TextureHash::hash_function(const char *key) | |
28 { | |
29 //float value = 0.0; | |
30 int value = 0; | |
31 | |
32 for (int i = 0; key[i]; i++) { | |
33 value += key[i]*(i+1)*17+1; | |
34 } | |
35 | |
36 return value%TABLE_SIZE; | |
37 } | |
38 | |
39 int | |
40 TextureHash::hash_regist(const char* key, int &id) | |
41 { | |
1114 | 42 int hash = hash_function(key); |
43 for (int i = 0; ; i++) { | |
44 if (table[hash].tx_id == -1) { | |
45 table[hash].key = (char*)key; | |
46 id = id_count++; | |
47 return 0; | |
48 | |
49 } else if (strcmp(key, table[hash].key) == 0 | |
50 && table[hash].tx_id != -1){ | |
51 id = table[hash].tx_id; | |
52 return 1; | |
53 } | |
54 hash = ((37*hash)^(11*i)) % TABLE_SIZE; | |
55 } | |
56 } | |
57 | |
58 int | |
59 TextureHash::sg_hash_regist(const char* key, int &id) | |
60 { | |
539 | 61 int hash = hash_function(key); |
62 | |
63 for (int i = 0; ; i++) { | |
64 if (table[hash].tx_id == -1) { | |
65 table[hash].key = (char*)key; | |
1114 | 66 table[hash].tx_id = id; |
539 | 67 return 0; |
68 | |
69 } else if (strcmp(key, table[hash].key) == 0 | |
70 && table[hash].tx_id != -1){ | |
1114 | 71 return table[hash].tx_id; |
539 | 72 } |
73 hash = ((37*hash)^(11*i)) % TABLE_SIZE; | |
74 } | |
75 } | |
1114 | 76 |
77 int | |
78 TextureHash::get_sgid(const char* key) | |
79 { | |
80 | |
81 int hash = hash_function(key); | |
82 for (int i = 0; ; i++) { | |
83 if (table[hash].tx_id == -1) { | |
84 return -1; | |
85 | |
86 } else if (strcmp(key, table[hash].key) == 0 | |
87 && table[hash].tx_id != -1){ | |
88 return table[hash].tx_id; | |
89 } | |
90 hash = ((37*hash)^(11*i)) % TABLE_SIZE; | |
91 } | |
92 } |