Exemplo n.º 1
0
int
main (void)
{
    AlphaMap    *alpha_map;
    Trie        *test_trie;
    AlphaChar    key[3];
    TrieData     data;

    msg_step ("Preparing alpha map");
    alpha_map = alpha_map_new ();
    if (!alpha_map) {
        printf ("Fail to allocate alpha map\n");
        goto err_alpha_map_not_created;
    }
    if (alpha_map_add_range (alpha_map, 0x00, 0xff) != 0) {
        printf ("Fail to add full alpha map range\n");
        goto err_alpha_map_created;
    }

    msg_step ("Preparing trie");
    test_trie = trie_new (alpha_map);
    alpha_map_free (alpha_map);
    if (!test_trie) {
        printf ("Fail to create test trie\n");
        goto err_alpha_map_created;
    }

    msg_step ("Storing key to test trie");
    key[0] = 0xff;
    key[1] = 0xff;
    key[2] = 0;
    if (!trie_store (test_trie, key, TEST_DATA)) {
        printf ("Fail to store key to test trie\n");
        goto err_trie_created;
    }

    msg_step ("Retrieving data from test trie");
    if (!trie_retrieve (test_trie, key, &data)) {
        printf ("Fail to retrieve key from test trie\n");
        goto err_trie_created;
    }
    if (TEST_DATA != data) {
        printf ("Retrieved data = %d, not %d\n", data, TEST_DATA);
        goto err_trie_created;
    }

    msg_step ("Freeing test trie");
    trie_free (test_trie);
    return 0;

err_trie_created:
    trie_free (test_trie);
err_alpha_map_created:
    alpha_map_free (alpha_map);
err_alpha_map_not_created:
    return 1;
}
Exemplo n.º 2
0
/**
 * @brief Create a new trie
 *
 * @param   alpha_map   : the alphabet set for the trie
 *
 * @return a pointer to the newly created trie, NULL on failure
 *
 * Create a new empty trie object based on the given @a alpha_map alphabet
 * set. The trie contents can then be added and deleted with trie_store() and
 * trie_delete() respectively.
 *
 * The created object must be freed with trie_free().
 */
Trie *
trie_new (const AlphaMap *alpha_map)
{
    Trie *trie;

    trie = (Trie *) malloc (sizeof (Trie));
    if (!trie)
        return NULL;

    trie->alpha_map = alpha_map_clone (alpha_map);
    if (!trie->alpha_map)
        goto exit_trie_created;

    trie->da = da_new ();
    if (!trie->da)
        goto exit_alpha_map_created;

    trie->tail = tail_new ();
    if (!trie->tail)
        goto exit_da_created;

    trie->is_dirty = TRUE;
    return trie;

exit_da_created:
    da_free (trie->da);
exit_alpha_map_created:
    alpha_map_free (trie->alpha_map);
exit_trie_created:
    free (trie);
    return NULL;
}
Exemplo n.º 3
0
/**
 * @brief Create a new trie by loading from a file
 *
 * @param path  : the path to the file
 *
 * @return a pointer to the created trie, NULL on failure
 *
 * Create a new trie and initialize its contents by loading from the file at
 * given @a path.
 *
 * The created object must be freed with trie_free().
 */
Trie *
trie_new_from_file (const char *path)
{
    Trie       *trie;
    FILE       *trie_file;

    trie_file = fopen (path, "r");
    if (!trie_file)
        return NULL;

    trie = (Trie *) malloc (sizeof (Trie));
    if (!trie)
        goto exit_file_openned;

    if (NULL == (trie->alpha_map = alpha_map_read_bin (trie_file)))
        goto exit_trie_created;
    if (NULL == (trie->da   = da_read (trie_file)))
        goto exit_alpha_map_created;
    if (NULL == (trie->tail = tail_read (trie_file)))
        goto exit_da_created;

    fclose (trie_file);
    trie->is_dirty = FALSE;
    return trie;

exit_da_created:
    da_free (trie->da);
exit_alpha_map_created:
    alpha_map_free (trie->alpha_map);
exit_trie_created:
    free (trie);
exit_file_openned:
    fclose (trie_file);
    return NULL;
}
Exemplo n.º 4
0
/**
 * @brief Create a new trie by reading from an open file
 *
 * @param file  : the handle of the open file
 *
 * @return a pointer to the created trie, NULL on failure
 *
 * Create a new trie and initialize its contents by reading from the open
 * @a file. After reading, the file pointer is left at the end of the trie data.
 * This can be useful for reading embedded trie index as part of a file data.
 *
 * The created object must be freed with trie_free().
 *
 * Available since: 0.2.4
 */
