function lfds700_btree_au_get_by_relative_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_relative_position
{
  LFDS700_BTREE_AU_RELATIVE_POSITION_UP,
  LFDS700_BTREE_AU_RELATIVE_POSITION_LEFT,
  LFDS700_BTREE_AU_RELATIVE_POSITION_RIGHT,
  LFDS700_BTREE_AU_RELATIVE_POSITION_SMALLEST_ELEMENT_BELOW_CURRENT_ELEMENT,
  LFDS700_BTREE_AU_RELATIVE_POSITION_LARGEST_ELEMENT_BELOW_CURRENT_ELEMENT,
  LFDS700_BTREE_AU_RELATIVE_POSITION_NEXT_SMALLER_ELEMENT_IN_ENTIRE_TREE,
  LFDS700_BTREE_AU_RELATIVE_POSITION_NEXT_LARGER_ELEMENT_IN_ENTIRE_TREE
};

Prototype

int lfds700_btree_au_get_by_relative_position( struct lfds700_btree_au_element **baue,
                                               enum lfds700_btree_au_relative_position relative_position );

Parameters

struct lfds700_btree_au_element **baue

This argument is the address of a pointer to an element in a btree. Typically this pointer is initially obtained by calling lfds700_btree_au_get_by_absolute_position. This argument is then set to point to the element retrieved by the function, according to the direction argument.

enum lfds700_btree_au_relative_position relative_position

Indicates which element *baue should be moved to, using the original element pointed to by *baue as the starting point.

Return Value

Returns 1 if a btree element was found in the given direction, 0 otherwise.

Notes

No notes.

Example

#include <stdio.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[4];

  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" );
  strcpy( td[3].key, "ddd" );
 
  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 );
  LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( td[3].baue, td[3].key );

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

           so the tree looks like this;

           aaa
              \
               bbb
                  \
                   ccc
                      \
                       ddd
  */

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

  lfds700_btree_au_get_by_absolute_position( &baus, &baue, LFDS700_BTREE_AU_ABSOLUTE_POSITION_ROOT ); // TRD : points to "aaa"
  lfds700_btree_au_get_by_relative_position( &baue, LFDS700_BTREE_AU_SMALLEST );   // TRD : points to NULL

  lfds700_btree_au_get_by_absolute_position( &baus, &baue, LFDS700_BTREE_AU_ABSOLUTE_POSITION_ROOT ); // TRD : points to "aaa"
  lfds700_btree_au_get_by_relative_position( &baue, LFDS700_BTREE_AU_GREATEST );   // TRD : points to "ddd"
  lfds700_btree_au_get_by_relative_position( &baue, LFDS700_BTREE_AU_UP );         // TRD : points to "ccc"
  lfds700_btree_au_get_by_relative_position( &baue, LFDS700_BTREE_AU_SMALLER );    // TRD : points to NULL

  lfds700_btree_au_get_by_absolute_position( &baus, &baue, LFDS700_BTREE_AU_ABSOLUTE_POSITION_ROOT ); // TRD : points to "aaa"
  lfds700_btree_au_get_by_relative_position( &baue, LFDS700_BTREE_AU_GREATEST );   // TRD : points to "ddd"
  lfds700_btree_au_get_by_relative_position( &baue, LFDS700_BTREE_AU_GREATER );    // TRD : points to NULL and the function returns 0

  lfds700_btree_au_cleanup( &baus );

  lfds700_misc_library_cleanup();

  return( EXIT_SUCCESS );
}

See Also