changeset 101:67c843471b77

add name_dictionary.cc
author Atuto SHIROMA <e095729@ie.u-ryukyu.ac.jp>
date Thu, 02 Jun 2011 19:50:06 +0900
parents e5f2eb98b575
children 31b24eaf05cc
files Makefile libps2.h name_dictionary.cc name_dictionary.h ps2util.cc ps2util.h test_vector
diffstat 7 files changed, 121 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Thu Jun 02 18:19:07 2011 +0900
+++ b/Makefile	Thu Jun 02 19:50:06 2011 +0900
@@ -1,6 +1,6 @@
 TARGET = main
 CC = g++
-SRCS = main.cc game.cc car.cc col.cc quotanion.cc controler.cc field.cc camera.cc gSprite.cc schedule.cc mytype.cc gFont.cc carNode.cc light.cc title_scene.cc game_time.cc ps2util.cc sjoy.cc 
+SRCS = main.cc game.cc car.cc col.cc quotanion.cc controler.cc field.cc camera.cc gSprite.cc schedule.cc mytype.cc gFont.cc carNode.cc light.cc title_scene.cc game_time.cc ps2util.cc sjoy.cc name_dictionary.cc
 #linda/lindaapi.cc linda.c
 OBJS = ${SRCS:.cc=.o}
 VU1DIR = vu1code
@@ -75,4 +75,5 @@
 light.o:
 title_scene.o:
 game_time.o:
-sjoy.o:
\ No newline at end of file
+sjoy.o:
+name_dictionary.o:
\ No newline at end of file
--- a/libps2.h	Thu Jun 02 18:19:07 2011 +0900
+++ b/libps2.h	Thu Jun 02 19:50:06 2011 +0900
@@ -9,6 +9,7 @@
 
 
 //object.h よりコピー
+
 typedef struct texture {
   int width;                 // texture's width size
   int height;                // texture's height size
@@ -89,8 +90,8 @@
   FVECTOR pnts;
 } PolygonInfo;
 
+int malloc_align16(void *head, void *aligned, int size);
 
