// Can replace with post-order traversal (to destroy a binary s t)
void destroyChain(Node *chain){
	if(chain){
		Node *next = chain->next;
		if(next) destroyChain(next);		
		destroyNode(chain);
	}
}
Exemple #2
0
/*--------------------------------------------------------------------------------------
 * Purpose: delete a Chain, stopping the thread if enabled and destroying memory
 * Input:	the chain's ID
 * Output:
 *
 * He Yan @ Sep 22, 2008
 * -------------------------------------------------------------------------------------*/ 
void 
deleteChain(int chainID)
{
	// check the chain ID is valid
	if (chainID >= MAX_CHAIN_IDS)
	{
		log_err("deleteChain: chain ID %d exceeds max %d", chainID, MAX_CHAIN_IDS);
		return;
	}
	// check if the chain is configured
	if( Chains[chainID] == NULL ) 
	{
		log_err("deleteChain: couldn't find a chain with ID:%d", chainID);
		return;
	}
	// if the chain is running, let its own thread clear the memory
	if (Chains[chainID]->runningFlag == TRUE) 
	{
		Chains[chainID]->enabled = FALSE;
		Chains[chainID]->deleteChain = TRUE;
	}
	// else if no thread running, destroy the chain memory
	else
		destroyChain(Chains[chainID]);
}
void destroyTable(HashTable *table){
	if(table){
		uint32_t i = 0;
		for(i = 0; i < table->maxSize; i++){
			Node *val = table->values[i];
			destroyChain(val);
		}
		// Here we have destroyed all the nodes
	}
}
void grow(HashTable *table){

	if(table){
		printf("Growing from size %02d to size %02d\n",table->maxSize,table->maxSize*2);

		HashTable *temp = initTable(table->maxSize);
		temp->values = table->values;
		temp->count = table->count;

		table->maxSize = table->maxSize*2;
		table->values = (Node **)malloc(sizeof(Node *)*table->maxSize);
		table->count = 0;

		for(int i = 0; i < temp->maxSize; i++){
			Node *n = temp->values[i];
			while(n){
				rehash(table,n->key,n->value);
				n = n->next;
			}
			if(temp->values[i]) destroyChain(temp->values[i]);
		}

		/*
		HashTable temp = *table;
		table->maxSize = table->maxSize * 2;
		// Here double the size of the array
		table->values = (Node **)malloc(sizeof(Node *)*(table->maxSize));
		
		uint32_t i = 0;
		
		for(i = 0; i < temp.maxSize; i++){
			Node *no = temp.values[i];
			if(no){
				while(no){
					printf("REHASHING %s\n",no->key);
					rehash(table,no->key,no->value);	
					no = no->next;
				}
			} 
		}
		
		for(i = 0; i < temp.maxSize; i++){
			if(temp.values[i]){
				destroyChain(temp.values[i]);
			}
		}
		*/
		
	}
}
void shrink(HashTable *table){
	if(table){
		printf("Shrinking from size %02d to size %02d\n",table->maxSize,(int)floor(table->maxSize/2));
		//Node *temp = table->values;
		//HashTable temp = *table;
		
		// table contains  a ptr to the old table
		// tvals contains a ptr to the node array in the old table
		// tmax is the max # of vals in the old table

		//Node ** tvals = table->values;
		//uint32_t tmax = table->maxSize;

		HashTable *theTable = initTable(table->maxSize);
		theTable->values = table->values;
/*
		printf("Printing all in old table\n");
		printAllPairs(table);
		printf("Printing all in new table\n");
		printAllPairs(theTable);
*/
		// Add clamp or something
		table->maxSize = floor(table->maxSize/2);
		table->count = 0;
		table->values = (Node **)malloc(sizeof(Node *)*table->maxSize);
		/*
		printf("Address of old val : %p new val %p\n",tvals,table->values);
		uint32_t i = 0;

		printf("Printing the values of the newly allocated table\n");
		printAllPairs(table);
		printf("The next table should mimick the first two\n");
		printAllPairs(theTable);

		printf("The old table's max size was: %08x\n",theTable->maxSize);
		*/
		for(int i = 0; i < theTable->maxSize; i++){
			//printf("Looking at index %02d\n",i);
			Node *n = theTable->values[i];
			while(n){
				//printf("Looking at element -> %s \n",n->key);
				rehash(table,n->key,n->value);
				n = n->next;
			}
			if(theTable->values[i]) destroyChain(theTable->values[i]);
		}

	//	printf("Done with rehashing everything, now showing the new table with n = %02d and m = %08x\n",table->count,table->maxSize);
		//printAllPairs(table);
/*
		// Make sure things are accessible in order
		printf("At shrinking point@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
		printAllPairs(theTable);

		for(int i = 0; i < theTable->maxSize; i++){
			Node *n = theTable->values[i];
			uint32_t s = 0;
			while(n){
				//printf("There is an element in the talbe with key %s and value %s stored at index %02d in a chain of size %02d \n",n->key,n->value,i,s);
				//s++;

				printf("REHASHING\n");
				rehash(table,n->key,n->value);
				n = n->next;
			}
		}
		*/
/*
		for(i = 0; i < tmax; i++){
			Node *no = tvals[i];
			printf("At index %02d ----------- in the original talbe \n",i);
			if(no){
				do{
					printf("REHASHING %s\n",no->key);
					rehash(table,no->key,no->value);
					no = no->next;
				}while(no);
			}
		}

		for(i = 0; i < tmax; i++){
			if(tvals[i]){
				destroyChain(tvals[i]);
			}
		}
	*/
	}
}