539
|
1 #include <string.h>
|
|
2 #include <stdlib.h>
|
|
3 #include "TextureHash.h"
|
|
4
|
|
5 static int id_count;
|
|
6
|
|
7 TextureHash::TextureHash(void)
|
|
8 {
|
|
9 table = (hashtable*)malloc(sizeof(hashtable)*TABLE_SIZE);
|
|
10
|
|
11 for (int i = 0; i < TABLE_SIZE; i++) {
|
|
12 table[i].tx_id = -1;
|
|
13 table[i].key = NULL;
|
|
14 }
|
|
15 }
|
|
16
|
|
17 TextureHash::~TextureHash(void)
|
|
18 {
|
|
19 free(table);
|
|
20 }
|
|
21
|
|
22 int
|
|
23 TextureHash::hash_function(const char *key)
|
|
24 {
|
|
25 //float value = 0.0;
|
|
26 int value = 0;
|
|
27
|
|
28 for (int i = 0; key[i]; i++) {
|
|
29 value += key[i]*(i+1)*17+1;
|
|
30 }
|
|
31
|
|
32 return value%TABLE_SIZE;
|
|
33 }
|
|
34
|
|
35 int
|
|
36 TextureHash::hash_regist(const char* key, int &id)
|
|
37 {
|
|
38 int hash = hash_function(key);
|
|
39
|
|
40 for (int i = 0; ; i++) {
|
|
41 if (table[hash].tx_id == -1) {
|
|
42 table[hash].key = (char*)key;
|
|
43 id = id_count++;
|
|
44 return 0;
|
|
45
|
|
46 } else if (strcmp(key, table[hash].key) == 0
|
|
47 && table[hash].tx_id != -1){
|
|
48 id = table[hash].tx_id;
|
|
49 return 1;
|
|
50 }
|
|
51 hash = ((37*hash)^(11*i)) % TABLE_SIZE;
|
|
52 }
|
|
53 }
|