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 
177 
187 void ucx_map_clear(UcxMap *map);
188 
189 
204 int ucx_map_copy(UcxMap *from, UcxMap *to, copy_func fnc, void *data);
205 
220 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
221 
239 
248 int ucx_map_put(UcxMap *map, UcxKey key, void *value);
249 
257 void* ucx_map_get(UcxMap *map, UcxKey key);
258 
266 void* ucx_map_remove(UcxMap *map, UcxKey key);
267 
276 #define ucx_map_sstr_put(map, key, value) \
277  ucx_map_put(map, ucx_key(key.ptr, key.length), (void*)value)
278 
287 #define ucx_map_cstr_put(map, key, value) \
288  ucx_map_put(map, ucx_key((void*)key, strlen(key)), (void*)value)
289 
298 #define ucx_map_int_put(map, key, value) \
299  ucx_map_put(map, ucx_key((void*)&key, sizeof(key)), (void*)value)
300 
308 #define ucx_map_sstr_get(map, key) \
309  ucx_map_get(map, ucx_key(key.ptr, key.length))
310 
318 #define ucx_map_cstr_get(map, key) \
319  ucx_map_get(map, ucx_key((void*)key, strlen(key)))
320 
328 #define ucx_map_int_get(map, key) \
329  ucx_map_get(map, ucx_key((void*)&key, sizeof(int)))
330 
338 #define ucx_map_sstr_remove(map, key) \
339  ucx_map_remove(map, ucx_key(key.ptr, key.length))
340 
348 #define ucx_map_cstr_remove(map, key) \
349  ucx_map_remove(map, ucx_key((void*)key, strlen(key)))
350 
358 #define ucx_map_int_remove(map, key) \
359  ucx_map_remove(map, ucx_key((void*)&key, sizeof(key)))
360 
371 UcxKey ucx_key(void *data, size_t len);
372 
380 int ucx_hash(const char *data, size_t len);
381 
400 
417 int ucx_map_iter_next(UcxMapIterator *iterator, UcxKey *key, void **value);
418 
419 
420 #ifdef __cplusplus
421 }
422 #endif
423 
424 #endif /* UCX_MAP_H */
425 
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:251
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:234
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:97
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:117
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:242
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:153
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:106
int ucx_map_iter_next(UcxMapIterator *iterator, UcxKey *key, void **value)
Proceeds to the next element of the map (if any).
Definition: map.c:301
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:238
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:127
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:293
void(* ucx_destructor)(void *)
A function pointer to a destructor function.
Definition: ucx.h:72