Beispiel #1
0
//在main中测试argv[1]的三个hash值:  
//./hash  "arr/units.dat"  
//./hash  "unit/neutral/acritter.grp"  
int main( int argc, char **argv )  
{  
    unsigned long ulHashValue;  
    int i = 0;  
    if ( argc != 2 )  
    {  
        printf("please input two arguments/n");  
        return -1;  
    }  
	/*初始化数组:crytTable[0x500]*/  
	prepareCryptTable();  
	/*打印数组crytTable[0x500]里面的值*/  
	// for ( ; i < 0x500; i++ )  
	// {  
	// 	if ( i % 10 == 0 )  
	// 	{  
	// 		printf("/n");  
	// 	}  
	// 	printf("%-12X", cryptTable[i] );  
	// }  
	ulHashValue = HashString( argv[1], 0 );  
	printf("----%lu ----\n", ulHashValue );  
	ulHashValue = HashString( argv[1], 1 );  
	printf("----%lu ----\n", ulHashValue );  
	ulHashValue = HashString( argv[1], 2 );  
	printf("----%lu ----\n", ulHashValue );
	ulHashValue = getHashIndex("haha", 100);
	printf("----%lu ----\n", ulHashValue );
	ulHashValue = getHashIndex("hehe", 100);
	printf("----%lu ----\n", ulHashValue );
	return 0;  
}  
Beispiel #2
0
char *test_deleteHashElement_single() {
// simple hash function	
	struct Symbol *symbol =  calloc(1, sizeof(struct Symbol));
   	struct hash *hash = createHash(&getHashedKeySimple);
	mu_assert("Call to createHash does not seg fault.", 1);	

	createHashElement(hash, "GREEN", symbol);
	createHashElement(hash, "bb", symbol);
	createHashElement(hash, "bbb", symbol);
	createHashElement(hash, "bbbb", symbol);

	int index  = getHashIndex(hash, "G");
	struct hashElement *element = findHashElementByKey(hash, "GREEN");

	mu_assert("unexpected element at in single bucket list.", 
 		hash->elements[index] == element);

	mu_assert("Delete function did not remove single bucket element.", 
 		deleteHashElement(hash, "GREEN") == 0);

	mu_assert("Single element not deleted.", 
 		hash->elements[index] == NULL);

	destroyHash(&hash);
	mu_assert("Call to destroyHash does not seg fault.", 1);

//normal hash function
	hash = createHash(&getHashedKeySimple);
	mu_assert("Call to createHash does not seg fault.", 1);	

	createHashElement(hash, "GREEN", symbol);
	createHashElement(hash, "bb", symbol);
	createHashElement(hash, "bbb", symbol);
	createHashElement(hash, "bbbb", symbol);

	index  = getHashIndex(hash, "G");
	element = findHashElementByKey(hash, "GREEN");

	mu_assert("unexpected element at in single bucket list.", 
 		hash->elements[index] == element);

	mu_assert("Delete function did not remove single bucket element.", 
 		deleteHashElement(hash, "GREEN") == 0);

	mu_assert("Single element not deleted.", 
 		hash->elements[index] == NULL);

	destroyHash(&hash);
	mu_assert("Call to destroyHash does not seg fault.", 1);

	return NULL;
}
void MapHashTableBuildable::
addItem( const MC2BoundingBox* bbox, uint32 itemID ) {

   uint32 hHashStart, vHashStart, hHashEnd, vHashEnd;
   
   //Get indexes for hashcells to put elements in
   getHashIndex( bbox->getMinLon(), bbox->getMinLat(), hHashStart, vHashStart );
   getHashIndex( bbox->getMaxLon(), bbox->getMaxLat(), hHashEnd, vHashEnd );

   for( uint32 v = vHashStart; v <= vHashEnd; v++ ) {
      for( uint32 h = hHashStart; h <= hHashEnd; h++ ) {
         m_cells[ v ][ h ].push_back( itemID );
      }
   }
}
Beispiel #4
0
//can't test on normal hash function. can't find collisons
char *test_deleteHashElement_begining() {
	struct Symbol *symbol =  calloc(1, sizeof(struct Symbol));
	struct hash *hash = createHash(&getHashedKeySimple);
	mu_assert("Call to createHash does not seg fault.", 1);	

   	createHashElement(hash, "GREEN", symbol);
	createHashElement(hash, "bb", symbol);
	createHashElement(hash, "bbb", symbol);
	createHashElement(hash, "bbbb", symbol);
	
	struct hashElement *element = findHashElementByKey(hash, "bb");
	struct hashElement *newHead = element->next;

	int index  = getHashIndex(hash, "b");
	mu_assert("unexpected element at head of bucket list.", 
 		hash->elements[index] == element);

	mu_assert("Delete function did not remove begining element.", 
 		deleteHashElement(hash, "bb") == 0);

	mu_assert("Begining element not deleted.", 
 		hash->elements[index] == newHead);

	mu_assert("Did not delete front of list propertly.", 
 		newHead->prev == NULL);

	mu_assert("Resulting bucket should have more than one element.", 
 		newHead->next != NULL);	

	destroyHash(&hash);
	mu_assert("Call to destroyHash does not seg fault.", 1);

	return NULL;
}
    void HashDictionary::insert(const int p_key, const int p_value)
    {
        unsigned int idx = 0;
        unsigned int count = 0;

        do {
            idx = getHashIndex(p_key, count);
            if(hashTable_[idx].state == Pair::State::EMPTY)
                break;
            if(hashTable_[idx].state == Pair::State::USED
                    && hashTable_[idx].key == p_key)
                break;
            ++count;
        } while(true);

        hashTable_[idx].value = p_value;

        if(hashTable_[idx].state != Pair::State::USED) {
            // this key was not in the dict before
            hashTable_[idx].key = p_key;
            hashTable_[idx].state = Pair::State::USED;
            ++size_;
        }

    }
