changeset 1108:cbfedf774354 draft

getSgid was made hash.
author tkaito
date Tue, 11 Jan 2011 07:06:24 +0900
parents 25d3cfb85439
children d2cb74984336
files Renderer/Engine/SceneGraph.cc Renderer/Engine/SceneGraphRoot.cc Renderer/Engine/SgidHash.cc Renderer/Engine/SgidHash.h
diffstat 4 files changed, 103 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Engine/SceneGraph.cc	Mon Jan 10 22:09:06 2011 +0900
+++ b/Renderer/Engine/SceneGraph.cc	Tue Jan 11 07:06:24 2011 +0900
@@ -19,7 +19,6 @@
 static TextureHash texture_hash;
 texture_list list[TABLE_SIZE];
 
-
 extern int decode(char *cont, FILE *outfile);
 
 static void
@@ -189,6 +188,7 @@
     get_data(manager, surface->children);
 
     finalize = &SceneGraph::finalize_original;
+
 }
 
 void
--- a/Renderer/Engine/SceneGraphRoot.cc	Mon Jan 10 22:09:06 2011 +0900
+++ b/Renderer/Engine/SceneGraphRoot.cc	Tue Jan 11 07:06:24 2011 +0900
@@ -7,13 +7,14 @@
 #include "TextureHash.h"
 #include "texture.h"
 #include "Application.h"
+#include "SgidHash.h"
 
 static int cnt = 0;
 static const int SGLIST_LENGTH = 138;
 static int sg_src_size = SGLIST_LENGTH ;
 static int sg_src_id = -1;
 static SceneGraphPtr *sg_src;
-
+static SgidHash sgid_hash;
 
 SceneGraphRoot *sgroot;
 
@@ -113,6 +114,7 @@
     }
     sg->sgid = ++sg_src_id;
     sg_src[sg->sgid] = sg;
+    sgid_hash.hash_regist((const char*)sg->name, sg->sgid);
 }
 
 
@@ -250,11 +252,15 @@
 int
 SceneGraphRoot::getSgid(const char *name)
 {
+/*
     for(int i =0;i<= sg_src_id; i++) {
 	if (sg_src[i] && strcmp(name,sg_src[i]->name) == 0)
 	    return i;
     }
     return -1;
+*/
+    return sgid_hash.get_sgid(name);
+    
 }
 
 int
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Renderer/Engine/SgidHash.cc	Tue Jan 11 07:06:24 2011 +0900
@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "SgidHash.h"
+
+SgidHash::SgidHash(void)
+{
+    int size = sizeof(sg_hashtable)*SGID_NUM;
+#if defined(__SPU__) || ! defined(HAS_POSIX_MEMALIGN)
+        table =  (sg_hashtable*)malloc(size);
+#else
+        posix_memalign((void**)&table, alignment, size);
+#endif
+    for (int i = 0; i < SGID_NUM; i++) {
+        table[i].sg_id = -1;
+        table[i].key = NULL;
+    }
+}
+
+SgidHash::~SgidHash(void)
+{
+    free(table);
+}
+
+int
+SgidHash::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%SGID_NUM;
+}
+
+int
+SgidHash::hash_regist(const char* key, int &id)
+{
+    int hash = hash_function(key);
+
+    for (int i = 0; ; i++) {
+        if (table[hash].sg_id == -1) {
+            table[hash].key   = (char*)key;
+	    table[hash].sg_id = id;
+            return 0;
+
+        } else if (strcmp(key, table[hash].key) == 0
+                   && table[hash].sg_id != -1){
+            return table[hash].sg_id;
+        }
+        hash = ((37*hash)^(11*i)) % SGID_NUM;
+    }
+}
+
+int
+SgidHash::get_sgid(const char* key)
+{
+
+    int hash = hash_function(key);
+    for (int i = 0; ; i++) {
+        if (table[hash].sg_id == -1) {
+            return -1;
+
+        } else if (strcmp(key, table[hash].key) == 0
+                   && table[hash].sg_id != -1){
+            return table[hash].sg_id;
+        }
+        hash = ((37*hash)^(11*i)) % SGID_NUM;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Renderer/Engine/SgidHash.h	Tue Jan 11 07:06:24 2011 +0900
@@ -0,0 +1,23 @@
+#ifndef INCLUDED_SGID_HASH
+#define INCLUDED_SGID_HASH
+
+const int SGID_NUM = 1000;
+
+struct sg_hashtable{
+    int sg_id;
+    char* key;
+};
+
+class SgidHash{
+public:
+    sg_hashtable *table;
+
+    SgidHash(void);
+    ~SgidHash(void);
+    int hash_function(const char* image_name);
+    int hash_regist(const char* image_name, int &sg_id);
+    int get_sgid(const char* key);
+    void remove(int id) {  table[id].sg_id = -1; }
+};
+
+#endif