function lfds700_btree_au_cleanup
Jump to navigation
Jump to search
Source Files
└───liblfds700 ├───inc │ └───liblfds700 │ lfds700_btree_addonly_unbalanced.h └───src └───llfds700_btree_addonly_unbalanced lfds700_btree_addonly_unbalanced_cleanup.c
Opaque Structures
struct lfds700_btree_au_element; struct lfds700_btree_au_state;
Prototype
void lfds700_btree_au_cleanup( struct lfds700_btree_au_state *baus, void (*element_cleanup_callback)(struct lfds700_btree_au_state *baus, struct lfds700_btree_au_element *baue) );
Parameters
struct lfds700_btree_au_state *baus
- A pointer to an initialized struct lfds700_btree_au_state.
void (*element_cleanup_callback)(struct lfds700_btree_au_state *baus, struct lfds700_btree_au_element *baue)
- A callback function which is called with every element present in the btree at the time of cleanup. This argument can be NULL.
Notes
On cleanup, if the cleanup callback is provided, the tree is not walked, but rather the root node is repeatedly passed to the callback and then replaced with one of its children - the upshot of this being that it is safe to free the struct lfds700_btree_au_element elements being passed into the cleanup function.
Example
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "liblfds700.h" struct test_data { char *name; int long long unsigned user_id; struct lfds700_btree_au_element baue; }; int key_compare_function( void const *new_key, void const *existing_key ) { int cr = 0; int long long unsigned *new_key = (int long long unsigned *) new_key, *existing_key = (int long long unsigned *) existing_key; if( *new_key < *existing_key ) cr = -1; if( *new_key > *existing_key ) cr = 1; return( cr ); } void element_cleanup_callback( struct lfds700_btree_au_state *baus, struct lfds700_btree_au_element *baue ) { struct test_data *test_data; test_data = LFDS700_BTREE_AU_GET_VALUE_FROM_ELEMENT( baue ); free( test_data.name ); return; } int main() { struct lfds700_btree_au_state baus; struct lfds700_liblfds_prng_state ps; struct test_data test_data; lfds700_liblfds_init(); lfds700_liblfds_prng_init( &ps ); lfds700_btree_au_init( &baus, key_compare_function, LFDS700_BTREE_AU_EXISTING_KEY_FAIL, NULL ); test_data.name = malloc( sizeof(char) * 64 ); strcpy( test_data.name, "Red Dwarf" ); test_data.user_id = 10; LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( baue, &test_data.user_id ); LFDS700_BTREE_AU_SET_VALUE_IN_ELEMENT( baue, &test_data ); lfds700_btree_au_link( &baus, &test_data.baue, NULL, &ps ); lfds700_btree_au_cleanup( &baus, element_cleanup_callback ); lfds700_liblfds_cleanup(); return( EXIT_SUCCESS ); }