1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "init_hashmap.h"
16 #include "init_log.h"
17
18 typedef struct {
19 HashNodeCompare nodeCompare;
20 HashKeyCompare keyCompare;
21 HashNodeFunction nodeHash;
22 HashKeyFunction keyHash;
23 HashNodeOnFree nodeFree;
24 int maxBucket;
25 uint32_t tableId;
26 HashNode *buckets[0];
27 } HashTab;
28
29 static uint32_t g_tableId = 0;
OH_HashMapCreate(HashMapHandle * handle,const HashInfo * info)30 int32_t OH_HashMapCreate(HashMapHandle *handle, const HashInfo *info)
31 {
32 INIT_ERROR_CHECK(handle != NULL && info != NULL && info->maxBucket > 0, return -1, "Invalid param");
33 INIT_ERROR_CHECK(info->keyHash != NULL && info->nodeHash != NULL, return -1, "Invalid param");
34 INIT_ERROR_CHECK(info->nodeCompare != NULL && info->keyCompare != NULL, return -1, "Invalid param");
35 HashTab *tab = (HashTab *)calloc(1, sizeof(HashTab) + sizeof(HashNode*) * info->maxBucket);
36 INIT_ERROR_CHECK(tab != NULL, return -1, "Failed to create hash tab");
37 tab->maxBucket = info->maxBucket;
38 tab->keyHash = info->keyHash;
39 tab->nodeCompare = info->nodeCompare;
40 tab->keyCompare = info->keyCompare;
41 tab->nodeHash = info->nodeHash;
42 tab->nodeFree = info->nodeFree;
43 tab->tableId = g_tableId++;
44 *handle = (HashMapHandle)tab;
45 return 0;
46 }
47
GetHashNodeByNode(const HashTab * tab,const HashNode * root,const HashNode * new)48 static HashNode *GetHashNodeByNode(const HashTab *tab, const HashNode *root, const HashNode *new)
49 {
50 HashNode *node = (HashNode *)root;
51 while (node != NULL) {
52 int ret = tab->nodeCompare(node, new);
53 if (ret == 0) {
54 return node;
55 }
56 node = node->next;
57 }
58 return NULL;
59 }
60
GetHashNodeByKey(const HashTab * tab,const HashNode * root,const void * key,HashKeyCompare keyCompare)61 static HashNode *GetHashNodeByKey(const HashTab *tab, const HashNode *root, const void *key, HashKeyCompare keyCompare)
62 {
63 (void)tab;
64 HashNode *node = (HashNode *)root;
65 while (node != NULL) {
66 int ret = keyCompare(node, key);
67 if (ret == 0) {
68 return node;
69 }
70 node = node->next;
71 }
72 return NULL;
73 }
74
OH_HashMapAdd(HashMapHandle handle,HashNode * node)75 int32_t OH_HashMapAdd(HashMapHandle handle, HashNode *node)
76 {
77 INIT_ERROR_CHECK(handle != NULL, return -1, "Invalid param");
78 INIT_ERROR_CHECK(node != NULL && node->next == NULL, return -1, "Invalid param");
79 HashTab *tab = (HashTab *)handle;
80 int hashCode = tab->nodeHash(node);
81 hashCode = (hashCode < 0) ? -hashCode : hashCode;
82 hashCode = hashCode % tab->maxBucket;
83 INIT_ERROR_CHECK(hashCode < tab->maxBucket, return -1, "Invalid hashcode %d %d", tab->maxBucket, hashCode);
84
85 // check key exist
86 HashNode *tmp = GetHashNodeByNode(tab, tab->buckets[hashCode], node);
87 if (tmp != NULL) {
88 INIT_LOGE("node hash been exist");
89 return -1;
90 }
91 node->next = tab->buckets[hashCode];
92 tab->buckets[hashCode] = node;
93 INIT_LOGV("OH_HashMapAdd tableId %d hashCode %d", tab->tableId, hashCode);
94 return 0;
95 }
96
OH_HashMapRemove(HashMapHandle handle,const void * key)97 void OH_HashMapRemove(HashMapHandle handle, const void *key)
98 {
99 INIT_ERROR_CHECK(handle != NULL && key != NULL, return, "Invalid param");
100 HashTab *tab = (HashTab *)handle;
101 int hashCode = tab->keyHash(key);
102 hashCode = (hashCode < 0) ? -hashCode : hashCode;
103 hashCode = hashCode % tab->maxBucket;
104 INIT_ERROR_CHECK(hashCode < tab->maxBucket, return, "Invalid hashcode %d %d", tab->maxBucket, hashCode);
105
106 HashNode *node = tab->buckets[hashCode];
107 HashNode *preNode = node;
108 while (node != NULL) {
109 int ret = tab->keyCompare(node, key);
110 if (ret == 0) {
111 if (node == tab->buckets[hashCode]) {
112 tab->buckets[hashCode] = node->next;
113 } else {
114 preNode->next = node->next;
115 }
116 return;
117 }
118 preNode = node;
119 node = node->next;
120 }
121 }
122
OH_HashMapGet(HashMapHandle handle,const void * key)123 HashNode *OH_HashMapGet(HashMapHandle handle, const void *key)
124 {
125 INIT_ERROR_CHECK(handle != NULL && key != NULL, return NULL, "Invalid param %s", key);
126 HashTab *tab = (HashTab *)handle;
127 int hashCode = tab->keyHash(key);
128 hashCode = (hashCode < 0) ? -hashCode : hashCode;
129 hashCode = hashCode % tab->maxBucket;
130 INIT_ERROR_CHECK(hashCode < tab->maxBucket, return NULL,
131 "Invalid hashcode %d %d", tab->maxBucket, hashCode);
132 return GetHashNodeByKey(tab, tab->buckets[hashCode], key, tab->keyCompare);
133 }
134
HashListFree(HashTab * tab,HashNode * root)135 static void HashListFree(HashTab *tab, HashNode *root)
136 {
137 if (root == NULL) {
138 return;
139 }
140 HashNode *node = root;
141 while (node != NULL) {
142 HashNode *next = node->next;
143 if (tab->nodeFree != NULL) {
144 tab->nodeFree(node);
145 }
146 node = next;
147 }
148 }
149
OH_HashMapDestory(HashMapHandle handle)150 void OH_HashMapDestory(HashMapHandle handle)
151 {
152 INIT_ERROR_CHECK(handle != NULL, return, "Invalid param");
153 HashTab *tab = (HashTab *)handle;
154 for (int i = 0; i < tab->maxBucket; i++) {
155 HashListFree(tab, tab->buckets[i]);
156 }
157 free(tab);
158 }
159
OH_HashMapFind(HashMapHandle handle,int hashCode,const void * key,HashKeyCompare keyCompare)160 HashNode *OH_HashMapFind(HashMapHandle handle,
161 int hashCode, const void *key, HashKeyCompare keyCompare)
162 {
163 INIT_ERROR_CHECK(handle != NULL && key != NULL, return NULL, "Invalid param");
164 INIT_ERROR_CHECK(handle != NULL && keyCompare != NULL, return NULL, "Invalid param");
165 HashTab *tab = (HashTab *)handle;
166 INIT_ERROR_CHECK(hashCode < tab->maxBucket, return NULL,
167 "Invalid hashcode %d %d", tab->maxBucket, hashCode);
168 return GetHashNodeByKey(tab, tab->buckets[hashCode], key, keyCompare);
169 }
170
OH_HashMapTraverse(HashMapHandle handle,void (* hashNodeTraverse)(const HashNode * node,const void * context),const void * context)171 void OH_HashMapTraverse(HashMapHandle handle, void (*hashNodeTraverse)(const HashNode *node, const void *context),
172 const void *context)
173 {
174 INIT_ERROR_CHECK(handle != NULL && hashNodeTraverse != NULL, return, "Invalid param");
175 HashTab *tab = (HashTab *)handle;
176 for (int i = 0; i < tab->maxBucket; i++) {
177 HashNode *node = tab->buckets[i];
178 while (node != NULL) {
179 HashNode *next = node->next;
180 hashNodeTraverse(node, context);
181 node = next;
182 }
183 }
184 }
185
OH_HashMapIsEmpty(HashMapHandle handle)186 int OH_HashMapIsEmpty(HashMapHandle handle)
187 {
188 INIT_ERROR_CHECK(handle != NULL, return 1, "Invalid param");
189 HashTab *tab = (HashTab *)handle;
190 for (int i = 0; i < tab->maxBucket; i++) {
191 HashNode *node = tab->buckets[i];
192 if (node != NULL) {
193 return 0;
194 }
195 }
196 return 1;
197 }
198