Changeset b5e68c8 in mainline for uspace/lib/c/include/adt/int_map.h
- Timestamp:
- 2011-05-12T16:49:44Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f36787d7
- Parents:
- e80329d6 (diff), 750636a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/include/adt/int_map.h
re80329d6 rb5e68c8 49 49 #define INT_MAP_ITEM_MAGIC_VALUE 0x55667788 50 50 51 /** Generic destructor function pointer. */ 52 #define DTOR_T(identifier) \ 53 void (*identifier)(const void *) 54 51 55 /** Integer to generic type map declaration. 52 56 * … … 56 60 #define INT_MAP_DECLARE(name, type) \ 57 61 typedef struct name name##_t; \ 58 typedef name##_t *name##_ref; \59 62 typedef struct name##_item name##_item_t; \ 60 typedef name##_item_t *name##_item_ref; \61 63 \ 62 64 struct name##_item { \ … … 69 71 int size; \ 70 72 int next; \ 71 name##_item_ refitems; \73 name##_item_t *items; \ 72 74 int magic; \ 73 75 }; \ 74 76 \ 75 int name##_add(name##_ ref, int, type *); \76 void name##_clear(name##_ ref); \77 int name##_count(name##_ ref); \78 void name##_destroy(name##_ ref); \79 void name##_exclude(name##_ ref, int); \80 void name##_exclude_index(name##_ ref, int); \81 type *name##_find(name##_ ref, int); \82 int name##_update(name##_ ref, int, int); \83 type *name##_get_index(name##_ ref, int); \84 int name##_initialize(name##_ ref); \85 int name##_is_valid(name##_ ref); \86 void name##_item_destroy(name##_item_ ref); \87 int name##_item_is_valid(name##_item_ ref);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 *); 88 90 89 91 /** Integer to generic type map implementation. … … 95 97 */ 96 98 #define INT_MAP_IMPLEMENT(name, type) \ 97 int name##_add(name##_ refmap, int key, type *value) \99 int name##_add(name##_t *map, int key, type *value) \ 98 100 { \ 99 101 if (name##_is_valid(map)) { \ 100 102 if (map->next == (map->size - 1)) { \ 101 name##_item_ reftmp; \102 tmp = (name##_item_ ref) realloc(map->items, \103 name##_item_t *tmp; \ 104 tmp = (name##_item_t *) realloc(map->items, \ 103 105 sizeof(name##_item_t) * 2 * map->size); \ 104 106 if (!tmp) \ … … 117 119 } \ 118 120 \ 119 void name##_clear(name##_ ref map) \121 void name##_clear(name##_t *map, DTOR_T(dtor)) \ 120 122 { \ 121 123 if (name##_is_valid(map)) { \ … … 124 126 if (name##_item_is_valid(&map->items[index])) { \ 125 127 name##_item_destroy( \ 126 &map->items[index] ); \128 &map->items[index], dtor); \ 127 129 } \ 128 130 } \ … … 132 134 } \ 133 135 \ 134 int name##_count(name##_ refmap) \136 int name##_count(name##_t *map) \ 135 137 { \ 136 138 return name##_is_valid(map) ? map->next : -1; \ 137 139 } \ 138 140 \ 139 void name##_destroy(name##_ ref map) \141 void name##_destroy(name##_t *map, DTOR_T(dtor)) \ 140 142 { \ 141 143 if (name##_is_valid(map)) { \ … … 145 147 if (name##_item_is_valid(&map->items[index])) { \ 146 148 name##_item_destroy( \ 147 &map->items[index] ); \149 &map->items[index], dtor); \ 148 150 } \ 149 151 } \ … … 152 154 } \ 153 155 \ 154 void name##_exclude(name##_ ref map, int key) \156 void name##_exclude(name##_t *map, int key, DTOR_T(dtor)) \ 155 157 { \ 156 158 if (name##_is_valid(map)) { \ … … 160 162 (map->items[index].key == key)) { \ 161 163 name##_item_destroy( \ 162 &map->items[index] ); \163 } \ 164 } \ 165 } \ 166 } \ 167 \ 168 void name##_exclude_index(name##_ ref map, int index) \164 &map->items[index], dtor); \ 165 } \ 166 } \ 167 } \ 168 } \ 169 \ 170 void name##_exclude_index(name##_t *map, int index, DTOR_T(dtor)) \ 169 171 { \ 170 172 if (name##_is_valid(map) && (index >= 0) && \ 171 173 (index < map->next) && \ 172 174 name##_item_is_valid(&map->items[index])) { \ 173 name##_item_destroy(&map->items[index] ); \174 } \ 175 } \ 176 \ 177 type *name##_find(name##_ refmap, int key) \175 name##_item_destroy(&map->items[index], dtor); \ 176 } \ 177 } \ 178 \ 179 type *name##_find(name##_t *map, int key) \ 178 180 { \ 179 181 if (name##_is_valid(map)) { \ … … 189 191 } \ 190 192 \ 191 int name##_update(name##_ refmap, int key, int new_key) \193 int name##_update(name##_t *map, int key, int new_key) \ 192 194 { \ 193 195 if (name##_is_valid(map)) { \ … … 208 210 } \ 209 211 \ 210 type *name##_get_index(name##_ refmap, int index) \212 type *name##_get_index(name##_t *map, int index) \ 211 213 { \ 212 214 if (name##_is_valid(map) && (index >= 0) && \ … … 218 220 } \ 219 221 \ 220 int name##_initialize(name##_ refmap) \222 int name##_initialize(name##_t *map) \ 221 223 { \ 222 224 if (!map) \ … … 224 226 map->size = 2; \ 225 227 map->next = 0; \ 226 map->items = (name##_item_ ref) malloc(sizeof(name##_item_t) * \228 map->items = (name##_item_t *) malloc(sizeof(name##_item_t) * \ 227 229 map->size); \ 228 230 if (!map->items) \ … … 233 235 } \ 234 236 \ 235 int name##_is_valid(name##_ refmap) \237 int name##_is_valid(name##_t *map) \ 236 238 { \ 237 239 return map && (map->magic == INT_MAP_MAGIC_VALUE); \ 238 240 } \ 239 241 \ 240 void name##_item_destroy(name##_item_ ref item) \242 void name##_item_destroy(name##_item_t *item, DTOR_T(dtor)) \ 241 243 { \ 242 244 if (name##_item_is_valid(item)) { \ 243 245 item->magic = 0; \ 244 246 if (item->value) { \ 245 free(item->value); \ 247 if (dtor) \ 248 dtor(item->value); \ 246 249 item->value = NULL; \ 247 250 } \ … … 249 252 } \ 250 253 \ 251 int name##_item_is_valid(name##_item_ refitem) \254 int name##_item_is_valid(name##_item_t *item) \ 252 255 { \ 253 256 return item && (item->magic == INT_MAP_ITEM_MAGIC_VALUE); \
Note:
See TracChangeset
for help on using the changeset viewer.