added map iterator

Fri, 25 May 2012 17:39:27 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 25 May 2012 17:39:27 +0200
changeset 31
91ac86557290
parent 30
23bb65cbf7a4
child 32
c7af4ec56e19

added map iterator

test/main.c file | annotate | diff | comparison | revisions
test/map_tests.c file | annotate | diff | comparison | revisions
test/map_tests.h file | annotate | diff | comparison | revisions
ucx/dlist.c file | annotate | diff | comparison | revisions
ucx/list.c file | annotate | diff | comparison | revisions
ucx/map.c file | annotate | diff | comparison | revisions
ucx/map.h file | annotate | diff | comparison | revisions
ucx/string.c file | annotate | diff | comparison | revisions
ucx/string.h file | annotate | diff | comparison | revisions
     1.1 --- a/test/main.c	Fri Feb 24 15:53:50 2012 +0100
     1.2 +++ b/test/main.c	Fri May 25 17:39:27 2012 +0200
     1.3 @@ -111,6 +111,7 @@
     1.4          ucx_test_register(suite, test_ucx_key);
     1.5          ucx_test_register(suite, test_ucx_map_put);
     1.6          ucx_test_register(suite, test_ucx_map_get);
     1.7 +        ucx_test_register(suite, test_ucx_map_iterator);
     1.8          
     1.9          ucx_test_run(suite, stdout);
    1.10          ucx_test_suite_free(suite);
     2.1 --- a/test/map_tests.c	Fri Feb 24 15:53:50 2012 +0100
     2.2 +++ b/test/map_tests.c	Fri May 25 17:39:27 2012 +0200
     2.3 @@ -71,3 +71,54 @@
     2.4  UCX_TEST_BEGIN(test_ucx_map_get) {
     2.5      UCX_TEST_END
     2.6  }
     2.7 +
     2.8 +UCX_TEST_BEGIN(test_ucx_map_iterator) {
     2.9 +    UcxMap *map = ucx_map_new(16);
    2.10 +    
    2.11 +    int v1 = 10;
    2.12 +    int v2 = 15;
    2.13 +    int v3 = 7;
    2.14 +    int v4 = 9;
    2.15 +    
    2.16 +    ucx_map_cstr_put(map, "v1", &v1);
    2.17 +    ucx_map_cstr_put(map, "v2", &v2);
    2.18 +    ucx_map_cstr_put(map, "v3", &v3);
    2.19 +    ucx_map_cstr_put(map, "v4", &v4);
    2.20 +    
    2.21 +    UcxMapIterator i = ucx_map_iterator(map);
    2.22 +    int check = 0;
    2.23 +    int hit = 0;
    2.24 +    
    2.25 +    UCX_MAP_FOREACH(int*, v, map, i) {
    2.26 +        check += *v;
    2.27 +        hit++;
    2.28 +    }
    2.29 +    
    2.30 +    UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
    2.31 +    UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
    2.32 +            
    2.33 +    ucx_map_free(map);
    2.34 +    
    2.35 +    map = ucx_map_new(1);
    2.36 +    ucx_map_cstr_put(map, "v1", &v1);
    2.37 +    ucx_map_cstr_put(map, "v2", &v2);
    2.38 +    ucx_map_cstr_put(map, "v3", &v3);
    2.39 +    ucx_map_cstr_put(map, "v4", &v4);
    2.40 +    
    2.41 +    i = ucx_map_iterator(map);
    2.42 +    check = 0;
    2.43 +    hit = 0;
    2.44 +    
    2.45 +    UCX_MAP_FOREACH(int*, v, map, i) {
    2.46 +        check += *v;
    2.47 +        hit++;
    2.48 +    }
    2.49 +    
    2.50 +    UCX_TEST_ASSERT(hit == 4, "test2: wrong number of hits");
    2.51 +    UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test2: wrong result");
    2.52 +    
    2.53 +    
    2.54 +    ucx_map_free(map);
    2.55 +    
    2.56 +    UCX_TEST_END
    2.57 +}
     3.1 --- a/test/map_tests.h	Fri Feb 24 15:53:50 2012 +0100
     3.2 +++ b/test/map_tests.h	Fri May 25 17:39:27 2012 +0200
     3.3 @@ -16,6 +16,7 @@
     3.4  UCX_TEST_DECLARE(test_ucx_key)
     3.5  UCX_TEST_DECLARE(test_ucx_map_put)
     3.6  UCX_TEST_DECLARE(test_ucx_map_get)
     3.7 +UCX_TEST_DECLARE(test_ucx_map_iterator)
     3.8  
     3.9  
    3.10  #ifdef	__cplusplus
     4.1 --- a/ucx/dlist.c	Fri Feb 24 15:53:50 2012 +0100
     4.2 +++ b/ucx/dlist.c	Fri May 25 17:39:27 2012 +0200
     4.3 @@ -125,8 +125,13 @@
     4.4  
     4.5  UcxDlist *ucx_dlist_remove(UcxDlist *l, UcxDlist *e) {
     4.6      if (e->prev == NULL) {
     4.7 -        e->next->prev = NULL;
     4.8 -        l = e->next;
     4.9 +        if(e->next != NULL) {
    4.10 +            e->next->prev = NULL;
    4.11 +            l = e->next;
    4.12 +        } else {
    4.13 +            l = NULL;
    4.14 +        }
    4.15 +        
    4.16      } else {
    4.17          e->prev->next = e->next;
    4.18          e->next->prev = e->prev;
     5.1 --- a/ucx/list.c	Fri Feb 24 15:53:50 2012 +0100
     5.2 +++ b/ucx/list.c	Fri May 25 17:39:27 2012 +0200
     5.3 @@ -118,7 +118,7 @@
     5.4          while (f->next != NULL && f->next != e) {
     5.5              f = f->next;
     5.6          }
     5.7 -        /* perform remove iff this element is found in this list */
     5.8 +        /* perform remove if this element is found in this list */
     5.9          if (f->next == e) {
    5.10              f->next = e->next;
    5.11              free(e);
     6.1 --- a/ucx/map.c	Fri Feb 24 15:53:50 2012 +0100
     6.2 +++ b/ucx/map.c	Fri May 25 17:39:27 2012 +0200
     6.3 @@ -148,3 +148,41 @@
     6.4  
     6.5      return h;
     6.6  }
     6.7 +
     6.8 +UcxMapIterator ucx_map_iterator(UcxMap *map) {
     6.9 +    UcxMapIterator i;
    6.10 +    i.map = map;
    6.11 +    i.cur = NULL;
    6.12 +    i.index = 0;
    6.13 +    return i;
    6.14 +}
    6.15 +
    6.16 +int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
    6.17 +    UcxMapElement *e = i->cur;
    6.18 +    
    6.19 +    if(e == NULL) {
    6.20 +        e = i->map->map[0];
    6.21 +    } else {
    6.22 +        e = e->next;
    6.23 +    }
    6.24 +    
    6.25 +    while(i->index < i->map->size) {
    6.26 +        if(e != NULL) {
    6.27 +            if(e->data != NULL) {
    6.28 +                i->cur = e;
    6.29 +                *elm = e->data;
    6.30 +                return 0;
    6.31 +            }
    6.32 +
    6.33 +            e = e->next;
    6.34 +        } else {
    6.35 +            i->index++;
    6.36 +            
    6.37 +            if(i->index < i->map->size) {
    6.38 +                e = i->map->map[i->index];
    6.39 +            }
    6.40 +        }
    6.41 +    }
    6.42 +    
    6.43 +    return 1;
    6.44 +}
     7.1 --- a/ucx/map.h	Fri Feb 24 15:53:50 2012 +0100
     7.2 +++ b/ucx/map.h	Fri May 25 17:39:27 2012 +0200
     7.3 @@ -12,9 +12,13 @@
     7.4  extern "C" {
     7.5  #endif
     7.6  
     7.7 -typedef struct UcxMap        UcxMap;
     7.8 -typedef struct UcxKey        UcxKey;
     7.9 -typedef struct UcxMapElement UcxMapElement;
    7.10 +#define UCX_MAP_FOREACH(type,elm,map,iter) \
    7.11 +        for(type elm;ucx_map_iter_next(&iter,(void*)&elm)==0;)
    7.12 +
    7.13 +typedef struct UcxMap          UcxMap;
    7.14 +typedef struct UcxKey          UcxKey;
    7.15 +typedef struct UcxMapElement   UcxMapElement;
    7.16 +typedef struct UcxMapIterator  UcxMapIterator;
    7.17  
    7.18  struct UcxMap {
    7.19      UcxMapElement **map;
    7.20 @@ -33,6 +37,12 @@
    7.21      UcxKey        key;
    7.22  };
    7.23  
    7.24 +struct UcxMapIterator {
    7.25 +    UcxMap        *map;
    7.26 +    UcxMapElement *cur;
    7.27 +    int           index;
    7.28 +};
    7.29 +
    7.30  
    7.31  UcxMap *ucx_map_new(size_t size);
    7.32  void ucx_map_free(UcxMap *map);
    7.33 @@ -49,6 +59,10 @@
    7.34  
    7.35  int ucx_hash(char *data, size_t len);
    7.36  
    7.37 +UcxMapIterator ucx_map_iterator(UcxMap *map);
    7.38 +
    7.39 +int ucx_map_iter_next(UcxMapIterator *i, void **elm);
    7.40 +
    7.41  #ifdef	__cplusplus
    7.42  }
    7.43  #endif
     8.1 --- a/ucx/string.c	Fri Feb 24 15:53:50 2012 +0100
     8.2 +++ b/ucx/string.c	Fri May 25 17:39:27 2012 +0200
     8.3 @@ -90,7 +90,7 @@
     8.4      return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length);
     8.5  }
     8.6  
     8.7 -sstr_t sstrdub(sstr_t s) {
     8.8 +sstr_t sstrdup(sstr_t s) {
     8.9      sstr_t newstring;
    8.10      newstring.ptr = (char*) malloc(s.length + 1);
    8.11      if (newstring.ptr != NULL) {
     9.1 --- a/ucx/string.h	Fri Feb 24 15:53:50 2012 +0100
     9.2 +++ b/ucx/string.h	Fri May 25 17:39:27 2012 +0200
     9.3 @@ -69,7 +69,7 @@
     9.4  
     9.5  int sstrcmp(sstr_t s1, sstr_t s2);
     9.6  
     9.7 -sstr_t sstrdub(sstr_t s);
     9.8 +sstr_t sstrdup(sstr_t s);
     9.9  
    9.10  #ifdef	__cplusplus
    9.11  }

mercurial