💾 Archived View for gmi.noulin.net › gitRepositories › hashtable › file › hashtableMain.c.gmi captured on 2023-07-10 at 18:03:00. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
hashtableMain.c (4688B)
1 #! /usr/bin/env sheepy 2 /* or direct path to sheepy: #! /usr/local/bin/sheepy */ 3 4 #include "libsheepyObject.h" 5 6 // hash table with 2 single linked list per bucket 7 #include "hashtable.h" 8 9 // hash functions for the hash table 10 #include "shpPackages/hashfunctions/hashfunctions.h" 11 12 int argc; char **argv; 13 14 hashTbDef(, m3232, M32, u32, u32, u32); 15 16 u32 h(u32 k) { 17 return k; 18 } 19 20 int cmp(u32 k1, u32 k2) { 21 return k1 == k2 ? 0 : 1; 22 } 23 24 void freeM(u32 *k, u32 *v) { 25 } 26 27 #define HASHFUNC h 28 #define CMPFUNC cmp 29 #define FREEFUNC freeM 30 31 hashTbFunctions(, m3232, M32, u32, u32); 32 33 #undef HASHFUNC 34 #undef CMPFUNC 35 #undef FREEFUNC 36 37 // hash table type definition 38 hashTbT(hashchart /* new hash table type */, char* /* key type*/, u32 /* value type */, u32 /* hash type*/); 39 40 41 // hash table type definition with 64 bit hashes 42 hashNodeT(hashSipNodet, char*, u32, u64); 43 hashtableT(hashsipt, hashSipNodet); 44 45 void freeNode(char **key, u32* UNUSED value) { 46 //logI("free key %x\n", key); 47 free(*key); 48 } 49 50 // siphash setup for hashsipt table 51 const u8 *dict_hash_function_seed; 52 53 u64 sipHash(char *k) { 54 return siphash(k,strlen(k),dict_hash_function_seed); 55 } 56 57 // hash function for hashTb* macros 58 #define HASHFUNC strHashFNV1A 59 60 // comparison function for hashTb* macros 61 #define CMPFUNC strcmp 62 63 // free key and value function for hashTb* macros 64 #define FREEFUNC freeNode 65 66 // load factor: 2 67 #define LOADFACTOR_NUMERATOR 2 68 #define LOADFACTOR_DENOMINATOR 1 69 70 #define TEST_COUNT 257 71 72 int main(int ARGC, char** ARGV) { 73 74 argc = ARGC; argv = ARGV; 75 76 initLibsheepy(argv[0]); 77 setLogMode(LOG_FUNC); 78 79 // STEPS 80 // 81 // HASH TABLE USING FNV HASH FUNCTION 82 // 83 // hash table declaration 84 // hash table setup 85 // set/insert some key, value pairs 86 // get/search values 87 // remove key, value 88 // free hash table 89 // 90 // HASH TABLE USING SIPHASH 91 // 92 // hash table declaration 93 // hash table setup 94 // set/insert some key, value pairs 95 // get/search values 96 // remove key, value 97 // free hash table 98 99 // hash table declaration 100 hashchart h; 101 102 // hash table setup 103 hashtableInit(h, 128, LOADFACTOR_NUMERATOR, LOADFACTOR_DENOMINATOR); 104 105 char *s; 106 107 // set/insert some key, value pairs 108 range(i, TEST_COUNT) { 109 s = intToS(i); 110 /* hashtableAdd(h, s, i, HASHFUNC, CMPFUNC); */ 111 hashTbAdd(h, s, i); 112 if (!h.res) logE("ERROR"); 113 } 114 115 // get/search values 116 u32 result = 0; 117 char S[30]; 118 range(i, TEST_COUNT) { 119 bIntToS(S, i); 120 /* hashtableGet(h, S, result, HASHFUNC, CMPFUNC); */ 121 hashTbGet(h, S, result); 122 if (!h.res) {logE("ERROR %s", S);} 123 } 124 125 // remove key, value 126 /* hashtableDel(h, "15", HASHFUNC, CMPFUNC, FREEFUNC); */ 127 hashTbDel(h, "15"); 128 logI("del 15"); 129 logVarG(h.res); 130 131 // free hash table 132 /* hashtableFree(h, FREEFUNC); */ 133 hashTbFree(h); 134 135 136 // hash table using siphash 137 138 #undef HASHFUNC 139 #define HASHFUNC sipHash 140 141 // hash table declaration 142 hashsipt H; 143 hashSipNodet *nd = NULL; 144 context_hashSipNodet ctx; 145 // hash table setup 146 hashtableInit(H, 128, LOADFACTOR_NUMERATOR, LOADFACTOR_DENOMINATOR); 147 148 dict_hash_function_seed = hashtableSeed(H); 149 150 s = strdup("sheepy"); 151 hashTbAdd(H, s, 234); 152 hashTbGetNode(H, "sheepy", nd, ctx); 153 154 // set/insert some key, value pairs 155 range(i, TEST_COUNT) { 156 s = intToS(i); 157 hashTbAdd(H, s, i); 158 if (!H.res) logE("ERROR"); 159 } 160 161 /* hashtableTest(H); */ 162 /* put put */ 163 hashTbUpdateNodeContext(H, ctx); 164 logVarG(h.res); 165 hashTbDelNode(H, ctx); 166 //hashTbDel(H, "sheepy"); 167 168 /* hashtableTest(H); */ 169 170 // get/search values 171 range(i, TEST_COUNT) { 172 bIntToS(S, i); 173 hashTbGet(H, S, result); 174 if (!H.res) {logE("ERROR %s", S);} 175 } 176 177 u32 *val = NULL; 178 bool isNew; 179 hashTbAddOrFind(H, "1", val, isNew); 180 logVarG(*val); 181 logVarG(isNew); 182 s = strdup("qwe"); 183 hashTbAddOrFind(H, s, val, isNew); 184 logVarG(*val); 185 logVarG(isNew); 186 187 // get node 188 //hashtableGetNode(H, "3", nd, ctx, HASHFUNC, CMPFUNC); 189 hashTbGetNode(H, "3", nd, ctx); 190 logVarG(ctx.node->key); 191 logVarG(ctx.node->value); 192 logI("%p", nd); 193 free(nd->key); 194 nd->key = strdup("sheep"); 195 //hashtableSetKeyNode(H, ctx, HASHFUNC, CMPFUNC); 196 hashTbSetKeyNode(H, ctx); 197 //hashTbGetNode(H, "sheep", nd, ctx); 198 //hashtableDelNode(H, ctx, FREEFUNC); 199 hashTbDelNode(H, ctx); 200 201 hashTbUnlinkNode(H, "1", nd); 202 logVarG(nd->key); 203 hashTbFreeUnlinkedNode(H, nd); 204 205 hashTbAddOrFindNode(H, "asd", nd, ctx, isNew); 206 nd->key = strdup("asd"); 207 nd->value = 12; 208 logVarG(isNew); 209 210 // remove key, value 211 hashTbDel(H, "10"); 212 logI("del 10"); 213 logVarG(H.res); 214 215 // free hash table 216 hashTbFree(H); 217 218 m3232t h32; 219 initM32(&h32); 220 addM32(&h32, 1, 1); 221 m3232Nodet *node; 222 context_m3232Nodet ctx32; 223 }