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 {
|
1087
|
9 int size = sizeof(hashtable)*TABLE_SIZE;
|
|
10 #if defined(__SPU__) || ! defined(HAS_POSIX_MEMALIGN)
|
|
11 table = (hashtable*)malloc(size);
|
|
12 #else
|
|
13 posix_memalign((void**)&table, alignment, size);
|
|
14 #endif
|
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 {
|
|
42 int hash = hash_function(key);
|
|
43
|
|
44 for (int i = 0; ; i++) {
|
|
45 if (table[hash].tx_id == -1) {
|
|
46 table[hash].key = (char*)key;
|
|
47 id = id_count++;
|
|
48 return 0;
|
|
49
|
|
50 } else if (strcmp(key, table[hash].key) == 0
|
|
51 && table[hash].tx_id != -1){
|
|
52 id = table[hash].tx_id;
|
|
53 return 1;
|
|
54 }
|
|
55 hash = ((37*hash)^(11*i)) % TABLE_SIZE;
|
|
56 }
|
|
57 }
|