2 * Hyper Operating System Application Framework
7 * Copyright (C) 2006 by Project HOS
8 * http://sourceforge.jp/projects/hos/
19 /*void Assoc_Constructor(C_ASSOC *self, C_MEMHEAP *pMemHeap) */
20 void Assoc_CreateEx(C_ASSOC *self, C_MEMHEAP *pMemHeap)
22 self->pMemHeap = pMemHeap;
28 void Assoc_Delete(C_ASSOC *self)
35 static ASSOC_ERR Assoc_AddNode(T_ASSOC_NODE *pParent, T_ASSOC_NODE *pNode)
41 pszParentKey = (char *)pParent + sizeof(T_ASSOC_NODE);
42 pszNodeKey = (char *)pNode + sizeof(T_ASSOC_NODE);
43 iCmp = strcmp(pszParentKey, pszNodeKey);
46 return ASSOC_ERR_NG; /* 既に存在する */
51 if ( pParent->pLeft != NULL )
53 return Assoc_AddNode(pParent->pLeft, pNode);
55 pParent->pLeft = pNode;
56 pNode->pParent = pParent;
60 if ( pParent->pRight != NULL )
62 return Assoc_AddNode(pParent->pRight, pNode);
64 pParent->pRight = pNode;
65 pNode->pParent = pParent;
73 ASSOC_ERR Assoc_Add(C_ASSOC *self, const char *pszKey, const void *pData, long lSize)
81 iKeyLen = MemHeap_AlignSize(self->pMemHeap, strlen(pszKey) + 1);
82 if ( (pMem = MemHeap_Alloc(self->pMemHeap, sizeof(T_ASSOC_NODE) + iKeyLen + lSize)) == NULL )
86 pNode = (T_ASSOC_NODE *)pMem;
87 pMem += sizeof(T_ASSOC_NODE);
90 memcpy(pMem, pData, lSize);
95 if ( self->pRoot == NULL )
97 pNode->pParent = NULL;
99 ErrCode = ASSOC_ERR_OK;
103 ErrCode = Assoc_AddNode(self->pRoot, pNode);
104 if ( ErrCode != ASSOC_ERR_OK )
106 MemHeap_Free(self->pMemHeap, pNode);
115 static void *Assoc_GetNode(C_ASSOC *self, T_ASSOC_NODE *pNode, const char *pszKey)
121 pszNodeKey = (char *)pNode + sizeof(T_ASSOC_NODE);
122 iCmp = strcmp(pszNodeKey, pszKey);
125 iKeyLen = MemHeap_AlignSize(self->pMemHeap, strlen(pszKey) + 1);
126 return (void *)(pszNodeKey + iKeyLen); /* ヒット */
131 if ( pNode->pLeft == NULL )
135 return Assoc_GetNode(self, pNode->pLeft, pszKey);
139 if ( pNode->pRight == NULL )
143 return Assoc_GetNode(self, pNode->pRight, pszKey);
149 const void *Assoc_Get(C_ASSOC *self, const char *pszKey)
154 if ( self->pRoot == NULL )
156 return ASSOC_POS_NULL;
159 return Assoc_GetNode(self, self->pRoot, pszKey);
164 ASSOC_POS Assoc_GetFirst(C_ASSOC *self)
168 if ( self->pRoot == NULL )
170 return ASSOC_POS_NULL;
174 while ( pNode->pLeft != NULL )
176 pNode = pNode->pLeft;
179 return (ASSOC_POS)pNode;
183 ASSOC_POS Assoc_GetNext(C_ASSOC *self, ASSOC_POS *Pos)
187 pNode = (T_ASSOC_NODE *)Pos;
190 if ( pNode->pRight != NULL )
192 pNode = pNode->pRight;
193 while ( pNode->pLeft != NULL )
195 pNode = pNode->pLeft;
197 return (ASSOC_POS)pNode;
201 if ( pNode->pParent == NULL )
203 return ASSOC_POS_NULL;
207 if ( pNode->pParent->pLeft == pNode )
209 return (ASSOC_POS)pNode->pParent;
215 pNode = pNode->pParent;
216 if ( pNode->pParent == NULL )
218 return ASSOC_POS_NULL;
220 } while ( pNode->pParent->pRight == pNode );
221 pNode = pNode->pParent;
223 return (ASSOC_POS)pNode;
227 const void *Assoc_GetAt(C_ASSOC *self, ASSOC_POS *Pos, const char **ppszKey)
234 pNode = (T_ASSOC_NODE *)Pos;
236 pszNodeKey = (char *)pNode + sizeof(T_ASSOC_NODE);
237 *ppszKey = pszNodeKey;
238 iKeyLen = MemHeap_AlignSize(self->pMemHeap, strlen(pszNodeKey) + 1);
240 return (void *)(pszNodeKey + iKeyLen);