コード例 #1
0
ファイル: hashTableTest.c プロジェクト: sergafts/softdevalg
int main(void)
{
	hashTable ht;
	InitHashTable(&ht, 100, print, equal, hashFun);
	int a1 =2;
	int a2 =4;

	InsertHashTable(ht, &a1);

	//print(SearchHashTable(ht, &a1));
	int *r =(int*)SearchHashTable(ht, &a1);
	assert( *r == 2 );

	r = SearchHashTable(ht, &a2);
	assert( r == NULL );

	InsertHashTable(ht, &a2);
	r = SearchHashTable(ht, &a2);
	assert( *r == 4);

	DestroyHashTable(&ht);
	printf("hashTable OK\n");
	
	
	return 0;
}
コード例 #2
0
ファイル: test.c プロジェクト: Andy1985/HashTable
int main(int argc,char* argv[])
{
	hashdata** ht;

	InitHashTable(&ht,N);

	const char* words[15] = {
		"ske","abs","mps","test","tt","total","smtp","mbsc",
		"adfads","asdfa","dfads","13413la","tt","linux","windows"
	};

	int i;
	for (i = 0; i < 15; i++)
	{
		AddData(ht,N,words[i]);
	}
	PrintHashTable(ht,N);

	for (i = 0; i < 15; i++)
	{
		DeleteData(ht,N,words[i]);
	}
	PrintHashTable(ht,N);

	DestroyHashTable(&ht,N);

	return 0;
}
コード例 #3
0
ファイル: LSH.c プロジェクト: sergafts/softdevalg
void DestroyLSH(LSH** lshptr) {
    int i;
    LSH *lsh = *lshptr;
    for(i=0; i<lsh->L; i++) {
        data.destroyStruct(&(lsh->tables[i].GConfiguration));
        DestroyHashTable(&(lsh->tables[i].tables));
    }
    free(lsh->tables);
    free(*lshptr);
}
コード例 #4
0
ファイル: hashtableTester.c プロジェクト: zhuowei/csc190
void sentinelTest() {
	HashTablePTR table = (HashTablePTR) junkdatasentinel;
	VER(InsertEntry(table, NULL, NULL, NULL), "Insert");
	VER(DeleteEntry(table, NULL, NULL), "Delete");
	VER(FindEntry(table, NULL, NULL), "Find");
	VER(GetKeys(table, NULL, NULL), "Get");
	VER(GetLoadFactor(table, NULL), "Load");
	VER(GetHashTableInfo(table, NULL), "Info");
	VER(SetResizeBehaviour(table, 0, 0, 0), "Resize factor");
	VER(DestroyHashTable(&table), "Destroy");
}
コード例 #5
0
ファイル: srusuprt.c プロジェクト: jossk/open-watcom-v2
void FreeSru( void ) {
/*********************/

    statement   *curr;

    if( SRU.name ) {
        MemFree( SRU.name );
    }
    if( SRU.con_name ) {
        MemFree( SRU.con_name );
    }
    if( SRU.name ) {
        MemFree( SRU.des_name );
    }
    while( SRU.head_stmt ) {
        curr = SRU.head_stmt;
        SRU.head_stmt = curr->next;
        freeStatement( curr );
    }
    DestroyHashTable( SRU.type_prots );
    DestroyHashTable( SRU.typ_tab );
    memset( &SRU, 0, sizeof( sru_file ) ); /* clear all records */
}
コード例 #6
0
ファイル: LSH.c プロジェクト: sergafts/softdevalg
int PNNRange(LSH* lsh, value query, int P,value* array) {

    int i=0;
    int hashSize=0;
    hashTable neighbors;
    double* distArr=NULL;

    for (i = 0; i < lsh->L; i++) {

        //gets bucket where the value is
        List bucketList = GetListHashTable(lsh->tables[i].tables,
                                           &query, lsh->tables[i].GConfiguration);
        hashSize+=SizeList(bucketList);
    }
    InitHashTable(&neighbors,hashSize,sizeof(value),NULL,
                  data.distance,hashFunc,NULL);

    for (i = 0; i < lsh->L; i++) {


        //gets bucket where the value is
        List bucketList = GetListHashTable(lsh->tables[i].tables,
                                           &query, lsh->tables[i].GConfiguration);
        value* lValue = GetFirst(bucketList);
        //get all neighbours in range an place them in a list
        while (lValue != NULL) {
            InsertHashTable(neighbors, lValue,NULL);
            lValue = GetNext(bucketList);
        }
    }
    value** hashArr=malloc( GetHashSize(neighbors) * sizeof(value*));
    // WARNING //
    HashToArr(neighbors,(void**)hashArr);
    distArr=malloc(GetHashSize(neighbors)*sizeof(double));


    for(i=0; i<GetHashSize(neighbors); i++) {
        distArr[i]=data.distance(hashArr[i],&query);
    }

    QuickSortValue(distArr, hashArr,  GetHashSize(neighbors));

    for(i=0; (i<P && i<GetHashSize(neighbors)-1 ); i++) {
        array[i]=*hashArr[i+1];
    }
    DestroyHashTable(&neighbors);
    free(hashArr);
    free(distArr);
    return i;
}
コード例 #7
0
ファイル: hashtable.c プロジェクト: aqshvartz/Third
int main()
{
    int n, m, i;
    if(DBG){
        f1 = fopen("d:\\test.txt", "rt");
        f2 = fopen("d:\\text1.txt", "wt");
    }
    DBG ? fscanf(f1, "%d%d", &n, &m) : scanf("%d%d", &n, &m);
    HashTable H;
    HashTable * h = &H;
    InitHashTable(h, m);

    char str[40];
    int key, val;
    for(i=0; i<n; i++){
/*
if(i==6869){
        printf("sdf");
}
if(i==6870){    printf("%d", At(h, 900473962));exit(0);}
if(i==9013){    printf("sdf");}
*/
        DBG ? fscanf(f1, "%s", str) : scanf("%s", str);
        if (str[1] == 'S') {
            DBG ? fscanf(f1,"%d%d", &key, &val) : scanf("%d%d", &key, &val);
//            if(DBG)fprintf(f2, "%d\t%s %d %d\n", i+3, str, key, val);
            Assign(h, key, val);
        }
        if (str[1] == 'T') {
            DBG ? fscanf(f1,"%d", &key) : scanf("%d", &key);
//            if(DBG)fprintf(f2, "%d\t%s %d\n", i+3, str, key);
            DBG ? fprintf(f2,"%d\n", At(h, key)) : printf("%d\n", At(h, key));
        }
    }
    DestroyHashTable(h);
    if(DBG){
        fclose(f1);
        fclose(f2);
    }
    return 0;
}
コード例 #8
0
 void main()
 {
   ElemType r[N]={{17,1},{60,2},{29,3},{38,4},{1,5},{2,6},{3,7},{4,8},{60,9},{13,10}};
   HashTable h;
   int i,p;
   Status j;
   KeyType k;
   InitHashTable(&h);
   for(i=0;i<N-1;i++)
   { /* 插入前N-1个记录 */
     j=InsertHash(&h,r[i]);
     if(j==DUPLICATE)
       printf("表中已有关键字为%d的记录,无法再插入记录(%d,%d)\n",r[i].key,r[i].key,r[i].ord);
   }
   printf("按哈希地址的顺序遍历哈希表:\n");
   TraverseHash(h,print);
   printf("请输入待查找记录的关键字: ");
   scanf("%d",&k);
   j=Find(h,k,&p);
   if(j==SUCCESS)
     print(p,h.elem[p]);
   else
     printf("没找到\n");
   j=InsertHash(&h,r[i]); /* 插入第N个记录 */
   if(j==ERROR) /* 重建哈希表 */
     j=InsertHash(&h,r[i]); /* 重建哈希表后重新插入第N个记录 */
   printf("按哈希地址的顺序遍历重建后的哈希表:\n");
   TraverseHash(h,print);
   printf("请输入待查找记录的关键字: ");
   scanf("%d",&k);
   j=Find(h,k,&p);
   if(j==SUCCESS)
     print(p,h.elem[p]);
   else
     printf("没找到\n");
   DestroyHashTable(&h);
 }
