1 /* 2 * Copyright (c) 2025 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 NETSTACK_HASH_MAP_C_H 17 #define NETSTACK_HASH_MAP_C_H 18 19 #include <cstdint> 20 #include <cstdio> 21 #include <string> 22 #include <cstdlib> 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 #define DEFAULT_MAP_CAPACITY 16 29 #define MAX_MAP_CAPACITY 16384 30 31 typedef struct Netstack_HashMapEntry { 32 char *key; 33 void *value; 34 struct Netstack_HashMapEntry *next; 35 } Netstack_HashMapEntry; 36 37 typedef struct Netstack_HashMap { 38 Netstack_HashMapEntry **entries; 39 uint32_t size; 40 uint32_t capacity; 41 } Netstack_HashMap; 42 43 typedef struct Netstack_MapIterator { 44 Netstack_HashMap *map; 45 uint32_t currentIdx; 46 Netstack_HashMapEntry *currentEntry; 47 } Netstack_MapIterator; 48 49 typedef void (*Netstack_DestroyValueFunction)(void *value); 50 51 Netstack_HashMap *CreateMap(void); 52 uint32_t Netstack_PutMapEntry(Netstack_HashMap *map, const char *key, void *value); 53 void *Netstack_GetMapEntry(Netstack_HashMap *map, const char *key); 54 uint32_t Netstack_DeleteMapEntry(Netstack_HashMap *map, const char *key); 55 void Netstack_DestroyMap(Netstack_HashMap *map); 56 void Netstack_DestroyMapWithValue(Netstack_HashMap *map, Netstack_DestroyValueFunction destroyFunction); 57 58 Netstack_MapIterator *Netstack_CreateMapIterator(Netstack_HashMap *map); 59 void Netstack_MapIterateNext(Netstack_MapIterator *iterator); 60 void Netstack_DestroyMapIterator(Netstack_MapIterator *iterator); 61 62 #ifdef __cplusplus 63 } 64 #endif 65 66 #endif