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; }
ATTrie :: ATTrie(const char * path) : data(NULL) { nodes=database_info.trie_nodes; data = new ATTrieData; #if 1 //added by Jad due to an error occuring in desctructor when trie is loaded from file 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); #endif data->trie = trie_new_from_file(path); if (data->trie == NULL) throw "TrieConstructionException"; }
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"; } }
/** * @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; }
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; if (NULL == (alpha_map = alpha_map_new ())) 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 (alpha_map, b, e); } return alpha_map; exit_map_created: alpha_map_free (alpha_map); exit_file_read: fseek (file, save_pos, SEEK_SET); return NULL; }