int main() { HashTable *addr = ht_create(5); int index; print_hashtable(addr); index = ht_put(addr, "a", "abcd"); printf("%d", index); return 0; }
int main(void) { HashTable *ht; ht = ht_create(3); if (ht == NULL) { return 1; } ht_put(ht, "key 1", "val 1"); ht_put(ht, "key 2", "val 2"); ht_put(ht, "key 3", "val 3"); ht_put(ht, "key 4", "val 4"); ht_put(ht, "key 5", "val 5"); ht_put(ht, "key 4", "NEW VAL 4"); print_hashtable(ht); print_str(ht_get(ht, "key 4")); print_str("\n"); ht_free(ht); return 0; }
int main() { const int TABLE_SIZE = 101; int i; Hashtable* ht_str_str = new_hashtable(TABLE_SIZE); print_hashtable(ht_str_str); srand(time(NULL)); for (i = 0; i < TABLE_SIZE << 1; ++i) { int len = rand() % 100 + 1; int key_size = len + 1; char* strkey = (char*)malloc(key_size); if (strkey == NULL) { continue; } strkey[len] = '\0'; len--; while (len >= 0) { strkey[len] = (char)rand(); if (strkey[len] == '\0') { continue; } len--; } len = rand() % 100 + 1; int val_size = len + 1; char* strval = (char*)malloc(val_size); if (strval == NULL) { free(strkey); continue; } strval[len] = '\0'; len--; while (len >= 0) { strval[len] = (char)rand(); if (strval[len] == '\0') { continue; } len--; } hashtable_add(ht_str_str, (BYTE*)strkey, key_size, (BYTE*)strval, val_size); } print_hashtable(ht_str_str); /* the function strlen(const char* s) return the length of string s(not include the end char '\0')*/ hashtable_add(ht_str_str, (BYTE*)"hello world", strlen("hello world") + 1, (BYTE*)"f**k haitai", strlen("hello world") +1); printf("find :%d\n", hashtable_contains_key(ht_str_str, (BYTE*)"hello world", strlen("hello world") + 1)); printf("find :%d\n", hashtable_contains_key(ht_str_str, (BYTE*)"f**k world", strlen("f**k world") + 1)); char* pstr = (char*)hashtable_getval(ht_str_str, (BYTE*)"hello world", 12); printf("get :%s\n", pstr); free(pstr); print_hashtable(ht_str_str); BYTE* p = NULL; hashtable_add(ht_str_str, (BYTE*)&p, sizeof(p), (BYTE*)"The key is NULL", 16); int* k = NULL; pstr = (char*)hashtable_getval(ht_str_str, (BYTE*)&k, sizeof(k)); printf("'null' key's value :%s\n", pstr); free(pstr); hashtable_remove(ht_str_str, (BYTE*)&p, sizeof(p)); hashtable_empty(ht_str_str); //print_hashtable(ht_str_str); hashtable_dispose(ht_str_str); //print_hashtable(ht_str_str); getchar(); return 0; }
int main( int argc, char **argv ) { int i; int N=NUMBER_KEYS; // The number of keys to insert in the hash_table char* keys[NUMBER_KEYS]={KEYS}; char* values[NUMBER_KEYS]={VALUES}; hashtable *hash_table = new_hashtable(); //The definition of the zize intialized to 8 srand(time(NULL)); // here we start by genarting a random keys and values printf("############ Initialisation of keys #################################################################\n\n"); for(i=0;i<N;i++) { printf("keys[%d]==\"%s\" with hash %d\n",i,keys[i],hash(keys[i])%hash_table->size); } printf("\n############ Initialisation of values #############################################################\n\n"); for(i=0;i<N;i++) { printf("values[%d]==\"%s\"\n",i,values[i]); } // insertion of key-value pairs in the hash_table printf("\n############ Insertion of (key,value) pairs ########################################################\n\n"); for(i=0;i<N;i++) { insert_resize_pair(&hash_table,keys[i],values[i]); printf("Insertion of (\"%s\",\"%s\") succed , SIZE = %d \n",keys [i],values[i],hash_table->size); } printf("\n Our hash table = "); print_hashtable(hash_table); // reinsertion of some pairs in the hash_table printf("\n############ Reinsertion of some pairs #############################################################\n\n"); insert_pair( hash_table, keys[1], "19" ); printf("Reinsertion of (\"%s\",\"%s\") succed \n",keys [1],"19"); insert_pair( hash_table, keys[2], "13" ); printf("Reinsertion of (\"%s\",\"%s\") succed \n",keys [2],"13"); printf("the hash table = "); print_hashtable(hash_table); // getting the values of a given key printf("\n############ Getting the value for a given key #######################################################\n\n"); for(i=0;i<N;i++) printf("hash_table of \"%s\" gives \"%s\"\n",keys[i], get_value( hash_table, keys[i])); // removing the some keys from the hash table printf("\n############ Removing some keys from hash table ######################################################\n\n"); printf("Initialy we have\n"); print_hashtable(hash_table); for(i=0;2*i<N;i++) { delate_key( hash_table, keys[2*i]); printf("we delate the key \"%s\"\n",keys[2*i]); print_hashtable(hash_table); } printf("\n#### After free we print the hash_table to verify #################################################\n\n"); free_hashtable(hash_table); print_hashtable(hash_table); return 0; }
void print_key(redis_instance* inst, char* key, size_t len) { long long size = 0; int flag = get_key_type(inst, key, len); long long pttl = get_key_pttl(inst, key, len); printf("{key:%s, type:%s, pttl:%lldms, db:%lu,", key, type_name[flag], pttl, inst->db); switch (flag) { case KSTRING: { redisReply* kv = (redisReply*)redisCommand(inst->cxt, "get %b", key, len); assert(kv != nil); assert(kv->type == REDIS_REPLY_STRING); assert(kv->len != 0); pline(" value:%s}", kv->str); freeReplyObject(kv); } break; case KHASH: { size = get_hashtable_size(inst, key, len); printf(" size:%lld", size); if (size) { printf(",\n kvs:["); print_hashtable(inst, key, len); pline("\b \n ]"); } pline("}"); } break; case KLIST: { size = get_list_size(inst, key, len); printf(" size:%lld", size); if (size) { printf(",\n values:["); print_list(inst, key, len); pline("\b \n ]"); } pline("}"); } break; case KSET: { size = get_set_size(inst, key, len); printf(" size:%lld", size); if (size) { printf(",\n values:["); print_set(inst, key, len); pline("\b \n ]"); } pline("}"); } break; case KSSET: { size = get_sset_size(inst, key, len); printf(" size:%lld", size); if (size) { printf(",\n values:["); print_sset(inst, key, len); pline("\b \n ]"); } pline("}"); } break; case KNONE: { FATAL("none type of key:%s", key); } break; case KUNKOWN: { FATAL("unknown type of key:%s", key); } } }