diff Renderer/Engine/TextureHash.cc @ 0:04e28d8d3c6f

first commit
author Daiki KINJYO <e085722@ie.u-ryukyu.ac.jp>
date Mon, 08 Nov 2010 01:23:25 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Renderer/Engine/TextureHash.cc	Mon Nov 08 01:23:25 2010 +0900
@@ -0,0 +1,53 @@
+#include <string.h>
+#include <stdlib.h>
+#include "TextureHash.h"
+
+static int id_count;
+
+TextureHash::TextureHash(void)
+{
+    table = (hashtable*)malloc(sizeof(hashtable)*TABLE_SIZE);
+
+    for (int i = 0; i < TABLE_SIZE; i++) {
+        table[i].tx_id = -1;
+        table[i].key = NULL;
+    }
+}
+
+TextureHash::~TextureHash(void)
+{
+    free(table);
+}
+
+int
+TextureHash::hash_function(const char *key)
+{
+    //float value = 0.0;
+    int value = 0;
+
+    for (int i = 0; key[i]; i++) {
+        value += key[i]*(i+1)*17+1;
+    }
+
+    return value%TABLE_SIZE;
+}
+
+int
+TextureHash::hash_regist(const char* key, int &id)
+{
+    int hash = hash_function(key);
+
+    for (int i = 0; ; i++) {
+        if (table[hash].tx_id == -1) {
+            table[hash].key   = (char*)key;
+            id = id_count++;
+            return 0;
+
+        } else if (strcmp(key, table[hash].key) == 0
+                   && table[hash].tx_id != -1){
+            id = table[hash].tx_id;
+            return 1;
+        }
+        hash = ((37*hash)^(11*i)) % TABLE_SIZE;
+    }
+}