Beispiel #6
0
/*-------------------------------------------------------------------------------------
 * PURPOSE: adds the link to the given node for the appropriate headerItem
 * PARM   : *node - that is to be linked to the header table
 * REMARKS: - this method links the node without question
 *-----------------------------------------------------------------------------------*/
void HeaderTable::linkNode(FPTreeNode *node){
    int hashIndex = 0;
    
    if (node != NULL && node->getData() != NULL){
        hashIndex = getHashIndex(node->getData());
        
        if (hashIndex != -1 && freqItems[hashIndex] != NULL){
            node->setNextSimilarNode(freqItems[hashIndex]->getNode()->getNextSimilarNode());
            freqItems[hashIndex]->getNode()->setNextSimilarNode(node);
        }            
    }
}
Beispiel #7
0
char *test_getHashIndex() {
//simple hash function	
	struct hash *symbolTable = createHash(&getHashedKeySimple);
	mu_assert("Call to createHash does not seg fault.", 1);	

	int index = getHashIndex(symbolTable, "b");
	mu_assert("getHashIndex does not return expected value on key 'b'", 
		index == 98);

	index = getHashIndex(symbolTable, "W");
	mu_assert("getHashIndex does not return expected value on key 'b'", 
		index == 87);

	index = getHashIndex(symbolTable, "=");
	mu_assert("getHashIndex does not return expected value on key 'b'", 
		index == 61);			

	destroyHash(&symbolTable);
	mu_assert("Call to destroyHash does not seg fault.", 1);	

//normal hash function	
	symbolTable = createHash(&getHashedKeyNormal);
	mu_assert("Call to createHash does not seg fault.", 1);	

	index = getHashIndex(symbolTable, "b");
	mu_assert("getHashIndex does not return expected value on key 'b'", 
		index == 671);

	index = getHashIndex(symbolTable, "W");
	mu_assert("getHashIndex does not return expected value on key 'b'", 
		index == 660);			

	destroyHash(&symbolTable);
	mu_assert("Call to destroyHash does not seg fault.", 1);	

	return NULL;
}
Beispiel #8
0
/** loads a file of the given name, returns the size unless size is a null pointer, and optionally decrypts it */
int global_files::check_file(const char *name, file_locations location)
{
	switch (location)
	{
		case FILE_REGULAR1:
			{
			char *realname = new char[strlen(name) + strlen(lineage_dir) + 1];
			strcpy(realname, lineage_dir);
			strcat(realname, name);
			FILE *thefile = fopen(realname, "rb");
			delete [] realname;
			realname = 0;
			if (thefile == 0)
				return -1;
			fclose(thefile);
			}
			break;
		case FILE_REGULAR2:
			{
			FILE *thefile = fopen(name, "rb");
			if (thefile == 0)
				return -1;
			fclose(thefile);
			}
			break;
		case FILE_TILEPACK:
			return tilepack->check_file(name);
			break;
		case FILE_SPRITEPACK:
			return spritepack[0]->check_file(name);
			break;
		case FILE_SPRITESPACK:
			{
			int index = getHashIndex(name) + 1;
			return spritepack[index]->check_file(name);
			}
			break;
		case FILE_TEXTPACK:
			return textpack->check_file(name);
			break;
		case FILE_LAUNCHER:
		case FILE_LAUNCHERU:
		case FILE_NOTHING:
		default:
			return -1;
		break;
	}
	return 1;
}
    bool HashDictionary::contains(const int p_key)
    {
        unsigned int idx = 0;
        unsigned int count = 0;

        do {
            idx = getHashIndex(p_key, count);
            if(hashTable_[idx].state == Pair::State::EMPTY)
                break;
            if(hashTable_[idx].state == Pair::State::USED
                    && hashTable_[idx].key == p_key)
                break;
            ++count;
        } while(true);

        return hashTable_[idx].state != Pair::State::EMPTY;
    }
