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 #include "init_log.h" 21 22 #ifdef __cplusplus 23 #if __cplusplus 24 extern "C" { 25 #endif 26 #endif 27 28 #define HASH_TAB_BUCKET_MAX 1024 29 #define HASH_TAB_BUCKET_MIN 16 30 31 typedef struct HashNode_ { 32 struct HashNode_ *next; 33 } HashNode; 34 35 #define HASHMAP_ENTRY(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member))) 36 #define HASHMAPInitNode(node) (node)->next = NULL 37 38 typedef int (*HashNodeCompare)(const HashNode *node1, const HashNode *node2); 39 typedef int (*HashKeyCompare)(const HashNode *node1, const void *key); 40 typedef int (*HashNodeFunction)(const HashNode *node); 41 typedef int (*HashKeyFunction)(const void *key); 42 typedef void (*HashNodeOnFree)(const HashNode *node); 43 44 typedef struct { 45 HashNodeCompare nodeCompare; 46 HashKeyCompare keyCompare; 47 HashNodeFunction nodeHash; 48 HashKeyFunction keyHash; 49 HashNodeOnFree nodeFree; 50 int maxBucket; 51 } HashInfo; 52 53 typedef void *HashMapHandle; 54 55 int32_t HashMapCreate(HashMapHandle *handle, const HashInfo *info); 56 void HashMapDestory(HashMapHandle handle); 57 int32_t HashMapAdd(HashMapHandle handle, HashNode *hashNode); 58 void HashMapRemove(HashMapHandle handle, const void *key); 59 HashNode *HashMapGet(HashMapHandle handle, const void *key); 60 HashNode *HashMapFind(HashMapHandle handle, 61 int hashCode, const void *key, HashKeyCompare keyCompare); 62 #ifdef __cplusplus 63 #if __cplusplus 64 } 65 #endif 66 #endif 67 #endif