Splaytree impl

Implements Splaytree.

Summary
Splaytree implImplements Splaytree.
CopyrightThis program is free software.
Files
C-kern/api/ds/inmem/splaytree.hHeader file of Splaytree.
C-kern/ds/inmem/splaytree.cImplementation file of Splaytree impl.
splaytree_t
helper
KEYCOMPARECasts node to type typeadapt_object_t and calls callcmpkeyobj_typeadapt.
OBJCOMPARECasts node to type typeadapt_object_t and calls callcmpobj_typeadapt.
test
invariant_splaytreeChecks that nodes of left subtree are smaller and nodes in right subtree are higher than root node.
lifetime
change
recursivesplay_splaytreeSearches for a node X with key ‘key’ and makes it the new root of the tree.
splaykey_splaytreeSearches for a node X with key ‘key’ and makes it the new root of the tree.
splaynode_splaytreeSame as splaykey_splaytree except node is used as key.
search
iterate
test

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2012 Jörg Seebohn

Files

C-kern/api/ds/inmem/splaytree.h

Header file of Splaytree.

C-kern/ds/inmem/splaytree.c

Implementation file of Splaytree impl.

splaytree_t

Summary
helper
KEYCOMPARECasts node to type typeadapt_object_t and calls callcmpkeyobj_typeadapt.
OBJCOMPARECasts node to type typeadapt_object_t and calls callcmpobj_typeadapt.
test
invariant_splaytreeChecks that nodes of left subtree are smaller and nodes in right subtree are higher than root node.
lifetime
change
recursivesplay_splaytreeSearches for a node X with key ‘key’ and makes it the new root of the tree.
splaykey_splaytreeSearches for a node X with key ‘key’ and makes it the new root of the tree.
splaynode_splaytreeSame as splaykey_splaytree except node is used as key.
search
iterate
test

helper

KEYCOMPARE

#define KEYCOMPARE(
   key,
   node
) callcmpkeyobj_typeadapt(typeadp, key, memberasobject_typeadaptnodeoffset(nodeoffset, node))

Casts node to type typeadapt_object_t and calls callcmpkeyobj_typeadapt.  This macro expects variable name tree to point to splaytree_t.

OBJCOMPARE

#define OBJCOMPARE(
   keyobject,
   node
) callcmpobj_typeadapt(typeadp, keyobject, memberasobject_typeadaptnodeoffset(nodeoffset, node))

Casts node to type typeadapt_object_t and calls callcmpobj_typeadapt.  This macro expects variable name tree to point to splaytree_t.

test

invariant_splaytree

int invariant_splaytree(splaytree_t *tree,
uint16_t nodeoffset,
typeadapt_t *typeadp)

Checks that nodes of left subtree are smaller and nodes in right subtree are higher than root node.  The check is done recursively.

lifetime

change

recursivesplay_splaytree

static splaytree_node_t * recursivesplay_splaytree(splaytree_t *tree,
splaytree_node_t *root,
const void *key,
uint16_t nodeoffset,
typeadapt_t *typeadp)

Searches for a node X with key ‘key’ and makes it the new root of the tree.  If the node is not in the tree the last node (with no left or right child) is used instead.  The rotations done to move X to the top are called ‘splaying’.  See http://en.wikipedia.org/wiki/Splay_tree for a description of splay trees.

This is a straight forward recursive implementation and it used only for test purposes.

splaykey_splaytree

static int splaykey_splaytree(splaytree_t *tree,
const void *key,
uint16_t nodeoffset,
typeadapt_t *typeadp)

Searches for a node X with key ‘key’ and makes it the new root of the tree.  If the node is not in the tree the last node (with no left or right child) is used instead.  The rotations done to move X to the top are called ‘splaying’.  See http://en.wikipedia.org/wiki/Splay_tree for a description of splay trees.

This version splays from top to down without recursionit is the production version.  The result is compared with recursivesplay_splaytree.

splaynode_splaytree

static int splaynode_splaytree(splaytree_t *tree,
const splaytree_node_t *keynode,
/*out*/int *rootcmp,
uint16_t nodeoffset,
typeadapt_t *typeadp)

Same as splaykey_splaytree except node is used as key.

search

iterate

test

Interface to splay tree which allows access to a set of sorted elements in O(log n) amortized time.
Implements Splaytree.
#define KEYCOMPARE(
   key,
   node
) callcmpkeyobj_typeadapt(typeadp, key, memberasobject_typeadaptnodeoffset(nodeoffset, node))
Casts node to type typeadapt_object_t and calls callcmpkeyobj_typeadapt.
typedef struct typeadapt_object_t typeadapt_object_t
Declares abstract object type.
#define callcmpkeyobj_typeadapt(
   typeadp,
   ...
) callcmpkeyobj_typeadaptcomparator(&(typeadp)->comparator, typeadp, __VA_ARGS__)
Implements typeadapt_t.callcmpkeyobj_typeadapt.
#define OBJCOMPARE(
   keyobject,
   node
) callcmpobj_typeadapt(typeadp, keyobject, memberasobject_typeadaptnodeoffset(nodeoffset, node))
Casts node to type typeadapt_object_t and calls callcmpobj_typeadapt.
#define callcmpobj_typeadapt(
   typeadp,
   ...
) callcmpobj_typeadaptcomparator(&(typeadp)->comparator, typeadp, __VA_ARGS__)
Implements typeadapt_t.callcmpobj_typeadapt.
int invariant_splaytree(splaytree_t *tree,
uint16_t nodeoffset,
typeadapt_t *typeadp)
Checks that nodes of left subtree are smaller and nodes in right subtree are higher than root node.
static splaytree_node_t * recursivesplay_splaytree(splaytree_t *tree,
splaytree_node_t *root,
const void *key,
uint16_t nodeoffset,
typeadapt_t *typeadp)
Searches for a node X with key ‘key’ and makes it the new root of the tree.
static int splaykey_splaytree(splaytree_t *tree,
const void *key,
uint16_t nodeoffset,
typeadapt_t *typeadp)
Searches for a node X with key ‘key’ and makes it the new root of the tree.
static int splaynode_splaytree(splaytree_t *tree,
const splaytree_node_t *keynode,
/*out*/int *rootcmp,
uint16_t nodeoffset,
typeadapt_t *typeadp)
Same as splaykey_splaytree except node is used as key.
Close