home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 1995 by Oracle Corporation. All Rights Reserved.
- *
- * yssp.h - Hash Table Manipulation
- *
- * DESCRIPTION
- *
- * SPlay trees. These are self-adjusting search trees
- * suitable for many purposes, including priority queues and ordered
- * search spaces.
- *
- * For compatibility with yshsh:
- *
- * ysspTree *ysSpCreate( ysspCmpFunc cmpfunc, ysmff delfunc);
- * void ysSpDestroy(ysspTree *t);
- * void ysSpIns(ysspTree *t, dvoid *key, size_t keysz, dvoid *val);
- * dvoid *ysSpRem(ysspTree *t, dvoid *key, size_t keysz);
- * dvoid *ysSpFind(ysspTree *t, dvoid *key, size_t keysz);
- *
- * For compatibility with mtsp:
- *
- * ysspTree *ysspNewTree( ysspTree *t, ysspCmpFunc *cmp );
- * ysspNode *ysspLookup( dvoid *key, ysspTree *t );
- * ysspNode *ysspFirstLookup( dvoid *key, ysspTree *t );
- * ysspNode *ysspPELookup( dvoid *key, ysspTree *t );
- * ysspNode *ysspNextLookup( dvoid *key, ysspTree *t );
- * ysspNode *ysspEnq( ysspNode *node, ysspTree *t );
- * ysspNode *ysspEnqAfter( ysspNode *node, ysspTree *t );
- * ysspNode *ysspDeq( ysspNode **np );
- * ysspNode *ysspDeqTree( ysspTree *t );
- * void ysspRemove( ysspNode *node, ysspTree *t );
- * void ysspSplay( ysspNode *n, ysspTree *t );
- * ysspNode *ysspFHead( ysspTree *t );
- * ysspNode *ysspFNext( ysspNode *n );
- *
- * ATTRS: public, external
- *
- * NOTES
- *
- * Idiomatic use of these trees using the second familiy of interfaces
- * is as follows.
- *
- * 1. Embed the node somewhere in another structure, and the tree
- * in some context.
- *
- * typedef
- * {
- * ysspNode n_mystruct;
- * myKeyType key_mystruct;
- * ...
- * } mystruct;
- *
- * typedef
- * {
- * ysspTree t_mycx;
- * ...
- * } mycx;
- *
- * 2. Initialize the tree when the context is set up.
- *
- * ysspNewTree( &cx->t_myccx, myKeyTypeCmp );
- *
- * 3. Insert with a duplicate check
- *
- * mystruct *m;
- * if( !ysspLookup( (dvoid*)&keyval, &cx->t_mycx ) )
- * {
- * m = (mystruct*)ysmGlbAlloc( sizeof(*n), "mystruct");
- * m->key_mystruct = keyval;
- * m->node_mystruct.key_ysspNode = (dvoid*)&m->key_mystruct;
- * ysspEnq( &m->node_mystruct, &cx->t_mycx );
- * }
- *
- * 4. Deletion by key value
- *
- * ysspNode *n;
- * if( (n = sspLookup( (dvoid*)&keyval, &cx->t_mycx ) ) )
- * {
- * ysspRemove( n, &cx->t_mycx );
- * ysmGlbFree( (dvoid*)n );
- * }
- *
- * 5. Read-only iterate over the tree
- *
- * ysspNode *n;
- * mystruct *m;
- * for( n = ysspFHead( &cx->t_mycx ) ; n ; n = ysspFnext( n ) )
- * {
- * m = (mystruct*)n;
- * mySomething( cx, m );
- * }
- *
- * 6. Iterate over tree when current node may be deleted.
- *
- * ysspNode *n, *next;
- * for( n = ysspFHead( &cx->t_mycx ) ; n ; n = next )
- * {
- * next = ysspFNext( n );
- * if( myPredicate( (mystruct*)n ) )
- * ysspRemove( n, &cx->t_mycx );
- * }
- *
- * 7. Iteration when anything may happen to the tree during the loop.
- *
- * This keeps a local copy of the current key and does a "next"
- * lookup on each iteration; slower, but safe and correct.
- *
- * ysspNode *n;
- * myKeyType key;
- * boolean first = TRUE;
- *
- * while( (n = first ? ysspFHead( &cx->t_mycx ) :
- * ysspNextLookup( (dvoid*)&key, &cx->t_mycx ) )
- * {
- * first = FALSE;
- * key = *(myKeyType*)n->key_ysspNode;
- * myDoAnythingToTheTree( (mystruct*)n, cx );
- * }
- *
- * 8. Tree deletion
- *
- * ysspNode *n;
- * while( (n = ysspDeqTree( &cx->t_mycx ) ) )
- * ysmGlbFree( (dvoid*)n );
- *
- *
- * MODIFIED (MM/DD/YY)
- * dbrower 08/ 2/95 - created, derived from mtsp.h and yshsh.h
- * dbrower 02/16/96 - add ysspDeqTree, improve documentation.
- * dbrower 03/ 1/96 - change cmp funcs to return sword, not sb4; olint.
- */
-
- /*
- * This is derived from code:
- *
- * Copyright (c) 1994 by Academic Press, Boston, Massachusetts.
- * Written by David Brower. Not derived from licensed software.
- * From the book "Software Solutions in C", edited by Dale Schumacher.
- *
- * Permission is granted to anyone to use this software for any
- * purpose on any computer system, and to redistribute it in any way,
- * subject to the following restrictions:
- *
- * 1. The author is not responsible for the consequences of use of
- * this software, no matter how awful, even if they arise
- * from defects in it.
- *
- * 2. The origin of this software must not be misrepresented, either
- * by explicit claim or by omission.
- *
- * 3. Altered versions must be plainly marked as such, and must not
- * be misrepresented (by explicit claim or omission) as being
- * the original software.
- *
- * 4. This notice must not be removed or altered.
- */
-
-
- #ifndef YSSP_ORACLE
- #define YSSP_ORACLE
-
- #ifndef SYSX_ORACLE
- #include <sysx.h>
- #endif
- #ifndef YS_ORACLE
- #include <ys.h>
- #endif
-
- EXTC_START
-
- /* PUBLIC TYPES AND CONSTANTS */
-
- typedef struct ysspTree ysspTree;
- typedef struct ysspNode ysspNode;
-
- /*
- * Comparison function handed to either ysSpCreate or ysSpNewTree
- * a and b are the values of the key_ysspNode fields of nodes, or
- * the "key" argument given to a lookup or find function.
- */
- typedef sword (*ysspCmpFunc)( CONST dvoid *a, CONST dvoid *b );
-
- struct ysspTree
- {
- ysspNode *root_ysspTree;
- ysspCmpFunc cmp_ysspTree;
- ysmff delfunc_ysspTree;
- };
-
- /* This node is assumed to be embedded in the data object; key must be
- set by hand before enqueueing with ysspEnq. */
-
- struct ysspNode
- {
- ysspNode *left_ysspNode;
- ysspNode *right_ysspNode;
- ysspNode *up_ysspNode;
- dvoid *key_ysspNode;
- };
-
- /*
- * ysSpCreate - create a splay tree
- *
- * DESCRIPTION
- * ysSpCreate() creates a splay tree.
- *
- * The cmpfunc is a function that compares two elements for order.
- * No default is provided; you must supply one.
- *
- * The delfunc is a function that is used to free the elements still
- * in the splay tree when ysSpDestroy() is called. If no delfunc is
- * needed, a null pointer may be passed.
- */
- ysspTree *ysSpCreate( ysspCmpFunc cmpfunc, ysmff delfunc);
-
- /*
- * ysSpDestroy - destroy a splay tree
- *
- * DESCRIPTION
- * ysSpDestroy() destroys a splay tree previously created by ysSpCreate().
- * For each node still in the splay tree, the delfunc passed to
- * ysSpCreate() is invoked (unless it is null) against each value,
- * and the node is also freed; finally the allocated tree is released.
- *
- * You probably don't want to call this on a tree started with
- * ysspNewTree!
- */
- void ysSpDestroy(ysspTree *t);
-
- /*
- * ysSpIns - insert element into a splay tree
- *
- * DESCRIPTION
- * ysSpIns() inserts an element into a splay tree. key and keysz specify
- * the key that will allow this element to located later. val is the value
- * of the element to insert. Duplicates are not detected by this routine.
- * ysSpFind() may be used first to decide whether there is a duplicate key
- * in the table.
- */
- void ysSpIns(ysspTree *t, dvoid *key, size_t keysz, dvoid *val);
-
- /*
- * ysSpRem - remove element from a splay tree
- *
- * DESCRIPTION
- * ysSpRem() removes an element from a splay tree. key and keysz specify
- * the key that is used to locate the element. The element is removed from
- * the splay tree and its value is returned. If the element is not found,
- * a null pointer is returned.
- *
- * keysz is ignored.
- */
- dvoid *ysSpRem(ysspTree *t, dvoid *key, size_t keysz);
-
- /*
- * ysSpFind - find element in splay tree
- *
- * DESCRIPTION
- * ysSpFind() finds an element in a splay tree. key and keysz specify
- * the key that is used to locate the element. If the element is found,
- * its value is returned. Otherwise, a null pointer is returned.
- *
- * keysz is ignored.
- */
- dvoid *ysSpFind(ysspTree *t, dvoid *key, size_t keysz);
-
-
- /* ---------------------------- ysspNewTree ---------------------------- */
- ysspTree *ysspNewTree( ysspTree *t, ysspCmpFunc cmp );
- /*
- NAME
- ysspNewTree
- DESCRIPTION
- Initialize a tree, using the supplied function for ordering comparisons.
- PARAMETERS
- t - pointer to tree to set up
- cmp - function to use for comparisons
- RETURNS
- pointer to the tree.
- */
-
- /* ---------------------------- ysspLookup ---------------------------- */
- ysspNode *ysspLookup( CONST dvoid *key, ysspTree *t );
- /*
- NAME
- ysspLookup
- DESCRIPTION
- Given a pointer to a key, do a lookup in the tree and return a
- pointer to the located tree node, or NULL.
- PARAMETERS
- key - pointer to a key value to locate.
- t - the tree to search
- RETURNS
- pointer to the ysspNode located, or NULL if not found.
- */
-
- /* ---------------------------- ysspFirstLookup ---------------------------- */
- ysspNode *ysspFirstLookup( CONST dvoid *key, ysspTree *t );
- /*
- NAME
- ysspFirstLookup
- DESCRIPTION
- Given a pointer to a key, do a lookup in the tree and return the
- first node with the key. Useful if duplicates are allowed. First
- is splayed to the root, so subsequent lookups return the first.
- PARAMETERS
- key - pointer to a key value to locate.
- t - the tree to search
- RETURNS
- pointer to the ysspNode located, or NULL if not found.
- */
-
- /* ---------------------------- ysspPELookup ---------------------------- */
- ysspNode *ysspPELookup( CONST dvoid *key, ysspTree *t );
- /*
- NAME
- ysspLookup
- DESCRIPTION
- Given a pointer to a key, do a lookup in the tree and return a
- pointer to the matching node, or the one prior. If no match and
- none prior, return NULL.
- PARAMETERS
- key - pointer to a key value to locate.
- t - the tree to search
- RETURNS
- pointer to the ysspNode located, or NULL if not found.
- */
-
- /* ---------------------------- ysspNextLookup ---------------------------- */
- ysspNode *ysspNextLookup( CONST dvoid *key, ysspTree *t );
- /*
- NAME
- ysspNextLookup
- DESCRIPTION
- Given a pointer to a key, do a lookup in the tree and return a
- pointer to the next node that follows the key, even if the key
- does not exist in the tree. Returns NULL only if there are no
- successors.
- PARAMETERS
- key - pointer to a key value to locate.
- t - the tree to search
- RETURNS
- pointer to the ysspNode located, or NULL if not found.
- */
-
- /* ---------------------------- ysspEnq ---------------------------- */
- ysspNode *ysspEnq( ysspNode *node, ysspTree *t );
- /*
- NAME
- ysspEnq
- DESCRIPTION
- Insert a new node into the tree. If nodes with the key already
- exist the node may be inserted in any location with respect to
- those nodes.
- PARAMETERS
- node - node to insert, with key_mtNode pointing to the key
- value.
- t - the tree to insert into.
- RETURNS
- pointer to the node.
- */
-
-
- /* ---------------------------- ysspEnqAfter ---------------------------- */
- ysspNode *ysspEnqAfter( ysspNode *newnd, ysspNode *old, ysspTree *t );
- /*
- NAME
- ysspEnqAfter
- DESCRIPTION
- Insert a new node into the tree after the old node. No checking
- of key values is performed.
- PARAMETERS
- newnd - node to insert, with key_mtNode pointing to the key value.
- old - node to insert new after.
- t - the tree to insert into.
- RETURNS
- pointer to the node.
- */
-
-
- /* ---------------------------- ysspDeq ---------------------------- */
- ysspNode *ysspDeq( ysspNode **np );
- /*
- NAME
- ysspDeq
- DESCRIPTION
- Return and remove (dequeue) the lowest node in a sub-tree,
- identified by a pointer to the node pointer.
- Repetitive dequeue operations without inserts are O(1).
- PARAMETERS
- np -- pointer to a node pointer, for instance the
- address of a tree root, eg: &t->root_ysspTree; (IN/OUT)
- RETURNS
- pointer to the dequeued node, or NULL if the tree was empty.
- */
-
-
- /* ---------------------------- ysspDeqTree ---------------------------- */
- #define ysspDeqTree( t ) (ysspDeq( &(t)->root_ysspTree))
- /*
- NAME
- ysspDeqTree
- DESCRIPTION
- Dequeue the first node in a tree, removing it. Successive
- operations are O(1) becuase of tree reorganization.
-
- PARAMETERS
- t -- the tree whose head to remove (IN/OUT)
- RETURNS
- pointer to the removed node, or NULL if tree is empty.
- */
-
-
-
- /* ---------------------------- ysspRemove ---------------------------- */
- void ysspRemove( ysspNode *node, ysspTree *t );
- /*
- NAME
- ysspRemove
- DESCRIPTION
- Remove an arbitrary node from a tree. Chaos will ensue if the
- node is not in the tree.
-
- PARAMETERS
- node -- the node to remove.
- t -- the tree to remove it from.
- RETURNS
- none
- */
-
-
- /* ---------------------------- ysspSplay ---------------------------- */
- void ysspSplay( ysspNode *n, ysspTree *t );
- /*
- NAME
- ysspSplay
- DESCRIPTION
- Rotate the node to the root of the tree, using the "splay"
- algorithm. This should rarely be called by clients.
- PARAMETERS
- n -- the node to pull up.
- t -- the tree containing it.
- RETURNS
- none
- */
-
-
- /* ---------------------------- ysspFHead ---------------------------- */
- ysspNode *ysspFHead( ysspTree *t );
- /*
- NAME
- ysspFHead
- DESCRIPTION
- Locate the lowest node in a tree, quickly without reorganizing.
- This is suitable for starting quick ordered scans of the tree.
- PARAMETERS
- t -- tree in question.
- RETURNS
- pointer to the lowest node, or NULL if the tree is empty.
- */
-
-
- /* ---------------------------- ysspFTail ---------------------------- */
- ysspNode *ysspFTail( ysspTree *t );
- /*
- NAME
- ysspFTail
- DESCRIPTION
- Locate the highest node in a tree, quickly without reorganizing.
- This is suitable for starting quick ordered scans of the tree.
- PARAMETERS
- t -- tree in question.
- RETURNS
- pointer to the lowest node, or NULL if the tree is empty.
- */
-
-
- /* ---------------------------- ysspFNext ---------------------------- */
- ysspNode *ysspFNext( ysspNode *n );
- /*
- NAME
- ysspFNext
- DESCRIPTION
- Return the next node in a tree, quickly without reorgainizing.
- This is suitable for increment steps in a fast tree scan.
- PARAMETERS
- n -- the node whose successor is to be found
- RETURNS
- pointer to the next node, or NULL if there is none.
- */
-
- /* ---------------------------- ysspFPrev ---------------------------- */
- ysspNode *ysspFPrev( ysspNode *n );
- /*
- NAME
- ysspFPrev
- DESCRIPTION
- Return the previous node in a tree, quickly without reorgainizing.
- This is suitable for increment steps in a fast tree scan.
- PARAMETERS
- n -- the node whose successor is to be found
- RETURNS
- pointer to the prev node, or NULL if there is none.
- */
-
- EXTC_END
- #endif /* YSSP_ORACLE */
-
-
-