function lfds700_btree_au_get_by_absolute_position

From liblfds.org
Jump to navigation Jump to search

Source Files

└───liblfds700
    ├───inc
    │   └───liblfds700
    │           lfds700_btree_addonly_unbalanced.h
    └───src
        └───lfds700_btree_addonly_unbalanced
                lfds700_btree_addonly_unbalanced_get.c

Opaque Structures

struct lfds700_btree_au_element;
struct lfds700_btree_au_state;

Enums

enum lfds700_btree_au_absolute_position
{
  LFDS700_BTREE_AU_ABSOLUTE_POSITION_ROOT,
  LFDS700_BTREE_AU_ABSOLUTE_POSITION_SMALLEST_IN_TREE,
  LFDS700_BTREE_AU_ABSOLUTE_POSITION_LARGEST_IN_TREE
};

Prototypes

int lfds700_btree_au_get_by_absolute_position( struct lfds700_btree_au_state *baus,
                                               struct lfds700_btree_au_element **baue,
                                               enum lfds700_btree_au_absolute_position absolute_position );

Parameters

struct lfds700_btree_au_state *baus

A pointer to an initialized lfds700_btree_au_state.

struct lfds700_btree_au_element **baue

Set to point to the element specified by position.

enum lfds700_btree_au_absolute_position absolute_position

Indicates which element to retrieve from the tree.

Return Value

Returns 1 if a btree element was found at the given position, 0 otherwise (i.e. the and the only time 0 is returned is when the btree is empty).

Notes

No notes.

Example

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

struct test_data
{
  char
    key[64];

  struct lfds700_btree_au_element
    buae;
};

int main()
{
  struct lfds700_btree_au_element
    *buae;

  struct lfds700_btree_au_state
    baus;

  struct lfds700_misc_prng_state
    ps;

  struct test_data
    td[3];

  void
    *key;

  lfds700_misc_library_init_valid_on_current_logical_core();

  lfds700_misc_prng_init( &ps );

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

  strcpy( td[0].key, "aaa" ); 
  strcpy( td[1].key, "bbb" ); 
  strcpy( td[2].key, "ccc" ); 

  LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( td[0].baue, td[0].key );
  LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( td[1].baue, td[1].key );
  LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( td[2].baue, td[2].key );

  /* TRD : link "bbb" first, then "aaa", then "ccc"

           so the tree looks like this;

              bbb
             /   \
          aaa     ccc
  */

  lfds700_btree_au_insert( baus, &td[1].baue, NULL, &ps );
  lfds700_btree_au_insert( baus, &td[0].baue, NULL, &ps );
  lfds700_btree_au_insert( baus, &td[2].baue, NULL, &ps );

  lfds700_btree_au_get_by_absolute_position( &baus, &baue, LFDS700_BTREE_AU_ABSOLUTE_POSITION_ROOT );
  key = LFDS700_BTREE_AU_GET_KEY_FROM_ELEMENT( *baue );
  printf( "key = \"%s\"\n", (char *) key ); // TRD : prints "bbb"

  lfds700_btree_au_get_by_absolute_position( &baus, &baue, LFDS700_BTREE_AU_ABSOLUTE_POSITION_SMALLEST_IN_TREE );
  key = LFDS700_BTREE_AU_GET_KEY_FROM_ELEMENT( *baue );
  printf( "key = \"%s\"\n", (char *) key ); // TRD : prints "aaa"

  lfds700_btree_au_get_by_absolute_position( &baus, &baue, LFDS700_BTREE_AU_ABSOLUTE_POSITION_LARGEST_IN_TREE );
  key = LFDS700_BTREE_AU_GET_KEY_FROM_ELEMENT( *baue );
  printf( "key = \"%s\"\n", (char *) key ); // TRD : prints "ccc"

  lfds700_btree_au_cleanup( &baus );

  lfds700_misc_library_cleanup();

  return( EXIT_SUCCESS );
}

See Also