aatree.c

Go to the documentation of this file.
00001 
00026 #include "aatree.h"
00027 #include <stdlib.h>
00028 #include <assert.h>
00029 
00030 #include "vector.h"
00031 
00032 static Vector copout = { NULL, NULL, NULL, sizeof(AANode), 0, 0, 16 };
00033 
00034 AANode aaTerminatorNode = { { NULL }, /* &aaTerminatorNode, &aaTerminatorNode, */
00035     0, -1 };
00036 
00037 AANode *AATreeFind(AANode *tree, AATreeKeyLevelType key) {
00038     AANode *node;
00039     int loop;
00040     if (copout.array == NULL) return NULL;
00041     for (loop = copout.items, node = copout.array; loop > 0; loop--, node++) {
00042         if (node->key == key)
00043             return node;
00044     }
00045     // the item sought is not in the tree
00046     return NULL;
00047 }
00048 
00049 AANode *AATreeAdd(AANode **tree, AATreeKeyLevelType key) {
00050     AANode *node;
00051     // the key must not be the same as the terminator
00052     assert(key != 0xFFFF);
00053     if (copout.array == NULL) {
00054         VectorInit(&copout, 16);
00055     }
00056     // attempt to allocate a new node and check for success
00057     if ((node = VectorAdd(&copout)) != NULL) {
00058         // initialize the new node
00059         node->key = key;
00060         *tree = node;
00061         return node;
00062     }
00063     // failed
00064     return NULL;
00065 }
00066 
00067 AANode *_AATreeRemove(AANode *tree, AATreeRemovalData *rd) {
00068     AANode *node;
00069     unsigned int loop;
00070     if (copout.array == NULL) return NULL;
00071     for (loop = 0, node = copout.array; loop < copout.items; loop++, node++) {
00072         if (node->key == rd->key) {
00073             VectorRemove(&copout, loop);
00074             if (copout.items)
00075                 return (AANode*)copout.array;
00076             return &aaTerminatorNode;
00077         }
00078     }
00079     // the item sought is not in the tree
00080     return NULL;
00081 }
00082 
00083 static Bool setExit = FALSE;
00084 
00085 static void treeExit(void) {
00086     VectorDestroy(&copout);
00087 }
00088 
00089 void AATreeDestroy(AANode *tree) {
00090     VectorClear(&copout);
00091     if (!setExit) {
00092         atexit(treeExit);
00093         setExit = TRUE;
00094     }
00095 }
00096 

Generated on Mon May 28 04:41:38 2007 for Retro Tank Super Attack by  doxygen 1.5.2