-extern int malloc_align16(void *head, void *aligned, int size);
 
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/name_dictionary.cc	Thu Jun 02 19:50:06 2011 +0900
@@ -0,0 +1,102 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "libps2.h"
+
+#define NAME_HASH_SIZE 127  // 127 is prime number
+#define REHASH_NUM (NAME_HASH_SIZE / 10)
+
+typedef struct name_dic {
+    int ref_num;
+    const char *name;
+} NAME_DIC;
+
+static NAME_DIC *name_hash=NULL;
+
+int hash_func(const char *string, int hash_size)
+{
+    int sum;
+
+    for (sum=0;*string;string++) {
+        sum += *string;
+    }
+    sum %= hash_size;
+    if (sum < 0) {
+        sum += hash_size;
+    }
+
+    return(sum);
+}
+
+// if error occured return NULL
+const char *refer_name_dictionary(const char *string)
+{
+    int number;
+    NAME_DIC *dic_ptr;
+
+    if (string == NULL) return(NULL);
+
+    number = hash_func(string,NAME_HASH_SIZE);
+
+    // detect string or regist space
+    for (dic_ptr=name_hash+number; dic_ptr->name ; dic_ptr=name_hash+number) {
+        if (!strcmp(string,dic_ptr->name)) {
+            dic_ptr->ref_num++;
+            return(dic_ptr->name);
+        }
+        number += REHASH_NUM;
+        if (number > NAME_HASH_SIZE) number -= NAME_HASH_SIZE;
+    }
+
+    dic_ptr->ref_num = 1;
+    dic_ptr->name = (char *)malloc(sizeof(char)*strlen(string)+1);
+    if (dic_ptr->name == NULL) {
+        fprintf(stderr,"malloc failed at refer_name_dictionary\n");
+        return(NULL);
+    }
+    strcpy((char*)dic_ptr->name,string);
+
+    return(dic_ptr->name);
+}
+
+void delete_name_dictionary(const char *string)
+{
+  int number;
+  NAME_DIC *dic_ptr;
+
+  if (string == NULL) return;
+
+  number = hash_func(string,NAME_HASH_SIZE);
+
+  for (dic_ptr=name_hash+number;dic_ptr->name;dic_ptr=name_hash+number) {
+    if (!strcmp(string,dic_ptr->name)) {
+      dic_ptr->ref_num--;
+      if (dic_ptr->ref_num == 0) {
+	free((void*)dic_ptr->name);
+	dic_ptr->name = NULL;
+      }
+      return;
+    } else {
+      number += REHASH_NUM;
+      if (number > NAME_HASH_SIZE) number -= NAME_HASH_SIZE;
+    }
+  }
+}
+
+// if error occured return -1
+int init_name_dictionary()
+{
+  int n;
+  NAME_DIC *dic;
+
+  free(name_hash);
+  name_hash = (NAME_DIC *)malloc(sizeof(NAME_DIC)*NAME_HASH_SIZE);
+  if (name_hash == NULL) {
+    fprintf(stderr,"malloc failed at init_name_dictionary\n");
+    return(-1);
+  }
+
+  for (n=0,dic=name_hash;n < NAME_HASH_SIZE;n++,dic++) dic->name = NULL;
+
+  return(1);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/name_dictionary.h	Thu Jun 02 19:50:06 2011 +0900
@@ -0,0 +1,10 @@
+#ifndef NAME_DICTIONARY_H
+#define NAME_DICTIONARY_H
+
+int hash_func(const char *string, int hash_size);
+const char *refer_name_dictionary(const char *string);
+void delete_name_dictionary(const char *string);
+int init_name_dictionary();
+
+
+#endif
--- a/ps2util.cc	Thu Jun 02 18:19:07 2011 +0900
+++ b/ps2util.cc	Thu Jun 02 19:50:06 2011 +0900
@@ -6,6 +6,7 @@
 #include <math.h> 
 #include "libps2.h"
 #include "ps2util.h"
+#include "name_dictionary.h"
 
 /*
   たぶん、本当は読み込んだ xml file から情報を取得するとかそんなんだと思う
@@ -280,41 +281,6 @@
 }
 
 
-
-
-/*
-  からの関数
-  ps2 依存関係にあったけど、( name_dictionary.c ) の中のもの
-  だけど、他にも持ってくるものが増えるので、とりあえず、からの関数に
-*/
-void delete_name_dictionary(const char *string)
-{
-  /*
-  int number;
-  NAME_DIC *dic_ptr;
-
-  if (string == NULL) return;
-
-  number = hash_func(string,NAME_HASH_SIZE);
-
-  for (dic_ptr=name_hash+number;dic_ptr->name;dic_ptr=name_hash+number) {
-    if (!strcmp(string,dic_ptr->name)) {
-      dic_ptr->ref_num--;
-      if (dic_ptr->ref_num == 0) {
-	free(dic_ptr->name);
-	dic_ptr->name = NULL;
-      }
-      return;
-    } else {
-      number += REHASH_NUM;
-      if (number > NAME_HASH_SIZE) number -= NAME_HASH_SIZE;
-    }
-  }
-  */
-}
-  
-
-
 /*
   ps2 依存関係から持ってきた。
   テクスチャに割り振ったメモリの解放処理?
@@ -323,8 +289,8 @@
 void free_texture(TEXTURE *tex)
 {
   if (tex == NULL) return;
-
-  delete_name_dictionary(tex->name);
+  
+  //delete_name_dictionary(tex->name);
   free(tex->free_texenv);
   free(tex->free_image);
   free(tex);
--- a/ps2util.h	Thu Jun 02 18:19:07 2011 +0900
+++ b/ps2util.h	Thu Jun 02 19:50:06 2011 +0900
@@ -83,6 +83,7 @@
 void ps2util_sprite_Destroy(ps2utilSprite *title_sprite);
 
 
+
 void ps2util_sprite_Set_basicAttribute(ps2utilSprite *p,
 	unsigned short x, unsigned short y,
 	unsigned short sptable_tw1, unsigned short sptable_th1,
Binary file test_vector has changed