1 | /*
|
---|
2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * Integer to generic type map.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef LIBC_INT_MAP_H_
|
---|
38 | #define LIBC_INT_MAP_H_
|
---|
39 |
|
---|
40 | #include <errno.h>
|
---|
41 | #include <malloc.h>
|
---|
42 | #include <mem.h>
|
---|
43 | #include <unistd.h>
|
---|
44 |
|
---|
45 | /** Internal magic value for a map consistency check. */
|
---|
46 | #define INT_MAP_MAGIC_VALUE 0x11223344
|
---|
47 |
|
---|
48 | /** Internal magic value for an item consistency check. */
|
---|
49 | #define INT_MAP_ITEM_MAGIC_VALUE 0x55667788
|
---|
50 |
|
---|
51 | /** Generic destructor function pointer. */
|
---|
52 | #define DTOR_T(identifier) \
|
---|
53 | void (*identifier)(const void *)
|
---|
54 |
|
---|
55 | /** Integer to generic type map declaration.
|
---|
56 | *
|
---|
57 | * @param[in] name Name of the map.
|
---|
58 | * @param[in] type Inner object type.
|
---|
59 | */
|
---|
60 | #define INT_MAP_DECLARE(name, type) \
|
---|
61 | typedef struct name name##_t; \
|
---|
62 | typedef struct name##_item name##_item_t; \
|
---|
63 | \
|
---|
64 | struct name##_item { \
|
---|
65 | int key; \
|
---|
66 | type *value; \
|
---|
67 | int magic; \
|
---|
68 | }; \
|
---|
69 | \
|
---|
70 | struct name { \
|
---|
71 | int size; \
|
---|
72 | int next; \
|
---|
73 | name##_item_t *items; \
|
---|
74 | int magic; \
|
---|
75 | }; \
|
---|
76 | \
|
---|
77 | int name##_add(name##_t *, int, type *); \
|
---|
78 | void name##_clear(name##_t *, DTOR_T()); \
|
---|
79 | int name##_count(name##_t *); \
|
---|
80 | void name##_destroy(name##_t *, DTOR_T()); \
|
---|
81 | void name##_exclude(name##_t *, int, DTOR_T()); \
|
---|
82 | void name##_exclude_index(name##_t *, int, DTOR_T()); \
|
---|
83 | type *name##_find(name##_t *, int); \
|
---|
84 | int name##_update(name##_t *, int, int); \
|
---|
85 | type *name##_get_index(name##_t *, int); \
|
---|
86 | int name##_initialize(name##_t *); \
|
---|
87 | int name##_is_valid(name##_t *); \
|
---|
88 | void name##_item_destroy(name##_item_t *, DTOR_T()); \
|
---|
89 | int name##_item_is_valid(name##_item_t *);
|
---|
90 |
|
---|
91 | /** Integer to generic type map implementation.
|
---|
92 | *
|
---|
93 | * Should follow declaration with the same parameters.
|
---|
94 | *
|
---|
95 | * @param[in] name Name of the map.
|
---|
96 | * @param[in] type Inner object type.
|
---|
97 | */
|
---|
98 | #define INT_MAP_IMPLEMENT(name, type) \
|
---|
99 | int name##_add(name##_t *map, int key, type *value) \
|
---|
100 | { \
|
---|
101 | if (name##_is_valid(map)) { \
|
---|
102 | if (map->next == (map->size - 1)) { \
|
---|
103 | name##_item_t *tmp; \
|
---|
104 | tmp = (name##_item_t *) realloc(map->items, \
|
---|
105 | sizeof(name##_item_t) * 2 * map->size); \
|
---|
106 | if (!tmp) \
|
---|
107 | return ENOMEM; \
|
---|
108 | map->size *= 2; \
|
---|
109 | map->items = tmp; \
|
---|
110 | } \
|
---|
111 | map->items[map->next].key = key; \
|
---|
112 | map->items[map->next].value = value; \
|
---|
113 | map->items[map->next].magic = INT_MAP_ITEM_MAGIC_VALUE; \
|
---|
114 | ++map->next; \
|
---|
115 | map->items[map->next].magic = 0; \
|
---|
116 | return map->next - 1; \
|
---|
117 | } \
|
---|
118 | return EINVAL; \
|
---|
119 | } \
|
---|
120 | \
|
---|
121 | void name##_clear(name##_t *map, DTOR_T(dtor)) \
|
---|
122 | { \
|
---|
123 | if (name##_is_valid(map)) { \
|
---|
124 | int index; \
|
---|
125 | for (index = 0; index < map->next; ++index) { \
|
---|
126 | if (name##_item_is_valid(&map->items[index])) { \
|
---|
127 | name##_item_destroy( \
|
---|
128 | &map->items[index], dtor); \
|
---|
129 | } \
|
---|
130 | } \
|
---|
131 | map->next = 0; \
|
---|
132 | map->items[map->next].magic = 0; \
|
---|
133 | } \
|
---|
134 | } \
|
---|
135 | \
|
---|
136 | int name##_count(name##_t *map) \
|
---|
137 | { \
|
---|
138 | return name##_is_valid(map) ? map->next : -1; \
|
---|
139 | } \
|
---|
140 | \
|
---|
141 | void name##_destroy(name##_t *map, DTOR_T(dtor)) \
|
---|
142 | { \
|
---|
143 | if (name##_is_valid(map)) { \
|
---|
144 | int index; \
|
---|
145 | map->magic = 0; \
|
---|
146 | for (index = 0; index < map->next; ++index) { \
|
---|
147 | if (name##_item_is_valid(&map->items[index])) { \
|
---|
148 | name##_item_destroy( \
|
---|
149 | &map->items[index], dtor); \
|
---|
150 | } \
|
---|
151 | } \
|
---|
152 | free(map->items); \
|
---|
153 | } \
|
---|
154 | } \
|
---|
155 | \
|
---|
156 | void name##_exclude(name##_t *map, int key, DTOR_T(dtor)) \
|
---|
157 | { \
|
---|
158 | if (name##_is_valid(map)) { \
|
---|
159 | int index; \
|
---|
160 | for (index = 0; index < map->next; ++index) { \
|
---|
161 | if (name##_item_is_valid(&map->items[index]) && \
|
---|
162 | (map->items[index].key == key)) { \
|
---|
163 | name##_item_destroy( \
|
---|
164 | &map->items[index], dtor); \
|
---|
165 | } \
|
---|
166 | } \
|
---|
167 | } \
|
---|
168 | } \
|
---|
169 | \
|
---|
170 | void name##_exclude_index(name##_t *map, int index, DTOR_T(dtor)) \
|
---|
171 | { \
|
---|
172 | if (name##_is_valid(map) && (index >= 0) && \
|
---|
173 | (index < map->next) && \
|
---|
174 | name##_item_is_valid(&map->items[index])) { \
|
---|
175 | name##_item_destroy(&map->items[index], dtor); \
|
---|
176 | } \
|
---|
177 | } \
|
---|
178 | \
|
---|
179 | type *name##_find(name##_t *map, int key) \
|
---|
180 | { \
|
---|
181 | if (name##_is_valid(map)) { \
|
---|
182 | int index; \
|
---|
183 | for (index = 0; index < map->next; ++index) { \
|
---|
184 | if (name##_item_is_valid(&map->items[index]) && \
|
---|
185 | (map->items[index].key == key)) { \
|
---|
186 | return map->items[index].value; \
|
---|
187 | } \
|
---|
188 | } \
|
---|
189 | } \
|
---|
190 | return NULL; \
|
---|
191 | } \
|
---|
192 | \
|
---|
193 | int name##_update(name##_t *map, int key, int new_key) \
|
---|
194 | { \
|
---|
195 | if (name##_is_valid(map)) { \
|
---|
196 | int index; \
|
---|
197 | for (index = 0; index < map->next; ++index) { \
|
---|
198 | if (name##_item_is_valid(&map->items[index])) { \
|
---|
199 | if (map->items[index].key == new_key) \
|
---|
200 | return EEXIST; \
|
---|
201 | if (map->items[index].key == key) { \
|
---|
202 | map->items[index].key = \
|
---|
203 | new_key; \
|
---|
204 | return EOK; \
|
---|
205 | } \
|
---|
206 | } \
|
---|
207 | } \
|
---|
208 | } \
|
---|
209 | return ENOENT; \
|
---|
210 | } \
|
---|
211 | \
|
---|
212 | type *name##_get_index(name##_t *map, int index) \
|
---|
213 | { \
|
---|
214 | if (name##_is_valid(map) && (index >= 0) && \
|
---|
215 | (index < map->next) && \
|
---|
216 | name##_item_is_valid(&map->items[index])) { \
|
---|
217 | return map->items[index].value; \
|
---|
218 | } \
|
---|
219 | return NULL; \
|
---|
220 | } \
|
---|
221 | \
|
---|
222 | int name##_initialize(name##_t *map) \
|
---|
223 | { \
|
---|
224 | if (!map) \
|
---|
225 | return EINVAL; \
|
---|
226 | map->size = 2; \
|
---|
227 | map->next = 0; \
|
---|
228 | map->items = (name##_item_t *) malloc(sizeof(name##_item_t) * \
|
---|
229 | map->size); \
|
---|
230 | if (!map->items) \
|
---|
231 | return ENOMEM; \
|
---|
232 | map->items[map->next].magic = 0; \
|
---|
233 | map->magic = INT_MAP_MAGIC_VALUE; \
|
---|
234 | return EOK; \
|
---|
235 | } \
|
---|
236 | \
|
---|
237 | int name##_is_valid(name##_t *map) \
|
---|
238 | { \
|
---|
239 | return map && (map->magic == INT_MAP_MAGIC_VALUE); \
|
---|
240 | } \
|
---|
241 | \
|
---|
242 | void name##_item_destroy(name##_item_t *item, DTOR_T(dtor)) \
|
---|
243 | { \
|
---|
244 | if (name##_item_is_valid(item)) { \
|
---|
245 | item->magic = 0; \
|
---|
246 | if (item->value) { \
|
---|
247 | if (dtor) \
|
---|
248 | free(item->value); \
|
---|
249 | item->value = NULL; \
|
---|
250 | } \
|
---|
251 | } \
|
---|
252 | } \
|
---|
253 | \
|
---|
254 | int name##_item_is_valid(name##_item_t *item) \
|
---|
255 | { \
|
---|
256 | return item && (item->magic == INT_MAP_ITEM_MAGIC_VALUE); \
|
---|
257 | }
|
---|
258 |
|
---|
259 | #endif
|
---|
260 |
|
---|
261 | /** @}
|
---|
262 | */
|
---|
263 |
|
---|