Mercurial > hg > Game > Cerium
view TaskManager/Test/test_render/spe/TileHash.cpp @ 173:1e62bd02f820 draft
add scale
author | gongo@localhost.localdomain |
---|---|
date | Thu, 11 Dec 2008 20:17:01 +0900 |
parents | 508beb59e0eb |
children | a19d3ed4ce5b |
line wrap: on
line source
#include <string.h> #include <stdlib.h> #include "TileHash.h" static unsigned short PRIME[8] = { 0x002, 0x065, 0x0c7, 0x133, 0x191, 0x1f3, 0x259, 0x2bd, }; int TileHash::hash(uint32 data) { int value = 0; int n = 0; int key; for (int i = 0; i < 8; i ++) { key = data & 0xf; value += key * PRIME[n++]; data >>= 4; } return value % hashSize; } TileHash::TileHash(void) { hashSize = 263; tableSize = sizeof(TilePtr)*hashSize; table = (TilePtr*)malloc(tableSize); clear(); } int TileHash::put(uint32 *key, TilePtr data) { int hashval = hash((uint32)key); for (int i = 0; i < hashSize/2; i++) { int index = (hashval + i*i)%hashSize; if (table[index] == 0) { table[index] = data; return index; } } return -1; } TilePtr TileHash::get(uint32 *key) { int hashval = hash((uint32)key); for (int i = 0; i < hashSize/2; i++) { int index = (hashval + i*i)%hashSize; if (table[index] != NULL && table[index]->texture_addr == key) { return table[index]; } } return NULL; } void TileHash::remove(uint32 *key) { int hashval = hash((uint32)key); for (int i = 0; i < hashSize/2; i++) { int index = (hashval + i*i)%hashSize; if (table[index] != NULL && table[index]->texture_addr == key) { table[index] = NULL; } } } void TileHash::clear(void) { bzero(table, tableSize); }