コード例 #9
0
ファイル: jc_goto.c プロジェクト: nightstyles/focp
jc_void PopGotoTable(CJcGotoStack * pStack)
{
	jc_uint i;
	CJcGoto* pGoto;
	CJcGotoTable *pPrev, * pTable;

	pTable = pStack->pGotoTable;
	if(pTable)
	{
		pPrev = pTable->pPrev;
		DestroyHashTable(pTable->pLabelTable);
		if(pTable->pGotoTable)
		{
			for(i=0; i<pTable->nCount; ++i)
			{
				pGoto = pTable->pGotoTable+i;
				ClearString(&pGoto->oLabelName);
			}
			g_oInterface.Free(pTable->pGotoTable);
		}
		g_oInterface.Free(pTable);
		pStack->pGotoTable = pPrev;
	}
}
コード例 #10
0
ファイル: hashtableTester.c プロジェクト: zhuowei/csc190
int main() {
	HashTablePTR table = NULL;
	for(;;) {
		char cmd[80];
		printf("Command: ");
		int retval = scanf("%79s", cmd);
		if (retval <= 0) break;
		if (strcmp(cmd, "create") == 0) {
			printf("Size: ");
			int size;
			scanf("%d", &size);
			/* note: this is a signed int because unsigned int
			 * conversion in scanf is drunk and return 0xffffffff
			 * for -1.
			 */
			if (size < 0) {
				printf("Size cannot be negative, clamping to 0.\n");
				size = 0;
			}
			if (table != NULL) DestroyHashTable(&table);
			CreateHashTable(&table, (unsigned int) size);
			
			if (table == NULL) {
				printf("create returned NULL\n");
			} else {
				printf("create returned non-NULL\n");
			}
		} else if (strcmp(cmd, "destroy") == 0) {
			freeHashTableContents(table);
			DestroyHashTable(&table);
			if (table != NULL) {
				printf("FAIL: destroy did not set NULL");
			}
		} else if (strcmp(cmd, "print") == 0) {
			printHashTable(table);
		} else if (strcmp(cmd, "set") == 0) {
			printf("Key: ");
			char key[81];
			scanf(" %80s", key);
			char* value = malloc(81 * sizeof(char));
			scanf(" %80s", value);
			char* existingData = NULL;
			int success = InsertEntry(table, key, value, (void**) &existingData);
			switch(success){
				case GET_NONE:
					printf("Inserted into blank space\n");
					break;
				case GET_COLLIDE:
					printf("Inserted after resolving hash collision\n");
					break;
				case GET_EXISTS:
					printf("Inserted after removing existing data: %s\n", existingData);
					free(existingData);
					break;
				default:
					printf("InsertEntry failed\n");
					free(value);
					break;
			}
			if (success >= 0) {
				HashTableInfo info;
				int status2 = GetHashTableInfo(table, &info);
				if (status2 == 0) {
					if (info.dynamicBehaviour && info.useFactor > info.expandUseFactor) {
						printf("Insert FAIL: use factor %f"
							" greater than expand use factor. %f\n",
							info.useFactor, info.expandUseFactor);
					}
				}
			}
		} else if (strcmp(cmd, "read") == 0) {
			printf("Key: ");
			char key[81];
			scanf(" %80s", key);
			char* value;
			int success = FindEntry(table, key, (void**) &value);
			if (success == 0) {
				printf("%s\n", value);
			} else {
				printf("readPosition returned failure\n");
			}
		} else if (strcmp(cmd, "delete") == 0) {
			printf("Key: ");
			char key[81];
			scanf(" %80s", key);
			char* value;
			int success = DeleteEntry(table, key, (void**) &value);
			if (success == 0) {
				printf("Deleted (was %s)\n", value);
				free(value);
				HashTableInfo info;
				int status2 = GetHashTableInfo(table, &info);
				if (status2 == 0) {
					if (info.dynamicBehaviour &&
						info.useFactor < info.contractUseFactor) {
						printf("Delete FAIL: use factor %f"
							" less than contract use factor. %f\n",
							info.useFactor, info.contractUseFactor);
					}
				}
			} else {
				printf("DeleteValue returned failure\n");
			}
		} else if (strcmp(cmd, "load") == 0) {
			float myfloat;
			int status = GetLoadFactor(table, &myfloat);
			if (status == 0) {
				printf("Load factor: %f\n", myfloat);
			} else {
				printf("Load factor returned failure\n");
			}
		} else if (strcmp(cmd, "sentinel") == 0) {
			sentinelTest();
		} else if (strcmp(cmd, "info") == 0) {
			HashTableInfo info;
			int status = GetHashTableInfo(table, &info);
			if (status == 0) {
				printf(
					"Bucket count: %u\n"
					"Load factor: %f\n"
					"Use factor: %f\n"
					"Largest bucket size: %u\n"
					"Dynamic behaviour: %d\n"
					"Expand use factor: %f\n"
					"Contract use factor: %f\n",
					info.bucketCount, info.loadFactor, info.useFactor, info.largestBucketSize,
					info.dynamicBehaviour, info.expandUseFactor, info.contractUseFactor);
			} else {
				printf("GetHashTableInfo returned failure\n");
			}
		} else if (strcmp(cmd, "setresize") == 0) {
			int resize;
			float expand, contract;
			printf("Resize: (0/1) ");
			scanf("%d", &resize);
			printf("Expand: ");
			scanf("%f", &expand);
			printf("Contract: ");
			scanf("%f", &contract);
			int status = SetResizeBehaviour(table, resize, expand, contract);
			if (status != 0) {
				printf("Set resize returned failure\n");
			}
		} else if (strcmp(cmd, "quit") == 0) {
			break;
		} else {
			printf("Invalid command\n");
		}
	}
	if (table != NULL) {
		freeHashTableContents(table);
		DestroyHashTable(&table);
	}
	return 0;
}