macro LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT

From liblfds.org
Revision as of 01:26, 30 December 2015 by Admin (talk | contribs) (→‎Example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Source File

└───liblfds700
    └───inc
        └───liblfds700
                lfds700_btree_addonly_unbalanced.h

Opaque Structures

struct lfds700_btree_au_element;

Macro

#define LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( btree_au_element, new_key )

Parameters

btree_au_element

A struct lfds700_btree_au_element, which is not currently part of a tree. Not a pointer to it - the struct itself.

new_key

A pointer, which will be cast by the macro to a void *, which the key in btree_au_element is set to.

Return Value

No return value.

Notes

The key can only be set before an element enters the btree. If it is set after, the btree will likely be broken (as the new key, being different to the old key, would most likely require the element to be in a different position in the tree, and this element movement is not supported) and also, key is not set atomically, so that even if a change was made, there is in principle no guarantee any other logical core would see that change anyway.

Key is not set atomically because it is known it will only be set before an element enters the tree. Matters can be (and have been) arranged to ensure key is despite the lack of an atomic set correctly propagated to other logical cores, but this is a behaviour of the linking code, not of the macro which sets key.

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

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

  struct lfds700_btree_au_state
    baus;

  struct lfds700_misc_prng_state
    ps;

  struct test_data
    td = { 15, "Tellurium-128" },
    *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 );

  LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( td.baue, &td.unique_id );
  LFDS700_BTREE_AU_SET_VALUE_IN_ELEMENT( td.baue, &td );
  lfds700_btree_au_link( baus, &td.baue, NULL, &ps );

  lfds700_btree_au_get_by_position( &baus, &baue, LFDS700_BTREE_AU_ROOT );

  temp_td = LFDS700_BTREE_AU_GET_VALUE_FROM_ELEMENT( *baue );

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

  lfds700_btree_au_cleanup( &baus );

  lfds700_misc_library_cleanup();

  return( EXIT_SUCCESS );
}

See Also