/** * Write out the pairs * @param root the document root * @param mvd the mvd we are writing to * @param encoding the XML encoding * @return 1 if it worked else 0 */ static int write_pairs( dom_item *root, MVD *mvd, char *encoding ) { int res = 1; dom_item *p_parent = dom_array_create( "pairs" ); if ( p_parent != NULL ) { hashmap *parents = hashmap_create( 12, 0 ); hashmap *orphans = hashmap_create( 12, 0 ); if ( parents != NULL && orphans != NULL ) { int i,id = 1; dyn_array *pairs = mvd_get_pairs( mvd ); for ( i=0; i<dyn_array_size(pairs); i++ ) { pair *p = dyn_array_get( pairs, i ); res = write_one_pair( p, p_parent, parents, orphans, &id ); if ( !res ) break; } hashmap_dispose( parents, NULL ); hashmap_dispose( orphans, NULL ); } } // attach pairs node to its parent node if ( res ) res = dom_add_child( root, p_parent ); return res; }
/** * Compute the range's length if it was a milestone * @param df the dest file whose queue is to be scanned * @param tlen the length of the underlying text * @return true if it worked else 0 */ static int compute_range_lengths( dest_file *df, int tlen ) { int res = 1; hashmap *map = hashmap_create(); if ( map != NULL ) { range *r = df->queue; while ( r != NULL ) { range *s = hashmap_get( map, range_get_name(r) ); if ( s != NULL ) range_set_len( s, range_get_start(r)-range_get_start(s) ); hashmap_put( map, range_get_name(r), r ); r = range_get_next( r ); } hashmap_iterator *iter = hashmap_iterator_create( map ); if ( iter != NULL ) { while ( hashmap_iterator_has_next(iter) ) { char *key = hashmap_iterator_next(iter); range *t = hashmap_get( map, key ); range_set_len( t, tlen-range_get_start(t) ); } hashmap_iterator_dispose( iter ); } else res = 0; hashmap_dispose( map ); } else res = 0; return res; }
void test_int_hashmap() { hashmap hm; hashmap_new(&hm, sizeof(char*), 20, hashmap_int_hash_fun, hashmap_int_compare_fun, NULL, Number); char* words[] = { "casa", "borde", "leche", "vaca", "mesa", "pila", "solo", "pared", "luz", "television", "dvd", "pata", "plato", "vinilo", "amplificador"}; int numbers[15]; for (int i = 0; i < 15; ++i) { int n = get_random(uniforme, 0, 150); numbers[i] = n; hashmap_enter(&hm, &numbers[i], words[i]); } printf("\n"); int k = 88; char* word = (char*) hashmap_get_value(&hm, &k); printf("word with key 88 is %s\n", word); vector* res = hashmap_get_values(&hm); for (int i = 0; i < res->len; ++i) { char* w = (char*) res->elements + i*hm.value_size; printf("%s\n", w); } hashmap_map(&hm, hashmap_int_print_pair, NULL); vector_dispose(res); free(res); res = NULL; hashmap_dispose(&hm); }
/** * Check if a list is broken by being circular * @param c a card pointer somewhere in the list * @return 1 if it has two ends else 0 */ int card_list_circular( card *c ) { int res = 0; hashmap *hm = hashmap_create( 12, 0 ); card *right = c->right; char junk[16]; UChar key[32]; char ckey[32]; strcpy( junk, "junk" ); snprintf(ckey,32,"%lx",(long)right); u_print( key, ckey, strlen(ckey) ); hashmap_put( hm, key, junk ); while ( !res && right != NULL ) { snprintf(ckey,32,"%lx",(long)right); //printf("%s\n",ckey); ascii_to_uchar( ckey, key, 32 ); if ( hashmap_contains(hm,key) ) res = 1; else { hashmap_put( hm, key, junk ); right = right->right; } } card *left = c->left; while ( !res && left != NULL ) { snprintf(ckey,32,"%lx",(long)left); //printf("%s\n",ckey); ascii_to_uchar( ckey, key, 32 ); if ( hashmap_contains(hm,key) ) res = 1; else { hashmap_put( hm, key, junk ); left = left->left; } } hashmap_dispose( hm, NULL ); return res; }
/** * Print out details of any unfreed blocks or report that all blocks were freed */ void memory_print() { if ( allocations != NULL ) { int unfreed = 0; hashmap_iterator *iter = hashmap_iterator_create( allocations ); while ( hashmap_iterator_has_next(iter) ) { UChar *key = hashmap_iterator_next( iter ); UChar *value = hashmap_get( allocations, key ); debug_report("unfreed block %s at %s \n", key, value ); unfreed++; } if ( unfreed == 0 ) debug_report("all blocks were freed\n"); hashmap_dispose( allocations ); free( strings ); strings = NULL; } }
/** * Dispose of a userdata * @param u the object to dispose */ void userdata_dispose( userdata *u ) { if ( u != NULL ) { if ( u->rules != NULL ) u->rules = recipe_dispose( u->rules ); if ( u->ignoring != NULL ) stack_delete( u->ignoring ); if ( u->range_stack != NULL ) stack_delete( u->range_stack ); if ( u->spell_config != NULL ) delete_aspell_config(u->spell_config); if ( u->spell_checker != NULL ) delete_aspell_speller(u->spell_checker); if ( u->last_word != NULL ) free( u->last_word ); if ( u->dest_map != NULL ) hashmap_dispose( u->dest_map ); // we don't own the hh_exceptions free( u ); } }
void hashmap_test(void) { printf("hashmap: running tests..."); Hashmap *map_1; map_1 = hashmap_create(100, NULL, &hashmap_hash_cstring, &hashmap_compare_cstring); hashmap_add(map_1, "pickles", (void*)pickle_value); assert( hashmap_count(map_1) == 1 ); assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND ); assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND ); assert( hashmap_item(map_1, "apricots") == NULL ); assert( hashmap_item(map_1, "pickles") == pickle_value ); hashmap_add(map_1, "pickles", (void*)pickle_value); assert( hashmap_count(map_1) == 1 ); hashmap_add(map_1, "apples", (void*)apple_value); assert( hashmap_count(map_1) == 2 ); assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND ); assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND ); assert( hashmap_contains(map_1, "apples") == HASHMAP_FOUND ); assert( hashmap_item(map_1, "apricots") == NULL ); assert( hashmap_item(map_1, "pickles") == pickle_value ); assert( hashmap_item(map_1, "apples") == apple_value ); hashmap_add(map_1, "pizza", (void*)pizza_value); assert( hashmap_count(map_1) == 3 ); assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND ); assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND ); assert( hashmap_contains(map_1, "apples") == HASHMAP_FOUND ); assert( hashmap_contains(map_1, "pizza") == HASHMAP_FOUND ); assert( hashmap_item(map_1, "apricots") == NULL ); assert( hashmap_item(map_1, "pickles") == pickle_value ); assert( hashmap_item(map_1, "apples") == apple_value ); assert( hashmap_item(map_1, "pizza") == pizza_value ); assert(map_1->buckets[28].pair_count == 1); assert(map_1->buckets[66].pair_count == 1); assert(map_1->buckets[51].pair_count == 1); assert(map_1->buckets[10].pair_count == 0); hashmap_enumerate(map_1, test_enumerator_); hashmap_remove(map_1, "apples"); assert( hashmap_count(map_1) == 2 ); assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND ); assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND ); assert( hashmap_contains(map_1, "apples") == HASHMAP_NOT_FOUND ); assert( hashmap_contains(map_1, "pizza") == HASHMAP_FOUND ); assert( hashmap_item(map_1, "apricots") == NULL ); assert( hashmap_item(map_1, "pickles") == pickle_value ); assert( hashmap_item(map_1, "apples") == NULL ); assert( hashmap_item(map_1, "pizza") == pizza_value ); hashmap_clear(map_1); assert( hashmap_count(map_1) == 0 ); hashmap_dispose(map_1); assert( (unsigned long)hashmap_hash_cstring(NULL, "pickles") % 100 == 28 ); assert( (unsigned long)hashmap_hash_cstring(NULL, "apples") % 100 == 66 ); assert( (unsigned long)hashmap_hash_cstring(NULL, "pizza") % 100 == 51 ); assert( (unsigned long)hashmap_hash_cstring(NULL, "apricots") % 100 == 10 ); map_1 = hashmap_create(1, NULL, &hashmap_hash_cstring, &hashmap_compare_cstring); hashmap_add(map_1, "pickles", (void*)pickle_value); assert( hashmap_count(map_1) == 1 ); hashmap_add(map_1, "pizza", (void*)pizza_value); assert( hashmap_count(map_1) == 2 ); hashmap_add(map_1, "apples", (void*)apple_value); assert( hashmap_count(map_1) == 3 ); assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND ); assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND ); assert( hashmap_contains(map_1, "apples") == HASHMAP_FOUND ); assert( hashmap_contains(map_1, "pizza") == HASHMAP_FOUND ); assert( hashmap_item(map_1, "apricots") == NULL ); assert( hashmap_item(map_1, "pickles") == pickle_value ); assert( hashmap_item(map_1, "apples") == apple_value ); assert( hashmap_item(map_1, "pizza") == pizza_value ); assert(map_1->buckets[0].pair_count == 3); hashmap_add(map_1, "pizza", (void*)apple_value); assert( hashmap_count(map_1) == 3 ); assert( hashmap_contains(map_1, "apricots") == HASHMAP_NOT_FOUND ); assert( hashmap_contains(map_1, "pickles") == HASHMAP_FOUND ); assert( hashmap_contains(map_1, "apples") == HASHMAP_FOUND ); assert( hashmap_contains(map_1, "pizza") == HASHMAP_FOUND ); assert( hashmap_item(map_1, "apricots") == NULL ); assert( hashmap_item(map_1, "pickles") == pickle_value ); assert( hashmap_item(map_1, "apples") == apple_value ); assert( hashmap_item(map_1, "pizza") == apple_value ); assert(strcmp(map_1->buckets[0].pairs[0]->key, "apples") == 0); assert(strcmp(map_1->buckets[0].pairs[1]->key, "pickles") == 0); assert(strcmp(map_1->buckets[0].pairs[2]->key, "pizza") == 0); hashmap_dispose(map_1); printf("ok.\n"); }