Trie *
trie_fread (FILE *file)
{
    Trie       *trie;

    trie = (Trie *) malloc (sizeof (Trie));
    if (!trie)
        return NULL;

    if (NULL == (trie->alpha_map = alpha_map_fread_bin (file)))
        goto exit_trie_created;
    if (NULL == (trie->da   = da_fread (file)))
        goto exit_alpha_map_created;
    if (NULL == (trie->tail = tail_fread (file)))
        goto exit_da_created;

    trie->is_dirty = FALSE;
    return trie;

exit_da_created:
    da_free (trie->da);
exit_alpha_map_created:
    alpha_map_free (trie->alpha_map);
exit_trie_created:
    free (trie);
    return NULL;
}
Exemplo n.º 5
0
/**
 * @brief Free a trie object
 *
 * @param trie  : the trie object to free
 *
 * Destruct the @a trie and free its allocated memory.
 */
void
trie_free (Trie *trie)
{
    alpha_map_free (trie->alpha_map);
    da_free (trie->da);
    tail_free (trie->tail);
    free (trie);
}
Exemplo n.º 6
0
ATTrie :: ~ATTrie() 
{
    if (data->trie != NULL)
        trie_free(data->trie);
    if (data->amap != NULL)
        alpha_map_free(data->amap);
    delete data;
}
Exemplo n.º 7
0
int
sb_trie_close (SBTrie *sb_trie)
{
    if (!sb_trie)
        return -1;

    alpha_map_free (sb_trie->alpha_map);
    return trie_close (sb_trie->trie);
}
Exemplo n.º 8
0
ATTrie :: ATTrie() : data(NULL) {
	nodes=database_info.trie_nodes;
    data = new ATTrieData;
    data->amap = alpha_map_new();
    if (data->amap == NULL)
        throw "AlphaMapConstructionException";

    // add all characters except 0 and FF FF FF FF 
    // AlphaChar is uint32
    // this needs to change later to allow only Arabic and Latin
    alpha_map_add_range(data->amap, 1, 0xFFFFFFFE);
    data->trie = trie_new(data->amap);

    if (data->trie == NULL){
        alpha_map_free(data->amap);
        throw "TrieConstructionException";
    }
}
Exemplo n.º 9
0
/**
 * @brief Create a clone of alphabet map
 *
 * @param a_map : the source alphabet map to clone
 *
 * @return a pointer to the alphabet map clone, NULL on failure
 *
 *  The created object must be freed with alpha_map_free().
 */
AlphaMap *
alpha_map_clone (const AlphaMap *a_map)
{
    AlphaMap   *alpha_map;
    AlphaRange *range;

    alpha_map = alpha_map_new ();
    if (!alpha_map)
        return NULL;

    for (range = a_map->first_range; range; range = range->next) {
        if (alpha_map_add_range (alpha_map, range->begin, range->end) != 0) {
            alpha_map_free (alpha_map);
            return NULL;
        }
    }

    return alpha_map;
}
Exemplo n.º 10
0
AlphaMap *
alpha_map_fread_bin (FILE *file)
{
    long        save_pos;
    uint32      sig;
    int32       total, i;
    AlphaMap   *alpha_map;

    /* check signature */
    save_pos = ftell (file);
    if (!file_read_int32 (file, (int32 *) &sig) || ALPHAMAP_SIGNATURE != sig)
        goto exit_file_read;

    alpha_map = alpha_map_new ();
    if (UNLIKELY (!alpha_map))
        goto exit_file_read;

    /* read number of ranges */
    if (!file_read_int32 (file, &total))
        goto exit_map_created;

    /* read character ranges */
    for (i = 0; i < total; i++) {
        int32   b, e;

        if (!file_read_int32 (file, &b) || !file_read_int32 (file, &e))
            goto exit_map_created;
        alpha_map_add_range_only (alpha_map, b, e);
    }

    /* work area */
    if (UNLIKELY (alpha_map_recalc_work_area (alpha_map) != 0))
        goto exit_map_created;

    return alpha_map;

exit_map_created:
    alpha_map_free (alpha_map);
exit_file_read:
    fseek (file, save_pos, SEEK_SET);
    return NULL;
}
Exemplo n.º 11
0
/**
 * @brief Create a clone of alphabet map
 *
 * @param a_map : the source alphabet map to clone
 *
 * @return a pointer to the alphabet map clone, NULL on failure
 *
 *  The created object must be freed with alpha_map_free().
 */
AlphaMap *
alpha_map_clone (const AlphaMap *a_map)
{
    AlphaMap   *alpha_map;
    AlphaRange *range;

    alpha_map = alpha_map_new ();
    if (UNLIKELY (!alpha_map))
        return NULL;

    for (range = a_map->first_range; range; range = range->next) {
        if (alpha_map_add_range_only (alpha_map, range->begin, range->end) != 0)
            goto exit_map_created;
    }

    if (alpha_map_recalc_work_area (alpha_map) != 0)
        goto exit_map_created;

    return alpha_map;

exit_map_created:
    alpha_map_free (alpha_map);
    return NULL;
}
Exemplo n.º 12
0
int
sb_trie_close (SBTrie *sb_trie)
{
    alpha_map_free (sb_trie->alpha_map);
    return trie_close (sb_trie->trie);
}