Overview
Comment: | Add map iteration. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
c339f2040e0abbb62e92b4c2a776ce3a |
User & Date: | js on 2012-04-22 02:34:55 |
Other Links: | manifest | tags |
Context
2012-04-22
| ||
12:50 | Add -Werror and -pipe if when using GCC. check-in: b18f80cc04 user: js tags: trunk | |
02:34 | Add map iteration. check-in: c339f2040e user: js tags: trunk | |
02:34 | Add map. check-in: 1965a6ab9e user: js tags: trunk | |
Changes
Modified src/map.c from [3404c5c5db] to [e42157fde0].
︙ | ︙ | |||
350 351 352 353 354 355 356 357 358 359 360 361 362 363 | if (!resize(map, map->items)) return false; } return true; } static CFWClass class = { .name = "CFWMap", .size = sizeof(CFWMap), .ctor = ctor, .dtor = dtor, .equal = equal, | > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | if (!resize(map, map->items)) return false; } return true; } void cfw_map_iter(CFWMap *map, cfw_map_iter_t *iter) { iter->key = NULL; iter->obj = NULL; iter->_map = map; iter->_pos = 0; } void cfw_map_iter_next(cfw_map_iter_t *iter) { CFWMap *map = iter->_map; for (; iter->_pos < map->size && (map->data[iter->_pos] == NULL || map->data[iter->_pos] == &deleted); iter->_pos++); if (iter->_pos < map->size) { iter->key = map->data[iter->_pos]->key; iter->obj = map->data[iter->_pos]->obj; iter->_pos++; } else { iter->key = NULL; iter->obj = NULL; } } static CFWClass class = { .name = "CFWMap", .size = sizeof(CFWMap), .ctor = ctor, .dtor = dtor, .equal = equal, |
︙ | ︙ |
Modified src/map.h from [e09d13811e] to [1c28d70477].
︙ | ︙ | |||
26 27 28 29 30 31 32 33 34 35 36 37 38 | #ifndef __COREFW_MAP_H__ #define __COREFW_MAP_H__ #include "class.h" typedef struct CFWMap CFWMap; extern CFWClass *cfw_map; extern size_t cfw_map_size(CFWMap*); extern void* cfw_map_get(CFWMap*, void*); extern bool cfw_map_set(CFWMap*, void*, void*); #endif | > > > > > > > > > > | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #ifndef __COREFW_MAP_H__ #define __COREFW_MAP_H__ #include "class.h" typedef struct CFWMap CFWMap; typedef struct cfw_map_iter_t { void *key, *obj; /* private */ CFWMap *_map; uint32_t _pos; } cfw_map_iter_t; extern CFWClass *cfw_map; extern size_t cfw_map_size(CFWMap*); extern void* cfw_map_get(CFWMap*, void*); extern bool cfw_map_set(CFWMap*, void*, void*); extern void cfw_map_iter(CFWMap*, cfw_map_iter_t*); extern void cfw_map_iter_next(cfw_map_iter_t*); #endif |
Modified tests/tests.c from [9dfdb6f41e] to [0544ff8a52].
︙ | ︙ | |||
26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include <stdio.h> #include "object.h" #include "string.h" #include "array.h" #include "map.h" int main() { CFWString *s[3]; CFWArray *a; CFWMap *m; | > > > > > > > > > > > > > > > > > > > > > > > | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <stdio.h> #include "object.h" #include "string.h" #include "array.h" #include "map.h" void print_map(CFWMap *map) { cfw_map_iter_t iter; cfw_map_iter(map, &iter); cfw_map_iter_next(&iter); fputs("{\n", stdout); while (iter.key != NULL) { if (iter.obj != NULL) printf("\t%s = %s\n", cfw_string_c(iter.key), cfw_string_c(iter.obj)); else printf("\t%s = NULL\n", cfw_string_c(iter.key)); cfw_map_iter_next(&iter); } fputs("}\n", stdout); } int main() { CFWString *s[3]; CFWArray *a; CFWMap *m; |
︙ | ︙ | |||
64 65 66 67 68 69 70 | cfw_unref(s[0]); s[0] = cfw_new(cfw_string, "Hallo"); s[1] = cfw_new(cfw_string, "Welt!"); m = cfw_new(cfw_map, s[0], s[1], NULL); | < | | | | 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | cfw_unref(s[0]); s[0] = cfw_new(cfw_string, "Hallo"); s[1] = cfw_new(cfw_string, "Welt!"); m = cfw_new(cfw_map, s[0], s[1], NULL); cfw_unref(s[1]); print_map(m); s[1] = cfw_new(cfw_string, "Test"); cfw_map_set(m, s[0], s[1]); cfw_unref(s[1]); print_map(m); cfw_map_set(m, s[0], NULL); print_map(m); cfw_unref(s[0]); cfw_unref(m); return 0; } |