# HG changeset patch # User Mike Becker # Date 1431880361 -7200 # Node ID 0c1b7676e74c61b2077b102a9ed7e85a34c466d1 # Parent fb05d315a0ba3398a8bf56463c3bbceb60470f95 finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp() diff -r fb05d315a0ba -r 0c1b7676e74c ucx/avl.c --- a/ucx/avl.c Sun May 17 17:59:07 2015 +0200 +++ b/ucx/avl.c Sun May 17 18:32:41 2015 +0200 @@ -28,3 +28,31 @@ #include "avl.h" +UcxAVLTree *ucx_avl_new(cmp_func cmpfunc) { + return ucx_avl_new_a(cmpfunc, ucx_default_allocator()); +} + +UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator) { + UcxAVLTree *tree = malloc(sizeof(UcxAVLTree)); + if (tree) { + tree->allocator = allocator; + tree->cmpfunc = cmpfunc; + tree->root = NULL; + tree->userdata = NULL; + } + + return tree; +} + +void *ucx_avl_get(UcxAVLTree *tree, intptr_t key) { + return NULL; +} + +void* ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value) { + return NULL; +} + +void* ucx_avl_remove(UcxAVLTree *tree, intptr_t key) { + return NULL; +} + diff -r fb05d315a0ba -r 0c1b7676e74c ucx/avl.h --- a/ucx/avl.h Sun May 17 17:59:07 2015 +0200 +++ b/ucx/avl.h Sun May 17 18:32:41 2015 +0200 @@ -40,10 +40,11 @@ */ #ifndef UCX_AVL_H -#define UCX_AVL_H +#define UCX_AVL_H #include "ucx.h" - +#include "allocator.h" +#include #ifdef __cplusplus extern "C" { @@ -63,7 +64,7 @@ /** * The key for this node. */ - void *key; + intptr_t key; /** * Data contained by this node. */ @@ -91,6 +92,10 @@ */ typedef struct { /** + * The UcxAllocator that shall be used to manage the memory for node data. + */ + UcxAllocator *allocator; + /** * Root node of the tree. */ UcxAVLNode *root; @@ -107,12 +112,42 @@ } UcxAVLTree; /** + * Initializes a new UcxAVLTree with a default allocator. + * + * @param cmpfunc the compare function that shall be used + * @return a new UcxAVLTree object + * @see ucx_avl_new_a() + */ +UcxAVLTree *ucx_avl_new(cmp_func cmpfunc); + +/** + * Initializes a new UcxAVLTree with the specified allocator. + * + * The cmpfunc should be capable of comparing two keys within this AVL tree. + * So if you want to use null terminated strings as keys, you could use the + * ucx_strcmp() function here. + * + * @param cmpfunc the compare function that shall be used + * @param allocator the UcxAllocator that shall be used + * @return a new UcxAVLTree object + */ +UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator); + +/** + * Macro for initializing a new UcxAVLTree with the default allocator and a + * ucx_ptrcmp() compare function. + * + * @return a new default UcxAVLTree object + */ +#define ucx_avl_default_new() ucx_avl_new_a(ucx_ptrcmp, ucx_default_allocator()) + +/** * Gets the value from the tree, that is associated with the specified key. * @param tree the UcxAVLTree * @param key the key * @return the value (or NULL, if the key is not present) */ -void *ucx_avl_get(UcxAVLTree *tree, void *key); +void *ucx_avl_get(UcxAVLTree *tree, intptr_t key); /** * Puts a key/value pair into the tree. @@ -122,7 +157,7 @@ * @return the replaced value (or NULL, if the key is new to the * tree) */ -void* ucx_avl_put(UcxAVLTree *tree, void *key, void *value); +void* ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value); /** * Removes an element from the AVL tree. @@ -130,7 +165,7 @@ * @param key the key * @return the removed value (or NULL, if the key was not present) */ -void* ucx_avl_remove(UcxAVLTree *tree, void *key); +void* ucx_avl_remove(UcxAVLTree *tree, intptr_t key); diff -r fb05d315a0ba -r 0c1b7676e74c ucx/utils.c --- a/ucx/utils.c Sun May 17 17:59:07 2015 +0200 +++ b/ucx/utils.c Sun May 17 18:32:41 2015 +0200 @@ -128,10 +128,12 @@ } int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { - if (ptr1 == ptr2) { + intptr_t p1 = (intptr_t) ptr1; + intptr_t p2 = (intptr_t) ptr2; + if (p1 == p2) { return 0; } else { - return ptr1 < ptr2 ? -1 : 1; + return p1 < p2 ? -1 : 1; } }