Beispiel #10
0
// Return node in the HashMap for given key, if present.
// NULL ==> key is not present in the HashMap.
//
// Function private to HashMap implementation
static node *
lookupNode(const HashMap *hm,   // IN
           const void *key)     // IN
{
    node *list;
    const int bucketIndex = getHashIndex(hm, key);
    const int keyVal = hm->fptr(key);

    for (list = hm->buckets[bucketIndex]; list != NULL; list = list->next) {
        if (hm->fptr(list->key) == keyVal) {
            // Found the key!
            return list;
        }
    }

    return NULL;
}
Beispiel #11
0
// 0  ==> key not present previously and new val successfully added
// 1  ==> key present previously and new val successfully updated
// -1 ==> error in key insertion and new val NOT added
int 
HashMap_put(HashMap *hm,            // IN
            void *key,              // IN
            void *val,              // IN
            void **prevVal)         // OUT
{
    node *n = lookupNode(hm, key);
    if (n != NULL) {
        // Key already present in the HashMap,
        // so simply replace the old val.
        *prevVal = n->val;
        n->val = val;
        return 1;
    }

    // Add the new key at the head of the bucket.
    {
        const int bucketIndex = getHashIndex(hm, key);
        node *newNode = malloc(sizeof *newNode);
        node *prevBucketHead;

        if (newNode == NULL) {
            return -1;
        }
        // Shallow copy of key.
        newNode->key = key;
        newNode->val = val;
        newNode->prev = NULL;

        prevBucketHead = hm->buckets[bucketIndex];
        if (prevBucketHead != NULL) {
            prevBucketHead->prev = newNode;
        }
        newNode->next = prevBucketHead;
        hm->buckets[bucketIndex] = newNode;
    }

    hm->totalElems++;

    *prevVal = NULL;
    return 0;
}
    int HashDictionary::get(const int p_key)
    {
        unsigned int idx = 0;
        unsigned int count = 0;

        do {
            idx = getHashIndex(p_key, count);
            if(hashTable_[idx].state == Pair::State::EMPTY)
                break;
            if(hashTable_[idx].state == Pair::State::USED
                    && hashTable_[idx].key == p_key)
                break;
            ++count;
        } while(true);

        if(hashTable_[idx].state != Pair::State::EMPTY)
            return hashTable_[idx].value;
        else
            throw std::logic_error("element not found");
    }
    void HashDictionary::remove(const int p_key)
    {
        unsigned int idx = 0;
        unsigned int count = 0;

        do {
            idx = getHashIndex(p_key, count);
            if(hashTable_[idx].state == Pair::State::EMPTY)
                break;
            if(hashTable_[idx].state == Pair::State::USED
                    && hashTable_[idx].key == p_key)
                break;
            ++count;
        } while(true);

        if(hashTable_[idx].state != Pair::State::EMPTY) {
            hashTable_[idx].state = Pair::State::DELETED;
            --size_;
        }
    }
Beispiel #14
0
/*-------------------------------------------------------------------------------------
 * PURPOSE: increments the count for the given item in the table
 *          - If the item does not exist, then adds the given item to table
 *          - else increments the existing similar item in table
 * PARM   : FPTreeItem, item to be incremented
 *-----------------------------------------------------------------------------------*/
