ucx/map.h

Wed, 27 Feb 2013 16:59:02 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 16:59:02 +0100
changeset 100
e0ec80179a5d
parent 95
ecfdc1c4a552
child 103
08018864fb91
permissions
-rw-r--r--

happy 100th commit + removed deprecated sstrcat + fixed sstrncat

2
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 *
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 */
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 #ifndef MAP_H
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 #define MAP_H
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
8 #include "ucx.h"
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
9 #include "string.h"
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
10 #include "mempool.h"
41
7f90a03e186e simplified UCX_MAP_FOREACH
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
11 #include <stdio.h>
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
12
2
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 #ifdef __cplusplus
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 extern "C" {
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 #endif
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16
41
7f90a03e186e simplified UCX_MAP_FOREACH
Mike Becker <universe@uap-core.de>
parents: 38
diff changeset
17 #define UCX_MAP_FOREACH(elm,iter) \
69
fb59270b1de3 made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
18 for(;ucx_map_iter_next(&iter,(void**)&elm)==0;)
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
19
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
20 typedef struct UcxMap UcxMap;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
21 typedef struct UcxKey UcxKey;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
22 typedef struct UcxMapElement UcxMapElement;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
23 typedef struct UcxMapIterator UcxMapIterator;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
24
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
25 /*
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
26 * param 1: element
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
27 * param 2: additional data
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
28 * param 3: size of encoded data will be stored here
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
29 * returns encoded / decoded string or NULL on failure
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
30 */
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
31 typedef void*(*ucx_map_coder)(void*,void*,size_t*);
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
32
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
33 struct UcxMap {
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
34 UcxMapElement **map;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
35 size_t size;
45
dd03226c1a6b map counts elements
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
36 size_t count;
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
37 };
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
38
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
39 struct UcxKey {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
40 void *data;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
41 size_t len;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
42 int hash;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
43 };
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
44
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
45 struct UcxMapElement {
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
46 void *data;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
47 UcxMapElement *next;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
48 UcxKey key;
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
49 };
2
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
51 struct UcxMapIterator {
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
52 UcxMap *map;
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
53 UcxMapElement *cur;
95
ecfdc1c4a552 added gnu++11 support
Mike Becker <universe@uap-core.de>
parents: 79
diff changeset
54 size_t index;
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
55 };
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
56
2
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
58 UcxMap *ucx_map_new(size_t size);
29
bce0d7f2912b fixed map with the help of new tests
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
59 void ucx_map_free(UcxMap *map);
51
1c78cd19fb6b added rehashing to maps by using clone function
Mike Becker <universe@uap-core.de>
parents: 48
diff changeset
60 /* you cannot clone maps with more than 390 mio entries */
67
27e67e725d35 added some qualifiers + removed pointer alias in mergesort
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
61 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
27e67e725d35 added some qualifiers + removed pointer alias in mergesort
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
62 copy_func fnc, void *data);
44
46356d74e873 added map clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 42
diff changeset
63 UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data);
52
34f50d0bada4 added ucx_map_copy and fixed ucx_map_rehash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 51
diff changeset
64 int ucx_map_rehash(UcxMap *map);
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
65
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
66 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
67 void* ucx_map_get(UcxMap *map, UcxKey key);
53
e533c170bfb8 added ucx_map_remove
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
68 void* ucx_map_remove(UcxMap *map, UcxKey key);
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
69
71
303dabadff1c made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
70 #define ucx_map_sstr_put(m, s, d) \
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
71 ucx_map_put(m, ucx_key(s.ptr, s.length), d)
71
303dabadff1c made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
72 #define ucx_map_cstr_put(m, s, d) \
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
73 ucx_map_put(m, ucx_key((void*)s, strlen(s)), d)
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
74 #define ucx_map_int_put(m, i, d) \
79
cf3757c60c8f fixed macros and ucx_map_store_enc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
75 ucx_map_put(m, ucx_key((void*)&i, sizeof(i)), d)
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
76
71
303dabadff1c made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
77 #define ucx_map_sstr_get(m, s) \
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
78 ucx_map_get(m, ucx_key(s.ptr, s.length))
71
303dabadff1c made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
79 #define ucx_map_cstr_get(m, s) \
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
80 ucx_map_get(m, ucx_key((void*)s, strlen(s)))
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
81 #define ucx_map_int_get(m, i) \
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
82 ucx_map_get(m, ucx_key((void*)&i, sizeof(int)))
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
83
71
303dabadff1c made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
84 #define ucx_map_sstr_remove(m, s) \
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
85 ucx_map_remove(m, ucx_key(s.ptr, s.length))
71
303dabadff1c made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
86 #define ucx_map_cstr_remove(m, s) \
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
87 ucx_map_remove(m, ucx_key((void*)s, strlen(s)))
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
88 #define ucx_map_int_remove(m, i) \
79
cf3757c60c8f fixed macros and ucx_map_store_enc
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 78
diff changeset
89 ucx_map_remove(m, ucx_key((void*)&i, sizeof(i)))
20
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
90
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
91 UcxKey ucx_key(void *data, size_t len);
db7d9860dbbd added some map functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
92
67
27e67e725d35 added some qualifiers + removed pointer alias in mergesort
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
93 int ucx_hash(const char *data, size_t len);
2
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
31
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
95 UcxMapIterator ucx_map_iterator(UcxMap *map);
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
96
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
97 int ucx_map_iter_next(UcxMapIterator *i, void **elm);
91ac86557290 added map iterator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
98
46
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
99 /* use macros for string maps only, values are not encoded */
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
100 #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
46
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
101 #define ucx_map_store(map, f) ucx_map_store_enc(map, f, NULL, NULL)
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
102
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
103 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
104 ucx_map_coder decoder, void* decdata);
46
48ca036d7d9c implemented encoder/decoder for map store/load
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
105 /* encoders shall provide null terminated strings*/
48
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
106 int ucx_map_store_enc(UcxMap *map, FILE *f,
621a4430c404 map can now load values from file into pooled memory
Mike Becker <universe@uap-core.de>
parents: 46
diff changeset
107 ucx_map_coder encoder, void* encdata);
42
ff3dd1ee7dee (broken-commit) - added load and store functions, tests failing
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
108
2
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 #ifdef __cplusplus
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 #endif
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 #endif /* MAP_H */
79646375a420 Added some source files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114

mercurial