function lfds700_btree_au_find

From liblfds.org
Revision as of 01:27, 30 December 2015 by Admin (talk | contribs) (→‎Example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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_find.c

Opaque Structures

struct lfds700_btree_au_element;
struct lfds700_btree_au_state;

Prototype

int lfds700_btree_au_find( struct lfds700_btree_au_state *baus, 
                           void *key,
                           struct lfds700_btree_au_element **baue );

Parameters

struct lfds700_btree_au_state *baus

A pointer to an initialized lfds700_btree_au_statee.

void *key

A void pointer to the key to find.

struct lfds700_btree_au_element **baue

Set to point to the element with the key key. If no such key is present in the btree, set to NULL.

Return Value

Returns 1 if an element is found, 0 otherwise.

Notes

No notes.

Example

#include <stdio.h>
#include "liblfds700.h"

struct test_data
{
  int long long unsigned
    unique_id;

  char
    payload[64];

  struct lfds700_btree_au_element
    buae;
};

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 );
}

int main()
{
  enum lfds700_btree_au_link_result
    baulr;

  int long long unsigned
    loop,
    search_key = 5;

  struct lfds700_btree_au_element
    *buae;

  struct lfds700_btree_au_state
    baus;

  struct lfds700_misc_prng_state
    ps;

  struct test_data
    *td,
    *temp_td;

  lfds700_misc_library_init_valid_on_current_logical_core();

  lfds700_misc_prng_init( &ps );

  lfds700_btree_au_init_valid_on_current_logical_core( &baus, key_compare_function, LFDS700_BTREE_AU_EXISTING_KEY_FAIL, NULL );

  // TRD : allocate ten test elements, populate with dummy data and link to tree
  td = malloc( sizeof(struct test_data) * 10 );

  for( loop = 0 ; loop < 10 ; loop++ )
  {
    td[loop].unique_id = loop;
    sprintf( td[loop].payload, "the unique id is %llu", loop );

    LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( td[loop].baue, &td[loop].unique_id );
    LFDS700_BTREE_AU_SET_VALUE_IN_ELEMENT( td[loop].baue, &td[loop] );
    lfds700_btree_au_link( baus, &td[loop].baue, NULL, &ps );
  }

  lfds700_btree_au_find( &baus, (void *) &search_key, &baue );

  temp_td = LFDS700_BTREE_AU_GET_VALUE_FROM_ELEMENT( *baue );
  printf( "element %llu has value \"%s\"\n", temp_td->unique_id, temp_id->payload );

  lfds700_btree_au_cleanup( &baus );

  free( td );

  lfds700_misc_library_cleanup();

  return( EXIT_SUCCESS );
}

See Also