void HeaderTable::increment(FPTreeItem *item){
    HeaderItem* found;
    int hashIndex;

    if (item != NULL){
        hashIndex = getHashIndex(item);
        
        if (hashIndex >=0 && hashIndex < MAX_DOMAIN_ITEMS){
            found = freqItems[hashIndex];
            
            if (found == NULL){ //if no entry
                FPTreeNode *tempFPTreeNode = new FPTreeNode(item, NULL, NULL);
                HeaderItem *tempHeader  = new HeaderItem(tempFPTreeNode);
                this->freqItems[hashIndex] = tempHeader;
                this->numDomainItems++;
            } else {
                found->getNode()->getData()->increaseSupport(item);
                delete(item);
            }
        }
    }
}
Beispiel #15
0
// TRUE  ==> key found and removed successfully
// FALSE ==> key not found
BOOL
HashMap_removeEntry(HashMap *hm,        // IN
                    const void *key)    // IN
{
    node *n = lookupNode(hm, key);
    if (!n) {
        return FALSE;
    }

    node *prevNode = n->prev;
    node *nextNode = n->next;

    if (prevNode != NULL) {
        prevNode->next = nextNode;
    }

    if (nextNode != NULL) {
        nextNode->prev= prevNode;
    }

    // Check whether head node of bucket is being removed.
    {
        const int bucketHead = getHashIndex(hm, key);
        if (hm->buckets[bucketHead] == n) {
            assert(prevNode == NULL);
            hm->buckets[bucketHead] = nextNode;        
        }
    }

    // Only a reference to key was stored(shallow copy), so need to free the key.

    free(n);

    hm->totalElems--;
    return TRUE;  
}
Beispiel #16
0
/** loads a file of the given name, returns the size unless size is a null pointer, and optionally decrypts it */
unsigned char* global_files::load_file(const char *name, int *size, file_locations location, int decrypting)
{
	unsigned char *buffer;
	buffer = 0;
	size_t filesize = 0;

	switch (location)
	{
		case FILE_REGULAR1:
			{
			char *realname = new char[strlen(name) + strlen(lineage_dir) + 1];
			strcpy(realname, lineage_dir);
			strcat(realname, name);
			FILE *thefile = fopen(realname, "rb");
			delete [] realname;
			realname = 0;
			if (thefile == 0)
				return 0;
			unsigned int result;
			fseek(thefile, 0, SEEK_END);
			filesize = ftell(thefile);
			fseek(thefile, 0, SEEK_SET);
			if (filesize <= 0)
				return 0;
			if (size != 0)
				*size = filesize;
			buffer = new unsigned char[filesize];
			result = fread(buffer, 1, filesize, thefile);
			if (result != filesize)
			{
				delete [] buffer;
				buffer = 0;
			}
			fclose(thefile);
			}
			break;
		case FILE_REGULAR2:
			{
			FILE *thefile = fopen(name, "rb");
			unsigned int result;
			filesize = 0;
			if (thefile != 0)
			{
				fseek(thefile, 0, SEEK_END);
				filesize = ftell(thefile);
				fseek(thefile, 0, SEEK_SET);
			}
			if (filesize == 0)
				return 0;
			buffer = new unsigned char[filesize];
			result = fread(buffer, 1, filesize, thefile);
			if (result != filesize)
			{
				delete [] buffer;
				buffer = 0;
			}
			fclose(thefile);
			}
			if (size != 0)
				*size = filesize;
			break;
		case FILE_TILEPACK:
			buffer = (unsigned char*)tilepack->load_file(name, size, decrypting);
			break;
		case FILE_SPRITEPACK:
			buffer = (unsigned char*)spritepack[0]->load_file(name, size, decrypting);
			break;
		case FILE_SPRITESPACK:
			{
			int index = getHashIndex(name) + 1;
			buffer = (unsigned char*)spritepack[index]->load_file(name, size, decrypting);
			}
			break;
		case FILE_TEXTPACK:
			buffer = (unsigned char*)textpack->load_file(name, size, decrypting);
			break;
		case FILE_LAUNCHER:
		case FILE_LAUNCHERU:
		case FILE_NOTHING:
		default:
			buffer = 0;
		break;
	}
//	if (buffer == 0)
//		printf("File %s was not loaded properly\n", name);
//	else
//		printf("File %s was loaded properly\n", name);
	return buffer;
}