cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

map.h
Go to the documentation of this file.
1 /*
2  Copyright 2020 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 #ifndef CFENGINE_MAP_H
26 #define CFENGINE_MAP_H
27 
28 #include <hash_map_priv.h>
29 #include <array_map_priv.h>
30 
31 /*
32  * Map structure. Details are encapsulated.
33  */
34 typedef struct Map_ Map;
35 
37  MapKeyEqualFn equal_fn,
38  MapDestroyDataFn destroy_key_fn,
39  MapDestroyDataFn destroy_value_fn);
40 
41 /**
42  * Insert a key-value pair in the map.
43  * If the key is in the map, value get replaced. Old value is destroyed.
44  *
45  * @retval true if key exists already.
46  */
47 bool MapInsert(Map *map, void *key, void *value);
48 
49 /*
50  * Returns whether the key is in the map.
51  */
52 bool MapHasKey(const Map *map, const void *key);
53 
54 /*
55  * Returns the value if the key is in map, NULL otherwise. To distinguish
56  * between NULL as a value and NULL as a lack of entry, use MapHasKey.
57  */
58 void *MapGet(Map *map, const void *key);
59 
60 /*
61  * Remove key/value pair from the map. Returns 'true' if key was present in the
62  * map.
63  */
64 bool MapRemove(Map *map, const void *key);
65 
66 size_t MapSize(const Map *map);
67 
68 /*
69  * MapIterator i = MapIteratorInit(map);
70  * MapKeyValue *item;
71  * while ((item = MapIteratorNext(&i)))
72  * {
73  * // do something with item->key, item->value
74  * }
75  */
76 
77 
78 typedef struct
79 {
80  bool is_array;
81  union
82  {
85  };
86 } MapIterator;
87 
89 
91 
92 /*
93  * Clear the whole map
94  */
95 void MapClear(Map *map);
96 
97 /*
98  * Destroy the map object.
99  */
100 void MapDestroy(Map *map);
101 
102 /*
103  * Destroy the map object without removing values.
104  */
105 void MapSoftDestroy(Map *map);
106 
107 /**
108  * Returns whether the two maps contain the same keys.
109  * The values DO NOT have to be equal, just the keys.
110  */
111 bool MapContainsSameKeys(const Map *map1, const Map *map2);
112 
113 void MapPrintStats(const Map *map, FILE *f);
114 
115 
116 #define TYPED_MAP_DECLARE(Prefix, KeyType, ValueType) \
117  typedef struct \
118  { \
119  Map *impl; \
120  } Prefix##Map; \
121  \
122  Prefix##Map *Prefix##MapNew(void); \
123  bool Prefix##MapInsert(const Prefix##Map *map, KeyType key, ValueType value); \
124  bool Prefix##MapHasKey(const Prefix##Map *map, const KeyType key); \
125  ValueType Prefix##MapGet(const Prefix##Map *map, const KeyType key); \
126  bool Prefix##MapRemove(const Prefix##Map *map, const KeyType key); \
127  void Prefix##MapClear(Prefix##Map *map); \
128  size_t Prefix##MapSize(const Prefix##Map *map); \
129  void Prefix##MapDestroy(Prefix##Map *map); \
130  void Prefix##MapSoftDestroy(Prefix##Map *map); \
131  bool Prefix##MapContainsSameKeys(const Prefix##Map *map1, const Prefix##Map *map2); \
132  void Prefix##MapPrintStats(const Prefix##Map *map, FILE *f); \
133 
134 #define TYPED_MAP_DEFINE(Prefix, KeyType, ValueType, hash_fn, equal_fn, \
135  destroy_key_fn, destroy_value_fn) \
136  \
137  Prefix##Map *Prefix##MapNew(void) \
138  { \
139  Prefix##Map *map = xcalloc(1, sizeof(Prefix##Map)); \
140  map->impl = MapNew(hash_fn, equal_fn, \
141  destroy_key_fn, destroy_value_fn); \
142  return map; \
143  } \
144  \
145  bool Prefix##MapInsert(const Prefix##Map *map, KeyType key, ValueType value) \
146  { \
147  assert(map); \
148  return MapInsert(map->impl, key, value); \
149  } \
150  \
151  bool Prefix##MapHasKey(const Prefix##Map *map, const KeyType key) \
152  { \
153  assert(map); \
154  return MapHasKey(map->impl, key); \
155  } \
156  \
157  ValueType Prefix##MapGet(const Prefix##Map *map, const KeyType key) \
158  { \
159  assert(map); \
160  return MapGet(map->impl, key); \
161  } \
162  \
163  bool Prefix##MapRemove(const Prefix##Map *map, const KeyType key) \
164  { \
165  assert(map); \
166  return MapRemove(map->impl, key); \
167  } \
168  \
169  void Prefix##MapClear(Prefix##Map *map) \
170  { \
171  assert(map); \
172  MapClear(map->impl); \
173  } \
174  \
175  size_t Prefix##MapSize(const Prefix##Map *map) \
176  { \
177  assert(map); \
178  return MapSize(map->impl); \
179  } \
180  \
181  void Prefix##MapDestroy(Prefix##Map *map) \
182  { \
183  if (map != NULL) \
184  { \
185  MapDestroy(map->impl); \
186  free(map); \
187  } \
188  } \
189  \
190  void Prefix##MapSoftDestroy(Prefix##Map *map) \
191  { \
192  if (map != NULL) \
193  { \
194  MapSoftDestroy(map->impl); \
195  free(map); \
196  } \
197  } \
198  \
199  bool Prefix##MapContainsSameKeys(const Prefix##Map *map1, const Prefix##Map *map2) \
200  { \
201  assert(map1); \
202  assert(map2); \
203  return MapContainsSameKeys(map1->impl, map2->impl); \
204  } \
205  \
206  void Prefix##MapPrintStats(const Prefix##Map *map, FILE *f) \
207  { \
208  assert(map); \
209  return MapPrintStats(map->impl, f); \
210  } \
211 
212 TYPED_MAP_DECLARE(String, char *, char *)
213 
214 #endif
MapIterator MapIteratorInit(Map *map)
Definition: map.c:325
bool MapRemove(Map *map, const void *key)
Definition: map.c:226
void MapPrintStats(const Map *map, FILE *f)
Definition: map.c:305
bool MapContainsSameKeys(const Map *map1, const Map *map2)
Definition: map.c:286
Map * MapNew(MapHashFn hash_fn, MapKeyEqualFn equal_fn, MapDestroyDataFn destroy_key_fn, MapDestroyDataFn destroy_value_fn)
Definition: map.c:81
void MapSoftDestroy(Map *map)
Definition: map.c:254
size_t MapSize(const Map *map)
Definition: map.c:112
bool MapInsert(Map *map, void *key, void *value)
Definition: map.c:161
#define TYPED_MAP_DECLARE(Prefix, KeyType, ValueType)
Definition: map.h:116
void * MapGet(Map *map, const void *key)
Definition: map.c:213
MapKeyValue * MapIteratorNext(MapIterator *i)
Definition: map.c:343
void MapClear(Map *map)
Definition: map.c:240
bool MapHasKey(const Map *map, const void *key)
Definition: map.c:203
void MapDestroy(Map *map)
Definition: map.c:270
void(* MapDestroyDataFn)(void *key)
Definition: map_common.h:41
bool(* MapKeyEqualFn)(const void *key1, const void *key2)
Definition: map_common.h:40
unsigned int(* MapHashFn)(const void *p, unsigned int seed)
Definition: map_common.h:39
ArrayMapIterator arraymap_iter
Definition: map.h:83
bool is_array
Definition: map.h:80
HashMapIterator hashmap_iter
Definition: map.h:84
Definition: map.c:46
MapHashFn hash_fn
Definition: map.c:47