ucx
UAP Common Extensions
map.h
Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
41 #ifndef UCX_MAP_H
42 #define UCX_MAP_H
43 
44 #include "ucx.h"
45 #include "string.h"
46 #include "allocator.h"
47 #include <stdio.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
65 #define UCX_MAP_FOREACH(key,value,iter) \
66  for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&value);)
67 
69 typedef struct UcxMap UcxMap;
70 
72 typedef struct UcxKey UcxKey;
73 
76 
79 
81 struct UcxMap {
87  size_t size;
89  size_t count;
90 };
91 
93 struct UcxKey {
95  void *data;
97  size_t len;
99  int hash;
100 };
101 
105  void *data;
106 
109 
112 };
113 
118 
121 
128  size_t index;
129 };
130 
136 UcxMap *ucx_map_new(size_t size);
137 
145 
155 void ucx_map_free(UcxMap *map);
156 
174 
184 void ucx_map_clear(UcxMap *map);
185 
186 
201 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data);
202 
217 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
218 
236 
245 int ucx_map_put(UcxMap *map, UcxKey key, void *value);
246 
254 void* ucx_map_get(UcxMap *map, UcxKey key);
255 
263 void* ucx_map_remove(UcxMap *map, UcxKey key);
264 
273 #define ucx_map_sstr_put(map, key, value) \
274  ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value)
275 
284 #define ucx_map_cstr_put(map, key, value) \
285  ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value)
286 
295 #define ucx_map_int_put(map, key, value) \
296  ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value)
297 
305 #define ucx_map_sstr_get(map, key) \
306  ucx_map_get(map, ucx_key(key.ptr, key.length))
307 
315 #define ucx_map_cstr_get(map, key) \
316  ucx_map_get(map, ucx_key((void*)key, strlen(key)))
317 
325 #define ucx_map_int_get(map, key) \
326  ucx_map_get(map, ucx_key((void*)&key, sizeof(int)))
327 
335 #define ucx_map_sstr_remove(map, key) \
336  ucx_map_remove(map, ucx_key(key.ptr, key.length))
337 
345 #define ucx_map_cstr_remove(map, key) \
346  ucx_map_remove(map, ucx_key((void*)key, strlen(key)))
347 
355 #define ucx_map_int_remove(map, key) \
356  ucx_map_remove(map, ucx_key((void*)&key, sizeof(key)))
357 
368 UcxKey ucx_key(void *data, size_t len);
369 
377 int ucx_hash(const char *data, size_t len);
378 
397 
414 int ucx_map_iter_next(UcxMapIterator *iterator, UcxKey *key, void **value);
415 
416 
417 #ifdef __cplusplus
418 }
419 #endif
420 
421 #endif /* UCX_MAP_H */
422 
void *(* copy_func)(const void *, void *)
Function pointer to a copy function.
Definition: ucx.h:106
UcxMap * map
The map to iterate over.
Definition: map.h:117
size_t index
The current index of the element list array.
Definition: map.h:128
Bounded string implementation.
Main UCX Header providing most common definitions.
int ucx_hash(const char *data, size_t len)
Computes a murmur hash-2.
Definition: map.c:247
void ucx_map_free_content(UcxMap *map, ucx_destructor destr)
Frees the contents of a hash map.
Definition: map.c:85
void * ucx_map_get(UcxMap *map, UcxKey key)
Retrieves a value by using a key.
Definition: map.c:230
int hash
The hash value of the key data.
Definition: map.h:99
void ucx_map_clear(UcxMap *map)
Clears a hash map.
Definition: map.c:93
Structure for an element of a UcxMap.
Definition: map.h:103
Structure for a key of a UcxMap.
Definition: map.h:93
size_t count
The count of elements currently stored in this map.
Definition: map.h:89
UcxMap * ucx_map_clone(UcxMap *map, copy_func fnc, void *data)
Clones the map and rehashes if necessary.
Definition: map.c:113
UcxMapElement * next
A pointer to the next element in the current list.
Definition: map.h:108
UcxKey ucx_key(void *data, size_t len)
Creates a UcxKey based on the given data.
Definition: map.c:238
Structure for an iterator over a UcxMap.
Definition: map.h:115
void * data
The value data.
Definition: map.h:105
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
UcxMap * ucx_map_new(size_t size)
Creates a new hash map with the specified size.
Definition: map.c:34
void * data
The key data.
Definition: map.h:95
UcxMapElement * cur
The current map element.
Definition: map.h:120
UcxMap * ucx_map_new_a(UcxAllocator *allocator, size_t size)
Creates a new hash map with the specified size using a UcxAllocator.
Definition: map.c:38
UcxKey key
The corresponding key.
Definition: map.h:111
UcxAllocator * allocator
An allocator that is used for the map elements.
Definition: map.h:83
int ucx_map_put(UcxMap *map, UcxKey key, void *value)
Puts a key/value-pair into the map.
Definition: map.c:149
size_t size
The size of the map is the length of the element list array.
Definition: map.h:87
int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data)
Copies contents from a map to another map using a copy function.
Definition: map.c:102
int ucx_map_iter_next(UcxMapIterator *iterator, UcxKey *key, void **value)
Proceeds to the next element of the map (if any).
Definition: map.c:297
Allocator for custom memory management.
Structure for the UCX map.
Definition: map.h:81
void * ucx_map_remove(UcxMap *map, UcxKey key)
Removes a key/value-pair from the map by using the key.
Definition: map.c:234
size_t len
The length of the key data.
Definition: map.h:97
int ucx_map_rehash(UcxMap *map)
Increases size of the hash map, if necessary.
Definition: map.c:123
void ucx_map_free(UcxMap *map)
Frees a hash map.
Definition: map.c:79
UcxMapElement ** map
The array of map element lists.
Definition: map.h:85
UcxMapIterator ucx_map_iterator(UcxMap *map)
Creates an iterator for a map.
Definition: map.c:289
void(* ucx_destructor)(void *)
A function pointer to a destructor function.
Definition: ucx.h:72