RNGRB_INT (RPGLE)

Red Black Tree Implementation : Wrapper for integer keys
Details ....

Copybooks  

de 'libtree_h.rpgle'
de 'libc_h.rpgle'

Procedures  

removeNodesRecursively
tree_rb_int_clearexported
Clear tree
tree_rb_int_compareexported
Integer compare function
tree_rb_int_container_ofexported
Return pointer to user data structure
tree_rb_int_containsKeyexported
Contains key
tree_rb_int_finalizeexported
Dispose tree
tree_rb_int_firstexported
Get first element
tree_rb_int_getexported
Get element
tree_rb_int_lastexported
Get last element
tree_rb_int_nextexported
Get next element
tree_rb_int_previousexported
Get previous element
tree_rb_int_putexported
Insert new element
tree_rb_int_removeexported
Remove node by key
tree_rb_int_removeByNodeexported
Remove node

Detailed Description  

This module is a wrapper around the red black tree implemtation from the libtree project at github.com.

The wrapper adds type specific procedures for integer keys. It also manages the dynamic memory allocation and deallocation. Memory will be allocated from the default heap. Depending on the compile options this is either from single level storage or from teraspace.

The tree has no maximum number of elements. As long as you can get memory from your heap you can add elements to the tree.

Author:
Mihael Schmidt
Date:
2011-02-07
Changes:
13.03.2011   —   Mihael Schmidt
Fixed bug in containsKey procedure

26.03.2011   —   Mihael Schmidt
Changed memory mangement from ILE CEE API to bifs. Now memory will be taken depending on the storage model option on compilation.

Links:
https://github.com/fbuihuu/libtree
http://www.rpgnextgen.com

Procedure Documentation  

removeNodesRecursively  

voidremoveNodesRecursively()

tree_rb_int_clear  

voidtree_rb_int_clear(Pointer)
Removes all nodes from the tree recursively from top to bottom. All memory allocated to the nodes will be freed.
Parameter:
Pointer   constPointer to tree
Exported.
Infos:
  This procedure traverses the whole tree to complete. The tree will also rebalance itself on every remove operation. This might be costly on huge trees. It might be simpler to build a whole new tree.

tree_rb_int_compare  

Integertree_rb_int_compare(Pointer, Pointer)
This procedure is used for a tree with integer keys. It can be used on the tree creation procedure like this: tree_rb_create(%paddr('tree_rb_int_compare'));
Parameter:
Pointer   valueEmbedded node pointer A
Pointer   valueEmbedded node pointer B
Return value:
Numerisch (Integer) (10)less than 0 = key A is less than key B
0 = key A and B are equal
greater than 0 = key A is greater than key B
Exported.

tree_rb_int_container_of  

Pointertree_rb_int_container_of(Pointer)
Returns a pointer to the user data structure of a node relative to the passed embedded data structure pointer.
Parameter:
Pointer   valuePointer to embedded data structure
Return value:
PointerPointer to user data structure or *null if input parameter is also *null
Exported.

tree_rb_int_containsKey  

Booleantree_rb_int_containsKey(Pointer, Integer)
Checks if the tree contains an element with the passed key.
Parameter:
Pointer   constPointer to tree
Numerisch (Integer) (10)   constKey
Return value:
Boolean*on = tree contains key
*off = tree does not contain key
Exported.

tree_rb_int_finalize  

voidtree_rb_int_finalize(Pointer)
Frees all allocated resources of the tree.
Parameter:
PointerPointer to tree
Exported.

tree_rb_int_first  

Pointertree_rb_int_first(Pointer)
Returns the first element of the tree.
Parameter:
Pointer   constPointer to tree
Return value:
PointerPointer to user data structure or *null if the tree is empty.
Exported.

tree_rb_int_get  

Pointertree_rb_int_get(Pointer, Integer)
Returns a pointer to the user node of the given key.
Parameter:
Pointer   constPointer to tree
Numerisch (Integer) (10)   constKey
Return value:
PointerPointer to user data structure or *null if there is no element with the given key in the tree.
Exported.

tree_rb_int_last  

Pointertree_rb_int_last(Pointer)
Returns the last element of the tree.
Parameter:
Pointer   constPointer to tree
Return value:
PointerPointer to user data structure or *null if the tree is empty.
Exported.

tree_rb_int_next  

Pointertree_rb_int_next(Pointer)
Returns the next element of the tree starting from the passed element.
Parameter:
Pointer   constPointer to user data structure
Return value:
PointerPointer to user data structure of the next node or *null if there are no more elements after the passed one.
Exported.

tree_rb_int_previous  

Pointertree_rb_int_previous(Pointer)
Returns the previous element of the tree starting from the passed element.
Parameter:
Pointer   constPointer to user data structure
Return value:
PointerPointer to user data structure of the previous node or *null if there are no more elements before the passed one.
Exported.

tree_rb_int_put  

voidtree_rb_int_put(Pointer, Integer, Pointer, Integer)
Inserts a new element into the tree. Any existing element will be replaced. The resources of any existing element will be freed.

The value is a passed as a pointer to the real value. If the length parameter is omitted it is assumed that the value is null-terminated.

The key and value will be copied to the tree node.

Parameter:
Pointer   constPointer to tree
Numerisch (Integer) (10)   constKey
Pointer   constPointer to value
Numerisch (Integer) (10)   const   optionalValue length (default: value must be null-terminated)
Exported.

tree_rb_int_remove  

voidtree_rb_int_remove(Pointer, Integer)
Removes a node from the tree. All allocated resources to the node will be freed. The procedure returns normally if the key does not exist in the tree.
Parameter:
Pointer   constPointer to tree
Numerisch (Integer) (10)   constKey
Exported.

tree_rb_int_removeByNode  

voidtree_rb_int_removeByNode(Pointer, Pointer)
Removes a node from the tree. All allocated resources to the node will be freed.
Parameter:
Pointer   constPointer to tree
PointerPointer to user data structure (node)
Exported.