Sun, 17 May 2015 18:32:41 +0200
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
ucx/avl.c | file | annotate | diff | comparison | revisions | |
ucx/avl.h | file | annotate | diff | comparison | revisions | |
ucx/utils.c | file | annotate | diff | comparison | revisions |
1.1 --- a/ucx/avl.c Sun May 17 17:59:07 2015 +0200 1.2 +++ b/ucx/avl.c Sun May 17 18:32:41 2015 +0200 1.3 @@ -28,3 +28,31 @@ 1.4 1.5 #include "avl.h" 1.6 1.7 +UcxAVLTree *ucx_avl_new(cmp_func cmpfunc) { 1.8 + return ucx_avl_new_a(cmpfunc, ucx_default_allocator()); 1.9 +} 1.10 + 1.11 +UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator) { 1.12 + UcxAVLTree *tree = malloc(sizeof(UcxAVLTree)); 1.13 + if (tree) { 1.14 + tree->allocator = allocator; 1.15 + tree->cmpfunc = cmpfunc; 1.16 + tree->root = NULL; 1.17 + tree->userdata = NULL; 1.18 + } 1.19 + 1.20 + return tree; 1.21 +} 1.22 + 1.23 +void *ucx_avl_get(UcxAVLTree *tree, intptr_t key) { 1.24 + return NULL; 1.25 +} 1.26 + 1.27 +void* ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value) { 1.28 + return NULL; 1.29 +} 1.30 + 1.31 +void* ucx_avl_remove(UcxAVLTree *tree, intptr_t key) { 1.32 + return NULL; 1.33 +} 1.34 +
2.1 --- a/ucx/avl.h Sun May 17 17:59:07 2015 +0200 2.2 +++ b/ucx/avl.h Sun May 17 18:32:41 2015 +0200 2.3 @@ -40,10 +40,11 @@ 2.4 */ 2.5 2.6 #ifndef UCX_AVL_H 2.7 -#define UCX_AVL_H 2.8 +#define UCX_AVL_H 2.9 2.10 #include "ucx.h" 2.11 - 2.12 +#include "allocator.h" 2.13 +#include <stdint.h> 2.14 2.15 #ifdef __cplusplus 2.16 extern "C" { 2.17 @@ -63,7 +64,7 @@ 2.18 /** 2.19 * The key for this node. 2.20 */ 2.21 - void *key; 2.22 + intptr_t key; 2.23 /** 2.24 * Data contained by this node. 2.25 */ 2.26 @@ -91,6 +92,10 @@ 2.27 */ 2.28 typedef struct { 2.29 /** 2.30 + * The UcxAllocator that shall be used to manage the memory for node data. 2.31 + */ 2.32 + UcxAllocator *allocator; 2.33 + /** 2.34 * Root node of the tree. 2.35 */ 2.36 UcxAVLNode *root; 2.37 @@ -107,12 +112,42 @@ 2.38 } UcxAVLTree; 2.39 2.40 /** 2.41 + * Initializes a new UcxAVLTree with a default allocator. 2.42 + * 2.43 + * @param cmpfunc the compare function that shall be used 2.44 + * @return a new UcxAVLTree object 2.45 + * @see ucx_avl_new_a() 2.46 + */ 2.47 +UcxAVLTree *ucx_avl_new(cmp_func cmpfunc); 2.48 + 2.49 +/** 2.50 + * Initializes a new UcxAVLTree with the specified allocator. 2.51 + * 2.52 + * The cmpfunc should be capable of comparing two keys within this AVL tree. 2.53 + * So if you want to use null terminated strings as keys, you could use the 2.54 + * ucx_strcmp() function here. 2.55 + * 2.56 + * @param cmpfunc the compare function that shall be used 2.57 + * @param allocator the UcxAllocator that shall be used 2.58 + * @return a new UcxAVLTree object 2.59 + */ 2.60 +UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator); 2.61 + 2.62 +/** 2.63 + * Macro for initializing a new UcxAVLTree with the default allocator and a 2.64 + * ucx_ptrcmp() compare function. 2.65 + * 2.66 + * @return a new default UcxAVLTree object 2.67 + */ 2.68 +#define ucx_avl_default_new() ucx_avl_new_a(ucx_ptrcmp, ucx_default_allocator()) 2.69 + 2.70 +/** 2.71 * Gets the value from the tree, that is associated with the specified key. 2.72 * @param tree the UcxAVLTree 2.73 * @param key the key 2.74 * @return the value (or <code>NULL</code>, if the key is not present) 2.75 */ 2.76 -void *ucx_avl_get(UcxAVLTree *tree, void *key); 2.77 +void *ucx_avl_get(UcxAVLTree *tree, intptr_t key); 2.78 2.79 /** 2.80 * Puts a key/value pair into the tree. 2.81 @@ -122,7 +157,7 @@ 2.82 * @return the replaced value (or <code>NULL</code>, if the key is new to the 2.83 * tree) 2.84 */ 2.85 -void* ucx_avl_put(UcxAVLTree *tree, void *key, void *value); 2.86 +void* ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value); 2.87 2.88 /** 2.89 * Removes an element from the AVL tree. 2.90 @@ -130,7 +165,7 @@ 2.91 * @param key the key 2.92 * @return the removed value (or <code>NULL</code>, if the key was not present) 2.93 */ 2.94 -void* ucx_avl_remove(UcxAVLTree *tree, void *key); 2.95 +void* ucx_avl_remove(UcxAVLTree *tree, intptr_t key); 2.96 2.97 2.98
3.1 --- a/ucx/utils.c Sun May 17 17:59:07 2015 +0200 3.2 +++ b/ucx/utils.c Sun May 17 18:32:41 2015 +0200 3.3 @@ -128,10 +128,12 @@ 3.4 } 3.5 3.6 int ucx_ptrcmp(void *ptr1, void *ptr2, void *data) { 3.7 - if (ptr1 == ptr2) { 3.8 + intptr_t p1 = (intptr_t) ptr1; 3.9 + intptr_t p2 = (intptr_t) ptr2; 3.10 + if (p1 == p2) { 3.11 return 0; 3.12 } else { 3.13 - return ptr1 < ptr2 ? -1 : 1; 3.14 + return p1 < p2 ? -1 : 1; 3.15 } 3.16 } 3.17