Beispiel #1
0
// 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", "w");
    // 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", "r");
    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;
}
Beispiel #2
0
char *cmph_op_build(bot_t * bot, cmphx_t ** cmphx, char *string)
{
	cmphx_t *cmphx_ptr = NULL;
	char *str = NULL;

	char **keys = NULL;
	int nkeys = 0;

	debug(NULL, "cmph_op_build: Entered: %p %p %p\n", bot, cmphx, string);

	if (!bot || !cmphx || !_sNULL(string))
		return NULL;

	cmphx_ptr = *cmphx;

	if (!cmphx_ptr) {
		cmphx_ptr = (cmphx_t *) calloc(1, sizeof(cmphx_t));
		if (!cmphx_ptr)
			return NULL;
		*cmphx = cmphx_ptr;
	}

	if (cmphx_ptr->hash) {
		cmph_op_clear(bot, cmphx, string);
	}

	keys =
	    tokenize_array(NULL, string,
			   TOKENIZE_NORMAL | TOKENIZE_EATWHITESPACE, " ",
			   &nkeys);
	if (!keys)
		goto cleanup;

	tokenize_sort_strings(keys, &nkeys,
			      TOKENIZE_SORT_STRINGS_FORWARD |
			      TOKENIZE_SORT_STRINGS_UNIQ);

	cmphx_ptr->fp = fopen("/tmp/cmph.mph", "w");
	if (!cmphx_ptr->fp)
		goto cleanup;

	cmphx_ptr->source = cmph_io_vector_adapter((char **)keys, nkeys);
	if (!cmphx_ptr->source)
		goto cleanup;

	cmphx_ptr->config = cmph_config_new(cmphx_ptr->source);
	if (!cmphx_ptr->config)
		goto cleanup;

	cmph_config_set_algo(cmphx_ptr->config, CMPH_BRZ);
	cmph_config_set_tmp_dir(cmphx_ptr->config, (cmph_uint8 *) "/tmp/");
	cmph_config_set_mphf_fd(cmphx_ptr->config, cmphx_ptr->fp);
	cmphx_ptr->hash = cmph_new(cmphx_ptr->config);
	if (!cmphx_ptr->hash)
		goto cleanup;

	cmph_config_destroy(cmphx_ptr->config);
	cmphx_ptr->config = NULL;

	cmph_dump(cmphx_ptr->hash, cmphx_ptr->fp);

	cmph_destroy(cmphx_ptr->hash);

	fclose(cmphx_ptr->fp);

	cmphx_ptr->fp = fopen("/tmp/cmph.mph", "r");

	tokenize_destroy_array(NULL, keys);

	debug(NULL, "cmph_op_build: Success\n");

	cmphx_ptr->hash = cmph_load(cmphx_ptr->fp);

	return str;

/* error */
 cleanup:
	cmph_op_clear(bot, cmphx, string);

	debug(NULL, "cmph_op_build: Failure\n");

	return str;
}