1 /* 2 * Copyright (c) 2021 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 #ifndef INIT_HASH_MAP_ 16 #define INIT_HASH_MAP_ 17 #include <stdint.h> 18 #include <stdlib.h> 19 #include <string.h> 20 21 #ifdef __cplusplus 22 #if __cplusplus 23 extern "C" { 24 #endif 25 #endif 26 27 #define HASH_TAB_BUCKET_MAX 1024 28 #define HASH_TAB_BUCKET_MIN 16 29 30 typedef struct HashNode_ { 31 struct HashNode_ *next; 32 } HashNode; 33 34 #define HASHMAP_ENTRY(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) 35 #define HASHMAPInitNode(node) (node)->next = NULL 36 37 typedef int (*HashNodeCompare)(const HashNode *node1, const HashNode *node2); 38 typedef int (*HashKeyCompare)(const HashNode *node1, const void *key); 39 typedef int (*HashNodeFunction)(const HashNode *node); 40 typedef int (*HashKeyFunction)(const void *key); 41 typedef void (*HashNodeOnFree)(const HashNode *node, void *context); 42 43 typedef struct { 44 HashNodeCompare nodeCompare; 45 HashKeyCompare keyCompare; 46 HashNodeFunction nodeHash; 47 HashKeyFunction keyHash; 48 HashNodeOnFree nodeFree; 49 int maxBucket; 50 } HashInfo; 51 52 typedef void *HashMapHandle; 53 54 int OH_HashMapIsEmpty(HashMapHandle handle); 55 int32_t OH_HashMapCreate(HashMapHandle *handle, const HashInfo *info); 56 void OH_HashMapDestory(HashMapHandle handle, void *context); 57 int32_t OH_HashMapAdd(HashMapHandle handle, HashNode *hashNode); 58 void OH_HashMapRemove(HashMapHandle handle, const void *key); 59 HashNode *OH_HashMapGet(HashMapHandle handle, const void *key); 60 HashNode *OH_HashMapFind(HashMapHandle handle, 61 int hashCode, const void *key, HashKeyCompare keyCompare); 62 void OH_HashMapTraverse(HashMapHandle handle, void (*hashNodeTraverse)(const HashNode *node, const void *context), 63 const void *context); 64 #ifdef __cplusplus 65 #if __cplusplus 66 } 67 #endif 68 #endif 69 #endif