cg_expansion* pick_expansion( cell_grammar *cg, chunk_neighborhood* nbh, block_index base, ptrdiff_t seed ) { size_t i; int success; cg_expansion *exp; list *options = create_list(); block root_block = *nb_block(nbh, base); for (i = 0; i < l_get_length(cg->expansions); ++i) { exp = (cg_expansion*) l_get_item(cg->expansions, i); success = check_expansion(exp, nbh, base, root_block); if (success) { l_append_element(options, (void*) exp); } } if (l_is_empty(options)) { cleanup_list(options); return NULL; } // Choose an option based on our seed: i = posmod(prng(seed + 819991), l_get_length(options)); exp = (cg_expansion*) l_get_item(options, i); cleanup_list(options); return exp; }
frequent_species pick_appropriate_frequent_species( list *sp_list, block substrate, ptrdiff_t seed ) { float weight; float total_weight = 0; float choice; float rare_smoothing; size_t i; frequent_species fqsp = 0; list *weights = create_list(); if (l_is_empty(sp_list)) { // Return an invalid species... frequent_species_set_species_type(&fqsp, SPT_NO_SPECIES); frequent_species_set_species(&fqsp, SP_INVALID); frequent_species_set_frequency(&fqsp, 0.0); return fqsp; } rare_smoothing = expdist(ptrf(seed + 31376), BIO_RARE_SPECIES_SMOOTHING_EXP); rare_smoothing *= BIO_RARE_SPECIES_SMOOTHING_ADJUST; for (i = 0; i < l_get_length(sp_list); ++i) { fqsp = (frequent_species) l_get_item(sp_list, i); weight = ( species_compatability(fqsp, substrate) * frequent_species_frequency(fqsp) ) + rare_smoothing; l_append_element(weights, f_as_p(weight)); total_weight += weight; } choice = ptrf(prng(seed + 866859)) * total_weight; for (i = 0; i < l_get_length(sp_list); ++i) { fqsp = (frequent_species) l_get_item(sp_list, i); choice -= p_as_f(l_get_item(weights, i)); if (choice < 0) { cleanup_list(weights); return fqsp; } } cleanup_list(weights); #ifdef DEBUG printf("Error: Ran out of appropriate species to pick from!\n"); exit(EXIT_FAILURE); #endif // Just arbitrarily return the first item (this shouldn't be reachable): return (frequent_species) l_get_item(sp_list, 0); }
void destroy_dictionary(dictionary *doomed) { size_t i; list *bucket; dictionary_entry *e; for (i = 0; i < l_get_length(doomed->ordered); ++i) { e = (dictionary_entry*) l_get_item(doomed->ordered, i); destroy_dictionary_entry(e); } cleanup_list(doomed->ordered); for (i = 0; i < doomed->table_size; ++i) { bucket = (list*) doomed->table[i]; if (bucket != NULL) { cleanup_list(bucket); } } free(doomed->table); free(doomed); }
// Application int main (void) { Tlist list; initialize_list(&list); insert_to_list_end(&list, 'a'); insert_to_list_end(&list, 'b'); insert_to_list_end(&list, 'c'); insert_to_list_end(&list, 'd'); print_list(list); cleanup_list(&list); fflush(stdin); getchar(); }
// Application int main (void) { Tlist list; initialize_list(&list); insert_to_list_end(&list, CreateComplex(1, 2)); // add 1+2i insert_to_list_end(&list, CreateComplex(2, 3)); // add 2+3i insert_to_list_end(&list, CreateComplex(3, 4)); // add 3+4i insert_to_list_end(&list, CreateComplex(4, 5)); // add 4+5i print_list(list); cleanup_list(&list); heapleak(); fflush(stdin); getchar(); //heapdump(); }
void cleanup_cell_grammar_expansion(cg_expansion *cge) { l_foreach(cge->children, &_iter_cleanup_cell_grammar_expansions); cleanup_list(cge->children); free(cge); }
void cleanup_cell_grammar(cell_grammar *cg) { l_foreach(cg->expansions, &_iter_cleanup_cell_grammar_expansions); cleanup_list(cg->expansions); free(cg); }
void cleanup_test_suite(test_suite *ts) { cleanup_list(ts->tests); cleanup_list(ts->results); free(ts); }
double_struct *merge_partitions(cog *c, long low, long high) { list *list = create_list(); int has_merge_work; gather_partitions(list, c); struct cog *right_replacement = NULL; struct cog *left_replacement = NULL; struct list *list_iter; struct iter_list *tail; struct stack_triple *stack = create_stack(); struct iter_list *head_list = malloc(sizeof(struct iter_list)); tail = head_list; list_iter = list; iterator merge_iter; cog *c1 = malloc(sizeof(struct cog)); while(list_has_next(list_iter)) { list_iter = get_cog_from_list(list_iter, c1); struct extracted_components *components = extract_partitions(c1, low, high); if(components->iter != NULL) { has_merge_work = 1; tail = iter_list_add(tail, components->iter); } if(components->rhs != NULL) { if(right_replacement == NULL) { right_replacement = components->rhs; } else { right_replacement = make_concat(right_replacement, components->rhs); } } if(components->lhs != NULL) { if(left_replacement == NULL) { left_replacement = components->lhs; } else { left_replacement = make_concat(left_replacement, components->lhs); } } free(components); } cleanup_list(list); free(c1); if(has_merge_work != 0) { double_struct *ret = create_double_struct(); merge_iter = iter_merge(head_list); record r = malloc(sizeof(struct record)); while(1) { int i; buffer out = buffer_alloc(MERGE_BLOCK_SIZE); record buf = out->data; for(i = 0; i < MERGE_BLOCK_SIZE && iter_has_next(merge_iter); i++) { iter_next(merge_iter, r); record_set(&buf[i], r->key, r->value); } if(i == 0) { break; } struct cog *buffer = make_sortedarray(0, i, out); fold_append(&stack, buffer, cog_min(buffer)); if(i < MERGE_BLOCK_SIZE) { break; } } cog *root = fold(&stack); if(root == NULL) { root = make_btree(left_replacement, right_replacement, high); } else { ret->iter = scan(root, low, high); if(left_replacement != NULL && cog_length(left_replacement) != 0) { root = make_btree(left_replacement, root, cog_min(root)); } if(right_replacement != NULL && cog_length(right_replacement) != 0) { root = make_btree(root, right_replacement, cog_min(right_replacement)); } } free(r); cleanup_stack(stack); iter_list_cleanup(head_list); iter_cleanup(merge_iter); ret->cog = root; return ret; } else { double_struct *ret; ret = amerge(list->cog, low, high); if(left_replacement != NULL) { ret->cog = make_concat(ret->cog, left_replacement); } if(right_replacement != NULL) { ret->cog = make_concat(ret->cog, right_replacement); } return ret; } }
/* * scamper_fds_cleanup * * tidy up the state allocated to maintain fd records. */ void scamper_fds_cleanup() { scamper_fd_t *fdn; /* clean up the lists */ cleanup_list(read_fds); read_fds = NULL; cleanup_list(write_fds); write_fds = NULL; cleanup_list(read_queue); read_queue = NULL; cleanup_list(write_queue); write_queue = NULL; /* reap anything on the reap list */ if(refcnt_0 != NULL) { while((fdn = (scamper_fd_t *)dlist_head_get(refcnt_0)) != NULL) { fd_close(fdn); fd_free(fdn); } dlist_free(refcnt_0); refcnt_0 = NULL; } /* clean up the tree */ if(fd_tree != NULL) { splaytree_free(fd_tree, NULL); fd_tree = NULL; } /* clean up the list */ if(fd_list != NULL) { dlist_free(fd_list); fd_list = NULL; } /* clean up the array */ if(fd_array != NULL) { free(fd_array); fd_array = NULL; } #ifdef HAVE_POLL if(poll_fds != NULL) { free(poll_fds); poll_fds = NULL; } #endif #ifdef HAVE_KQUEUE if(kq != -1) { close(kq); kq = -1; } if(kevlist != NULL) { free(kevlist); kevlist = NULL; } #endif #ifdef HAVE_EPOLL if(ep != -1) { close(ep); ep = -1; } if(ep_events != NULL) { free(ep_events); ep_events = NULL; } #endif return; }
int main(int argc, char* argv[]){ if(argc > 3) { printf("You may only enter a filename and an option.\necho sort tail tail-remove\nExiting...\n"); exit(-1); } else if (argc < 3) { printf("You must enter both a filename and an option.\necho sort tail tail-remove\nExiting...\n"); exit(-1); } // Open the supplied input file read only FILE *input_file = fopen(argv[1], "r"); if(input_file == NULL) { perror("Please enter a different filename"); exit(-1); } // Echo the file if(strcmp(argv[2], "echo") == 0) { char echo_buf[41]; while(read_line(input_file, echo_buf, 41)) { printf("%s\n", echo_buf); memset(echo_buf, '\0', 41); } } // Put the file in a list and visit-print the list else if(strcmp(argv[2], "tail") == 0) { list_t list; list_init(&list, list_item_compare_string, string_data_delete); char *tail_buf = malloc(41*sizeof(char)); if(tail_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } while(read_line(input_file, tail_buf, 41)) { list_insert_tail(&list, tail_buf); tail_buf = malloc(41*sizeof(char)); if(tail_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } } // Free the last buffer created when we looped last free(tail_buf); list_visit_items(&list, print_data_string); cleanup_list(&list); } // Insert the file into a sorted list ** Does not seem to sort correctly** else if(strcmp(argv[2], "sort") == 0) { list_t list; list_init(&list, list_item_compare_string, string_data_delete); char *sort_buf = malloc(41*sizeof(char)); if(sort_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } while(read_line(input_file, sort_buf, 41)) { list_insert_sorted(&list, sort_buf); sort_buf = malloc(41*sizeof(char)); if(sort_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } } // Free last buffer created as it is unused free(sort_buf); list_visit_items(&list, print_data_string); cleanup_list(&list); } // Insert the file into a list in order and then remove 3 head nodes until it is empty else if(strcmp(argv[2], "tail-remove") == 0) { printf("Running tail-remove\n"); list_t list; list_init(&list, list_item_compare_string, string_data_delete); char *rem_buf = malloc(41*sizeof(char)); if(rem_buf == NULL) { perror("malloc failed to create new buffer."); exit(-1); } while(read_line(input_file, rem_buf, 41)) { list_insert_tail(&list, rem_buf); rem_buf = malloc(41*sizeof(char)); } // Free last buffer created free(rem_buf); unsigned int three_sets = (list.length)/3; unsigned int i; list_visit_items(&list, print_data_string); for(i = 0; i < three_sets; i++) { list_remove_head(&list); list_remove_head(&list); list_remove_head(&list); printf("----------------------------------------\n"); list_visit_items(&list, print_data_string); } cleanup_list(&list); printf("----------------------------------------\n"); list_visit_items(&list, print_data_string); } fclose(input_file); return 0; };