Add 'deps/cmph/' from commit 'a250982ade093f4eed0552bbdd22dd7b0432007f'
git-subtree-dir: deps/cmph git-subtree-mainline:5040f4007bgit-subtree-split:a250982ade
This commit is contained in:
15
deps/cmph/examples/Makefile.am
vendored
Executable file
15
deps/cmph/examples/Makefile.am
vendored
Executable file
@@ -0,0 +1,15 @@
|
||||
noinst_PROGRAMS = vector_adapter_ex1 file_adapter_ex2 struct_vector_adapter_ex3 small_set_ex4
|
||||
|
||||
AM_CPPFLAGS = -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
|
||||
|
||||
struct_vector_adapter_ex3_LDADD = ../src/libcmph.la
|
||||
struct_vector_adapter_ex3_SOURCES = struct_vector_adapter_ex3.c
|
||||
|
||||
small_set_ex4_LDADD = ../src/libcmph.la
|
||||
small_set_ex4_SOURCES = small_set_ex4.c
|
||||
32
deps/cmph/examples/file_adapter_ex2.c
vendored
Normal file
32
deps/cmph/examples/file_adapter_ex2.c
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <cmph.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
// Create minimal perfect hash function from in-disk keys using BDZ 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_BDZ);
|
||||
hash = cmph_new(config);
|
||||
cmph_config_destroy(config);
|
||||
|
||||
//Find key
|
||||
const char *key = "jjjjjjjjjj";
|
||||
unsigned int id = cmph_search(hash, key, (cmph_uint32)strlen(key));
|
||||
fprintf(stderr, "Id:%u\n", id);
|
||||
//Destroy hash
|
||||
cmph_destroy(hash);
|
||||
cmph_io_nlfile_adapter_destroy(source);
|
||||
fclose(keys_fd);
|
||||
return 0;
|
||||
}
|
||||
10
deps/cmph/examples/keys.txt
vendored
Normal file
10
deps/cmph/examples/keys.txt
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
aaaaaaaaaa
|
||||
bbbbbbbbbb
|
||||
cccccccccc
|
||||
dddddddddd
|
||||
eeeeeeeeee
|
||||
ffffffffff
|
||||
gggggggggg
|
||||
hhhhhhhhhh
|
||||
iiiiiiiiii
|
||||
jjjjjjjjjj
|
||||
105
deps/cmph/examples/small_set_ex4.c
vendored
Normal file
105
deps/cmph/examples/small_set_ex4.c
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <cmph.h>
|
||||
|
||||
int test(cmph_uint32* items_to_hash, cmph_uint32 items_len, CMPH_ALGO alg_n)
|
||||
{
|
||||
cmph_t *hash;
|
||||
cmph_config_t *config;
|
||||
cmph_io_adapter_t *source;
|
||||
cmph_uint32 i;
|
||||
char filename[256];
|
||||
FILE* mphf_fd = NULL;
|
||||
|
||||
printf("%s (%u)\n", cmph_names[alg_n], alg_n);
|
||||
|
||||
source = cmph_io_struct_vector_adapter(items_to_hash,
|
||||
(cmph_uint32)sizeof(cmph_uint32),
|
||||
0,
|
||||
(cmph_uint32)sizeof(cmph_uint32),
|
||||
items_len);
|
||||
config = cmph_config_new(source);
|
||||
cmph_config_set_algo(config, alg_n);
|
||||
if (alg_n == CMPH_BRZ) {
|
||||
sprintf(filename, "%s_%u.mph", cmph_names[alg_n], items_len);
|
||||
mphf_fd = fopen(filename, "w");
|
||||
cmph_config_set_mphf_fd(config, mphf_fd);
|
||||
}
|
||||
hash = cmph_new(config);
|
||||
cmph_config_destroy(config);
|
||||
|
||||
if (alg_n == CMPH_BRZ) {
|
||||
cmph_dump(hash, mphf_fd);
|
||||
cmph_destroy(hash);
|
||||
fclose(mphf_fd);
|
||||
mphf_fd = fopen(filename, "r");
|
||||
hash = cmph_load(mphf_fd);
|
||||
}
|
||||
printf("packed_size %u\n",cmph_packed_size(hash));
|
||||
|
||||
for (i=0; i<items_len; ++i)
|
||||
printf("%d -> %u\n",
|
||||
items_to_hash[i],
|
||||
cmph_search(hash,
|
||||
(char*)(items_to_hash+i),
|
||||
(cmph_uint32)sizeof(cmph_uint32)));
|
||||
printf("\n");
|
||||
|
||||
cmph_io_vector_adapter_destroy(source);
|
||||
cmph_destroy(hash);
|
||||
|
||||
if (alg_n == CMPH_BRZ) {
|
||||
fclose(mphf_fd);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main (void)
|
||||
{
|
||||
cmph_uint32 vec1[] = {1,2,3,4,5};
|
||||
cmph_uint32 vec1_len = 5;
|
||||
|
||||
cmph_uint32 vec2[] = {7576423, 7554496}; //CMPH_FCH, CMPH_BDZ, CMPH_BDZ_PH (4,5,6)
|
||||
cmph_uint32 vec2_len = 2;
|
||||
cmph_uint32 vec3[] = {2184764, 1882984, 1170551}; // CMPH_CHD_PH, CMPH_CHD (7,8)
|
||||
cmph_uint32 vec3_len = 3;
|
||||
cmph_uint32 vec4[] = {2184764}; // CMPH_CHD_PH, CMPH_CHD (7,8)
|
||||
cmph_uint32 vec4_len = 1;
|
||||
cmph_uint32 i;
|
||||
|
||||
// Testing with vec1
|
||||
cmph_uint32* values = (cmph_uint32*)vec1;
|
||||
cmph_uint32 length = vec1_len;
|
||||
printf("TESTING VECTOR WITH %u INTEGERS\n", length);
|
||||
for (i = 0; i < CMPH_COUNT; i++)
|
||||
{
|
||||
test(values, length, i);
|
||||
}
|
||||
|
||||
// Testing with vec2
|
||||
values = (cmph_uint32*)vec2;
|
||||
length = vec2_len;
|
||||
printf("TESTING VECTOR WITH %u INTEGERS\n", length);
|
||||
for (i = 0; i < CMPH_COUNT; i++)
|
||||
{
|
||||
test(values, length, i);
|
||||
}
|
||||
|
||||
// Testing with vec3
|
||||
values = (cmph_uint32*)vec3;
|
||||
length = vec3_len;
|
||||
printf("TESTING VECTOR WITH %u INTEGERS\n", length);
|
||||
for (i = 0; i < CMPH_COUNT; i++)
|
||||
{
|
||||
test(values, length, i);
|
||||
}
|
||||
|
||||
// Testing with vec4
|
||||
values = (cmph_uint32*)vec4;
|
||||
length = vec4_len;
|
||||
printf("TESTING VECTOR WITH %u INTEGERS\n", length);
|
||||
for (i = 0; i < CMPH_COUNT; i++)
|
||||
{
|
||||
test(values, length, i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
51
deps/cmph/examples/struct_vector_adapter_ex3.c
vendored
Normal file
51
deps/cmph/examples/struct_vector_adapter_ex3.c
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <cmph.h>
|
||||
#include <string.h>
|
||||
// Create minimal perfect hash function from in-memory vector
|
||||
|
||||
#pragma pack(1)
|
||||
typedef struct {
|
||||
cmph_uint32 id;
|
||||
char key[11];
|
||||
cmph_uint32 year;
|
||||
} rec_t;
|
||||
#pragma pack(0)
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// Creating a filled vector
|
||||
unsigned int i = 0;
|
||||
rec_t vector[10] = {{1, "aaaaaaaaaa", 1999}, {2, "bbbbbbbbbb", 2000}, {3, "cccccccccc", 2001},
|
||||
{4, "dddddddddd", 2002}, {5, "eeeeeeeeee", 2003}, {6, "ffffffffff", 2004},
|
||||
{7, "gggggggggg", 2005}, {8, "hhhhhhhhhh", 2006}, {9, "iiiiiiiiii", 2007},
|
||||
{10,"jjjjjjjjjj", 2008}};
|
||||
unsigned int nkeys = 10;
|
||||
FILE* mphf_fd = fopen("temp_struct_vector.mph", "wb");
|
||||
// Source of keys
|
||||
cmph_io_adapter_t *source = cmph_io_struct_vector_adapter(vector, (cmph_uint32)sizeof(rec_t), (cmph_uint32)sizeof(cmph_uint32), 11, nkeys);
|
||||
|
||||
//Create minimal perfect hash function using the BDZ algorithm.
|
||||
cmph_config_t *config = cmph_config_new(source);
|
||||
cmph_config_set_algo(config, CMPH_BDZ);
|
||||
cmph_config_set_mphf_fd(config, mphf_fd);
|
||||
cmph_t *hash = cmph_new(config);
|
||||
cmph_config_destroy(config);
|
||||
cmph_dump(hash, mphf_fd);
|
||||
cmph_destroy(hash);
|
||||
fclose(mphf_fd);
|
||||
|
||||
//Find key
|
||||
mphf_fd = fopen("temp_struct_vector.mph", "rb");
|
||||
hash = cmph_load(mphf_fd);
|
||||
while (i < nkeys) {
|
||||
const char *key = vector[i].key;
|
||||
unsigned int id = cmph_search(hash, key, 11);
|
||||
fprintf(stderr, "key:%s -- hash:%u\n", key, id);
|
||||
i++;
|
||||
}
|
||||
|
||||
//Destroy hash
|
||||
cmph_destroy(hash);
|
||||
cmph_io_vector_adapter_destroy(source);
|
||||
fclose(mphf_fd);
|
||||
return 0;
|
||||
}
|
||||
41
deps/cmph/examples/vector_adapter_ex1.c
vendored
Executable file
41
deps/cmph/examples/vector_adapter_ex1.c
vendored
Executable file
@@ -0,0 +1,41 @@
|
||||
#include <cmph.h>
|
||||
#include <string.h>
|
||||
// Create minimal perfect hash function from in-memory vector
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
// Creating a filled vector
|
||||
unsigned int i = 0;
|
||||
const char *vector[] = {"aaaaaaaaaa", "bbbbbbbbbb", "cccccccccc", "dddddddddd", "eeeeeeeeee",
|
||||
"ffffffffff", "gggggggggg", "hhhhhhhhhh", "iiiiiiiiii", "jjjjjjjjjj"};
|
||||
unsigned int nkeys = 10;
|
||||
FILE* mphf_fd = fopen("temp.mph", "wb");
|
||||
// Source of keys
|
||||
cmph_io_adapter_t *source = cmph_io_vector_adapter((char **)vector, nkeys);
|
||||
|
||||
//Create minimal perfect hash function using the brz algorithm.
|
||||
cmph_config_t *config = cmph_config_new(source);
|
||||
cmph_config_set_algo(config, CMPH_BRZ);
|
||||
cmph_config_set_mphf_fd(config, mphf_fd);
|
||||
cmph_t *hash = cmph_new(config);
|
||||
cmph_config_destroy(config);
|
||||
cmph_dump(hash, mphf_fd);
|
||||
cmph_destroy(hash);
|
||||
fclose(mphf_fd);
|
||||
|
||||
//Find key
|
||||
mphf_fd = fopen("temp.mph", "rb");
|
||||
hash = cmph_load(mphf_fd);
|
||||
while (i < nkeys) {
|
||||
const char *key = vector[i];
|
||||
unsigned int id = cmph_search(hash, key, (cmph_uint32)strlen(key));
|
||||
fprintf(stderr, "key:%s -- hash:%u\n", key, id);
|
||||
i++;
|
||||
}
|
||||
|
||||
//Destroy hash
|
||||
cmph_destroy(hash);
|
||||
cmph_io_vector_adapter_destroy(source);
|
||||
fclose(mphf_fd);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user