Loading...
Searching...
No Matches
as_map.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2023 Aerospike, Inc.
3 *
4 * Portions may be licensed to Aerospike, Inc. under one or more contributor
5 * license agreements.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17
18#pragma once
19
21#include <aerospike/as_std.h>
22#include <aerospike/as_util.h>
23#include <aerospike/as_val.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/******************************************************************************
30 * TYPES
31 *****************************************************************************/
32
33#define AS_MAP_FLAGS_MASK 0x13
34
35union as_map_iterator_u;
36struct as_map_hooks_s;
37
38/**
39 * Callback function for `as_map_foreach()`. Called for each entry in the
40 * map.
41 *
42 * @param key The key of the current entry.
43 * @param value The value of the current entry.
44 * @param udata The user-data provided to `as_map_foreach()`.
45 *
46 * @return true to continue iterating through the map.
47 * false to stop iterating.
48 */
49typedef bool (*as_map_foreach_callback) (const as_val* key, const as_val* value, void* udata);
50
51/**
52 * as_map is an interface for Map based data types.
53 *
54 * Implementations:
55 * - as_hashmap
56 * - as_orderedmap
57 *
58 * @extends as_val
59 * @ingroup aerospike_t
60 */
61typedef struct as_map_s {
62
63 /**
64 * @private
65 * as_map is a subtype of as_val.
66 * You can cast as_map to as_val.
67 */
68 as_val _;
69
70 /**
71 * Information for this instance of as_map.
72 */
73 uint32_t flags;
74
75 /**
76 * Hooks for subtypes of as_map to implement.
77 */
78 const struct as_map_hooks_s* hooks;
79
80} as_map;
81
82/**
83 * Map Function Hooks
84 */
85typedef struct as_map_hooks_s {
86
87 /***************************************************************************
88 * instance hooks
89 **************************************************************************/
90
91 /**
92 * Releases the subtype of as_map.
93 *
94 * @param map The map instance to destroy.
95 *
96 * @return true on success. Otherwise false.
97 */
98 bool (*destroy)(as_map* map);
99
100 /***************************************************************************
101 * info hooks
102 **************************************************************************/
103
104 /**
105 * The hash value of an as_map.
106 *
107 * @param map The map to get the hashcode value for.
108 *
109 * @return The hashcode value.
110 */
111 uint32_t (*hashcode)(const as_map* map);
112
113 /**
114 * The size of the as_map.
115 *
116 * @param map The map to get the size of.
117 *
118 * @return The number of entries in the map.
119 */
120 uint32_t (*size)(const as_map* map);
121
122 /***************************************************************************
123 * accessor and modifier hooks
124 **************************************************************************/
125
126 /**
127 * Set a value of the given key in a map.
128 *
129 * @param map The map to store the (key,value) pair.
130 * @param key The key for the given value.
131 * @param val The value for the given key.
132 *
133 * @return 0 on success. Otherwise an error occurred.
134 */
135 int (*set)(as_map* map, const as_val* key, const as_val* val);
136
137 /**
138 * Set a value at the given key of the map.
139 *
140 * @param map The map to containing the (key,value) pair.
141 * @param key The key of the value.
142 *
143 * @return The value on success. Otherwise NULL.
144 */
145 as_val* (*get)(const as_map* map, const as_val* key);
146
147 /**
148 * Clear all entries of the map.
149 *
150 * @param map The map to clear.
151 *
152 * @return 0 on success. Otherwise an error occurred.
153 */
154 int (*clear)(as_map* map);
155
156 /**
157 * Remove the entry specified by the key.
158 *
159 * @param map The map to remove the entry from.
160 * @param key The key of the entry to be removed.
161 *
162 * @return 0 on success. Otherwise an error occurred.
163 */
164 int (*remove)(as_map* map, const as_val* key);
165
166 /**
167 * Set map attributes.
168 *
169 * @param map The map to remove the entry from.
170 * @param flags Flags to set.
171 *
172 * @return 0 on success. Otherwise an error occurred.
173 */
174 void (*set_flags)(as_map* map, uint32_t flags);
175
176 /***************************************************************************
177 * iteration hooks
178 **************************************************************************/
179
180 /**
181 * Iterate over each entry in the map can call the callback function.
182 *
183 * @param map The map to iterate.
184 * @param callback The function to call for each entry in the map.
185 * @param udata User-data to be passed to the callback.
186 *
187 * @return true on success. Otherwise false.
188 */
189 bool (*foreach)(const as_map* map, as_map_foreach_callback callback, void* udata);
190
191 /**
192 * Create and initialize a new heap allocated iterator to traverse over the entries map.
193 *
194 * @param map The map to iterate.
195 *
196 * @return true on success. Otherwise false.
197 */
198 union as_map_iterator_u* (*iterator_new)(const as_map* map);
199
200 /**
201 * Initialize a stack allocated iterator to traverse over the entries map.
202 *
203 * @param map The map to iterate.
204 *
205 * @return true on success. Otherwise false.
206 */
207 union as_map_iterator_u* (*iterator_init)(const as_map* map, union as_map_iterator_u* it);
208
210
211/******************************************************************************
212 * INSTANCE FUNCTIONS
213 *****************************************************************************/
214
215/**
216 * @private
217 * Utilized by subtypes of as_map to initialize the parent.
218 *
219 * @param map The map to initialize
220 * @param free If TRUE, then as_map_destory() will free the map.
221 * @param flags Map attributes.
222 * @param hooks Implementaton for the map interface.
223 *
224 * @return The initialized as_map on success. Otherwise NULL.
225 * @relatesalso as_map
226 */
227 AS_EXTERN as_map* as_map_cons(as_map* map, bool free, uint32_t flags, const as_map_hooks* hooks);
228
229/**
230 * Initialize a stack allocated map.
231 *
232 * @param map Stack allocated map to initialize.
233 * @param hooks Implementation for the map interface.
234 *
235 * @return On success, the initialized map. Otherwise NULL.
236 * @relatesalso as_map
237 */
239
240/**
241 * Create and initialize a new heap allocated map.
242 *
243 * @param hooks Implementation for the map interface.
244 *
245 * @return On success, a new map. Otherwise NULL.
246 * @relatesalso as_map
247 */
249
250/**
251 * Destroy the as_map and associated resources.
252 * @relatesalso as_map
253 */
254static inline void as_map_destroy(as_map* map)
255{
256 as_val_destroy((as_val*) map);
257}
258
259/*******************************************************************************
260 * INFO FUNCTIONS
261 ******************************************************************************/
262
263/**
264 * Hash value for the map
265 *
266 * @param map The map
267 *
268 * @return The hashcode value of the map.
269 * @relatesalso as_map
270 */
271static inline uint32_t as_map_hashcode(const as_map* map)
272{
273 return as_util_hook(hashcode, 0, map);
274}
275
276/**
277 * Get the number of entries in the map.
278 *
279 * @param map The map
280 *
281 * @return The size of the map.
282 * @relatesalso as_map
283 */
284static inline uint32_t as_map_size(const as_map* map)
285{
286 return as_util_hook(size, 0, map);
287}
288
289/*******************************************************************************
290 * ACCESSOR AND MODIFIER FUNCTIONS
291 ******************************************************************************/
292
293/**
294 * Get the value for specified key.
295 *
296 * @param map The map.
297 * @param key The key.
298 *
299 * @return The value for the specified key on success. Otherwise NULL.
300 * @relatesalso as_map
301 */
302static inline as_val* as_map_get(const as_map* map, const as_val* key)
303{
304 return as_util_hook(get, NULL, map, key);
305}
306
307/**
308 * Set the value for specified key.
309 *
310 * @param map The map.
311 * @param key The key.
312 * @param val The value for the key.
313 *
314 * @return 0 on success. Otherwise an error occurred.
315 * @relatesalso as_map
316 */
317static inline int as_map_set(as_map* map, const as_val* key, const as_val* val)
318{
319 return as_util_hook(set, 1, map, key, val);
320}
321
322/**
323 * Remove all entries from the map.
324 *
325 * @param map The map.
326 *
327 * @return 0 on success. Otherwise an error occurred.
328 * @relatesalso as_map
329 */
330static inline int as_map_clear(as_map* map)
331{
332 return as_util_hook(clear, 1, map);
333}
334
335/**
336 * Remove the entry specified by the key.
337 *
338 * @param map The map to remove the entry from.
339 * @param key The key of the entry to be removed.
340 *
341 * @return 0 on success. Otherwise an error occurred.
342 *
343 * @relatesalso as_map
344 */
345static inline int as_map_remove(as_map* map, const as_val* key)
346{
347 return as_util_hook(remove, 1, map, key);
348}
349
350/**
351 * Set map attributes.
352 *
353 * @relatesalso as_map
354 */
355static inline void as_map_set_flags(as_map* map, uint32_t flags)
356{
357 as_util_hook_ret_void(set_flags, map, flags);
358}
359
360/******************************************************************************
361 * ITERATION FUNCTIONS
362 *****************************************************************************/
363
364/**
365 * Call the callback function for each entry in the map.
366 *
367 * @param map The map.
368 * @param callback The function to call for each entry.
369 * @param udata User-data to be passed to the callback.
370 *
371 * @return true if iteration completes fully. false if iteration was aborted.
372 *
373 * @relatesalso as_map
374 */
375static inline bool as_map_foreach(const as_map* map, as_map_foreach_callback callback, void* udata)
376{
377 return as_util_hook(foreach, false, map, callback, udata);
378}
379
380/**
381 * Creates and initializes a new heap allocated iterator over the given map.
382 *
383 * @param map The map to iterate.
384 *
385 * @return On success, a new as_iterator. Otherwise NULL.
386 * @relatesalso as_map
387 */
388static inline union as_map_iterator_u* as_map_iterator_new(const as_map* map)
389{
390 return as_util_hook(iterator_new, NULL, map);
391}
392
393/**
394 * Initialzies a stack allocated iterator over the given map.
395 *
396 * @param map The map to iterate.
397 * @param it The iterator to initialize.
398 *
399 * @return On success, the initializes as_iterator. Otherwise NULL.
400 * @relatesalso as_map
401 */
402static inline union as_map_iterator_u* as_map_iterator_init(union as_map_iterator_u* it, const as_map* map)
403{
404 return as_util_hook(iterator_init, NULL, map, it);
405}
406
407/******************************************************************************
408 * CONVERSION FUNCTIONS
409 *****************************************************************************/
410
411/**
412 * Convert to an as_val.
413 * @relatesalso as_map
414 */
415static inline as_val* as_map_toval(const as_map* map)
416{
417 return (as_val*) map;
418}
419
420/**
421 * Convert from an as_val.
422 * @relatesalso as_map
423 */
424static inline as_map* as_map_fromval(const as_val* val)
425{
426 return as_util_fromval(val, AS_MAP, as_map);
427}
428
429/******************************************************************************
430 * as_val FUNCTIONS
431 *****************************************************************************/
432
433/**
434 * @private
435 * Internal helper function for destroying an as_val.
436 */
438
439/**
440 * @private
441 * Internal helper function for getting the hashcode of an as_val.
442 */
444
445/**
446 * @private
447 * Internal helper function for getting the string representation of an as_val.
448 */
450
451#ifdef __cplusplus
452} // end extern "C"
453#endif
AS_EXTERN void as_map_val_destroy(as_val *val)
AS_EXTERN char * as_map_val_tostring(const as_val *val)
AS_EXTERN uint32_t as_map_val_hashcode(const as_val *val)
bool(* as_map_foreach_callback)(const as_val *key, const as_val *value, void *udata)
Definition as_map.h:49
#define AS_EXTERN
Definition as_std.h:25
#define as_util_hook(hook, default, object,...)
Definition as_util.h:34
#define as_util_fromval(object, type_id, type)
Definition as_util.h:43
#define as_util_hook_ret_void(hook, object,...)
Definition as_util.h:37
@ AS_MAP
Definition as_val.h:42
#define as_val_destroy(__v)
Definition as_val.h:114
AS_EXTERN as_map * as_map_cons(as_map *map, bool free, uint32_t flags, const as_map_hooks *hooks)
static as_val * as_map_get(const as_map *map, const as_val *key)
Definition as_map.h:302
static int as_map_set(as_map *map, const as_val *key, const as_val *val)
Definition as_map.h:317
static as_val * as_map_toval(const as_map *map)
Definition as_map.h:415
static bool as_map_foreach(const as_map *map, as_map_foreach_callback callback, void *udata)
Definition as_map.h:375
static uint32_t as_map_size(const as_map *map)
Definition as_map.h:284
static union as_map_iterator_u * as_map_iterator_new(const as_map *map)
Definition as_map.h:388
static int as_map_remove(as_map *map, const as_val *key)
Definition as_map.h:345
static void as_map_set_flags(as_map *map, uint32_t flags)
Definition as_map.h:355
static void as_map_destroy(as_map *map)
Definition as_map.h:254
AS_EXTERN as_map * as_map_new(const as_map_hooks *hooks)
const struct as_map_hooks_s * hooks
Definition as_map.h:78
static union as_map_iterator_u * as_map_iterator_init(union as_map_iterator_u *it, const as_map *map)
Definition as_map.h:402
static uint32_t as_map_hashcode(const as_map *map)
Definition as_map.h:271
AS_EXTERN as_map * as_map_init(as_map *map, const as_map_hooks *hooks)
uint32_t flags
Definition as_map.h:73
static int as_map_clear(as_map *map)
Definition as_map.h:330
static as_map * as_map_fromval(const as_val *val)
Definition as_map.h:424