comparison TaskManager/Test/test_render/task/TileHash.cpp @ 167:c8b868871dce

DrawSpan で使う Tile の Hash の扱いは class TileHash を生成する事に。
author gongo@localhost.localdomain
date Tue, 09 Dec 2008 15:07:31 +0900
parents
children 56be4a6e5513
comparison
equal deleted inserted replaced
166:e297ecaf2b4d 167:c8b868871dce
1 #include <string.h>
2 #include <stdlib.h>
3 #include "TileHash.h"
4
5 static unsigned short PRIME[8] = {
6 0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd,
7 };
8
9 int
10 TileHash::hash(uint32 data)
11 {
12 int value = 0;
13 int n = 0;
14 int key;
15
16 for (int i = 0; i < 8; i ++) {
17 key = data & 0xf;
18 value += key * PRIME[n++];
19 data >>= 4;
20 }
21
22 return value % hashSize;
23 }
24
25 TileHash::TileHash(void)
26 {
27 hashSize = 263;
28 tableSize = sizeof(TilePtr)*hashSize;
29
30 table = (TilePtr*)malloc(tableSize);
31 clear();
32 }
33
34 int
35 TileHash::put(uint32 *key, TilePtr data)
36 {
37 int hashval = hash((uint32)key);
38
39 for (int i = 0; i < hashSize/2; i++) {
40 int index = (hashval + i*i)%hashSize;
41
42 if (table[index] == 0) {
43 table[index] = data;
44 return index;
45 }
46 }
47
48 return -1;
49 }
50
51 TilePtr
52 TileHash::get(uint32 *key)
53 {
54 int hashval = hash((uint32)key);
55
56 for (int i = 0; i < hashSize/2; i++) {
57 int index = (hashval + i*i)%hashSize;
58
59 if (table[index] != NULL &&
60 table[index]->texture_addr == key) {
61 return table[index];
62 }
63 }
64
65 return NULL;
66 }
67
68 void
69 TileHash::clear(void)
70 {
71 bzero(table, tableSize);
72 }