⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example.c

📁 google hash table算法
💻 C
字号:
#include <stdlib.h>#include <stdio.h>#include <assert.h>#include <time.h>#include "libchash.h"static void TestInsert() {  struct HashTable* ht;  HTItem* bck;  ht = AllocateHashTable(10, 0);    /* value is 1 byte, 0: don't copy keys */  HashInsert(ht, PTR_KEY(ht, "January"), 31);  /* 0: don't overwrite old val */  bck = HashInsert(ht, PTR_KEY(ht, "February"), 28);  bck = HashInsert(ht, PTR_KEY(ht, "March"), 32);  bck = HashInsert(ht, PTR_KEY(ht, "Februaryw"), 11);  bck = HashFind(ht, PTR_KEY(ht, "Februaryw"));  printf("%d\t%d\n", bck->data, bck->key);		  	  bck = HashFind(ht, PTR_KEY(ht, "March"));  printf("%d\t%d\n", bck->data, bck->key);		  	  bck = HashFind(ht, PTR_KEY(ht, "February"));  printf("%d\t%d\n", bck->data, bck->key);		  	//  assert(bck);//  assert(bck->data == 28);  FreeHashTable(ht);}static void TestFindOrInsert() {  struct HashTable* ht;  int i;  int iterations = 1000000;  int range = 30;         /* random number between 1 and 30 */	  ht = AllocateHashTable(4, 0);    /* value is 4 bytes, 0: don't copy keys */	HashInsert(ht, 1, 8888);	HashInsert(ht, 1, 666);	  /* We'll test how good rand() is as a random number generator *///  for (i = 0; i < iterations; ++i) {//    int key = rand() % 100; //range;//    HTItem* bck = HashFindOrInsert(ht, key, 0);     /* initialize to 0 *///    bck->data++;                   /* found one more of them *///  }  for (i = 0; i < 10; ++i) {    HTItem* bck = HashFind(ht, i);    if (bck) {      printf("%3d: %d\n", bck->key, bck->data);  			    } else { //     printf("%3d: 0\n", i);    }  }// PrintHashTable(ht, 0, 2);  FreeHashTable(ht);}int main(int argc, char** argv) {  TestInsert();  TestFindOrInsert();  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -