| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include <esl_config.h> |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <limits.h> |
| 19 |
|
| 20 |
#include "easel.h" |
| 21 |
#include "esl_mem.h" |
| 22 |
|
| 23 |
#include "esl_keyhash.h" |
| 24 |
|
| 25 |
static ESL_KEYHASH *keyhash_create(uint32_t hashsize, int init_key_alloc, int init_string_alloc); |
| 26 |
static uint32_t jenkins_hash(const char *key, esl_pos_t n, uint32_t hashsize); |
| 27 |
static int key_upsize(ESL_KEYHASH *kh); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
ESL_KEYHASH * |
| 46 |
esl_keyhash_Create(void) |
| 47 |
{ |
| 48 |
return keyhash_create(128, |
| 49 |
128, |
| 50 |
2048); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
ESL_KEYHASH * |
| 72 |
esl_keyhash_CreateCustom(uint32_t hashsize, int kalloc, int salloc) |
| 73 |
{ |
| 74 |
ESL_DASSERT1((hashsize && ((hashsize & (hashsize-1)) == 0))); |
| 75 |
return keyhash_create(hashsize, kalloc, salloc); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
ESL_KEYHASH * |
| 87 |
esl_keyhash_Clone(const ESL_KEYHASH *kh) |
| 88 |
{ |
| 89 |
ESL_KEYHASH *nw; |
| 90 |
int h; |
| 91 |
|
| 92 |
if ((nw = keyhash_create(kh->hashsize, kh->kalloc, kh->salloc)) == NULL) goto ERROR; |
| 93 |
|
| 94 |
for (h = 0; h < kh->hashsize; h++) |
| 95 |
nw->hashtable[h] = kh->hashtable[h]; |
| 96 |
|
| 97 |
for (h = 0; h < kh->nkeys; h++) |
| 98 |
{ |
| 99 |
nw->nxt[h] = kh->nxt[h]; |
| 100 |
nw->key_offset[h] = kh->key_offset[h]; |
| 101 |
} |
| 102 |
nw->nkeys = kh->nkeys; |
| 103 |
|
| 104 |
memcpy(nw->smem, kh->smem, sizeof(char) * kh->sn); |
| 105 |
nw->sn = kh->sn; |
| 106 |
return nw; |
| 107 |
|
| 108 |
ERROR: |
| 109 |
esl_keyhash_Destroy(nw); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
char * |
| 123 |
esl_keyhash_Get(const ESL_KEYHASH *kh, int idx) |
| 124 |
{ |
| 125 |
return kh->smem + kh->key_offset[idx]; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
int |
| 135 |
esl_keyhash_GetNumber(const ESL_KEYHASH *kh) |
| 136 |
{ |
| 137 |
return kh->nkeys; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
size_t |
| 144 |
esl_keyhash_Sizeof(const ESL_KEYHASH *kh) |
| 145 |
{ |
| 146 |
size_t n = 0; |
| 147 |
|
| 148 |
if (kh) |
| 149 |
{ |
| 150 |
n += sizeof(ESL_KEYHASH); |
| 151 |
n += sizeof(int) * kh->hashsize; |
| 152 |
n += sizeof(int) * kh->kalloc * 2; |
| 153 |
n += sizeof(char) * kh->salloc; |
| 154 |
} |
| 155 |
return n; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
int |
| 167 |
esl_keyhash_Reuse(ESL_KEYHASH *kh) |
| 168 |
{ |
| 169 |
int i; |
| 170 |
|
| 171 |
for (i = 0; i < kh->hashsize; i++) kh->hashtable[i] = -1; |
| 172 |
kh->nkeys = 0; |
| 173 |
kh->sn = 0; |
| 174 |
return eslOK; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
void |
| 187 |
esl_keyhash_Destroy(ESL_KEYHASH *kh) |
| 188 |
{ |
| 189 |
if (kh == NULL) return; |
| 190 |
if (kh->hashtable != NULL) free(kh->hashtable); |
| 191 |
if (kh->key_offset != NULL) free(kh->key_offset); |
| 192 |
if (kh->nxt != NULL) free(kh->nxt); |
| 193 |
if (kh->smem != NULL) free(kh->smem); |
| 194 |
free(kh); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
void |
| 206 |
esl_keyhash_Dump(FILE *fp, const ESL_KEYHASH *kh) |
| 207 |
{ |
| 208 |
int idx; |
| 209 |
int h; |
| 210 |
int nkeys; |
| 211 |
int nempty = 0; |
| 212 |
int maxkeys = -1; |
| 213 |
int minkeys = INT_MAX; |
| 214 |
|
| 215 |
for (h = 0; h < kh->hashsize; h++) |
| 216 |
{ |
| 217 |
for (nkeys = 0, idx = kh->hashtable[h]; idx != -1; idx = kh->nxt[idx]) nkeys++; |
| 218 |
|
| 219 |
if (nkeys == 0) nempty++; |
| 220 |
if (nkeys > maxkeys) maxkeys = nkeys; |
| 221 |
if (nkeys < minkeys) minkeys = nkeys; |
| 222 |
} |
| 223 |
|
| 224 |
fprintf(fp, "Total keys: %d\n", kh->nkeys); |
| 225 |
fprintf(fp, "Hash table size: %d\n", kh->hashsize); |
| 226 |
fprintf(fp, "Average occupancy: %.2f\n", (float) kh->nkeys /(float) kh->hashsize); |
| 227 |
fprintf(fp, "Unoccupied slots: %d\n", nempty); |
| 228 |
fprintf(fp, "Most in one slot: %d\n", maxkeys); |
| 229 |
fprintf(fp, "Least in one slot: %d\n", minkeys); |
| 230 |
fprintf(fp, "Keys allocated for: %d\n", kh->kalloc); |
| 231 |
fprintf(fp, "Key string space alloc: %d\n", kh->salloc); |
| 232 |
fprintf(fp, "Key string space used: %d\n", kh->sn); |
| 233 |
fprintf(fp, "Total obj size, bytes: %d\n", (int) esl_keyhash_Sizeof(kh)); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
int |
| 267 |
esl_keyhash_Store(ESL_KEYHASH *kh, const char *key, esl_pos_t n, int *opt_index) |
| 268 |
{ |
| 269 |
uint32_t val = jenkins_hash(key, n, kh->hashsize); |
| 270 |
int idx; |
| 271 |
int status; |
| 272 |
|
| 273 |
if (n == -1) n = strlen(key); |
| 274 |
|
| 275 |
|
| 276 |
for (idx = kh->hashtable[val]; idx != -1; idx = kh->nxt[idx]) |
| 277 |
if (esl_memstrcmp(key, n, kh->smem + kh->key_offset[idx])) |
| 278 |
{ |
| 279 |
if (opt_index) *opt_index = idx; |
| 280 |
return eslEDUP; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
if (kh->nkeys == kh->kalloc) |
| 285 |
{ |
| 286 |
ESL_REALLOC(kh->key_offset, sizeof(int)*kh->kalloc*2); |
| 287 |
ESL_REALLOC(kh->nxt, sizeof(int)*kh->kalloc*2); |
| 288 |
kh->kalloc *= 2; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
while (kh->sn + n + 1 > kh->salloc) |
| 293 |
{ |
| 294 |
ESL_REALLOC(kh->smem, sizeof(char) * kh->salloc * 2); |
| 295 |
kh->salloc *= 2; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
idx = kh->nkeys; |
| 300 |
kh->key_offset[idx] = kh->sn; |
| 301 |
kh->sn += n+1; |
| 302 |
esl_memstrcpy(key, n, kh->smem + kh->key_offset[idx]); |
| 303 |
kh->nkeys++; |
| 304 |
|
| 305 |
|
| 306 |
kh->nxt[idx] = kh->hashtable[val]; |
| 307 |
kh->hashtable[val] = idx; |
| 308 |
|
| 309 |
|
| 310 |
if (kh->nkeys > 3*kh->hashsize) |
| 311 |
if ((status = key_upsize(kh)) != eslOK) goto ERROR; |
| 312 |
|
| 313 |
if (opt_index != NULL) *opt_index = idx; |
| 314 |
return eslOK; |
| 315 |
|
| 316 |
ERROR: |
| 317 |
if (opt_index != NULL) *opt_index = -1; |
| 318 |
return status; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
int |
| 333 |
esl_keyhash_Lookup(const ESL_KEYHASH *kh, const char *key, esl_pos_t n, int *opt_index) |
| 334 |
{ |
| 335 |
uint32_t val = jenkins_hash(key, n, kh->hashsize); |
| 336 |
int idx; |
| 337 |
|
| 338 |
if (n == -1) |
| 339 |
{ |
| 340 |
for (idx = kh->hashtable[val]; idx != -1; idx = kh->nxt[idx]) |
| 341 |
if (strcmp(key, kh->smem + kh->key_offset[idx]) == 0) |
| 342 |
{ |
| 343 |
if (opt_index) *opt_index = idx; |
| 344 |
return eslOK; |
| 345 |
} |
| 346 |
} |
| 347 |
else |
| 348 |
{ |
| 349 |
for (idx = kh->hashtable[val]; idx != -1; idx = kh->nxt[idx]) |
| 350 |
if (esl_memstrcmp(key, n, kh->smem + kh->key_offset[idx])) |
| 351 |
{ |
| 352 |
if (opt_index) *opt_index = idx; |
| 353 |
return eslOK; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
if (opt_index != NULL) *opt_index = -1; |
| 358 |
return eslENOTFOUND; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
ESL_KEYHASH * |
| 384 |
keyhash_create(uint32_t hashsize, int init_key_alloc, int init_string_alloc) |
| 385 |
{ |
| 386 |
ESL_KEYHASH *kh = NULL; |
| 387 |
int i; |
| 388 |
int status; |
| 389 |
|
| 390 |
ESL_ALLOC(kh, sizeof(ESL_KEYHASH)); |
| 391 |
kh->hashtable = NULL; |
| 392 |
kh->key_offset = NULL; |
| 393 |
kh->nxt = NULL; |
| 394 |
kh->smem = NULL; |
| 395 |
|
| 396 |
kh->hashsize = hashsize; |
| 397 |
kh->kalloc = init_key_alloc; |
| 398 |
kh->salloc = init_string_alloc; |
| 399 |
|
| 400 |
ESL_ALLOC(kh->hashtable, sizeof(int) * kh->hashsize); |
| 401 |
for (i = 0; i < kh->hashsize; i++) kh->hashtable[i] = -1; |
| 402 |
|
| 403 |
ESL_ALLOC(kh->key_offset, sizeof(int) * kh->kalloc); |
| 404 |
ESL_ALLOC(kh->nxt, sizeof(int) * kh->kalloc); |
| 405 |
for (i = 0; i < kh->kalloc; i++) kh->nxt[i] = -1; |
| 406 |
|
| 407 |
ESL_ALLOC(kh->smem, sizeof(char) * kh->salloc); |
| 408 |
kh->nkeys = 0; |
| 409 |
kh->sn = 0; |
| 410 |
return kh; |
| 411 |
|
| 412 |
ERROR: |
| 413 |
esl_keyhash_Destroy(kh); |
| 414 |
return NULL; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
static uint32_t |
| 430 |
jenkins_hash(const char *key, esl_pos_t n, uint32_t hashsize) |
| 431 |
{ |
| 432 |
esl_pos_t pos; |
| 433 |
uint32_t val = 0; |
| 434 |
|
| 435 |
if (n == -1) |
| 436 |
{ |
| 437 |
for (; *key != '\0'; key++) |
| 438 |
{ |
| 439 |
val += *key; |
| 440 |
val += (val << 10); |
| 441 |
val ^= (val >> 6); |
| 442 |
} |
| 443 |
} |
| 444 |
else |
| 445 |
{ |
| 446 |
for (pos = 0; pos < n; pos++) |
| 447 |
{ |
| 448 |
val += key[pos]; |
| 449 |
val += (val << 10); |
| 450 |
val ^= (val >> 6); |
| 451 |
} |
| 452 |
} |
| 453 |
val += (val << 3); |
| 454 |
val ^= (val >> 11); |
| 455 |
val += (val << 15); |
| 456 |
|
| 457 |
return (val & (hashsize - 1)); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
static int |
| 474 |
key_upsize(ESL_KEYHASH *kh) |
| 475 |
{ |
| 476 |
void *p; |
| 477 |
int i; |
| 478 |
uint32_t val; |
| 479 |
int status; |
| 480 |
|
| 481 |
|
| 482 |
if (kh->hashsize >= (1<<28)) return eslOK; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
ESL_RALLOC(kh->hashtable, p, sizeof(int) * (kh->hashsize << 3)); |
| 490 |
kh->hashsize = kh->hashsize << 3; |
| 491 |
for (i = 0; i < kh->hashsize; i++) kh->hashtable[i] = -1; |
| 492 |
|
| 493 |
|
| 494 |
for (i = 0; i < kh->nkeys; i++) |
| 495 |
{ |
| 496 |
val = jenkins_hash(kh->smem + kh->key_offset[i], -1, kh->hashsize); |
| 497 |
kh->nxt[i] = kh->hashtable[val]; |
| 498 |
kh->hashtable[val] = i; |
| 499 |
} |
| 500 |
return eslOK; |
| 501 |
|
| 502 |
ERROR: |
| 503 |
return eslEMEM; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
#ifdef eslKEYHASH_BENCHMARK |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
#include <esl_config.h> |
| 517 |
|
| 518 |
#include <stdio.h> |
| 519 |
|
| 520 |
#include "easel.h" |
| 521 |
#include "esl_getopts.h" |
| 522 |
#include "esl_keyhash.h" |
| 523 |
#include "esl_stopwatch.h" |
| 524 |
|
| 525 |
static ESL_OPTIONS options[] = { |
| 526 |
|
| 527 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 528 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 529 |
}; |
| 530 |
static char usage[] = "[-options] <keyfile1> <keyfile2>"; |
| 531 |
static char banner[] = "benchmarking speed of keyhash module"; |
| 532 |
|
| 533 |
int |
| 534 |
main(int argc, char **argv) |
| 535 |
{ |
| 536 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 2, argc, argv, banner, usage); |
| 537 |
ESL_KEYHASH *kh = esl_keyhash_Create(); |
| 538 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 539 |
char *file1 = esl_opt_GetArg(go, 1); |
| 540 |
char *file2 = esl_opt_GetArg(go, 2); |
| 541 |
FILE *fp; |
| 542 |
char buf[256]; |
| 543 |
char *s, *tok; |
| 544 |
int idx; |
| 545 |
int nstored, nsearched, nshared; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
esl_stopwatch_Start(w); |
| 550 |
if ((fp = fopen(file1, "r")) == NULL) |
| 551 |
{ fprintf(stderr, "couldn't open %s\n", argv[1]); exit(1); } |
| 552 |
nstored = 0; |
| 553 |
while (fgets(buf, 256, fp) != NULL) |
| 554 |
{ |
| 555 |
s = buf; |
| 556 |
esl_strtok(&s, " \t\r\n", &tok); |
| 557 |
esl_keyhash_Store(kh, tok, -1, &idx); |
| 558 |
nstored++; |
| 559 |
} |
| 560 |
fclose(fp); |
| 561 |
printf("Stored %d keys.\n", nstored); |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
if ((fp = fopen(file2, "r")) == NULL) |
| 566 |
{ fprintf(stderr, "couldn't open %s\n", argv[2]); exit(1); } |
| 567 |
nsearched = nshared = 0; |
| 568 |
while (fgets(buf, 256, fp) != NULL) |
| 569 |
{ |
| 570 |
s = buf; |
| 571 |
esl_strtok(&s, " \t\r\n", &tok); |
| 572 |
|
| 573 |
if (esl_keyhash_Lookup(kh, tok, -1, &idx) == eslOK) nshared++; |
| 574 |
nsearched++; |
| 575 |
} |
| 576 |
fclose(fp); |
| 577 |
esl_stopwatch_Stop(w); |
| 578 |
printf("Looked up %d keys.\n", nsearched); |
| 579 |
printf("In common: %d keys.\n", nshared); |
| 580 |
esl_stopwatch_Display(stdout, w, "# CPU Time: "); |
| 581 |
|
| 582 |
esl_stopwatch_Destroy(w); |
| 583 |
esl_keyhash_Destroy(kh); |
| 584 |
esl_getopts_Destroy(go); |
| 585 |
return 0; |
| 586 |
} |
| 587 |
#endif |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
#ifdef eslKEYHASH_BENCHMARK2 |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
#include <esl_config.h> |
| 602 |
|
| 603 |
#include <stdio.h> |
| 604 |
#include <math.h> |
| 605 |
|
| 606 |
#include "easel.h" |
| 607 |
#include "esl_fileparser.h" |
| 608 |
#include "esl_getopts.h" |
| 609 |
#include "esl_keyhash.h" |
| 610 |
#include "esl_stats.h" |
| 611 |
#include "esl_stopwatch.h" |
| 612 |
#include "esl_vectorops.h" |
| 613 |
|
| 614 |
static ESL_OPTIONS options[] = { |
| 615 |
|
| 616 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 617 |
{ "-s", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show statistical test for hash uniformity", 0 }, |
| 618 |
{ "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose: print hash values for keys", 0 }, |
| 619 |
{ "-x", eslARG_INT, "32768", NULL, NULL, NULL, NULL, NULL, "set hash table size to <n>", 0 }, |
| 620 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 621 |
}; |
| 622 |
static char usage[] = "[-options] <keyfile>"; |
| 623 |
static char banner[] = "benchmarking speed of hash function in keyhash module"; |
| 624 |
|
| 625 |
int |
| 626 |
main(int argc, char **argv) |
| 627 |
{ |
| 628 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 1, argc, argv, banner, usage); |
| 629 |
ESL_FILEPARSER *efp = NULL; |
| 630 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 631 |
ESL_KEYHASH *kh = esl_keyhash_Create(); |
| 632 |
char *keyfile = esl_opt_GetArg(go, 1); |
| 633 |
uint32_t hashsize = esl_opt_GetInteger(go, "-x"); |
| 634 |
char *key; |
| 635 |
int keylen; |
| 636 |
char **karr = NULL; |
| 637 |
int kalloc; |
| 638 |
int *ct = NULL; |
| 639 |
int nkeys; |
| 640 |
int i; |
| 641 |
int status; |
| 642 |
uint32_t (*hashfunc)(const char*,uint32_t) = jenkins_hash; |
| 643 |
|
| 644 |
|
| 645 |
kalloc = 256; |
| 646 |
ESL_ALLOC(karr, sizeof(char *) * kalloc); |
| 647 |
|
| 648 |
if (esl_fileparser_Open(keyfile, NULL, &efp) != eslOK) esl_fatal("Failed to open key file %s\n", keyfile); |
| 649 |
|
| 650 |
nkeys = 0; |
| 651 |
while (esl_fileparser_NextLine(efp) == eslOK) |
| 652 |
{ |
| 653 |
if (esl_fileparser_GetTokenOnLine(efp, &key, &keylen) != eslOK) esl_fatal("Failure in parsing key file\n"); |
| 654 |
|
| 655 |
if (nkeys == kalloc) { |
| 656 |
void *tmp; |
| 657 |
ESL_RALLOC(karr, tmp, sizeof(char *) * kalloc * 2); |
| 658 |
kalloc *= 2; |
| 659 |
} |
| 660 |
|
| 661 |
esl_strdup(key, keylen, &(karr[nkeys])); |
| 662 |
nkeys++; |
| 663 |
} |
| 664 |
esl_fileparser_Close(efp); |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
esl_stopwatch_Start(w); |
| 670 |
for (i = 0; i < nkeys; i++) (*hashfunc)(karr[i], hashsize); |
| 671 |
esl_stopwatch_Stop(w); |
| 672 |
esl_stopwatch_Display(stdout, w, "# CPU Time: "); |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
if (esl_opt_GetBoolean(go, "-v")) |
| 678 |
{ |
| 679 |
for (i = 0; i < nkeys; i++) |
| 680 |
printf("%-20s %9d\n", karr[i], (*hashfunc)(karr[i], hashsize)); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
if (esl_opt_GetBoolean(go, "-s")) |
| 686 |
{ |
| 687 |
double mean, var, X2, pval; |
| 688 |
|
| 689 |
ESL_ALLOC(ct, sizeof(int) * hashsize); |
| 690 |
esl_vec_ISet(ct, hashsize, 0); |
| 691 |
for (i = 0; i < nkeys; i++) ct[(*hashfunc)(karr[i], hashsize)]++; |
| 692 |
|
| 693 |
esl_stats_IMean(ct, hashsize, &mean, &var); |
| 694 |
for (X2 = 0.0, i = 0; i < hashsize; i++) |
| 695 |
X2 += (((double) ct[i] - mean) * ((double) ct[i] - mean)) / mean; |
| 696 |
|
| 697 |
esl_stats_ChiSquaredTest(hashsize-1, X2, &pval); |
| 698 |
|
| 699 |
printf("Number of keys: %d\n", nkeys); |
| 700 |
printf("Hash table size: %d\n", hashsize); |
| 701 |
printf("Mean hash occupancy: %.2f\n", mean); |
| 702 |
printf("Minimum: %d\n", esl_vec_IMin(ct, hashsize)); |
| 703 |
printf("Maximum: %d\n", esl_vec_IMax(ct, hashsize)); |
| 704 |
printf("Variance: %.2f\n", var); |
| 705 |
printf("Chi-squared: %.2f\n", X2); |
| 706 |
printf("Chi-squared p-value: %.4f\n", pval); |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
for (i = 0; i < nkeys; i++) free(karr[i]); |
| 712 |
free(karr); |
| 713 |
esl_stopwatch_Destroy(w); |
| 714 |
esl_getopts_Destroy(go); |
| 715 |
return 0; |
| 716 |
|
| 717 |
ERROR: |
| 718 |
return status; |
| 719 |
} |
| 720 |
#endif |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
#ifdef eslKEYHASH_TESTDRIVE |
| 730 |
#include "esl_matrixops.h" |
| 731 |
#include "esl_random.h" |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
static void |
| 737 |
utest_stringkeys(void) |
| 738 |
{ |
| 739 |
char msg[] = "keyhash stringkeys test failed"; |
| 740 |
ESL_RANDOMNESS *rng = esl_randomness_Create(42); |
| 741 |
ESL_KEYHASH *kh = esl_keyhash_Create(); |
| 742 |
int nstore = 1200; |
| 743 |
int nlookup = 1200; |
| 744 |
int keylen = 2; |
| 745 |
char **keys = esl_mat_CCreate(nstore+nlookup, keylen+1); |
| 746 |
int nmissed = 0; |
| 747 |
int nk; |
| 748 |
int i,j,h,h42; |
| 749 |
int status; |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
for (i = 0; i < nstore+nlookup; i++) |
| 758 |
{ |
| 759 |
for (j = 0; j < keylen; j++) |
| 760 |
keys[i][j] = 'a' + esl_rnd_Roll(rng, 26); |
| 761 |
keys[i][j] = '\0'; |
| 762 |
} |
| 763 |
|
| 764 |
keys[42][0] = keys[42][1] = 'X'; |
| 765 |
|
| 766 |
|
| 767 |
nk = 0; |
| 768 |
for (i = 0; i < nstore; i++) |
| 769 |
{ |
| 770 |
status = esl_keyhash_Store(kh, keys[i], -1, &h); |
| 771 |
if (status == eslOK ) { if (h != nk) esl_fatal(msg); nk++; } |
| 772 |
else if (status == eslEDUP) { if (h >= nk) esl_fatal(msg); } |
| 773 |
else esl_fatal(msg); |
| 774 |
|
| 775 |
if (i == 42) { h42 = h; } |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
for (i = nstore; i < nstore+nlookup; i++) |
| 780 |
{ |
| 781 |
status = esl_keyhash_Lookup(kh, keys[i], -1, &h); |
| 782 |
if (status == eslOK) { if (h < 0 || h >= nk) esl_fatal(msg); } |
| 783 |
else if (status == eslENOTFOUND) { nmissed++; if (h != -1) esl_fatal(msg); } |
| 784 |
} |
| 785 |
if ( esl_keyhash_Lookup(kh, "XX", -1, &h) != eslOK || h != h42) esl_fatal(msg); |
| 786 |
|
| 787 |
if (nmissed != 209) esl_fatal(msg); |
| 788 |
|
| 789 |
esl_mat_CDestroy(keys); |
| 790 |
esl_keyhash_Destroy(kh); |
| 791 |
esl_randomness_Destroy(rng); |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
static void |
| 799 |
utest_memkeys(void) |
| 800 |
{ |
| 801 |
char msg[] = "keyhash memkeys test failed"; |
| 802 |
ESL_RANDOMNESS *rng = esl_randomness_Create(42); |
| 803 |
ESL_KEYHASH *kh = esl_keyhash_Create(); |
| 804 |
int nstore = 1200; |
| 805 |
int nlookup = 1200; |
| 806 |
int keylen = 2; |
| 807 |
char **keys = esl_mat_CCreate(nstore+nlookup, keylen); |
| 808 |
int nmissed = 0; |
| 809 |
int nk; |
| 810 |
int i,h,h42; |
| 811 |
int status; |
| 812 |
|
| 813 |
for (i = 0; i < (nstore+nlookup)*keylen; i++) |
| 814 |
keys[0][i] = 'a' + esl_rnd_Roll(rng, 26); |
| 815 |
keys[42][0] = keys[42][1] = 'X'; |
| 816 |
|
| 817 |
nk = 0; |
| 818 |
for (i = 0; i < nstore; i++) |
| 819 |
{ |
| 820 |
status = esl_keyhash_Store(kh, keys[i], keylen, &h); |
| 821 |
if (status == eslOK ) { if (h != nk) esl_fatal(msg); nk++; } |
| 822 |
else if (status == eslEDUP) { if (h >= nk) esl_fatal(msg); } |
| 823 |
else esl_fatal(msg); |
| 824 |
if (i == 42) { h42 = h; } |
| 825 |
} |
| 826 |
|
| 827 |
for (i = nstore; i < nstore+nlookup; i++) |
| 828 |
{ |
| 829 |
status = esl_keyhash_Lookup(kh, keys[i], keylen, &h); |
| 830 |
if (status == eslOK) { if (h < 0 || h >= nk) esl_fatal(msg); } |
| 831 |
else if (status == eslENOTFOUND) { nmissed++; if (h != -1) esl_fatal(msg); } |
| 832 |
} |
| 833 |
if ( esl_keyhash_Lookup(kh, keys[42], keylen, &h) != eslOK || h != h42) esl_fatal(msg); |
| 834 |
if (nmissed != 209) esl_fatal(msg); |
| 835 |
|
| 836 |
esl_mat_CDestroy(keys); |
| 837 |
esl_keyhash_Destroy(kh); |
| 838 |
esl_randomness_Destroy(rng); |
| 839 |
} |
| 840 |
#endif |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
#ifdef eslKEYHASH_TESTDRIVE |
| 848 |
#include <esl_config.h> |
| 849 |
|
| 850 |
#include <stdlib.h> |
| 851 |
#include <stdio.h> |
| 852 |
|
| 853 |
#include "easel.h" |
| 854 |
#include "esl_getopts.h" |
| 855 |
|
| 856 |
|
| 857 |
static ESL_OPTIONS options[] = { |
| 858 |
|
| 859 |
{"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 860 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 861 |
}; |
| 862 |
static char usage[] = "[-options]"; |
| 863 |
static char banner[] = "test driver for keyhash module"; |
| 864 |
|
| 865 |
|
| 866 |
int |
| 867 |
main(int argc, char **argv) |
| 868 |
{ |
| 869 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 870 |
|
| 871 |
fprintf(stderr, "## %s\n", argv[0]); |
| 872 |
|
| 873 |
utest_stringkeys(); |
| 874 |
utest_memkeys(); |
| 875 |
|
| 876 |
fprintf(stderr, "# status = ok\n"); |
| 877 |
esl_getopts_Destroy(go); |
| 878 |
return eslOK; |
| 879 |
} |
| 880 |
#endif |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
#ifdef eslKEYHASH_EXAMPLE |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
#include <stdio.h> |
| 895 |
#include "easel.h" |
| 896 |
#include "esl_keyhash.h" |
| 897 |
|
| 898 |
int |
| 899 |
main(int argc, char **argv) |
| 900 |
{ |
| 901 |
ESL_KEYHASH *h = esl_keyhash_Create(); |
| 902 |
FILE *fp; |
| 903 |
char buf[256]; |
| 904 |
char *s, *tok; |
| 905 |
int idx; |
| 906 |
int nstored, nsearched, nshared; |
| 907 |
|
| 908 |
|
| 909 |
if ((fp = fopen(argv[1], "r")) == NULL) esl_fatal("couldn't open %s\n", argv[1]); |
| 910 |
nstored = 0; |
| 911 |
while (fgets(buf, 256, fp) != NULL) |
| 912 |
{ |
| 913 |
s = buf; |
| 914 |
esl_strtok(&s, " \t\r\n", &tok); |
| 915 |
esl_keyhash_Store(h, tok, -1, &idx); |
| 916 |
nstored++; |
| 917 |
} |
| 918 |
fclose(fp); |
| 919 |
printf("Stored %d keys.\n", nstored); |
| 920 |
|
| 921 |
|
| 922 |
if ((fp = fopen(argv[2], "r")) == NULL) esl_fatal("couldn't open %s\n", argv[1]); |
| 923 |
nsearched = nshared = 0; |
| 924 |
while (fgets(buf, 256, fp) != NULL) |
| 925 |
{ |
| 926 |
s = buf; |
| 927 |
esl_strtok(&s, " \t\r\n", &tok); |
| 928 |
if (esl_keyhash_Lookup(h, tok, -1, &idx) == eslOK) nshared++; |
| 929 |
nsearched++; |
| 930 |
} |
| 931 |
fclose(fp); |
| 932 |
printf("Looked up %d keys.\n", nsearched); |
| 933 |
printf("In common: %d keys.\n", nshared); |
| 934 |
esl_keyhash_Destroy(h); |
| 935 |
return 0; |
| 936 |
} |
| 937 |
|
| 938 |
#endif |
| 939 |
|