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 LNN_MAP_H
17 #define LNN_MAP_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif /* __cplusplus */
25 
26 /**
27  * LNN map node struct
28  */
29 typedef struct tagMapNode {
30     uint32_t hash;
31     uint32_t valueSize;
32     void *key;
33     void *value;
34     struct tagMapNode *next;
35 } MapNode;
36 
37 /**
38  * LNN map struct define.
39  */
40 typedef struct {
41     MapNode **nodes; /* Map node bucket */
42     uint32_t nodeSize; /* Map node count */
43     uint32_t bucketSize; /* Map node bucket size */
44 } Map;
45 
46 /**
47  * LNN map node struct
48  */
49 typedef struct {
50     MapNode *node; /* Map node */
51     uint32_t nodeNum; /* Map node  */
52     uint32_t bucketNum; /* Map node */
53     Map *map;
54 } MapIterator;
55 
56 MapIterator *LnnMapInitIterator(Map *map);
57 bool LnnMapHasNext(MapIterator *it);
58 MapIterator *LnnMapNext(MapIterator *it);
59 void LnnMapDeinitIterator(MapIterator *it);
60 
61 /**
62  * Initialize map
63  *
64  * @param : map Map see details in type Map
65  */
66 void LnnMapInit(Map *map);
67 
68 /**
69  * Delete map, free the map memory
70  *
71  * @param : map Map see details in type Map
72  */
73 void LnnMapDelete(Map *map);
74 
75 /**
76  * Add map element
77  *
78  * @param : map Map see details in type Map
79  *          key Map key
80  *          value Map value
81  *          valueSize Map value size
82  * @return : SOFTBUS_OK or other error
83  */
84 int32_t LnnMapSet(Map *map, const char *key, const void *value, uint32_t valueSize);
85 
86 /**
87  * Get map value
88  *
89  * @param : map Map see details in type Map
90  *          key Map key
91  * @return : Value of key or NULL
92  */
93 void *LnnMapGet(const Map *map, const char *key);
94 
95 /**
96  * Erase map element
97  * Erase cannot be used on the iterator
98  *
99  * @param : map Map see details in type Map
100  *          key Map key
101  */
102 int32_t LnnMapErase(Map *map, const char *key);
103 
104 /**
105  * get map size
106  *
107  * @param : map Map see details in type Map
108  */
109 uint32_t MapGetSize(Map *map);
110 
111 #ifdef __cplusplus
112 }
113 #endif /* __cplusplus */
114 
115 #endif /* LNN_MAP_H */