comparison TaskManager/Test/test_render/spe/TileHash.cc @ 423:afb717c43a3c

change spe/*.cpp -> spe/*.cc
author hiroki@henri.cr.ie.u-ryukyu.ac.jp
date Thu, 24 Sep 2009 15:26:58 +0900
parents TaskManager/Test/test_render/spe/TileHash.cpp@b4b8345b5366
children 44c0bce54dcf
comparison
equal deleted inserted replaced
422:976ca6f00d65 423:afb717c43a3c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "TileHash.h"
5
6 static unsigned short PRIME[8] = {
7 0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd,
8 };
9
10 int
11 TileHash::hash(memaddr data)
12 {
13 int value = 0;
14 int n = 0;
15 int key;
16
17 for (uint32 i = 0; i < sizeof(memaddr) * 2; i ++) {
18 key = data & 0xf;
19 value += key * PRIME[n++ & 7];
20 data >>= 4;
21 }
22
23 return value % hashSize;
24 }
25
26 TileHash::TileHash(void)
27 {
28 //hashSize = 263;
29 //tableSize = sizeof(TilePtr)*hashSize;
30
31 table = (TilePtr*)malloc(tableSize);
32 clear();
33 }
34
35 int
36 TileHash::put(memaddr key, TilePtr data)
37 {
38 int hashval = hash(key);
39
40 for (int i = 0; i < hashSize/2; i++) {
41 int index = (hashval + i*i)%hashSize;
42
43 if (table[index] == 0) { // 空の table に入れる
44 table[index] = data;
45 return index;
46 }
47 }
48
49 return -1;
50 }
51
52 TilePtr
53 TileHash::get(memaddr key)
54 {
55 int hashval = hash(key);
56
57 for (int i = 0; i < hashSize/2; i++) {
58 int index = (hashval + i*i)%hashSize;
59
60 if (table[index] != NULL &&
61 table[index]->address == key) {
62 return table[index];
63 }
64 }
65
66 return NULL;
67 }
68
69 void
70 TileHash::remove(memaddr key)
71 {
72 int hashval = hash(key);
73
74 for (int i = 0; i < hashSize/2; i++) {
75 int index = (hashval + i*i)%hashSize;
76
77 if (table[index] != NULL &&
78 table[index]->address == key) {
79 table[index] = NULL;
80 }
81 }
82 }
83
84 void
85 TileHash::clear(void)
86 {
87 bzero(table, tableSize);
88 }