[21580dd] | 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 |
|
---|
[2544442] | 29 | /** @addtogroup libc
|
---|
[21580dd] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Integer to generic type map.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[2544442] | 37 | #ifndef LIBC_INT_MAP_H_
|
---|
| 38 | #define LIBC_INT_MAP_H_
|
---|
[21580dd] | 39 |
|
---|
| 40 | #include <errno.h>
|
---|
| 41 | #include <malloc.h>
|
---|
| 42 | #include <mem.h>
|
---|
| 43 | #include <unistd.h>
|
---|
| 44 |
|
---|
[70ce016] | 45 | /** Internal magic value for a map consistency check. */
|
---|
| 46 | #define INT_MAP_MAGIC_VALUE 0x11223344
|
---|
[21580dd] | 47 |
|
---|
[70ce016] | 48 | /** Internal magic value for an item consistency check. */
|
---|
[21580dd] | 49 | #define INT_MAP_ITEM_MAGIC_VALUE 0x55667788
|
---|
| 50 |
|
---|
[5fe7692] | 51 | /** Generic destructor function pointer. */
|
---|
| 52 | #define DTOR_T(identifier) \
|
---|
| 53 | void (*identifier)(const void *)
|
---|
| 54 |
|
---|
[21580dd] | 55 | /** Integer to generic type map declaration.
|
---|
[70ce016] | 56 | *
|
---|
| 57 | * @param[in] name Name of the map.
|
---|
| 58 | * @param[in] type Inner object type.
|
---|
[21580dd] | 59 | */
|
---|
[70ce016] | 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; \
|
---|
[aaa3f33a] | 73 | name##_item_t *items; \
|
---|
[70ce016] | 74 | int magic; \
|
---|
| 75 | }; \
|
---|
| 76 | \
|
---|
[aaa3f33a] | 77 | int name##_add(name##_t *, int, type *); \
|
---|
[5fe7692] | 78 | void name##_clear(name##_t *, DTOR_T()); \
|
---|
[aaa3f33a] | 79 | int name##_count(name##_t *); \
|
---|
[5fe7692] | 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()); \
|
---|
[aaa3f33a] | 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 *); \
|
---|
[5fe7692] | 88 | void name##_item_destroy(name##_item_t *, DTOR_T()); \
|
---|
[aaa3f33a] | 89 | int name##_item_is_valid(name##_item_t *);
|
---|
[21580dd] | 90 |
|
---|
| 91 | /** Integer to generic type map implementation.
|
---|
[70ce016] | 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.
|
---|
[21580dd] | 97 | */
|
---|
[70ce016] | 98 | #define INT_MAP_IMPLEMENT(name, type) \
|
---|
[aaa3f33a] | 99 | int name##_add(name##_t *map, int key, type *value) \
|
---|
[70ce016] | 100 | { \
|
---|
| 101 | if (name##_is_valid(map)) { \
|
---|
| 102 | if (map->next == (map->size - 1)) { \
|
---|
[aaa3f33a] | 103 | name##_item_t *tmp; \
|
---|
| 104 | tmp = (name##_item_t *) realloc(map->items, \
|
---|
[70ce016] | 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 | \
|
---|
[5fe7692] | 121 | void name##_clear(name##_t *map, DTOR_T(dtor)) \
|
---|
[70ce016] | 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( \
|
---|
[5fe7692] | 128 | &map->items[index], dtor); \
|
---|
[70ce016] | 129 | } \
|
---|
| 130 | } \
|
---|
| 131 | map->next = 0; \
|
---|
| 132 | map->items[map->next].magic = 0; \
|
---|
| 133 | } \
|
---|
| 134 | } \
|
---|
| 135 | \
|
---|
[aaa3f33a] | 136 | int name##_count(name##_t *map) \
|
---|
[70ce016] | 137 | { \
|
---|
| 138 | return name##_is_valid(map) ? map->next : -1; \
|
---|
| 139 | } \
|
---|
| 140 | \
|
---|
[5fe7692] | 141 | void name##_destroy(name##_t *map, DTOR_T(dtor)) \
|
---|
[70ce016] | 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( \
|
---|
[5fe7692] | 149 | &map->items[index], dtor); \
|
---|
[70ce016] | 150 | } \
|
---|
| 151 | } \
|
---|
| 152 | free(map->items); \
|
---|
| 153 | } \
|
---|
| 154 | } \
|
---|
| 155 | \
|
---|
[5fe7692] | 156 | void name##_exclude(name##_t *map, int key, DTOR_T(dtor)) \
|
---|
[70ce016] | 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( \
|
---|
[5fe7692] | 164 | &map->items[index], dtor); \
|
---|
[70ce016] | 165 | } \
|
---|
| 166 | } \
|
---|
| 167 | } \
|
---|
| 168 | } \
|
---|
| 169 | \
|
---|
[5fe7692] | 170 | void name##_exclude_index(name##_t *map, int index, DTOR_T(dtor)) \
|
---|
[70ce016] | 171 | { \
|
---|
| 172 | if (name##_is_valid(map) && (index >= 0) && \
|
---|
| 173 | (index < map->next) && \
|
---|
| 174 | name##_item_is_valid(&map->items[index])) { \
|
---|
[5fe7692] | 175 | name##_item_destroy(&map->items[index], dtor); \
|
---|
[70ce016] | 176 | } \
|
---|
| 177 | } \
|
---|
| 178 | \
|
---|
[aaa3f33a] | 179 | type *name##_find(name##_t *map, int key) \
|
---|
[70ce016] | 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 | \
|
---|
[aaa3f33a] | 193 | int name##_update(name##_t *map, int key, int new_key) \
|
---|
[70ce016] | 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 | \
|
---|
[aaa3f33a] | 212 | type *name##_get_index(name##_t *map, int index) \
|
---|
[70ce016] | 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 | \
|
---|
[aaa3f33a] | 222 | int name##_initialize(name##_t *map) \
|
---|
[70ce016] | 223 | { \
|
---|
| 224 | if (!map) \
|
---|
| 225 | return EINVAL; \
|
---|
| 226 | map->size = 2; \
|
---|
| 227 | map->next = 0; \
|
---|
[aaa3f33a] | 228 | map->items = (name##_item_t *) malloc(sizeof(name##_item_t) * \
|
---|
[70ce016] | 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 | \
|
---|
[aaa3f33a] | 237 | int name##_is_valid(name##_t *map) \
|
---|
[70ce016] | 238 | { \
|
---|
| 239 | return map && (map->magic == INT_MAP_MAGIC_VALUE); \
|
---|
| 240 | } \
|
---|
| 241 | \
|
---|
[5fe7692] | 242 | void name##_item_destroy(name##_item_t *item, DTOR_T(dtor)) \
|
---|
[70ce016] | 243 | { \
|
---|
| 244 | if (name##_item_is_valid(item)) { \
|
---|
| 245 | item->magic = 0; \
|
---|
| 246 | if (item->value) { \
|
---|
[5fe7692] | 247 | if (dtor) \
|
---|
[93ebe4e] | 248 | dtor(item->value); \
|
---|
[70ce016] | 249 | item->value = NULL; \
|
---|
| 250 | } \
|
---|
| 251 | } \
|
---|
| 252 | } \
|
---|
| 253 | \
|
---|
[aaa3f33a] | 254 | int name##_item_is_valid(name##_item_t *item) \
|
---|
[70ce016] | 255 | { \
|
---|
| 256 | return item && (item->magic == INT_MAP_ITEM_MAGIC_VALUE); \
|
---|
| 257 | }
|
---|
[21580dd] | 258 |
|
---|
| 259 | #endif
|
---|
| 260 |
|
---|
| 261 | /** @}
|
---|
| 262 | */
|
---|
| 263 |
|
---|