new map foreach macro

Mon, 15 Jul 2013 14:25:50 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 15 Jul 2013 14:25:50 +0200
changeset 111
c8c59d7f4536
parent 110
1cf71e56f01e
child 112
6384016df2a3

new map foreach macro

test/map_tests.c file | annotate | diff | comparison | revisions
ucx/map.c file | annotate | diff | comparison | revisions
ucx/map.h file | annotate | diff | comparison | revisions
ucx/properties.c file | annotate | diff | comparison | revisions
     1.1 --- a/test/map_tests.c	Mon Jul 15 13:53:51 2013 +0200
     1.2 +++ b/test/map_tests.c	Mon Jul 15 14:25:50 2013 +0200
     1.3 @@ -179,7 +179,7 @@
     1.4      int hit = 0;
     1.5  
     1.6      int* v;
     1.7 -    UCX_MAP_FOREACH(v, i) {
     1.8 +    UCX_MAP_FOREACH(key, v, i) {
     1.9          check += *v;
    1.10          hit++;
    1.11      }
    1.12 @@ -274,7 +274,7 @@
    1.13  
    1.14      void *d;
    1.15      UcxMapIterator iter = ucx_map_iterator(map);
    1.16 -    UCX_MAP_FOREACH(d, iter) {
    1.17 +    UCX_MAP_FOREACH(key, d, iter) {
    1.18          free(d);
    1.19      }
    1.20      ucx_map_free(map);
    1.21 @@ -308,7 +308,7 @@
    1.22      UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
    1.23      UcxMapIterator iter = ucx_map_iterator(map);
    1.24      const char *value; size_t n;
    1.25 -    UCX_MAP_FOREACH(value, iter) {
    1.26 +    UCX_MAP_FOREACH(key, value, iter) {
    1.27          n = strlen(value);
    1.28          UCX_TEST_ASSERT(strncmp((const char*) pool->data[iter.index], value, n),
    1.29                  "values of map does not match pooled values");
     2.1 --- a/ucx/map.c	Mon Jul 15 13:53:51 2013 +0200
     2.2 +++ b/ucx/map.c	Mon Jul 15 14:25:50 2013 +0200
     2.3 @@ -88,7 +88,7 @@
     2.4          copy_func fnc, void *data) {
     2.5      UcxMapIterator i = ucx_map_iterator(from);
     2.6      void *value;
     2.7 -    UCX_MAP_FOREACH(value, i) {
     2.8 +    UCX_MAP_FOREACH(key, value, i) {
     2.9          int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value);
    2.10          if(ret != 0) {
    2.11              return 1;
    2.12 @@ -282,7 +282,7 @@
    2.13      return i;
    2.14  }
    2.15  
    2.16 -int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
    2.17 +int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) {
    2.18      UcxMapElement *e = i->cur;
    2.19      
    2.20      if(e == NULL) {
    2.21 @@ -296,6 +296,7 @@
    2.22              if(e->data != NULL) {
    2.23                  i->cur = e;
    2.24                  *elm = e->data;
    2.25 +                *key = e->key;
    2.26                  return 0;
    2.27              }
    2.28  
    2.29 @@ -402,13 +403,12 @@
    2.30  int ucx_map_store_enc(UcxMap *map, FILE *f,
    2.31          ucx_map_coder encoder, void *encdata) {
    2.32      UcxMapIterator iter = ucx_map_iterator(map);
    2.33 -    char *k, *v;
    2.34 +    char *v;
    2.35      sstr_t key, value;
    2.36      size_t written;
    2.37  
    2.38 -    UCX_MAP_FOREACH(v, iter) {
    2.39 -        k = (char*) iter.cur->key.data;
    2.40 -        key = sstrn(k, iter.cur->key.len);
    2.41 +    UCX_MAP_FOREACH(k, v, iter) {
    2.42 +        key = sstrn(k.data, k.len);
    2.43          if (encoder) {
    2.44              size_t encodedSize;
    2.45              void *encoded = encoder(v, encdata, &encodedSize);
     3.1 --- a/ucx/map.h	Mon Jul 15 13:53:51 2013 +0200
     3.2 +++ b/ucx/map.h	Mon Jul 15 14:25:50 2013 +0200
     3.3 @@ -38,8 +38,8 @@
     3.4  extern "C" {
     3.5  #endif
     3.6  
     3.7 -#define UCX_MAP_FOREACH(elm,iter) \
     3.8 -        for(;ucx_map_iter_next(&iter,(void**)&elm)==0;)
     3.9 +#define UCX_MAP_FOREACH(key,elm,iter) \
    3.10 +        for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&elm)==0;)
    3.11  
    3.12  typedef struct UcxMap          UcxMap;
    3.13  typedef struct UcxKey          UcxKey;
    3.14 @@ -120,7 +120,7 @@
    3.15  
    3.16  UcxMapIterator ucx_map_iterator(UcxMap *map);
    3.17  
    3.18 -int ucx_map_iter_next(UcxMapIterator *i, void **elm);
    3.19 +int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm);
    3.20  
    3.21  /* use macros for string maps only, values are not encoded */
    3.22  #define ucx_map_load(map, f, alloc) ucx_map_load_enc(map, f, alloc, NULL, NULL)
     4.1 --- a/ucx/properties.c	Mon Jul 15 13:53:51 2013 +0200
     4.2 +++ b/ucx/properties.c	Mon Jul 15 14:25:50 2013 +0200
     4.3 @@ -97,7 +97,7 @@
     4.4              parser->tmp = NULL;
     4.5              parser->tmpcap = 0;
     4.6              parser->tmplen = 0;
     4.7 -            // run parse with the tmp buffer as main buffer
     4.8 +            // run ucx_properties_next with the tmp buffer as main buffer
     4.9              int ret = ucx_properties_next(parser, name, value);
    4.10              
    4.11              // restore original buffer
    4.12 @@ -244,13 +244,13 @@
    4.13  
    4.14  int ucx_properties_store(UcxMap *map, FILE *file) {
    4.15      UcxMapIterator iter = ucx_map_iterator(map);
    4.16 -    char *k, *v;
    4.17 +    char *v;
    4.18      sstr_t key, value;
    4.19      size_t written;
    4.20  
    4.21 -    UCX_MAP_FOREACH(v, iter) {
    4.22 -        k = (char*) iter.cur->key.data;
    4.23 -        key = sstrn(k, iter.cur->key.len);
    4.24 +    UCX_MAP_FOREACH(k, v, iter) {
    4.25 +        //k = (char*) iter.cur->key.data;
    4.26 +        key = sstrn(k.data, k.len);
    4.27          value = sstr(v);
    4.28  
    4.29          written = 0;

mercurial