1.1 --- a/docs/src/modules.md Thu May 03 10:09:49 2018 +0200 1.2 +++ b/docs/src/modules.md Thu May 03 10:44:33 2018 +0200 1.3 @@ -51,6 +51,47 @@ 1.4 All common binary tree operations are implemented. Furthermore, this module 1.5 provides search functions via lower and upper bounds. 1.6 1.7 +### Filtering items with a time window 1.8 + 1.9 +Suppose you have a list of items which contain a `time_t` value and your task 1.10 +is to find all items within a time window `[t_start, t_end]`. 1.11 +With AVL Trees this is easy: 1.12 +```C 1.13 +/* --------------------- 1.14 + * Somewhere in a header 1.15 + */ 1.16 +typedef struct { 1.17 + time_t ts; 1.18 + // other important data 1.19 +} MyObject; 1.20 + 1.21 +/* ----------- 1.22 + * Source code 1.23 + */ 1.24 + 1.25 +UcxAVLTree* tree = ucx_avl_new(ucx_longintcmp); 1.26 +// ... populate tree with objects, use '& MyObject.ts' as key ... 1.27 + 1.28 + 1.29 +// Now find every item, with 30 <= ts <= 70 1.30 +time_t ts_start = 30; 1.31 +time_t ts_end = 70; 1.32 + 1.33 +printf("Values in range:\n"); 1.34 +for ( 1.35 + UcxAVLNode* node = ucx_avl_find_node( 1.36 + tree, (intptr_t) &ts_start, 1.37 + ucx_longintdist, UCX_AVL_FIND_LOWER_BOUNDED); 1.38 + node && (*(time_t*)node->key) <= ts_end; 1.39 + node = ucx_avl_succ(node) 1.40 + ) { 1.41 + printf(" ts: %ld\n", ((MyObject*)node->value)->ts); 1.42 +} 1.43 + 1.44 +ucx_avl_free_content(tree, free); 1.45 +ucx_avl_free(tree); 1.46 +``` 1.47 + 1.48 ## Buffer 1.49 1.50 *Header file:* [buffer.h](api/buffer_8h.html)