OSDN Git Service

(none)
authorryuz <ryuz>
Tue, 25 Aug 2009 14:55:06 +0000 (14:55 +0000)
committerryuz <ryuz>
Tue, 25 Aug 2009 14:55:06 +0000 (14:55 +0000)
aplfw/library/container/hashtable/hashtable.h [new file with mode: 0755]
aplfw/library/container/hashtable/hashtable_createnode.c [new file with mode: 0755]
aplfw/library/container/hashtable/hashtable_get.c [new file with mode: 0755]
aplfw/library/container/hashtable/hashtable_local.h [new file with mode: 0755]
aplfw/library/container/hashtable/hashtable_searchnode.c [new file with mode: 0755]
aplfw/library/container/hashtable/hashtable_set.c [new file with mode: 0755]

diff --git a/aplfw/library/container/hashtable/hashtable.h b/aplfw/library/container/hashtable/hashtable.h
new file mode 100755 (executable)
index 0000000..3f21f0a
--- /dev/null
@@ -0,0 +1,90 @@
+/** 
+ *  Hyper Operating System  Application Framework
+ *
+ * @file  hashtable.h
+ * @brief %jp{ハッシュテーブル}%en{Hash table}
+ *
+ * Copyright (C) 2006-2009 by Project HOS
+ * http://sourceforge.jp/projects/hos/
+ */
+
+
+#ifndef __HOS__hashtable_h__
+#define __HOS__hashtable_h__
+
+
+#include "library/container/memheap/memheap.h"
+
+
+#define HASHTABLE_ERR_OK               0
+#define HASHTABLE_ERR_NG               (-1)
+#define HASHTABLE_POS_NULL             (0)
+
+typedef int     HASHTABLE_ERR;
+
+
+struct c_hashtableiterator;
+struct c_hashtable;
+
+
+/** %jp{ノード格納用構造体}%en{node} */
+typedef struct t_hashtable_node
+{
+       int                                                     iIndex;
+       struct t_hashtable_node         *pNext;
+       struct c_hashtableiterator      *pIterator;
+} T_HASHTABLE_NODE;
+
+
+/** %jp{ハッシュ用イテレータクラス}%en{Iterator class} */
+typedef struct c_hashtableiterator
+{
+       struct c_hashtable                      *pHashTable;
+       struct c_hashtableiterator      *pNext;
+       struct c_hashtableiterator      *pPrev;
+       
+       struct t_hashtable_node         *pNode;
+} C_HASHTABLEITERATOR;
+
+
+/** %jp{ハッシュテーブルクラス}%en{Hash table class} */
+typedef struct c_hashtable
+{
+       T_HASHTABLE_NODE        **ppTable;
+       C_MEMHEAP                       *pMemHeap;
+       int                                     iTableNum;
+} C_HASHTABLE;
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* 生成/削除 */
+C_HASHTABLE         *HashTable_Create(void);                                                                                                                           /* %jp{生成}%en{Create} */
+C_HASHTABLE         *HashTable_CreateEx(C_MEMHEAP *pMemHeap);                                                                                          /* %jp{生成}%en{Create} */
+void                HashTable_Delete(C_HASHTABLE *self);                                                                                                       /* %jp{削除}%en{Delete} */
+void                HashTable_Constructor(C_HASHTABLE *self, C_MEMHEAP *pMemHeap);                                                     /* %jp{コンストラクタ}%en{Constructor} */
+void                HashTable_Destructor(C_HASHTABLE *self);                                                                                           /* %jp{デストラクタ}%en{Destructor} */
+
+/* 操作 */
+HASHTABLE_ERR       HashTable_Add(C_HASHTABLE *self, const char *pszKey, const void *pData, MEMSIZE Size);     /* データの追加 */
+HASHTABLE_ERR       HashTable_Set(C_HASHTABLE *self, const char *pszKey, const void *pData, MEMSIZE Size);     /* データの設定 */
+const void         *HashTable_Get(C_HASHTABLE *self, const char *pszKey);                                                                      /* データの参照 */
+HASHTABLE_ERR       HashTable_Remove(C_HASHTABLE *self, const char *pszKey);                                                           /* データの削除 */
+
+/* イテレータ */
+C_HASHTABLEITERATOR *HashTableIterator_Create(C_HASHTABLE *pHashTable);                                                                                /* イテレータの生成 */
+void                HashTableIterator_Delete(C_HASHTABLEITERATOR *self, C_HASHTABLEITERATOR *pIterator);       /* イテレータの削除 */
+const void          *HashtableIterator_FindNext(C_HASHTABLEITERATOR *self, const char **ppszKey);                      /**/
+
+#ifdef __cplusplus
+}
+#endif
+
+
+
+#endif /* __HOS__assoc_h__ */
+
+
+/* end of file */
diff --git a/aplfw/library/container/hashtable/hashtable_createnode.c b/aplfw/library/container/hashtable/hashtable_createnode.c
new file mode 100755 (executable)
index 0000000..d344629
--- /dev/null
@@ -0,0 +1,44 @@
+/** 
+ *  Hyper Operating System  Application Framework
+ *
+ * @file  hashtable_get.c
+ * @brief %jp{ハッシュテーブルクラス}%en{hash table class}
+ *
+ * Copyright (C) 2006-2009 by Project HOS
+ * http://sourceforge.jp/projects/hos/
+ */
+
+
+#include <stdio.h>
+#include <string.h>
+#include "hashtable_local.h"
+
+
+/* ノードの作成 */
+T_HASHTABLE_NODE *HashTable_CreateNode(C_HASHTABLE *self, const char *pszKey, const void *pData, MEMSIZE Size)
+{
+       T_HASHTABLE_NODE        *pNode;
+       MEMSIZE                         KeySize;
+       char                            *pszNodeKey;
+       void                            *pNodeData;
+       
+       /* メモリ確保 */
+       KeySize  = MemHeap_AlignSize(self->pMemHeap, strlen(pszKey));
+       pNode = MemHeap_Alloc(self->pMemHeap, sizeof(T_HASHTABLE_NODE) + KeySize + Size);
+       if ( pNode == NULL )
+       {
+               return NULL;
+       }
+       
+       /* データ格納 */
+       pszNodeKey = (char *)pNode + sizeof(T_HASHTABLE_NODE);
+       pNodeData  = pszNodeKey + Size;
+       strcpy(pszNodeKey, pszKey);
+       memcpy(pNodeData, pData, Size);
+       pNode->pIterator = NULL;
+       
+       return pNode;
+}
+
+
+/* end of file */
diff --git a/aplfw/library/container/hashtable/hashtable_get.c b/aplfw/library/container/hashtable/hashtable_get.c
new file mode 100755 (executable)
index 0000000..6504f3d
--- /dev/null
@@ -0,0 +1,35 @@
+/** 
+ *  Hyper Operating System  Application Framework
+ *
+ * @file  hashtable_get.c
+ * @brief %jp{ハッシュテーブルクラス}%en{hash table class}
+ *
+ * Copyright (C) 2006-2009 by Project HOS
+ * http://sourceforge.jp/projects/hos/
+ */
+
+
+#include <stdio.h>
+#include <string.h>
+#include "hashtable_local.h"
+
+
+/* データの参照 */
+const void *HashTable_Get(C_HASHTABLE *self, const char *pszKey)
+{
+       T_HASHTABLE_NODE        *pNode;
+       T_HASHTABLE_NODE        *pNodePrev;
+       int                                     iIndex;
+       
+       /* 探索 */
+       if ( (pNode = HashTable_SearchNode(self, pszKey, &pNodePrev, &iIndex)) == NULL )
+       {
+               return NULL;
+       }
+       
+       /* データ部を返す */
+       return (const void *)((char *)pNode + sizeof(T_HASHTABLE_NODE) + MemHeap_AlignSize(self->pMemHeap, strlen(pszKey)));
+}
+
+
+/* end of file */
diff --git a/aplfw/library/container/hashtable/hashtable_local.h b/aplfw/library/container/hashtable/hashtable_local.h
new file mode 100755 (executable)
index 0000000..5791899
--- /dev/null
@@ -0,0 +1,33 @@
+/** 
+ *  Hyper Operating System  Application Framework
+ *
+ * @file  hashtable_local.h
+ * @brief %jp{ハッシュテーブル}%en{Hash table}
+ *
+ * Copyright (C) 2006-2009 by Project HOS
+ * http://sourceforge.jp/projects/hos/
+ */
+
+
+#ifndef __HOS__hashtable_local_h__
+#define __HOS__hashtable_local_h__
+
+#include "hashtable.h"
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+T_HASHTABLE_NODE *HashTable_CreateNode(C_HASHTABLE *self, const char *pszKey, const void *pData, MEMSIZE Size);                                /* ノードの作成 */
+#define           HashTable_DeleteNode(self, pNode)            do { MemHeap_Free((self)->pMemHeap, (pNode)); } while (0)                       /* ノードの削除 */
+T_HASHTABLE_NODE *HashTable_SearchNode(C_HASHTABLE *self, const char *pszKey, T_HASHTABLE_NODE **ppNodePrev, int *piIndex);    /* ノードの探索 */
+
+#ifdef __cplusplus
+}
+#endif
+
+
+
+#endif /* __HOS__assoc_local_h__ */
+
diff --git a/aplfw/library/container/hashtable/hashtable_searchnode.c b/aplfw/library/container/hashtable/hashtable_searchnode.c
new file mode 100755 (executable)
index 0000000..c867fb1
--- /dev/null
@@ -0,0 +1,54 @@
+/** 
+ *  Hyper Operating System  Application Framework
+ *
+ * @file  hashtable_get.c
+ * @brief %jp{ハッシュテーブルクラス}%en{hash table class}
+ *
+ * Copyright (C) 2006-2009 by Project HOS
+ * http://sourceforge.jp/projects/hos/
+ */
+
+
+#include <stdio.h>
+#include <string.h>
+#include "hashtable_local.h"
+
+
+/* ノードの探索 */
+T_HASHTABLE_NODE *HashTable_SearchNode(C_HASHTABLE *self, const char *pszKey, T_HASHTABLE_NODE **ppNodePrev, int *piIndex)
+{
+       T_HASHTABLE_NODE        *pNode;
+       T_HASHTABLE_NODE        *pNodePrev;
+       const char                      *pszNodeKey;
+       const char                      *pszPtr;
+       int                                     iIndex;
+       int                                     c;
+       
+       /* ハッシュ計算 */
+       iIndex = 0; 
+       for ( pszPtr = pszKey; (c = *pszPtr) != '\0'; pszPtr++ )
+       {
+               iIndex += c;
+       }
+       *piIndex = iIndex = iIndex % self->iTableNum;
+       
+       
+       /* 探索 */
+       pNodePrev = NULL;
+       for ( pNode = self->ppTable[iIndex]; pNode != NULL; pNode = pNode->pNext )
+       {
+               pszNodeKey = (const char *)pNode + sizeof(T_HASHTABLE_NODE);
+               if ( strcmp(pszNodeKey, pszKey) == 0 )
+               {
+                       *ppNodePrev = pNodePrev;
+                       return pNode;
+               }
+               pNodePrev = pNode;
+       }
+       
+       *ppNodePrev = NULL;
+
+       return NULL;
+}
+
+/* end of file */
diff --git a/aplfw/library/container/hashtable/hashtable_set.c b/aplfw/library/container/hashtable/hashtable_set.c
new file mode 100755 (executable)
index 0000000..14ccef3
--- /dev/null
@@ -0,0 +1,71 @@
+/** 
+ *  Hyper Operating System  Application Framework
+ *
+ * @file  hashtable_get.c
+ * @brief %jp{ハッシュテーブルクラス}%en{hash table class}
+ *
+ * Copyright (C) 2006-2009 by Project HOS
+ * http://sourceforge.jp/projects/hos/
+ */
+
+
+#include <stdio.h>
+#include <string.h>
+#include "hashtable_local.h"
+
+
+/* データの設定 */
+HASHTABLE_ERR HashTable_Set(C_HASHTABLE *self, const char *pszKey, const void *pData, MEMSIZE Size)
+{
+       T_HASHTABLE_NODE        *pNode;
+       T_HASHTABLE_NODE        *pNodeOld;
+       T_HASHTABLE_NODE        *pNodePrev;
+       C_HASHTABLEITERATOR     *pIterator;
+       int                                     iIndex;
+       
+       /* ノード生成 */
+       if ( (pNode = HashTable_CreateNode(self, pszKey, pData, Size)) == NULL )
+       {
+               return HASHTABLE_ERR_NG;
+       }
+
+       /* 既に無いか探索 */
+       pNodeOld = HashTable_SearchNode(self, pszKey, &pNodePrev, &iIndex);
+       
+       /* 既にあれば更新 */
+       if ( pNodeOld != NULL )
+       {
+               /* 指しているイテレータがあれば付け替え */
+               if ( (pIterator = pNodeOld->pIterator) != NULL )
+               {
+                       pNode->pIterator = pIterator;
+                       do
+                       {
+                               pIterator->pNode = pNode;
+                               pIterator = pIterator->pNext;
+                       } while ( pIterator != pNodeOld->pIterator );
+               }
+               
+               /* リストから取り外し */
+               if ( pNodePrev != NULL )
+               {
+                       pNodePrev->pNext = pNodeOld->pNext;
+               }
+               {
+                       self->ppTable[iIndex] = pNodeOld->pNext;
+               }
+
+               /* 削除 */
+               HashTable_DeleteNode(self, pNodeOld);
+       }
+       
+       /* 新ノード設定 */
+       pNode->iIndex = iIndex;
+       pNode->pNext  = self->ppTable[iIndex];
+       self->ppTable[iIndex] = pNode;
+       
+       return HASHTABLE_ERR_OK;
+}
+
+
+/* end of file */