/* * complete parameter create function */ cp_avltree * cp_avltree_create_by_option(int mode, cp_compare_fn cmp, cp_copy_fn key_copy, cp_destructor_fn key_dtr, cp_copy_fn val_copy, cp_destructor_fn val_dtr) { cp_avltree *tree = cp_avltree_create(cmp); if (tree == NULL) return NULL; tree->mode = mode; tree->key_copy = key_copy; tree->key_dtr = key_dtr; tree->value_copy = val_copy; tree->value_dtr = val_dtr; if (!(mode & COLLECTION_MODE_NOSYNC)) { tree->lock = malloc(sizeof(cp_lock)); if (tree->lock == NULL) { cp_avltree_destroy(tree); return NULL; } if (cp_lock_init(tree->lock, NULL) != 0) { cp_avltree_destroy(tree); return NULL; } } return tree; }
int main(int argc, char *argv[]) { FILE *in = stdin; char buf[0x4000]; /* limited to 16K character word length */ cp_avltree *wordlist; cp_avltree *freqlist; int count = 0; int one = 1; int *wc; if (argc > 1) { in = fopen(argv[1], "r"); if (in == NULL) { fprintf(stderr, "can\'t open %s\n", argv[1]); exit(1); } } /* wordlist keys are distinct */ wordlist = cp_avltree_create_by_option(COLLECTION_MODE_NOSYNC | COLLECTION_MODE_COPY | COLLECTION_MODE_DEEP, (cp_compare_fn) strcmp, (cp_copy_fn) strdup, free, (cp_copy_fn) intdup, free); while ((fscanf(in, "%s", buf)) != EOF) { filter_string(buf, PUNCT); to_lowercase(buf); wc = cp_avltree_get(wordlist, buf); if (wc) (*wc)++; else cp_avltree_insert(wordlist, buf, &one); count++; } fclose(in); /* frequency list entry keys are not distinct */ freqlist = cp_avltree_create_by_option(COLLECTION_MODE_NOSYNC | COLLECTION_MODE_MULTIPLE_VALUES, (cp_compare_fn) freqcmp, NULL, NULL, NULL, NULL); cp_avltree_callback(wordlist, add_freq, freqlist); cp_avltree_callback(freqlist, print_word_freq, &one); printf("%d words, %d distinct entries\n", count, cp_avltree_count(wordlist)); cp_avltree_destroy(freqlist); cp_avltree_destroy(wordlist); return 0; }
void cp_avltree_destroy_custom(cp_avltree *tree, cp_destructor_fn key_dtr, cp_destructor_fn val_dtr) { tree->mode |= COLLECTION_MODE_DEEP; tree->key_dtr = key_dtr; tree->value_dtr = val_dtr; cp_avltree_destroy(tree); }
void process_and_free_chromosome_avls(allocate_splice_elements_t *chromosome_avls, list_t* write_list_p, unsigned int write_size) { int c; allocate_buffers_t *allocate_batches = (allocate_buffers_t *)malloc(sizeof(allocate_buffers_t)); write_batch_t *exact_splice_write_p; write_batch_t *extend_splice_write_p; for(c = 0; c < CHROMOSOME_NUMBER; c++){ if(chromosome_avls[c].avl_splice->root != NULL) { allocate_batches->write_exact_sp = write_batch_new(write_size, SPLICE_EXACT_FLAG); allocate_batches->write_extend_sp = write_batch_new(write_size, SPLICE_EXTEND_FLAG); //allocate_batches->write_extend_sp = write_batch_new(1000, SPLICE_EXTEND_FLAG); allocate_batches = process_avlnode_in_order(chromosome_avls[c].avl_splice->root, c, write_list_p, write_size, allocate_batches); exact_splice_write_p = allocate_batches->write_exact_sp; extend_splice_write_p = allocate_batches->write_extend_sp; if(exact_splice_write_p != NULL) { list_item_t* item_p = NULL; if(exact_splice_write_p->size > 0) { item_p = list_item_new(0, WRITE_ITEM, exact_splice_write_p); list_insert_item(item_p, write_list_p); } else { write_batch_free(exact_splice_write_p); } } if(extend_splice_write_p != NULL) { list_item_t* item_p = NULL; if(extend_splice_write_p->size > 0) { item_p = list_item_new(0, WRITE_ITEM, extend_splice_write_p); list_insert_item(item_p, write_list_p); } else { write_batch_free(extend_splice_write_p); } } }//end IF chromosome splice not NULL cp_avltree_destroy(chromosome_avls[c].avl_splice); } free(allocate_batches); if (statistics_on) { statistics_set(TOTAL_ST, 3, total_splice, statistics_p); } list_decr_writers(write_list_p); }