nodeT* createTreeNode(int data) { nodeT *tempNode = allocTreeNode(); tempNode->data = data; tempNode->left = NULL; tempNode->right = NULL; return tempNode; }
/** * Recursive helper function for findNode() */ StateTreeNode * findNodeRec(StateTree * tree, StateTreeNode * parent, unsigned int * data, unsigned int numElements, bool * found) { unsigned int direction = 0; int i; for (i = numElements - 1; i >= 0; --i) { if (data[i] > parent->data[i]) { direction = 1; break; } if (data[i] < parent->data[i]) { direction = 2; break; } } switch(direction) { case 0: *found = true; return parent; case 1: if (parent->rightChild == 0) { parent->rightChild = allocTreeNode(tree, 0,0,0,data,numElements,0,0); *found = false; return parent->rightChild; } else return findNodeRec(tree, parent->rightChild,data,numElements,found); case 2: if (parent->leftChild == 0) { parent->leftChild = allocTreeNode(tree, 0,0,0,data,numElements,0,0); *found = false; return parent->leftChild; } else return findNodeRec(tree, parent->leftChild,data,numElements,found); } // should never be reached return 0; }
/** * Recursively find the node corresponding to state <data> in the state tree <root>, * or insert the node if it does not exist. * <numElements> is the size of the state vector <data>. * The return value of <found> indicates whether the element previously existed in the tree * * Returns the (possibly newly created) node belonging to <data>. If the tree is empty, * <root> is set to this node. */ StateTreeNode * findNode(StateTree * tree, unsigned int * data, unsigned int numElements, bool * found) { if (tree->root == 0) { tree->root = allocTreeNode(tree, 0,0,0,data,numElements,0,0); *found = false; return tree->root; } return findNodeRec(tree, tree->root,data,numElements, found); }
TA_RetCode TA_FileIndexAddTreeValue( TA_FileIndexPriv *data, TA_String *string, TA_ValueTreeNode **added ) { TA_PROLOG; TA_ValueTreeNode *node; unsigned int allocateEmptyString; TA_Libc *libHandle; TA_StringCache *stringCache; libHandle = data->libHandle; TA_TRACE_BEGIN( libHandle, TA_FileIndexAddTreeValue ); stringCache = TA_GetGlobalStringCache( libHandle ); allocateEmptyString = 0; TA_ASSERT( libHandle, data != NULL ); if( added ) *added = NULL; if( !string ) { string = TA_StringAlloc( stringCache, "" ); if( !string ) { TA_TRACE_RETURN( TA_ALLOC_ERR ); } allocateEmptyString = 1; } /* Alloc the TA_ValueTreeNode */ node = allocTreeNode( libHandle, data->currentNode, string ); if( allocateEmptyString ) TA_StringFree( stringCache, string ); if( !node ) { TA_TRACE_RETURN( TA_ALLOC_ERR ); } data->currentNode = node; if( added ) *added = node; TA_TRACE_RETURN( TA_SUCCESS ); }
/**** Global functions definitions. ****/ TA_FileIndexPriv *TA_FileIndexPrivAlloc( TA_Libc *libHandle, TA_String *initialCategory, TA_String *initialCategoryCountry, TA_String *initialCategoryExchange, TA_String *initialCategoryType ) { TA_FileIndexPriv *fileIndexPrivData; TA_StringCache *stringCache; stringCache = TA_GetGlobalStringCache( libHandle ); /* Initialize the TA_FileIndexPriv element. */ fileIndexPrivData = (TA_FileIndexPriv *)TA_Malloc( libHandle, sizeof( TA_FileIndexPriv ) ); if( !fileIndexPrivData ) return NULL; /* initialize all fields to NULL. */ memset( fileIndexPrivData, 0, sizeof( TA_FileIndexPriv ) ); /* Now attempt to allocate all sub-elements. */ fileIndexPrivData->libHandle = libHandle; stringCache = TA_GetGlobalStringCache( libHandle ); fileIndexPrivData->initialCategoryString = TA_StringDup( stringCache, initialCategory ); fileIndexPrivData->initialCategoryCountryString = TA_StringDup( stringCache, initialCategoryCountry); fileIndexPrivData->initialCategoryExchangeString = TA_StringDup( stringCache, initialCategoryExchange ); fileIndexPrivData->initialCategoryTypeString = TA_StringDup( stringCache, initialCategoryType ); if( (fileIndexPrivData->initialCategoryString == NULL) || (fileIndexPrivData->initialCategoryCountryString == NULL) || (fileIndexPrivData->initialCategoryExchangeString == NULL) || (fileIndexPrivData->initialCategoryTypeString == NULL) ) { freeFileIndexPriv( (void *)fileIndexPrivData ); return NULL; } fileIndexPrivData->scratchPad = (char *)TA_Malloc( libHandle, TA_SOURCELOCATION_MAX_LENGTH+2 ); if( !fileIndexPrivData->scratchPad ) { freeFileIndexPriv( (void *)fileIndexPrivData ); return NULL; } fileIndexPrivData->listLocationToken = TA_ListAlloc( libHandle ); if( !fileIndexPrivData->listLocationToken ) { freeFileIndexPriv( (void *)fileIndexPrivData ); return NULL; } fileIndexPrivData->listCategory = TA_ListAlloc( libHandle ); if( !fileIndexPrivData->listCategory ) { freeFileIndexPriv( (void *)fileIndexPrivData ); return NULL; } fileIndexPrivData->root = allocTreeNode( libHandle, NULL, NULL ); if( !fileIndexPrivData->root ) { freeFileIndexPriv( (void *)fileIndexPrivData ); return NULL; } fileIndexPrivData->currentNode = fileIndexPrivData->root; fileIndexPrivData->wildOneChar = TA_StringAlloc( stringCache, "?" ); fileIndexPrivData->wildZeroOrMoreChar = TA_StringAlloc( stringCache, "*" ); fileIndexPrivData->wildOneOrMoreChar = TA_StringAlloc( stringCache, "?*" ); if( (!fileIndexPrivData->wildOneChar) || (!fileIndexPrivData->wildZeroOrMoreChar) || (!fileIndexPrivData->wildOneOrMoreChar) ) { freeFileIndexPriv( (void *)fileIndexPrivData ); return NULL; } return fileIndexPrivData; }