OSDN Git Service

(none)
authorryuz <ryuz>
Mon, 31 Aug 2009 06:50:37 +0000 (06:50 +0000)
committerryuz <ryuz>
Mon, 31 Aug 2009 06:50:37 +0000 (06:50 +0000)
aplfw/library/container/hashtable/hashtable.h
aplfw/library/container/hashtable/hashtable_constructor.c [new file with mode: 0755]
aplfw/library/container/hashtable/hashtable_findclose.c [new file with mode: 0755]
aplfw/library/container/hashtable/hashtable_findnext.c
aplfw/library/container/hashtable/hashtable_remove.c [new file with mode: 0755]
aplfw/library/container/hashtable/hashtable_searchnode.c
aplfw/library/container/hashtable/hashtable_set.c

index cda3326..5860aa4 100755 (executable)
@@ -52,7 +52,8 @@ typedef struct c_hashtable
 {
        T_HASHTABLE_NODE        **ppTable;
        C_MEMHEAP                       *pMemHeap;
-       int                                     iTableNum;
+       int                                     iTableSize;
+       int                                     iDataNum;
 } C_HASHTABLE;
 
 
@@ -64,7 +65,7 @@ extern "C" {
 C_HASHTABLE          *HashTable_Create(int iTableSize);                                                                                                                                        /**< %jp{生成}%en{Create} */
 C_HASHTABLE          *HashTable_CreateEx(int iTableSize, 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} */
+HASHTABLE_ERR         HashTable_Constructor(C_HASHTABLE *self, C_MEMHEAP *pMemHeap, int iTableSize);                                   /**< %jp{コンストラクタ}%en{Constructor} */
 void                  HashTable_Destructor(C_HASHTABLE *self);                                                                                                                 /**< %jp{デストラクタ}%en{Destructor} */
 
 /* 操作 */
@@ -75,7 +76,7 @@ HASHTABLE_ERR         HashTable_Remove(C_HASHTABLE *self, const char *pszKey);
 
 /* イテレータ */
 T_HASHTABLE_ITERATOR *HashTable_FindOpen(C_HASHTABLE *self);                                                                                                                   /**< イテレータの生成 */
-#define              HashTable_FindClose(self, pIterator)      do{ MemHeap_Free((self)->pMemHeap, (pIterator)); } while(0)     /**< イテレータの削除 */
+void                 HashTable_FindClose(C_HASHTABLE *self, T_HASHTABLE_ITERATOR *pIterator);                                                  /**< イテレータの削除 */
 const void           *Hashtable_FindNext(C_HASHTABLE *self, T_HASHTABLE_ITERATOR *pIterator, const char **ppszKey);            /**< 次を取得 */
 
 #ifdef __cplusplus
diff --git a/aplfw/library/container/hashtable/hashtable_constructor.c b/aplfw/library/container/hashtable/hashtable_constructor.c
new file mode 100755 (executable)
index 0000000..4db2ae1
--- /dev/null
@@ -0,0 +1,46 @@
+/** 
+ *  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"
+
+
+
+/**< %jp{コンストラクタ}%en{Constructor} */
+HASHTABLE_ERR HashTable_Constructor(C_HASHTABLE *self, C_MEMHEAP *pMemHeap, int iTableSize)
+{
+       T_HASHTABLE_NODE        **ppTable;
+       int                                     i;
+
+       /* メモリ確保 */
+       if ( (ppTable = (T_HASHTABLE_NODE **)MemHeap_Alloc(pMemHeap, sizeof(T_HASHTABLE_NODE *) * iTableSize)) == NULL )
+       {
+               return HASHTABLE_ERR_NG;
+       }
+
+       /* 初期化 */
+       for ( i = 0; i < iTableSize; i++ )
+       {
+               ppTable[i] = NULL;
+       }
+
+       /* 値設定 */
+       self->pMemHeap   = pMemHeap;
+       self->ppTable    = ppTable;
+       self->iTableSize = iTableSize;
+       self->iDataNum   = 0;
+       
+       return HASHTABLE_ERR_OK;
+}
+
+
+/* end of file */
diff --git a/aplfw/library/container/hashtable/hashtable_findclose.c b/aplfw/library/container/hashtable/hashtable_findclose.c
new file mode 100755 (executable)
index 0000000..d3d9843
--- /dev/null
@@ -0,0 +1,42 @@
+/** 
+ *  Hyper Operating System  Application Framework
+ *
+ * @file  hashtable_findopen.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"
+
+
+/** イテレータの削除 */
+void HashTable_FindClose(C_HASHTABLE *self, T_HASHTABLE_ITERATOR *pIterator)
+{
+       if ( pIterator->pNode != NULL )
+       {
+               if ( pIterator->pNext == pIterator )
+               {
+                       pIterator->pNode->pIterator = NULL;
+               }
+               else
+               {
+                       pIterator->pNext->pPrev = pIterator->pPrev;
+                       pIterator->pPrev->pNext = pIterator->pNext;
+                       if ( pIterator->pNode->pIterator == pIterator )
+                       {
+                               pIterator->pNode->pIterator = pIterator->pNext;
+                       }
+               }
+       }
+       
+       /* メモリ開放 */
+       MemHeap_Free(self->pMemHeap, pIterator);
+}
+
+
+/* end of file */
index f5fe400..80cf2c3 100755 (executable)
@@ -57,7 +57,7 @@ const void *Hashtable_FindNext(C_HASHTABLE *self, T_HASHTABLE_ITERATOR *pIterato
        
        if ( pNode == NULL )
        {
-               for ( iIndex = pIterator->iIndex; iIndex < self->iTableNum; iIndex++ )
+               for ( iIndex = pIterator->iIndex; iIndex < self->iTableSize; iIndex++ )
                {
                        if ( (pNode = self->ppTable[iIndex]) != NULL )
                        {
diff --git a/aplfw/library/container/hashtable/hashtable_remove.c b/aplfw/library/container/hashtable/hashtable_remove.c
new file mode 100755 (executable)
index 0000000..0a72c11
--- /dev/null
@@ -0,0 +1,67 @@
+/** 
+ *  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_Remove(C_HASHTABLE *self, const char *pszKey)
+{
+       T_HASHTABLE_NODE                *pNode;
+       T_HASHTABLE_NODE                *pNodePrev;
+       T_HASHTABLE_ITERATOR    *pIterator;
+       int                                             iIndex;
+
+       /* 探索 */
+       if ( (pNode = HashTable_SearchNode(self, pszKey, &pNodePrev, &iIndex)) == NULL )
+       {
+               return HASHTABLE_ERR_NG;
+       }
+       
+       /* 指しているイテレータがあれば付け替え */
+       if ( (pIterator = pNode->pIterator) != NULL )
+       {
+               if ( pNode->pNext != NULL )
+               {
+                       pNode->pNext->pIterator = pIterator;
+               }
+               
+               pNode->pIterator = pIterator;
+               do
+               {
+                       pIterator->pNode = pNode->pNext;
+                       pIterator = pIterator->pNext;
+               } while ( pIterator != pNode->pIterator );
+       }
+       
+       /* リストからはずす */
+       if ( pNodePrev != NULL )
+       {
+               pNodePrev->pNext = pNode->pNext;
+       }
+       else
+       {
+               self->ppTable[iIndex] = pNode->pNext;
+       }
+       
+       /* 削除 */
+       HashTable_DeleteNode(self, pNode);
+       self->iDataNum--;
+       
+       return HASHTABLE_ERR_OK;
+}
+
+
+
+/* end of file */
index c867fb1..21e132c 100755 (executable)
@@ -30,7 +30,7 @@ T_HASHTABLE_NODE *HashTable_SearchNode(C_HASHTABLE *self, const char *pszKey, T_
        {
                iIndex += c;
        }
-       *piIndex = iIndex = iIndex % self->iTableNum;
+       *piIndex = iIndex = iIndex % self->iTableSize;
        
        
        /* 探索 */
index 6d13162..8e2ecaa 100755 (executable)
@@ -58,9 +58,13 @@ HASHTABLE_ERR HashTable_Set(C_HASHTABLE *self, const char *pszKey, const void *p
                /* 削除 */
                HashTable_DeleteNode(self, pNodeOld);
        }
+       else
+       {
+               self->iDataNum++;
+       }
        
        /* 新ノード設定 */
-       pNode->pNext  = self->ppTable[iIndex];
+       pNode->pNext          = self->ppTable[iIndex];
        self->ppTable[iIndex] = pNode;
        
        return HASHTABLE_ERR_OK;