macro LFDS710_BTREE_AU_GET_VALUE_FROM_ELEMENT

From liblfds.org
Revision as of 21:04, 7 May 2016 by Admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Source File

└───liblfds710
    └───inc
        └───liblfds710
                lfds710_btree_addonly_unbalanced.h

Opaque Structures

struct lfds710_btree_au_element;

Macro

#define LFDS710_BTREE_AU_GET_VALUE_FROM_ELEMENT( btree_au_element )

Parameters

btree_au_element

A struct lfds710_btree_au_element. Not a pointer to it - the struct itself.

Return Value

Returns a void pointer, the value from the element.

Notes

The value in an element is set atomically by LFDS710_BTREE_AU_SET_VALUE_IN_ELEMENT and this macro performs the necessary matching read-side work to ensure that it correctly reads the set value, the upshot being that the value can be safely and correctly read at any time, on any element, inside or outside of the btree.

As with all liblfds macros, the macro operates on the structure itself, not a pointer to it.

Example

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

struct test_data
{
  int long long unsigned
    unique_id;

  char
    payload[64];

  struct lfds710_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()
{
  struct lfds710_btree_au_element
    *buae;

  struct lfds710_btree_au_state
    baus;

  struct lfds710_misc_prng_state
    ps;

  struct test_data
    td = { 15, "Germanium-76" },
    *temp_td;

  lfds710_misc_library_init_valid_on_current_logical_core();

  lfds710_misc_prng_init( &ps );

  lfds710_btree_au_init_valid_on_current_logical_core( &baus, key_compare_function, LFDS710_BTREE_AU_EXISTING_KEY_FAIL, NULL );

  LFDS710_BTREE_AU_SET_KEY_IN_ELEMENT( td.baue, &td.unique_id );
  LFDS710_BTREE_AU_SET_VALUE_IN_ELEMENT( td.baue, &td );
  lfds710_btree_au_link( baus, &td.baue, NULL, &ps );

  lfds710_btree_au_get_by_position( &baus, &baue, LFDS710_BTREE_AU_ROOT );

  temp_td = LFDS710_BTREE_AU_GET_VALUE_FROM_ELEMENT( *baue );

  printf( "payload in element is \"%s\"\n", temp_td->payload );

  lfds710_btree_au_cleanup( &baus );

  lfds710_misc_library_cleanup();

  return( EXIT_SUCCESS );
}

See Also