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 16 #ifndef OHOS_HASH_TABLE_H 17 #define OHOS_HASH_TABLE_H 18 19 #include <stdint.h> 20 #include <stddef.h> 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 #define HASH_MINI_CAPACITY 8 27 28 enum HashTableResult { 29 HASH_SUCCESS = 0, 30 HASH_ERROR, 31 HASH_UPDATED, 32 HASH_INSERTED, 33 HASH_KEY_NOT_FOUND, 34 HASH_NOT_EMPTY, 35 }; 36 37 typedef int (*HashComparison)(uintptr_t, uintptr_t, size_t); 38 typedef size_t (*HashType)(uintptr_t, size_t); 39 typedef struct HashNode { 40 struct HashNode *next; 41 uintptr_t key; 42 uintptr_t value; 43 } HashNode; 44 45 typedef struct HashTable { 46 size_t size; 47 size_t threshold; 48 size_t capacity; 49 size_t keySize; 50 size_t valueSize; 51 HashComparison compare; 52 HashType hash; 53 HashNode **nodes; 54 } HashTable; 55 56 int CreateHashTable(HashTable *table, size_t keySize, size_t valueSize, size_t capacity); 57 int Initialized(const HashTable *table); 58 int DestroyHashTable(HashTable *table); 59 int Insert(HashTable *table, uintptr_t key, uintptr_t value); 60 int ContainsKey(const HashTable *table, uintptr_t key); 61 uintptr_t At(HashTable *table, uintptr_t key); 62 int Remove(HashTable *table, uintptr_t key); 63 int ClearAll(HashTable *table); 64 int Empty(const HashTable *table); 65 int Resize(HashTable *table, size_t newSize); 66 67 #ifdef __cplusplus 68 } 69 #endif 70 #endif