# HG changeset patch # User Mike Becker # Date 1361962109 -3600 # Node ID 57ea041df22f713d2e9605d86da09ba888bd56cb # Parent a6a99e7216608b61453390efb828ee3003500d5e renamed comparator to utils module and added copy functions diff -r a6a99e721660 -r 57ea041df22f Makefile --- a/Makefile Wed Feb 27 11:37:27 2013 +0100 +++ b/Makefile Wed Feb 27 11:48:29 2013 +0100 @@ -54,6 +54,7 @@ clean: FORCE $(RM) $(RMFLAGS) build/*.${OBJ_EXT} + $(RM) $(RMFLAGS) build/*.${LIB_EXT} FORCE: diff -r a6a99e721660 -r 57ea041df22f test/dlist_tests.c --- a/test/dlist_tests.c Wed Feb 27 11:37:27 2013 +0100 +++ b/test/dlist_tests.c Wed Feb 27 11:48:29 2013 +0100 @@ -3,7 +3,7 @@ */ #include "dlist_tests.h" -#include "ucx/comparator.h" +#include "ucx/utils.h" UCX_TEST_IMPLEMENT(test_ucx_dlist_append) { UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello"); @@ -171,7 +171,7 @@ UcxDlist *list = ucx_dlist_append(NULL, hello); list = ucx_dlist_append(list, world); - UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL); + UcxDlist *copy = ucx_dlist_clone(list, ucx_strcpy, NULL); UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, ucx_strcmp, NULL), "failed"); diff -r a6a99e721660 -r 57ea041df22f test/list_tests.c --- a/test/list_tests.c Wed Feb 27 11:37:27 2013 +0100 +++ b/test/list_tests.c Wed Feb 27 11:48:29 2013 +0100 @@ -3,7 +3,7 @@ */ #include "list_tests.h" -#include "ucx/comparator.h" +#include "ucx/utils.h" UCX_TEST_IMPLEMENT(test_ucx_list_append) { UcxList *list = ucx_list_append(NULL, (void*)"Hello"); @@ -159,7 +159,7 @@ UcxList *list = ucx_list_append(NULL, hello); list = ucx_list_append(list, world); - UcxList *copy = ucx_list_clone(list, copy_string, NULL); + UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL); UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed"); diff -r a6a99e721660 -r 57ea041df22f test/main.c --- a/test/main.c Wed Feb 27 11:37:27 2013 +0100 +++ b/test/main.c Wed Feb 27 11:48:29 2013 +0100 @@ -41,14 +41,6 @@ #include "map_tests.h" #include "buffer_tests.h" -void* copy_string(void* e, void* data) { - char *str = (char*) e; - size_t n = 1+strlen(str); - char *cpy = (char*) malloc(n); - memcpy(cpy, str, n); - return cpy; -} - UCX_TEST_IMPLEMENT(testTestSuitePositive) { UCX_TEST_BEGIN UCX_TEST_ASSERT(2*2 == 4, "the test framework fails"); diff -r a6a99e721660 -r 57ea041df22f ucx/Makefile --- a/ucx/Makefile Wed Feb 27 11:37:27 2013 +0100 +++ b/ucx/Makefile Wed Feb 27 11:48:29 2013 +0100 @@ -29,7 +29,7 @@ include ../$(CONF).mk # list of source files -SRC = comparator.c +SRC = utils.c SRC += list.c SRC += dlist.c SRC += map.c diff -r a6a99e721660 -r 57ea041df22f ucx/comparator.c --- a/ucx/comparator.c Wed Feb 27 11:37:27 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -#include "comparator.h" -#include "math.h" - -int ucx_strcmp(void *s1, void *s2, void *data) { - return strcmp((char*)s1, (char*)s2); -} - -int ucx_strncmp(void *s1, void *s2, void *n) { - return strncmp((char*)s1, (char*)s2, *((size_t*) n)); -} - -int ucx_intcmp(void *i1, void *i2, void *data) { - int a = *((int*) i1); - int b = *((int*) i2); - if (a == b) { - return 0; - } else { - return a < b ? -1 : 1; - } -} - -int ucx_floatcmp(void *f1, void *f2, void *epsilon) { - float a = *((float*) f1); - float b = *((float*) f2); - float e = !epsilon ? 1e-6f : *((float*)epsilon); - if (fabsf(a - b) < e) { - return 0; - } else { - return a < b ? -1 : 1; - } -} - -int ucx_doublecmp(void *d1, void *d2, void *epsilon) { - double a = *((float*) d1); - double b = *((float*) d2); - double e = !epsilon ? 1e-14 : *((double*)epsilon); - if (fabs(a - b) < e) { - return 0; - } else { - return a < b ? -1 : 1; - } -} - -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { - if (ptr1 == ptr2) { - return 0; - } else { - return ptr1 < ptr2 ? -1 : 1; - } -} - -int ucx_memcmp(void *ptr1, void *ptr2, void *n) { - return memcmp(ptr1, ptr2, *((size_t*)n)); -} diff -r a6a99e721660 -r 57ea041df22f ucx/comparator.h --- a/ucx/comparator.h Wed Feb 27 11:37:27 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ -#ifndef COMPARATOR_H -#define COMPARATOR_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "ucx.h" -#include - -/** - * Wraps the strcmp function. - * @param s1 string one - * @param s2 string two - * @param data omitted - * @return the result of strcmp(s1, s2) - */ -int ucx_strcmp(void *s1, void *s2, void *data); - -/** - * Wraps the strncmp function. - * @param s1 string one - * @param s2 string two - * @param n a pointer to the size_t containing the third strncmp parameter - * @return the result of strncmp(s1, s2, *n) - */ -int ucx_strncmp(void *s1, void *s2, void *n); - -/** - * Compares two integers of type int. - * @param i1 pointer to integer one - * @param i2 pointer to integer two - * @param data omitted - * @return -1, if *i1 is less than *i2, 0 if both are equal, - * 1 if *i1 is greater than *i2 - */ - -int ucx_intcmp(void *i1, void *i2, void *data); - -/** - * Compares two real numbers of type float. - * @param f1 pointer to float one - * @param f2 pointer to float two - * @param if provided: a pointer to precision (default: 1e-6f) - * @return -1, if *f1 is less than *f2, 0 if both are equal, - * 1 if *f1 is greater than *f2 - */ - -int ucx_floatcmp(void *f1, void *f2, void *data); - -/** - * Compares two real numbers of type double. - * @param f1 pointer to double one - * @param f2 pointer to double two -* @param if provided: a pointer to precision (default: 1e-14) - * @return -1, if *d1 is less than *d2, 0 if both are equal, - * 1 if *d1 is greater than *d2 - */ - -int ucx_doublecmp(void *d1, void *d2, void *data); - -/** - * Compares two pointers. - * @param ptr1 pointer one - * @param ptr2 pointer two - * @param data omitted - * @return -1 if ptr1 is less than ptr2, 0 if both are equal, - * 1 if ptr1 is greater than ptr2 - */ -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); - -/** - * Compares two memory areas. - * @param ptr1 pointer one - * @param ptr2 pointer two - * @param n a pointer to the size_t containing the third parameter for memcmp - * @return the result of memcmp(ptr1, ptr2, *n) - */ -int ucx_memcmp(void *ptr1, void *ptr2, void *n); - -#ifdef __cplusplus -} -#endif - -#endif /* COMPARATOR_H */ - diff -r a6a99e721660 -r 57ea041df22f ucx/utils.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ucx/utils.c Wed Feb 27 11:48:29 2013 +0100 @@ -0,0 +1,72 @@ +#include "utils.h" +#include "math.h" + +/* COPY FUCNTIONS */ +void* ucx_strcpy(void* s, void* data) { + char *str = (char*) s; + size_t n = 1+strlen(str); + char *cpy = (char*) malloc(n); + memcpy(cpy, str, n); + return cpy; +} + +void* ucx_memcpy(void* m, void* n) { + size_t k = *((size_t*)n); + void *cpy = malloc(k); + memcpy(cpy, m, k); + return cpy; +} + +/* COMPARE FUNCTION */ + +int ucx_strcmp(void *s1, void *s2, void *data) { + return strcmp((char*)s1, (char*)s2); +} + +int ucx_strncmp(void *s1, void *s2, void *n) { + return strncmp((char*)s1, (char*)s2, *((size_t*) n)); +} + +int ucx_intcmp(void *i1, void *i2, void *data) { + int a = *((int*) i1); + int b = *((int*) i2); + if (a == b) { + return 0; + } else { + return a < b ? -1 : 1; + } +} + +int ucx_floatcmp(void *f1, void *f2, void *epsilon) { + float a = *((float*) f1); + float b = *((float*) f2); + float e = !epsilon ? 1e-6f : *((float*)epsilon); + if (fabsf(a - b) < e) { + return 0; + } else { + return a < b ? -1 : 1; + } +} + +int ucx_doublecmp(void *d1, void *d2, void *epsilon) { + double a = *((float*) d1); + double b = *((float*) d2); + double e = !epsilon ? 1e-14 : *((double*)epsilon); + if (fabs(a - b) < e) { + return 0; + } else { + return a < b ? -1 : 1; + } +} + +int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { + if (ptr1 == ptr2) { + return 0; + } else { + return ptr1 < ptr2 ? -1 : 1; + } +} + +int ucx_memcmp(void *ptr1, void *ptr2, void *n) { + return memcmp(ptr1, ptr2, *((size_t*)n)); +} diff -r a6a99e721660 -r 57ea041df22f ucx/utils.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ucx/utils.h Wed Feb 27 11:48:29 2013 +0100 @@ -0,0 +1,103 @@ +#ifndef COMPARATOR_H +#define COMPARATOR_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ucx.h" +#include + +/** + * Copies a string. + * @param s the string to copy + * @param data omitted + * @return a pointer to a copy of s1 that can be passed to free(void*) + */ +void *ucx_strcpy(void *s, void *data); + +/** + * Copies a memory area. + * @param m a pointer to the memory area + * @param n a pointer to the size_t containing the size of the memory area + * @return a pointer to a copy of the specified memory area that can + * be passed to free(void*) + */ +void *ucx_memcpy(void *m, void *n); + +/** + * Wraps the strcmp function. + * @param s1 string one + * @param s2 string two + * @param data omitted + * @return the result of strcmp(s1, s2) + */ +int ucx_strcmp(void *s1, void *s2, void *data); + +/** + * Wraps the strncmp function. + * @param s1 string one + * @param s2 string two + * @param n a pointer to the size_t containing the third strncmp parameter + * @return the result of strncmp(s1, s2, *n) + */ +int ucx_strncmp(void *s1, void *s2, void *n); + +/** + * Compares two integers of type int. + * @param i1 pointer to integer one + * @param i2 pointer to integer two + * @param data omitted + * @return -1, if *i1 is less than *i2, 0 if both are equal, + * 1 if *i1 is greater than *i2 + */ + +int ucx_intcmp(void *i1, void *i2, void *data); + +/** + * Compares two real numbers of type float. + * @param f1 pointer to float one + * @param f2 pointer to float two + * @param if provided: a pointer to precision (default: 1e-6f) + * @return -1, if *f1 is less than *f2, 0 if both are equal, + * 1 if *f1 is greater than *f2 + */ + +int ucx_floatcmp(void *f1, void *f2, void *data); + +/** + * Compares two real numbers of type double. + * @param f1 pointer to double one + * @param f2 pointer to double two +* @param if provided: a pointer to precision (default: 1e-14) + * @return -1, if *d1 is less than *d2, 0 if both are equal, + * 1 if *d1 is greater than *d2 + */ + +int ucx_doublecmp(void *d1, void *d2, void *data); + +/** + * Compares two pointers. + * @param ptr1 pointer one + * @param ptr2 pointer two + * @param data omitted + * @return -1 if ptr1 is less than ptr2, 0 if both are equal, + * 1 if ptr1 is greater than ptr2 + */ +int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); + +/** + * Compares two memory areas. + * @param ptr1 pointer one + * @param ptr2 pointer two + * @param n a pointer to the size_t containing the third parameter for memcmp + * @return the result of memcmp(ptr1, ptr2, *n) + */ +int ucx_memcmp(void *ptr1, void *ptr2, void *n); + +#ifdef __cplusplus +} +#endif + +#endif /* COMPARATOR_H */ +