49 This binary search tree implementation allows average O(1) insertion and |
49 This binary search tree implementation allows average O(1) insertion and |
50 removal of elements (excluding binary search time). |
50 removal of elements (excluding binary search time). |
51 All common binary tree operations are implemented. Furthermore, this module |
51 All common binary tree operations are implemented. Furthermore, this module |
52 provides search functions via lower and upper bounds. |
52 provides search functions via lower and upper bounds. |
53 |
53 |
|
54 ### Filtering items with a time window |
|
55 |
|
56 Suppose you have a list of items which contain a `time_t` value and your task |
|
57 is to find all items within a time window `[t_start, t_end]`. |
|
58 With AVL Trees this is easy: |
|
59 ```C |
|
60 /* --------------------- |
|
61 * Somewhere in a header |
|
62 */ |
|
63 typedef struct { |
|
64 time_t ts; |
|
65 // other important data |
|
66 } MyObject; |
|
67 |
|
68 /* ----------- |
|
69 * Source code |
|
70 */ |
|
71 |
|
72 UcxAVLTree* tree = ucx_avl_new(ucx_longintcmp); |
|
73 // ... populate tree with objects, use '& MyObject.ts' as key ... |
|
74 |
|
75 |
|
76 // Now find every item, with 30 <= ts <= 70 |
|
77 time_t ts_start = 30; |
|
78 time_t ts_end = 70; |
|
79 |
|
80 printf("Values in range:\n"); |
|
81 for ( |
|
82 UcxAVLNode* node = ucx_avl_find_node( |
|
83 tree, (intptr_t) &ts_start, |
|
84 ucx_longintdist, UCX_AVL_FIND_LOWER_BOUNDED); |
|
85 node && (*(time_t*)node->key) <= ts_end; |
|
86 node = ucx_avl_succ(node) |
|
87 ) { |
|
88 printf(" ts: %ld\n", ((MyObject*)node->value)->ts); |
|
89 } |
|
90 |
|
91 ucx_avl_free_content(tree, free); |
|
92 ucx_avl_free(tree); |
|
93 ``` |
|
94 |
54 ## Buffer |
95 ## Buffer |
55 |
96 |
56 *Header file:* [buffer.h](api/buffer_8h.html) |
97 *Header file:* [buffer.h](api/buffer_8h.html) |
57 *Required modules:* None. |
98 *Required modules:* None. |
58 |
99 |