it was included a examples directory
This commit is contained in:
10
examples/Makefile.am
Executable file
10
examples/Makefile.am
Executable file
@@ -0,0 +1,10 @@
|
||||
noinst_PROGRAMS = vector_adapter_ex1 file_adapter_ex2
|
||||
|
||||
INCLUDES = -I../src/
|
||||
|
||||
vector_adapter_ex1_LDADD = ../src/libcmph.la
|
||||
vector_adapter_ex1_SOURCES = vector_adapter_ex1.c
|
||||
|
||||
file_adapter_ex2_LDADD = ../src/libcmph.la
|
||||
file_adapter_ex2_SOURCES = file_adapter_ex2.c
|
||||
|
||||
32
examples/file_adapter_ex2.c
Normal file
32
examples/file_adapter_ex2.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <cmph.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Create minimal perfect hash function from in-disk keys using BMZ algorithm
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
//Open file with newline separated list of keys
|
||||
FILE * keys_fd = fopen("keys.txt", "r");
|
||||
cmph_t *hash = NULL;
|
||||
if (keys_fd == NULL)
|
||||
{
|
||||
fprintf(stderr, "File \"keys.txt\" not found\n");
|
||||
exit(1);
|
||||
}
|
||||
// Source of keys
|
||||
cmph_io_adapter_t *source = cmph_io_nlfile_adapter(keys_fd);
|
||||
|
||||
cmph_config_t *config = cmph_config_new(source);
|
||||
cmph_config_set_algo(config, CMPH_BMZ);
|
||||
hash = cmph_new(config);
|
||||
cmph_config_destroy(config);
|
||||
|
||||
//Find key
|
||||
const char *key = "jjjjjjjjjj";
|
||||
unsigned int id = cmph_search(hash, key, strlen(key));
|
||||
fprintf(stderr, "Id:%u\n", id);
|
||||
//Destroy hash
|
||||
cmph_destroy(hash);
|
||||
free(source);
|
||||
fclose(keys_fd);
|
||||
return 0;
|
||||
}
|
||||
10
examples/keys.txt
Normal file
10
examples/keys.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
aaaaaaaaaa
|
||||
bbbbbbbbbb
|
||||
cccccccccc
|
||||
dddddddddd
|
||||
eeeeeeeeee
|
||||
ffffffffff
|
||||
gggggggggg
|
||||
hhhhhhhhhh
|
||||
iiiiiiiiii
|
||||
jjjjjjjjjj
|
||||
26
examples/vector_adapter_ex1.c
Executable file
26
examples/vector_adapter_ex1.c
Executable file
@@ -0,0 +1,26 @@
|
||||
#include <cmph.h>
|
||||
|
||||
// Create minimal perfect hash function from in-memory vector
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// Creating a filled vector
|
||||
const char *vector[] = {"aaaaaaaaaa", "bbbbbbbbbb", "cccccccccc", "dddddddddd", "eeeeeeeeee",
|
||||
"ffffffffff", "gggggggggg", "hhhhhhhhhh", "iiiiiiiiii", "jjjjjjjjjj"};
|
||||
unsigned int nkeys = 10;
|
||||
// Source of keys
|
||||
cmph_io_adapter_t *source = cmph_io_vector_adapter(vector, nkeys);
|
||||
|
||||
//Create minimal perfect hash function using the default (chm) algorithm.
|
||||
cmph_config_t *config = cmph_config_new(source);
|
||||
cmph_t *hash = cmph_new(config);
|
||||
cmph_config_destroy(config);
|
||||
|
||||
//Find key
|
||||
const char *key = "jjjjjjjjjj";
|
||||
unsigned int id = cmph_search(hash, key, strlen(key));
|
||||
fprintf(stderr, "Id:%u\n", id);
|
||||
//Destroy hash
|
||||
cmph_destroy(hash);
|
||||
free(source);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user