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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
539
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <string.h>
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #include <stdlib.h>
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 #include "TextureHash.h"
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 TextureHash::TextureHash(void)
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
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
20f09564c586 fix (not yet tested)
root@localhost.localdomain
parents: 539
diff changeset
8 int size = sizeof(hashtable)*TABLE_SIZE;
20f09564c586 fix (not yet tested)
root@localhost.localdomain
parents: 539
diff changeset
9 #if defined(__SPU__) || ! defined(HAS_POSIX_MEMALIGN)
20f09564c586 fix (not yet tested)
root@localhost.localdomain
parents: 539
diff changeset
10 table = (hashtable*)malloc(size);
20f09564c586 fix (not yet tested)
root@localhost.localdomain
parents: 539
diff changeset
11 #else
1133
kaito@dolphins
parents: 1114
diff changeset
12 posix_memalign((void**)&table, 16, size);
1087
20f09564c586 fix (not yet tested)
root@localhost.localdomain
parents: 539
diff changeset
13 #endif
1133
kaito@dolphins
parents: 1114
diff changeset
14
539
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 for (int i = 0; i < TABLE_SIZE; i++) {
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 table[i].tx_id = -1;
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 table[i].key = NULL;
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 }
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 }
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 TextureHash::~TextureHash(void)
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 {
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 free(table);
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 }
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 int
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 TextureHash::hash_function(const char *key)
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 {
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 //float value = 0.0;
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 int value = 0;
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 for (int i = 0; key[i]; i++) {
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 value += key[i]*(i+1)*17+1;
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 }
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 return value%TABLE_SIZE;
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 }
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 int
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 TextureHash::hash_regist(const char* key, int &id)
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 {
1114
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
42 int hash = hash_function(key);
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
43 for (int i = 0; ; i++) {
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
44 if (table[hash].tx_id == -1) {
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
45 table[hash].key = (char*)key;
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
46 id = id_count++;
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
47 return 0;
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
48
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
49 } else if (strcmp(key, table[hash].key) == 0
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
50 && table[hash].tx_id != -1){
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
51 id = table[hash].tx_id;
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
52 return 1;
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
53 }
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
54 hash = ((37*hash)^(11*i)) % TABLE_SIZE;
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
55 }
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
56 }
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
57
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
58 int
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
59 TextureHash::sg_hash_regist(const char* key, int &id)
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
60 {
539
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 int hash = hash_function(key);
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 for (int i = 0; ; i++) {
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 if (table[hash].tx_id == -1) {
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 table[hash].key = (char*)key;
1114
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
66 table[hash].tx_id = id;
539
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 return 0;
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 } else if (strcmp(key, table[hash].key) == 0
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 && table[hash].tx_id != -1){
1114
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
71 return table[hash].tx_id;
539
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 }
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 hash = ((37*hash)^(11*i)) % TABLE_SIZE;
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 }
3bc98f6d31ff Reorganization..
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 }
1114
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
76
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
77 int
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
78 TextureHash::get_sgid(const char* key)
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
79 {
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
80
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
81 int hash = hash_function(key);
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
82 for (int i = 0; ; i++) {
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
83 if (table[hash].tx_id == -1) {
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
84 return -1;
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
85
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
86 } else if (strcmp(key, table[hash].key) == 0
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
87 && table[hash].tx_id != -1){
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
88 return table[hash].tx_id;
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
89 }
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
90 hash = ((37*hash)^(11*i)) % TABLE_SIZE;
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
91 }
ee481853d5dd SgidHash remove.
tkaito
parents: 1087
diff changeset
92 }