1 #include "cmph.h"
2 #include "cmph_structs.h"
3 #include "chm.h"
4 #include "bmz.h"
5 #include "bmz8.h"
6 #include "brz.h"
7 #include "fch.h"
8 #include "bdz.h"
9 #include "bdz_ph.h"
10 #include "chd_ph.h"
11 #include "chd.h"
12
13 #include <stdlib.h>
14 #include <assert.h>
15 #include <string.h>
16 //#define DEBUG
17 #include "debug.h"
18
19 const char *cmph_names[] = {"bmz", "bmz8", "chm", "brz", "fch", "bdz", "bdz_ph", "chd_ph", "chd", NULL };
20
21 typedef struct
22 {
23 void *vector;
24 cmph_uint32 position; // access position when data is a vector
25 } cmph_vector_t;
26
27
28
29 /**
30 * Support a vector of struct as the source of keys.
31 *
32 * E.g. The keys could be the fieldB's in a vector of struct rec where
33 * struct rec is defined as:
34 * struct rec {
35 * fieldA;
36 * fieldB;
37 * fieldC;
38 * }
39 */
40 typedef struct
41 {
42 void *vector; /* Pointer to the vector of struct */
43 cmph_uint32 position; /* current position */
44 cmph_uint32 struct_size; /* The size of the struct */
45 cmph_uint32 key_offset; /* The byte offset of the key in the struct */
46 cmph_uint32 key_len; /* The length of the key */
47 } cmph_struct_vector_t;
48
49
50 static cmph_io_adapter_t *cmph_io_vector_new(void * vector, cmph_uint32 nkeys);
51 static void cmph_io_vector_destroy(cmph_io_adapter_t * key_source);
52
53 static cmph_io_adapter_t *cmph_io_struct_vector_new(void * vector, cmph_uint32 struct_size, cmph_uint32 key_offset, cmph_uint32 key_len, cmph_uint32 nkeys);
54 static void cmph_io_struct_vector_destroy(cmph_io_adapter_t * key_source);
55
56 static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen)
57 {
58 FILE *fd = (FILE *)data;
59 *key = NULL;
60 *keylen = 0;
61 while(1)
62 {
63 char buf[BUFSIZ];
64 char *c = fgets(buf, BUFSIZ, fd);
65 if (c == NULL) return -1;
66 if (feof(fd)) return -1;
67 *key = (char *)realloc(*key, *keylen + strlen(buf) + 1);
68 memcpy(*key + *keylen, buf, strlen(buf));
69 *keylen += (cmph_uint32)strlen(buf);
70 if (buf[strlen(buf) - 1] != '\n') continue;
71 break;
72 }
73 if ((*keylen) && (*key)[*keylen - 1] == '\n')
74 {
75 (*key)[(*keylen) - 1] = 0;
76 --(*keylen);
77 }
78 return (int)(*keylen);
79 }
80
81 static int key_byte_vector_read(void *data, char **key, cmph_uint32 *keylen)
82 {
83 cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
84 cmph_uint8 **keys_vd = (cmph_uint8 **)cmph_vector->vector;
85 size_t size;
86 memcpy(keylen, keys_vd[cmph_vector->position], sizeof(*keylen));
87 size = *keylen;
88 *key = (char *)malloc(size);
89 memcpy(*key, keys_vd[cmph_vector->position] + sizeof(*keylen), size);
90 cmph_vector->position = cmph_vector->position + 1;
91 return (int)(*keylen);
92
93 }
94
95 static int key_struct_vector_read(void *data, char **key, cmph_uint32 *keylen)
96 {
97 cmph_struct_vector_t *cmph_struct_vector = (cmph_struct_vector_t *)data;
98 char *keys_vd = (char *)cmph_struct_vector->vector;
99 size_t size;
100 *keylen = cmph_struct_vector->key_len;
101 size = *keylen;
102 *key = (char *)malloc(size);
103 memcpy(*key, (keys_vd + (cmph_struct_vector->position * cmph_struct_vector->struct_size) + cmph_struct_vector->key_offset), size);
104 cmph_struct_vector->position = cmph_struct_vector->position + 1;
105 return (int)(*keylen);
106 }
107
108 static int key_vector_read(void *data, char **key, cmph_uint32 *keylen)
109 {
110 cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
111 char **keys_vd = (char **)cmph_vector->vector;
112 size_t size;
113 *keylen = (cmph_uint32)strlen(keys_vd[cmph_vector->position]);
114 size = *keylen;
115 *key = (char *)malloc(size + 1);
116 strcpy(*key, keys_vd[cmph_vector->position]);
117 cmph_vector->position = cmph_vector->position + 1;
118 return (int)(*keylen);
119
120 }
121
122
123 static void key_nlfile_dispose(void *data, char *key, cmph_uint32 keylen)
124 {
125 free(key);
126 }
127
128 static void key_vector_dispose(void *data, char *key, cmph_uint32 keylen)
129 {
130 free(key);
131 }
132
133 static void key_nlfile_rewind(void *data)
134 {
135 FILE *fd = (FILE *)data;
136 rewind(fd);
137 }
138
139 static void key_struct_vector_rewind(void *data)
140 {
141 cmph_struct_vector_t *cmph_struct_vector = (cmph_struct_vector_t *)data;
142 cmph_struct_vector->position = 0;
143 }
144
145 static void key_vector_rewind(void *data)
146 {
147 cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
148 cmph_vector->position = 0;
149 }
150
151 static cmph_uint32 count_nlfile_keys(FILE *fd)
152 {
153 cmph_uint32 count = 0;
154 rewind(fd);
155 while(1)
156 {
157 char buf[BUFSIZ];
158 if (fgets(buf, BUFSIZ, fd) == NULL) break;
159 if (feof(fd)) break;
160 if (buf[strlen(buf) - 1] != '\n') continue;
161 ++count;
162 }
163 rewind(fd);
164 return count;
165 }
166
167 cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd)
168 {
169 cmph_io_adapter_t * key_source = (cmph_io_adapter_t *)malloc(sizeof(cmph_io_adapter_t));
170 assert(key_source);
171 key_source->data = (void *)keys_fd;
172 key_source->nkeys = count_nlfile_keys(keys_fd);
173 key_source->read = key_nlfile_read;
174 key_source->dispose = key_nlfile_dispose;
175 key_source->rewind = key_nlfile_rewind;
176 return key_source;
177 }
178
179 void cmph_io_nlfile_adapter_destroy(cmph_io_adapter_t * key_source)
180 {
181 free(key_source);
182 }
183
184 cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys)
185 {
186 cmph_io_adapter_t * key_source = (cmph_io_adapter_t *)malloc(sizeof(cmph_io_adapter_t));
187 assert(key_source);
188 key_source->data = (void *)keys_fd;
189 key_source->nkeys = nkeys;
190 key_source->read = key_nlfile_read;
191 key_source->dispose = key_nlfile_dispose;
192 key_source->rewind = key_nlfile_rewind;
193 return key_source;
194 }
195
196 void cmph_io_nlnkfile_adapter_destroy(cmph_io_adapter_t * key_source)
197 {
198 free(key_source);
199 }
200
201
202 static cmph_io_adapter_t *cmph_io_struct_vector_new(void * vector, cmph_uint32 struct_size, cmph_uint32 key_offset, cmph_uint32 key_len, cmph_uint32 nkeys)
203 {
204 cmph_io_adapter_t * key_source = (cmph_io_adapter_t *)malloc(sizeof(cmph_io_adapter_t));
205 cmph_struct_vector_t * cmph_struct_vector = (cmph_struct_vector_t *)malloc(sizeof(cmph_struct_vector_t));
206 assert(key_source);
207 assert(cmph_struct_vector);
208 cmph_struct_vector->vector = vector;
209 cmph_struct_vector->position = 0;
210 cmph_struct_vector->struct_size = struct_size;
211 cmph_struct_vector->key_offset = key_offset;
212 cmph_struct_vector->key_len = key_len;
213 key_source->data = (void *)cmph_struct_vector;
214 key_source->nkeys = nkeys;
215 return key_source;
216 }
217
218 static void cmph_io_struct_vector_destroy(cmph_io_adapter_t * key_source)
219 {
220 cmph_struct_vector_t *cmph_struct_vector = (cmph_struct_vector_t *)key_source->data;
221 cmph_struct_vector->vector = NULL;
222 free(cmph_struct_vector);
223 free(key_source);
224 }
225
226 static cmph_io_adapter_t *cmph_io_vector_new(void * vector, cmph_uint32 nkeys)
227 {
228 cmph_io_adapter_t * key_source = (cmph_io_adapter_t *)malloc(sizeof(cmph_io_adapter_t));
229 cmph_vector_t * cmph_vector = (cmph_vector_t *)malloc(sizeof(cmph_vector_t));
230 assert(key_source);
231 assert(cmph_vector);
232 cmph_vector->vector = vector;
233 cmph_vector->position = 0;
234 key_source->data = (void *)cmph_vector;
235 key_source->nkeys = nkeys;
236 return key_source;
237 }
238
239 static void cmph_io_vector_destroy(cmph_io_adapter_t * key_source)
240 {
241 cmph_vector_t *cmph_vector = (cmph_vector_t *)key_source->data;
242 cmph_vector->vector = NULL;
243 free(cmph_vector);
244 free(key_source);
245 }
246
247 cmph_io_adapter_t *cmph_io_byte_vector_adapter(cmph_uint8 ** vector, cmph_uint32 nkeys)
248 {
249 cmph_io_adapter_t * key_source = cmph_io_vector_new(vector, nkeys);
250 key_source->read = key_byte_vector_read;
251 key_source->dispose = key_vector_dispose;
252 key_source->rewind = key_vector_rewind;
253 return key_source;
254 }
255 void cmph_io_byte_vector_adapter_destroy(cmph_io_adapter_t * key_source)
256 {
257 cmph_io_vector_destroy(key_source);
258 }
259
260 cmph_io_adapter_t *cmph_io_struct_vector_adapter(void * vector, cmph_uint32 struct_size, cmph_uint32 key_offset, cmph_uint32 key_len, cmph_uint32 nkeys)
261 {
262 cmph_io_adapter_t * key_source = cmph_io_struct_vector_new(vector, struct_size, key_offset, key_len, nkeys);
263 key_source->read = key_struct_vector_read;
264 key_source->dispose = key_vector_dispose;
265 key_source->rewind = key_struct_vector_rewind;
266 return key_source;
267 }
268
269 void cmph_io_struct_vector_adapter_destroy(cmph_io_adapter_t * key_source)
270 {
271 cmph_io_struct_vector_destroy(key_source);
272 }
273
274 cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys)
275 {
276 cmph_io_adapter_t * key_source = cmph_io_vector_new(vector, nkeys);
277 key_source->read = key_vector_read;
278 key_source->dispose = key_vector_dispose;
279 key_source->rewind = key_vector_rewind;
280 return key_source;
281 }
282
283 void cmph_io_vector_adapter_destroy(cmph_io_adapter_t * key_source)
284 {
285 cmph_io_vector_destroy(key_source);
286 }
287
288 cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source)
289 {
290 cmph_config_t *mph = NULL;
291 mph = __config_new(key_source);
292 assert(mph);
293 mph->algo = CMPH_CHM; // default value
294 mph->data = chm_config_new();
295 return mph;
296 }
297
298 void cmph_config_set_algo(cmph_config_t *mph, CMPH_ALGO algo)
299 {
300 if (algo != mph->algo)
301 {
302 switch (mph->algo)
303 {
304 case CMPH_CHM:
305 chm_config_destroy(mph);
306 break;
307 case CMPH_BMZ:
308 bmz_config_destroy(mph);
309 break;
310 case CMPH_BMZ8:
311 bmz8_config_destroy(mph);
312 break;
313 case CMPH_BRZ:
314 brz_config_destroy(mph);
315 break;
316 case CMPH_FCH:
317 fch_config_destroy(mph);
318 break;
319 case CMPH_BDZ:
320 bdz_config_destroy(mph);
321 break;
322 case CMPH_BDZ_PH:
323 bdz_ph_config_destroy(mph);
324 break;
325 case CMPH_CHD_PH:
326 chd_ph_config_destroy(mph);
327 break;
328 case CMPH_CHD:
329 chd_config_destroy(mph);
330 break;
331 default:
332 assert(0);
333 }
334 switch(algo)
335 {
336 case CMPH_CHM:
337 mph->data = chm_config_new();
338 break;
339 case CMPH_BMZ:
340 mph->data = bmz_config_new();
341 break;
342 case CMPH_BMZ8:
343 mph->data = bmz8_config_new();
344 break;
345 case CMPH_BRZ:
346 mph->data = brz_config_new();
347 break;
348 case CMPH_FCH:
349 mph->data = fch_config_new();
350 break;
351 case CMPH_BDZ:
352 mph->data = bdz_config_new();
353 break;
354 case CMPH_BDZ_PH:
355 mph->data = bdz_ph_config_new();
356 break;
357 case CMPH_CHD_PH:
358 mph->data = chd_ph_config_new();
359 break;
360 case CMPH_CHD:
361 mph->data = chd_config_new(mph);
362 break;
363 default:
364 assert(0);
365 }
366 }
367 mph->algo = algo;
368 }
369
370 void cmph_config_set_tmp_dir(cmph_config_t *mph, cmph_uint8 *tmp_dir)
371 {
372 if (mph->algo == CMPH_BRZ)
373 {
374 brz_config_set_tmp_dir(mph, tmp_dir);
375 }
376 }
377
378
379 void cmph_config_set_mphf_fd(cmph_config_t *mph, FILE *mphf_fd)
380 {
381 if (mph->algo == CMPH_BRZ)
382 {
383 brz_config_set_mphf_fd(mph, mphf_fd);
384 }
385 }
386
387 void cmph_config_set_b(cmph_config_t *mph, cmph_uint32 b)
388 {
389 if (mph->algo == CMPH_BRZ)
390 {
391 brz_config_set_b(mph, b);
392 }
393 else if (mph->algo == CMPH_BDZ)
394 {
395 bdz_config_set_b(mph, b);
396 }
397 else if (mph->algo == CMPH_CHD_PH)
398 {
399 chd_ph_config_set_b(mph, b);
400 }
401 else if (mph->algo == CMPH_CHD)
402 {
403 chd_config_set_b(mph, b);
404 }
405 }
406
407 void cmph_config_set_keys_per_bin(cmph_config_t *mph, cmph_uint32 keys_per_bin)
408 {
409 if (mph->algo == CMPH_CHD_PH)
410 {
411 chd_ph_config_set_keys_per_bin(mph, keys_per_bin);
412 }
413 else if (mph->algo == CMPH_CHD)
414 {
415 chd_config_set_keys_per_bin(mph, keys_per_bin);
416 }
417 }
418
419 void cmph_config_set_memory_availability(cmph_config_t *mph, cmph_uint32 memory_availability)
420 {
421 if (mph->algo == CMPH_BRZ)
422 {
423 brz_config_set_memory_availability(mph, memory_availability);
424 }
425 }
426
427 void cmph_config_destroy(cmph_config_t *mph)
428 {
429 if(mph)
430 {
431 DEBUGP("Destroying mph with algo %s\n", cmph_names[mph->algo]);
432 switch (mph->algo)
433 {
434 case CMPH_CHM:
435 chm_config_destroy(mph);
436 break;
437 case CMPH_BMZ: /* included -- Fabiano */
438 bmz_config_destroy(mph);
439 break;
440 case CMPH_BMZ8: /* included -- Fabiano */
441 bmz8_config_destroy(mph);
442 break;
443 case CMPH_BRZ: /* included -- Fabiano */
444 brz_config_destroy(mph);
445 break;
446 case CMPH_FCH: /* included -- Fabiano */
447 fch_config_destroy(mph);
448 break;
449 case CMPH_BDZ: /* included -- Fabiano */
450 bdz_config_destroy(mph);
451 break;
452 case CMPH_BDZ_PH: /* included -- Fabiano */
453 bdz_ph_config_destroy(mph);
454 break;
455 case CMPH_CHD_PH: /* included -- Fabiano */
456 chd_ph_config_destroy(mph);
457 break;
458 case CMPH_CHD: /* included -- Fabiano */
459 chd_config_destroy(mph);
460 break;
461 default:
462 assert(0);
463 }
464 __config_destroy(mph);
465 }
466 }
467
468 void cmph_config_set_verbosity(cmph_config_t *mph, cmph_uint32 verbosity)
469 {
470 mph->verbosity = verbosity;
471 }
472
473 void cmph_config_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs)
474 {
475 switch (mph->algo)
476 {
477 case CMPH_CHM:
478 chm_config_set_hashfuncs(mph, hashfuncs);
479 break;
480 case CMPH_BMZ: /* included -- Fabiano */
481 bmz_config_set_hashfuncs(mph, hashfuncs);
482 break;
483 case CMPH_BMZ8: /* included -- Fabiano */
484 bmz8_config_set_hashfuncs(mph, hashfuncs);
485 break;
486 case CMPH_BRZ: /* included -- Fabiano */
487 brz_config_set_hashfuncs(mph, hashfuncs);
488 break;
489 case CMPH_FCH: /* included -- Fabiano */
490 fch_config_set_hashfuncs(mph, hashfuncs);
491 break;
492 case CMPH_BDZ: /* included -- Fabiano */
493 bdz_config_set_hashfuncs(mph, hashfuncs);
494 break;
495 case CMPH_BDZ_PH: /* included -- Fabiano */
496 bdz_ph_config_set_hashfuncs(mph, hashfuncs);
497 break;
498 case CMPH_CHD_PH: /* included -- Fabiano */
499 chd_ph_config_set_hashfuncs(mph, hashfuncs);
500 break;
501 case CMPH_CHD: /* included -- Fabiano */
502 chd_config_set_hashfuncs(mph, hashfuncs);
503 break;
504 default:
505 break;
506 }
507 return;
508 }
509 void cmph_config_set_graphsize(cmph_config_t *mph, double c)
510 {
511 mph->c = c;
512 return;
513 }
514
515 cmph_t *cmph_new(cmph_config_t *mph)
516 {
517 cmph_t *mphf = NULL;
518 double c = mph->c;
519
520 DEBUGP("Creating mph with algorithm %s\n", cmph_names[mph->algo]);
521 switch (mph->algo)
522 {
523 case CMPH_CHM:
524 DEBUGP("Creating chm hash\n");
525 mphf = chm_new(mph, c);
526 break;
527 case CMPH_BMZ: /* included -- Fabiano */
528 DEBUGP("Creating bmz hash\n");
529 mphf = bmz_new(mph, c);
530 break;
531 case CMPH_BMZ8: /* included -- Fabiano */
532 DEBUGP("Creating bmz8 hash\n");
533 mphf = bmz8_new(mph, c);
534 break;
535 case CMPH_BRZ: /* included -- Fabiano */
536 DEBUGP("Creating brz hash\n");
537 if (c >= 2.0) brz_config_set_algo(mph, CMPH_FCH);
538 else brz_config_set_algo(mph, CMPH_BMZ8);
539 mphf = brz_new(mph, c);
540 break;
541 case CMPH_FCH: /* included -- Fabiano */
542 DEBUGP("Creating fch hash\n");
543 mphf = fch_new(mph, c);
544 break;
545 case CMPH_BDZ: /* included -- Fabiano */
546 DEBUGP("Creating bdz hash\n");
547 mphf = bdz_new(mph, c);
548 break;
549 case CMPH_BDZ_PH: /* included -- Fabiano */
550 DEBUGP("Creating bdz_ph hash\n");
551 mphf = bdz_ph_new(mph, c);
552 break;
553 case CMPH_CHD_PH: /* included -- Fabiano */
554 DEBUGP("Creating chd_ph hash\n");
555 mphf = chd_ph_new(mph, c);
556 break;
557 case CMPH_CHD: /* included -- Fabiano */
558 DEBUGP("Creating chd hash\n");
559 mphf = chd_new(mph, c);
560 break;
561 default:
562 assert(0);
563 }
564 return mphf;
565 }
566
567 int cmph_dump(cmph_t *mphf, FILE *f)
568 {
569 switch (mphf->algo)
570 {
571 case CMPH_CHM:
572 return chm_dump(mphf, f);
573 case CMPH_BMZ: /* included -- Fabiano */
574 return bmz_dump(mphf, f);
575 case CMPH_BMZ8: /* included -- Fabiano */
576 return bmz8_dump(mphf, f);
577 case CMPH_BRZ: /* included -- Fabiano */
578 return brz_dump(mphf, f);
579 case CMPH_FCH: /* included -- Fabiano */
580 return fch_dump(mphf, f);
581 case CMPH_BDZ: /* included -- Fabiano */
582 return bdz_dump(mphf, f);
583 case CMPH_BDZ_PH: /* included -- Fabiano */
584 return bdz_ph_dump(mphf, f);
585 case CMPH_CHD_PH: /* included -- Fabiano */
586 return chd_ph_dump(mphf, f);
587 case CMPH_CHD: /* included -- Fabiano */
588 return chd_dump(mphf, f);
589 default:
590 assert(0);
591 }
592 assert(0);
593 return 0;
594 }
595 cmph_t *cmph_load(FILE *f)
596 {
597 cmph_t *mphf = NULL;
598 DEBUGP("Loading mphf generic parts\n");
599 mphf = __cmph_load(f);
600 if (mphf == NULL) return NULL;
601 DEBUGP("Loading mphf algorithm dependent parts\n");
602
603 switch (mphf->algo)
604 {
605 case CMPH_CHM:
606 chm_load(f, mphf);
607 break;
608 case CMPH_BMZ: /* included -- Fabiano */
609 DEBUGP("Loading bmz algorithm dependent parts\n");
610 bmz_load(f, mphf);
611 break;
612 case CMPH_BMZ8: /* included -- Fabiano */
613 DEBUGP("Loading bmz8 algorithm dependent parts\n");
614 bmz8_load(f, mphf);
615 break;
616 case CMPH_BRZ: /* included -- Fabiano */
617 DEBUGP("Loading brz algorithm dependent parts\n");
618 brz_load(f, mphf);
619 break;
620 case CMPH_FCH: /* included -- Fabiano */
621 DEBUGP("Loading fch algorithm dependent parts\n");
622 fch_load(f, mphf);
623 break;
624 case CMPH_BDZ: /* included -- Fabiano */
625 DEBUGP("Loading bdz algorithm dependent parts\n");
626 bdz_load(f, mphf);
627 break;
628 case CMPH_BDZ_PH: /* included -- Fabiano */
629 DEBUGP("Loading bdz_ph algorithm dependent parts\n");
630 bdz_ph_load(f, mphf);
631 break;
632 case CMPH_CHD_PH: /* included -- Fabiano */
633 DEBUGP("Loading chd_ph algorithm dependent parts\n");
634 chd_ph_load(f, mphf);
635 break;
636 case CMPH_CHD: /* included -- Fabiano */
637 DEBUGP("Loading chd algorithm dependent parts\n");
638 chd_load(f, mphf);
639 break;
640 default:
641 assert(0);
642 }
643 DEBUGP("Loaded mphf\n");
644 return mphf;
645 }
646
647
648 cmph_uint32 cmph_search(cmph_t *mphf, const char *key, cmph_uint32 keylen)
649 {
650 DEBUGP("mphf algorithm: %u \n", mphf->algo);
651 switch(mphf->algo)
652 {
653 case CMPH_CHM:
654 return chm_search(mphf, key, keylen);
655 case CMPH_BMZ: /* included -- Fabiano */
656 DEBUGP("bmz algorithm search\n");
657 return bmz_search(mphf, key, keylen);
658 case CMPH_BMZ8: /* included -- Fabiano */
659 DEBUGP("bmz8 algorithm search\n");
660 return bmz8_search(mphf, key, keylen);
661 case CMPH_BRZ: /* included -- Fabiano */
662 DEBUGP("brz algorithm search\n");
663 return brz_search(mphf, key, keylen);
664 case CMPH_FCH: /* included -- Fabiano */
665 DEBUGP("fch algorithm search\n");
666 return fch_search(mphf, key, keylen);
667 case CMPH_BDZ: /* included -- Fabiano */
668 DEBUGP("bdz algorithm search\n");
669 return bdz_search(mphf, key, keylen);
670 case CMPH_BDZ_PH: /* included -- Fabiano */
671 DEBUGP("bdz_ph algorithm search\n");
672 return bdz_ph_search(mphf, key, keylen);
673 case CMPH_CHD_PH: /* included -- Fabiano */
674 DEBUGP("chd_ph algorithm search\n");
675 return chd_ph_search(mphf, key, keylen);
676 case CMPH_CHD: /* included -- Fabiano */
677 DEBUGP("chd algorithm search\n");
678 return chd_search(mphf, key, keylen);
679 default:
680 assert(0);
681 }
682 assert(0);
683 return 0;
684 }
685
686 cmph_uint32 cmph_size(cmph_t *mphf)
687 {
688 return mphf->size;
689 }
690
691 void cmph_destroy(cmph_t *mphf)
692 {
693 switch(mphf->algo)
694 {
695 case CMPH_CHM:
696 chm_destroy(mphf);
697 return;
698 case CMPH_BMZ: /* included -- Fabiano */
699 bmz_destroy(mphf);
700 return;
701 case CMPH_BMZ8: /* included -- Fabiano */
702 bmz8_destroy(mphf);
703 return;
704 case CMPH_BRZ: /* included -- Fabiano */
705 brz_destroy(mphf);
706 return;
707 case CMPH_FCH: /* included -- Fabiano */
708 fch_destroy(mphf);
709 return;
710 case CMPH_BDZ: /* included -- Fabiano */
711 bdz_destroy(mphf);
712 return;
713 case CMPH_BDZ_PH: /* included -- Fabiano */
714 bdz_ph_destroy(mphf);
715 return;
716 case CMPH_CHD_PH: /* included -- Fabiano */
717 chd_ph_destroy(mphf);
718 return;
719 case CMPH_CHD: /* included -- Fabiano */
720 chd_destroy(mphf);
721 return;
722 default:
723 assert(0);
724 }
725 assert(0);
726 return;
727 }
728
729
730 /** \fn void cmph_pack(cmph_t *mphf, void *packed_mphf);
731 * \brief Support the ability to pack a perfect hash function into a preallocated contiguous memory space pointed by packed_mphf.
732 * \param mphf pointer to the resulting mphf
733 * \param packed_mphf pointer to the contiguous memory area used to store the resulting mphf. The size of packed_mphf must be at least cmph_packed_size()
734 */
735 void cmph_pack(cmph_t *mphf, void *packed_mphf)
736 {
737 // packing algorithm type to be used in cmph.c
738 cmph_uint32 * ptr = (cmph_uint32 *) packed_mphf;
739 *ptr++ = mphf->algo;
740 DEBUGP("mphf->algo = %u\n", mphf->algo);
741 switch(mphf->algo)
742 {
743 case CMPH_CHM:
744 chm_pack(mphf, ptr);
745 break;
746 case CMPH_BMZ: /* included -- Fabiano */
747 bmz_pack(mphf, ptr);
748 break;
749 case CMPH_BMZ8: /* included -- Fabiano */
750 bmz8_pack(mphf, ptr);
751 break;
752 case CMPH_BRZ: /* included -- Fabiano */
753 brz_pack(mphf, ptr);
754 break;
755 case CMPH_FCH: /* included -- Fabiano */
756 fch_pack(mphf, ptr);
757 break;
758 case CMPH_BDZ: /* included -- Fabiano */
759 bdz_pack(mphf, ptr);
760 break;
761 case CMPH_BDZ_PH: /* included -- Fabiano */
762 bdz_ph_pack(mphf, ptr);
763 break;
764 case CMPH_CHD_PH: /* included -- Fabiano */
765 chd_ph_pack(mphf, ptr);
766 break;
767 case CMPH_CHD: /* included -- Fabiano */
768 chd_pack(mphf, ptr);
769 break;
770 default:
771 assert(0);
772 }
773 return;
774 }
775
776 /** \fn cmph_uint32 cmph_packed_size(cmph_t *mphf);
777 * \brief Return the amount of space needed to pack mphf.
778 * \param mphf pointer to a mphf
779 * \return the size of the packed function or zero for failures
780 */
781 cmph_uint32 cmph_packed_size(cmph_t *mphf)
782 {
783 switch(mphf->algo)
784 {
785 case CMPH_CHM:
786 return chm_packed_size(mphf);
787 case CMPH_BMZ: /* included -- Fabiano */
788 return bmz_packed_size(mphf);
789 case CMPH_BMZ8: /* included -- Fabiano */
790 return bmz8_packed_size(mphf);
791 case CMPH_BRZ: /* included -- Fabiano */
792 return brz_packed_size(mphf);
793 case CMPH_FCH: /* included -- Fabiano */
794 return fch_packed_size(mphf);
795 case CMPH_BDZ: /* included -- Fabiano */
796 return bdz_packed_size(mphf);
797 case CMPH_BDZ_PH: /* included -- Fabiano */
798 return bdz_ph_packed_size(mphf);
799 case CMPH_CHD_PH: /* included -- Fabiano */
800 return chd_ph_packed_size(mphf);
801 case CMPH_CHD: /* included -- Fabiano */
802 return chd_packed_size(mphf);
803 default:
804 assert(0);
805 }
806 return 0; // FAILURE
807 }
808
809 /** cmph_uint32 cmph_search(void *packed_mphf, const char *key, cmph_uint32 keylen);
810 * \brief Use the packed mphf to do a search.
811 * \param packed_mphf pointer to the packed mphf
812 * \param key key to be hashed
813 * \param keylen key legth in bytes
814 * \return The mphf value
815 */
816 cmph_uint32 cmph_search_packed(void *packed_mphf, const char *key, cmph_uint32 keylen)
817 {
818 cmph_uint32 *ptr = (cmph_uint32 *)packed_mphf;
819 // fprintf(stderr, "algo:%u\n", *ptr);
820 switch(*ptr)
821 {
822 case CMPH_CHM:
823 return chm_search_packed(++ptr, key, keylen);
824 case CMPH_BMZ: /* included -- Fabiano */
825 return bmz_search_packed(++ptr, key, keylen);
826 case CMPH_BMZ8: /* included -- Fabiano */
827 return bmz8_search_packed(++ptr, key, keylen);
828 case CMPH_BRZ: /* included -- Fabiano */
829 return brz_search_packed(++ptr, key, keylen);
830 case CMPH_FCH: /* included -- Fabiano */
831 return fch_search_packed(++ptr, key, keylen);
832 case CMPH_BDZ: /* included -- Fabiano */
833 return bdz_search_packed(++ptr, key, keylen);
834 case CMPH_BDZ_PH: /* included -- Fabiano */
835 return bdz_ph_search_packed(++ptr, key, keylen);
836 case CMPH_CHD_PH: /* included -- Fabiano */
837 return chd_ph_search_packed(++ptr, key, keylen);
838 case CMPH_CHD: /* included -- Fabiano */
839 return chd_search_packed(++ptr, key, keylen);
840 default:
841 assert(0);
842 }
843 return 0; // FAILURE
844 }