added clone and equals to lists

Wed, 04 Jan 2012 14:51:54 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 04 Jan 2012 14:51:54 +0100
changeset 18
69636f81db31
parent 17
2e7050c3a18e
child 19
cdd7a3173249

added clone and equals to lists

test/list_tests.c file | annotate | diff | comparison | revisions
ucx/dlist.c file | annotate | diff | comparison | revisions
ucx/dlist.h file | annotate | diff | comparison | revisions
ucx/list.c file | annotate | diff | comparison | revisions
ucx/list.h file | annotate | diff | comparison | revisions
ucx/ucx.h file | annotate | diff | comparison | revisions
     1.1 --- a/test/list_tests.c	Sat Dec 31 22:48:28 2011 +0100
     1.2 +++ b/test/list_tests.c	Wed Jan 04 14:51:54 2012 +0100
     1.3 @@ -13,20 +13,32 @@
     1.4      int i;
     1.5  };
     1.6  
     1.7 -int list_tests_foreach1(void *v, void *custom) {
     1.8 +int int_cmp(void* e1, void *e2, void *data) {
     1.9 +    if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1;
    1.10 +    
    1.11 +    int *i1 = (int*)e1, *i2 = (int*)e2;
    1.12 +    int r = (*i1) - (*i2);
    1.13 +    return (r < 0) ? -1 : (r == 0 ? 0 : 1);
    1.14 +}
    1.15 +
    1.16 +int dlist_tests_foreach(void *v, void *custom) {
    1.17      UcxDlist *dl = (UcxDlist*)v;
    1.18      struct test1_data *tdata = (struct test1_data*)custom;
    1.19  
    1.20      tdata->values[tdata->i] = *(int*)dl->data;
    1.21      tdata->i++;
    1.22 +    
    1.23 +    return 0;
    1.24  }
    1.25  
    1.26 -int list_tests_foreach2(void *v, void *custom) {
    1.27 +int list_tests_foreach(void *v, void *custom) {
    1.28      UcxList *dl = (UcxList*)v;
    1.29      struct test1_data *tdata = (struct test1_data*)custom;
    1.30  
    1.31      tdata->values[tdata->i] = *(int*)dl->data;
    1.32      tdata->i++;
    1.33 +    
    1.34 +    return 0;
    1.35  }
    1.36  
    1.37  int dlist_tests() {
    1.38 @@ -73,14 +85,45 @@
    1.39  
    1.40      struct test1_data tdata;
    1.41      tdata.i = 0;
    1.42 -    ucx_dlist_foreach(dl, list_tests_foreach1, &tdata);
    1.43 +    ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata);
    1.44  
    1.45      if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
    1.46          fprintf(stderr, "prepend/append test failed\n");
    1.47 -        fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]);
    1.48 +        fprintf(stderr, "content: [%d, %d, %d]\n",
    1.49 +                tdata.values[0], tdata.values[1], tdata.values[2]);
    1.50          r--;
    1.51      }
    1.52  
    1.53 +    printf("   Test ucx_dlist_equals\n");
    1.54 +    UcxDlist *dl2 = NULL;
    1.55 +    dl2 = ucx_dlist_append(dl2, &v[4]);
    1.56 +    dl2 = ucx_dlist_append(dl2, &v[0]);
    1.57 +    dl2 = ucx_dlist_append(dl2, &v[4]);
    1.58 +    if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
    1.59 +        fprintf(stderr, "ucx_dlist_equals failed (false negative)\n");
    1.60 +    }
    1.61 +    dl2->next->data = NULL;
    1.62 +    if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
    1.63 +        fprintf(stderr, "ucx_dlist_equals failed (false positive)\n");
    1.64 +    }
    1.65 +    dl2->next->data = &(tdata.values[1]);
    1.66 +    if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) {
    1.67 +        fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n");
    1.68 +    }
    1.69 +    if (ucx_dlist_equals(dl, dl2, NULL, NULL)) {
    1.70 +        fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n");
    1.71 +    }
    1.72 +    ucx_dlist_free(dl2);
    1.73 +    
    1.74 +    printf("   Test ucx_dlist_clone\n");
    1.75 +    dl2 = ucx_dlist_clone(dl, NULL, NULL);
    1.76 +    if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) {
    1.77 +        fprintf(stderr, "ucx_dlist_clone (without copy) failed\n");
    1.78 +    }
    1.79 +    ucx_dlist_free(dl2);
    1.80 +    
    1.81 +    printf("   TODO: test clone with copy\n");
    1.82 +
    1.83      return r;
    1.84  }
    1.85  
    1.86 @@ -128,13 +171,44 @@
    1.87  
    1.88      struct test1_data tdata;
    1.89      tdata.i = 0;
    1.90 -    ucx_list_foreach(dl, list_tests_foreach1, &tdata);
    1.91 +    ucx_list_foreach(dl, list_tests_foreach, &tdata);
    1.92  
    1.93      if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) {
    1.94          fprintf(stderr, "prepend/append test failed\n");
    1.95 -        fprintf(stderr, "content: [%d, %d, %d]\n", tdata.values[0], tdata.values[1], tdata.values[2]);
    1.96 +        fprintf(stderr, "content: [%d, %d, %d]\n",
    1.97 +                tdata.values[0], tdata.values[1], tdata.values[2]);
    1.98          r--;
    1.99      }
   1.100 +    
   1.101 +    printf("   Test ucx_list_equals\n");
   1.102 +    UcxList *dl2 = NULL;
   1.103 +    dl2 = ucx_list_append(dl2, &v[4]);
   1.104 +    dl2 = ucx_list_append(dl2, &v[0]);
   1.105 +    dl2 = ucx_list_append(dl2, &v[4]);
   1.106 +    if (!ucx_list_equals(dl, dl2, NULL, NULL)) {
   1.107 +        fprintf(stderr, "ucx_list_equals failed (false negative)\n");
   1.108 +    }
   1.109 +    dl2->next->data = NULL;
   1.110 +    if (ucx_list_equals(dl, dl2, NULL, NULL)) {
   1.111 +        fprintf(stderr, "ucx_list_equals failed (false positive)\n");
   1.112 +    }
   1.113 +    dl2->next->data = &(tdata.values[1]);
   1.114 +    if (!ucx_list_equals(dl, dl2, int_cmp, NULL)) {
   1.115 +        fprintf(stderr, "ucx_list_equals failed (cmp_func false negative)\n");
   1.116 +    }
   1.117 +    if (ucx_list_equals(dl, dl2, NULL, NULL)) {
   1.118 +        fprintf(stderr, "ucx_list_equals failed (cmp_func false positive)\n");
   1.119 +    }
   1.120 +    ucx_list_free(dl2);
   1.121 +    
   1.122 +    printf("   Test ucx_list_clone\n");
   1.123 +    dl2 = ucx_list_clone(dl, NULL, NULL);
   1.124 +    if (!ucx_list_equals(dl, dl2, NULL, NULL)) {
   1.125 +        fprintf(stderr, "ucx_list_clone (without copy) failed\n");
   1.126 +    }
   1.127 +    ucx_list_free(dl2);
   1.128 +    
   1.129 +    printf("   TODO: test clone with copy\n");
   1.130  
   1.131      return r;
   1.132  }
     2.1 --- a/ucx/dlist.c	Sat Dec 31 22:48:28 2011 +0100
     2.2 +++ b/ucx/dlist.c	Wed Jan 04 14:51:54 2012 +0100
     2.3 @@ -1,5 +1,34 @@
     2.4  #include "dlist.h"
     2.5  
     2.6 +UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void *data) {
     2.7 +    UcxDlist *ret = NULL;
     2.8 +    while (l != NULL) {
     2.9 +        if (fnc != NULL) {
    2.10 +            ret = ucx_dlist_append(ret, fnc(l->data, data));
    2.11 +        } else {
    2.12 +            ret = ucx_dlist_append(ret, l->data);
    2.13 +        }
    2.14 +        l = l->next;
    2.15 +    }
    2.16 +    return ret;
    2.17 +}
    2.18 +
    2.19 +int ucx_dlist_equals(UcxDlist *l1, UcxDlist *l2, cmp_func fnc, void* data) {
    2.20 +    if (l1 == l2) return 1;
    2.21 +    
    2.22 +    while (l1 != NULL && l2 != NULL) {
    2.23 +        if (fnc == NULL) {
    2.24 +            if (l1->data != l2->data) return 0;
    2.25 +        } else {
    2.26 +            if (fnc(l1->data, l2->data, data) != 0) return 0;
    2.27 +        }
    2.28 +        l1 = l1->next;
    2.29 +        l2 = l2->next;
    2.30 +    }
    2.31 +    
    2.32 +    return (l1 == NULL && l2 == NULL);
    2.33 +}
    2.34 +
    2.35  void ucx_dlist_free(UcxDlist *l) {
    2.36      UcxDlist *e = l, *f;
    2.37      while (e != NULL) {
     3.1 --- a/ucx/dlist.h	Sat Dec 31 22:48:28 2011 +0100
     3.2 +++ b/ucx/dlist.h	Wed Jan 04 14:51:54 2012 +0100
     3.3 @@ -19,6 +19,9 @@
     3.4      UcxDlist *prev;
     3.5  };
     3.6  
     3.7 +UcxDlist *ucx_dlist_clone(UcxDlist *l, copy_func fnc, void* data);
     3.8 +int ucx_dlist_equals(UcxDlist *l1, UcxDlist *l2, cmp_func fnc, void* data);
     3.9 +
    3.10  void ucx_dlist_free(UcxDlist *l);
    3.11  UcxDlist *ucx_dlist_append(UcxDlist *l, void *data);
    3.12  UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data);
     4.1 --- a/ucx/list.c	Sat Dec 31 22:48:28 2011 +0100
     4.2 +++ b/ucx/list.c	Wed Jan 04 14:51:54 2012 +0100
     4.3 @@ -1,5 +1,34 @@
     4.4  #include "list.h"
     4.5  
     4.6 +UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) {
     4.7 +    UcxList *ret = NULL;
     4.8 +    while (l != NULL) {
     4.9 +        if (fnc != NULL) {
    4.10 +            ret = ucx_list_append(ret, fnc(l->data, data));
    4.11 +        } else {
    4.12 +            ret = ucx_list_append(ret, l->data);
    4.13 +        }
    4.14 +        l = l->next;
    4.15 +    }
    4.16 +    return ret;
    4.17 +}
    4.18 +
    4.19 +int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void* data) {
    4.20 +    if (l1 == l2) return 1;
    4.21 +    
    4.22 +    while (l1 != NULL && l2 != NULL) {
    4.23 +        if (fnc == NULL) {
    4.24 +            if (l1->data != l2->data) return 0;
    4.25 +        } else {
    4.26 +            if (fnc(l1->data, l2->data, data) != 0) return 0;
    4.27 +        }
    4.28 +        l1 = l1->next;
    4.29 +        l2 = l2->next;
    4.30 +    }
    4.31 +    
    4.32 +    return (l1 == NULL && l2 == NULL);
    4.33 +}
    4.34 +
    4.35  void ucx_list_free(UcxList *l) {
    4.36      UcxList *e = l, *f;
    4.37      while (e != NULL) {
     5.1 --- a/ucx/list.h	Sat Dec 31 22:48:28 2011 +0100
     5.2 +++ b/ucx/list.h	Wed Jan 04 14:51:54 2012 +0100
     5.3 @@ -18,6 +18,9 @@
     5.4      UcxList *next;
     5.5  };
     5.6  
     5.7 +UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data);
     5.8 +int ucx_list_equals(UcxList *l1, UcxList *l2, cmp_func fnc, void *data);
     5.9 +
    5.10  void ucx_list_free(UcxList *l);
    5.11  UcxList *ucx_list_append(UcxList *l, void *data);
    5.12  UcxList *ucx_list_prepend(UcxList *l, void *data);
     6.1 --- a/ucx/ucx.h	Sat Dec 31 22:48:28 2011 +0100
     6.2 +++ b/ucx/ucx.h	Wed Jan 04 14:51:54 2012 +0100
     6.3 @@ -14,8 +14,15 @@
     6.4  extern "C" {
     6.5  #endif
     6.6  
     6.7 +/* source,data -> errno */
     6.8  typedef int(*ucx_callback)(void*,void*);
     6.9  
    6.10 +/* element1,element2,custom data -> {-1,0,1} */
    6.11 +typedef int(*cmp_func)(void*,void*,void*);
    6.12 +
    6.13 +/* element,custom data -> copy of element */
    6.14 +typedef void*(*copy_func)(void*,void*);
    6.15 +
    6.16  #ifdef	__cplusplus
    6.17  }
    6.18  #endif

mercurial