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 | /** Integer to generic type map declaration.
|
---|
52 | *
|
---|
53 | * @param[in] name Name of the map.
|
---|
54 | * @param[in] type Inner object type.
|
---|
55 | */
|
---|
56 | #define INT_MAP_DECLARE(name, type) \
|
---|
57 | typedef struct name name##_t; \
|
---|
58 | typedef struct name##_item name##_item_t; \
|
---|
59 | \
|
---|
60 | struct name##_item { \
|
---|
61 | int key; \
|
---|
62 | type *value; \
|
---|
63 | int magic; \
|
---|
64 | }; \
|
---|
65 | \
|
---|
66 | struct name { \
|
---|
67 | int size; \
|
---|
68 | int next; \
|
---|
69 | name##_item_t *items; \
|
---|
70 | int magic; \
|
---|
71 | }; \
|
---|
72 | \
|
---|
73 | int name##_add(name##_t *, int, type *); \
|
---|
74 | void name##_clear(name##_t *); \
|
---|
75 | int name##_count(name##_t *); \
|
---|
76 | void name##_destroy(name##_t *); \
|
---|
77 | void name##_exclude(name##_t *, int); \
|
---|
78 | void name##_exclude_index(name##_t *, int); \
|
---|
79 | type *name##_find(name##_t *, int); \
|
---|
80 | int name##_update(name##_t *, int, int); \
|
---|
81 | type *name##_get_index(name##_t *, int); \
|
---|
82 | int name##_initialize(name##_t *); \
|
---|
83 | int name##_is_valid(name##_t *); \
|
---|
84 | void name##_item_destroy(name##_item_t *); \
|
---|
85 | int name##_item_is_valid(name##_item_t *);
|
---|
86 |
|
---|
87 | /** Integer to generic type map implementation.
|
---|
88 | *
|
---|
89 | * Should follow declaration with the same parameters.
|
---|
90 | *
|
---|
91 | * @param[in] name Name of the map.
|
---|
92 | * @param[in] type Inner object type.
|
---|
93 | */
|
---|
94 | #define INT_MAP_IMPLEMENT(name, type) \
|
---|
95 | int name##_add(name##_t *map, int key, type *value) \
|
---|
96 | { \
|
---|
97 | if (name##_is_valid(map)) { \
|
---|
98 | if (map->next == (map->size - 1)) { \
|
---|
99 | name##_item_t *tmp; \
|
---|
100 | tmp = (name##_item_t *) realloc(map->items, \
|
---|
101 | sizeof(name##_item_t) * 2 * map->size); \
|
---|
102 | if (!tmp) \
|
---|
103 | return ENOMEM; \
|
---|
104 | map->size *= 2; \
|
---|
105 | map->items = tmp; \
|
---|
106 | } \
|
---|
107 | map->items[map->next].key = key; \
|
---|
108 | map->items[map->next].value = value; \
|
---|
109 | map->items[map->next].magic = INT_MAP_ITEM_MAGIC_VALUE; \
|
---|
110 | ++map->next; \
|
---|
111 | map->items[map->next].magic = 0; \
|
---|
112 | return map->next - 1; \
|
---|
113 | } \
|
---|
114 | return EINVAL; \
|
---|
115 | } \
|
---|
116 | \
|
---|
117 | void name##_clear(name##_t *map) \
|
---|
118 | { \
|
---|
119 | if (name##_is_valid(map)) { \
|
---|
120 | int index; \
|
---|
121 | for (index = 0; index < map->next; ++index) { \
|
---|
122 | if (name##_item_is_valid(&map->items[index])) { \
|
---|
123 | name##_item_destroy( \
|
---|
124 | &map->items[index]); \
|
---|
125 | } \
|
---|
126 | } \
|
---|
127 | map->next = 0; \
|
---|
128 | map->items[map->next].magic = 0; \
|
---|
129 | } \
|
---|
130 | } \
|
---|
131 | \
|
---|
132 | int name##_count(name##_t *map) \
|
---|
133 | { \
|
---|
134 | return name##_is_valid(map) ? map->next : -1; \
|
---|
135 | } \
|
---|
136 | \
|
---|
137 | void name##_destroy(name##_t *map) \
|
---|
138 | { \
|
---|
139 | if (name##_is_valid(map)) { \
|
---|
140 | int index; \
|
---|
141 | map->magic = 0; \
|
---|
142 | for (index = 0; index < map->next; ++index) { \
|
---|
143 | if (name##_item_is_valid(&map->items[index])) { \
|
---|
144 | name##_item_destroy( \
|
---|
145 | &map->items[index]); \
|
---|
146 | } \
|
---|
147 | } \
|
---|
148 | free(map->items); \
|
---|
149 | } \
|
---|
150 | } \
|
---|
151 | \
|
---|
152 | void name##_exclude(name##_t *map, int key) \
|
---|
153 | { \
|
---|
154 | if (name##_is_valid(map)) { \
|
---|
155 | int index; \
|
---|
156 | for (index = 0; index < map->next; ++index) { \
|
---|
157 | if (name##_item_is_valid(&map->items[index]) && \
|
---|
158 | (map->items[index].key == key)) { \
|
---|
159 | name##_item_destroy( \
|
---|
160 | &map->items[index]); \
|
---|
161 | } \
|
---|
162 | } \
|
---|
163 | } \
|
---|
164 | } \
|
---|
165 | \
|
---|
166 | void name##_exclude_index(name##_t *map, int index) \
|
---|
167 | { \
|
---|
168 | if (name##_is_valid(map) && (index >= 0) && \
|
---|
169 | (index < map->next) && \
|
---|
170 | name##_item_is_valid(&map->items[index])) { \
|
---|
171 | name##_item_destroy(&map->items[index]); \
|
---|
172 | } \
|
---|
173 | } \
|
---|
174 | \
|
---|
175 | type *name##_find(name##_t *map, int key) \
|
---|
176 | { \
|
---|
177 | if (name##_is_valid(map)) { \
|
---|
178 | int index; \
|
---|
179 | for (index = 0; index < map->next; ++index) { \
|
---|
180 | if (name##_item_is_valid(&map->items[index]) && \
|
---|
181 | (map->items[index].key == key)) { \
|
---|
182 | return map->items[index].value; \
|
---|
183 | } \
|
---|
184 | } \
|
---|
185 | } \
|
---|
186 | return NULL; \
|
---|
187 | } \
|
---|
188 | \
|
---|
189 | int name##_update(name##_t *map, int key, int new_key) \
|
---|
190 | { \
|
---|
191 | if (name##_is_valid(map)) { \
|
---|
192 | int index; \
|
---|
193 | for (index = 0; index < map->next; ++index) { \
|
---|
194 | if (name##_item_is_valid(&map->items[index])) { \
|
---|
195 | if (map->items[index].key == new_key) \
|
---|
196 | return EEXIST; \
|
---|
197 | if (map->items[index].key == key) { \
|
---|
198 | map->items[index].key = \
|
---|
199 | new_key; \
|
---|
200 | return EOK; \
|
---|
201 | } \
|
---|
202 | } \
|
---|
203 | } \
|
---|
204 | } \
|
---|
205 | return ENOENT; \
|
---|
206 | } \
|
---|
207 | \
|
---|
208 | type *name##_get_index(name##_t *map, int index) \
|
---|
209 | { \
|
---|
210 | if (name##_is_valid(map) && (index >= 0) && \
|
---|
211 | (index < map->next) && \
|
---|
212 | name##_item_is_valid(&map->items[index])) { \
|
---|
213 | return map->items[index].value; \
|
---|
214 | } \
|
---|
215 | return NULL; \
|
---|
216 | } \
|
---|
217 | \
|
---|
218 | int name##_initialize(name##_t *map) \
|
---|
219 | { \
|
---|
220 | if (!map) \
|
---|
221 | return EINVAL; \
|
---|
222 | map->size = 2; \
|
---|
223 | map->next = 0; \
|
---|
224 | map->items = (name##_item_t *) malloc(sizeof(name##_item_t) * \
|
---|
225 | map->size); \
|
---|
226 | if (!map->items) \
|
---|
227 | return ENOMEM; \
|
---|
228 | map->items[map->next].magic = 0; \
|
---|
229 | map->magic = INT_MAP_MAGIC_VALUE; \
|
---|
230 | return EOK; \
|
---|
231 | } \
|
---|
232 | \
|
---|
233 | int name##_is_valid(name##_t *map) \
|
---|
234 | { \
|
---|
235 | return map && (map->magic == INT_MAP_MAGIC_VALUE); \
|
---|
236 | } \
|
---|
237 | \
|
---|
238 | void name##_item_destroy(name##_item_t *item) \
|
---|
239 | { \
|
---|
240 | if (name##_item_is_valid(item)) { \
|
---|
241 | item->magic = 0; \
|
---|
242 | if (item->value) { \
|
---|
243 | free(item->value); \
|
---|
244 | item->value = NULL; \
|
---|
245 | } \
|
---|
246 | } \
|
---|
247 | } \
|
---|
248 | \
|
---|
249 | int name##_item_is_valid(name##_item_t *item) \
|
---|
250 | { \
|
---|
251 | return item && (item->magic == INT_MAP_ITEM_MAGIC_VALUE); \
|
---|
252 | }
|
---|
253 |
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | /** @}
|
---|
257 | */
|
---|
258 |
|
---|