GtRankedListIter *gt_ranked_list_iter_new_from_last(GtRankedList *ranked_list) { /* return gt_rbtree_iter_new_from_first(ranked_list->root); */ GtRankedListIter *iter = gt_calloc((size_t) 1, sizeof (*iter)); iter->current_elem = gt_dlist_first(ranked_list->list); return iter; }
void gt_ranked_list_insert(GtRankedList *ranked_list, void *elem) { /* bool nodecreated = false; */ if (ranked_list->currentsize < ranked_list->maxsize) { if (ranked_list->currentsize == 0 || ranked_list->comparefunction(elem,ranked_list->worstelement, ranked_list->compareinfo) < 0) { ranked_list->worstelement = elem; } /* (void) gt_rbtree_search(ranked_list->root, elem, &nodecreated); */ gt_dlist_add(ranked_list->list, elem); /* if (nodecreated) { */ ranked_list->currentsize++; /* } */ } else { /* new element is not as bad as worst element, so insert it and and delete the worst element */ if (ranked_list->comparefunction(ranked_list->worstelement, elem, ranked_list->compareinfo) <= 0) { GtDlistelem *oldelem = gt_dlist_first(ranked_list->list); if (ranked_list->free_func != NULL) ranked_list->free_func(gt_dlistelem_get_data(oldelem)); gt_dlist_remove(ranked_list->list, oldelem); gt_dlist_add(ranked_list->list, elem); ranked_list->worstelement = gt_dlistelem_get_data(gt_dlist_first(ranked_list->list)); /* (void) gt_rbtree_search(ranked_list->root, elem, &nodecreated); if (nodecreated) { if (gt_rbtree_erase(ranked_list->root, ranked_list->worstelement) != 0) { fprintf(stderr,"%s: deletion failed\n",__func__); exit(GT_EXIT_PROGRAMMING_ERROR); } ranked_list->worstelement = gt_rbtree_minimum_key(ranked_list->root); } */ } else if (ranked_list->free_func != NULL) { ranked_list->free_func(elem); } } }
void* gt_ranked_list_last(const GtRankedList *ranked_list) { /* return gt_rbtree_minimum_key(ranked_list->root); */ GtDlistelem *elem = gt_dlist_first(ranked_list->list); if (elem != NULL) return gt_dlistelem_get_data(elem); else return NULL; }
GtDlistelem* gt_dlist_find(const GtDlist *dlist, void *new_data) { GtDlistelem *dlistelem; void *old_data; gt_assert(dlist); for (dlistelem = gt_dlist_first(dlist); dlistelem != NULL; dlistelem = gt_dlistelem_next(dlistelem)) { old_data = gt_dlistelem_get_data(dlistelem); if (dlist->cmp_func && !dlist->cmp_func(old_data, new_data)) return dlistelem; else if (old_data == new_data) return dlistelem; } return NULL; }
void gt_ranked_list_delete(GtRankedList *ranked_list) { if (ranked_list == NULL) return; /* gt_rbtree_delete(ranked_list->root); */ if (ranked_list->free_func != NULL) { GtDlistelem *dlistelem; for (dlistelem = gt_dlist_first(ranked_list->list); dlistelem != NULL; dlistelem = gt_dlistelem_next(dlistelem)) { void *data = gt_dlistelem_get_data(dlistelem); ranked_list->free_func(data); } } gt_dlist_delete(ranked_list->list); gt_free(ranked_list); }
static void feature_node_free(GtGenomeNode *gn) { GtFeatureNode *fn = gt_feature_node_cast(gn); gt_str_delete(fn->seqid); gt_str_delete(fn->source); gt_tag_value_map_delete(fn->attributes); if (fn->children) { GtDlistelem *dlistelem; for (dlistelem = gt_dlist_first(fn->children); dlistelem != NULL; dlistelem = gt_dlistelem_next(dlistelem)) { gt_genome_node_delete(gt_dlistelem_get_data(dlistelem)); } } gt_dlist_delete(fn->children); if (fn->observer && fn->observer->deleted) fn->observer->deleted(fn, fn->observer->data); if (fn->observer) gt_feature_node_observer_delete(fn->observer); }
static int targetbest_filter_stream_next(GtNodeStream *gs, GtGenomeNode **gn, GtError *err) { GtTargetbestFilterStream *tfs; GtGenomeNode *node; int had_err = 0; gt_error_check(err); tfs = targetbest_filter_stream_cast(gs); if (!tfs->in_stream_processed) { while (!(had_err = gt_node_stream_next(tfs->in_stream, &node, err)) && node) { if (gt_feature_node_try_cast(node) && gt_feature_node_get_attribute((GtFeatureNode*) node, "Target")) { filter_targetbest((GtFeatureNode*) node, tfs->trees, tfs->target_to_elem); } else gt_dlist_add(tfs->trees, node); } tfs->next = gt_dlist_first(tfs->trees); tfs->in_stream_processed = true; } if (!had_err) { gt_assert(tfs->in_stream_processed); if (tfs->next) { *gn = gt_dlistelem_get_data(tfs->next); tfs->next = gt_dlistelem_next(tfs->next); } else *gn = NULL; return 0; } return had_err; }
int gt_dlist_example(GT_UNUSED GtError *err) { GtDlistelem *dlistelem; GtDlist *dlist; void *data; int elem = 1984; gt_error_check(err); dlist = gt_dlist_new(NULL); gt_dlist_add(dlist, &elem); gt_dlist_add(dlist, &elem); gt_dlist_add(dlist, &elem); /* a typical iterator loop */ for (dlistelem = gt_dlist_first(dlist); dlistelem != NULL; dlistelem = gt_dlistelem_next(dlistelem)) { data = gt_dlistelem_get_data(dlistelem); /* do something with data */ } gt_dlist_delete(dlist); return 0; }
static void gt_aligned_segments_pile_delete_finishing_before( GtAlignedSegmentsPile *asp, unsigned long position) { GtDlistelem *dlistelem, *dlistelem_to_remove = NULL; for (dlistelem = gt_dlist_first(asp->set); dlistelem != NULL; dlistelem = gt_dlistelem_next(dlistelem)) { GtAlignedSegment *as; if (dlistelem_to_remove != NULL) { gt_dlist_remove(asp->set, dlistelem_to_remove); dlistelem_to_remove = NULL; } as = gt_dlistelem_get_data(dlistelem); if (gt_aligned_segment_refregion_endpos(as) < position) { if (asp->process_complete != NULL) { asp->process_complete(as, asp->process_complete_data); } if (asp->delete_processed_segments) gt_aligned_segment_delete(as); /* delay elem removal to avoid disrupting list traversal */ dlistelem_to_remove = dlistelem; } else { break; } } if (dlistelem_to_remove != NULL) { gt_dlist_remove(asp->set, dlistelem_to_remove); dlistelem_to_remove = NULL; } }
int gt_dlist_unit_test(GtError *err) { GtDlist *dlist; GtDlistelem *dlistelem; int i, j, size, *data, elem_a = 7, elem_b = 6, elems[MAX_SIZE], elems_backup[MAX_SIZE], had_err = 0; gt_error_check(err); /* boundary case: empty dlist */ dlist = gt_dlist_new(intcompare); ensure(had_err, !gt_dlist_size(dlist)); gt_dlist_delete(dlist); dlist = gt_dlist_new(NULL); ensure(had_err, !gt_dlist_size(dlist)); gt_dlist_delete(dlist); /* boundary case: dlist containing one element */ dlist = gt_dlist_new(intcompare); gt_dlist_add(dlist, &elem_a); ensure(had_err, gt_dlist_size(dlist) == 1); ensure(had_err, elem_a == *(int*) gt_dlistelem_get_data(gt_dlist_first(dlist))); gt_dlist_delete(dlist); dlist = gt_dlist_new(NULL); gt_dlist_add(dlist, &elem_a); ensure(had_err, gt_dlist_size(dlist) == 1); ensure(had_err, elem_a == *(int*) gt_dlistelem_get_data(gt_dlist_first(dlist))); gt_dlist_delete(dlist); /* boundary case: dlist containing two elements */ dlist = gt_dlist_new(intcompare); gt_dlist_add(dlist, &elem_a); gt_dlist_add(dlist, &elem_b); ensure(had_err, gt_dlist_size(dlist) == 2); ensure(had_err, elem_b == *(int*) gt_dlistelem_get_data(gt_dlist_first(dlist))); gt_dlist_delete(dlist); dlist = gt_dlist_new(NULL); gt_dlist_add(dlist, &elem_a); gt_dlist_add(dlist, &elem_b); ensure(had_err, gt_dlist_size(dlist) == 2); ensure(had_err, elem_a == *(int*) gt_dlistelem_get_data(gt_dlist_first(dlist))); gt_dlist_delete(dlist); for (i = 0; i < NUM_OF_TESTS && !had_err; i++) { /* construct the random elements for the list */ size = gt_rand_max(MAX_SIZE); for (j = 0; j < size; j++) { elems[j] = gt_rand_max(INT_MAX); elems_backup[j] = elems[j]; } /* sort the backup elements */ qsort(elems_backup, size, sizeof (int), intcompare); /* test with compare function */ dlist = gt_dlist_new(intcompare); ensure(had_err, !gt_dlist_size(dlist)); for (j = 0; j < size && !had_err; j++) { gt_dlist_add(dlist, elems + j); ensure(had_err, gt_dlist_size(dlist) == j+1); for (dlistelem = gt_dlist_first(dlist); dlistelem != NULL; dlistelem = gt_dlistelem_next(dlistelem)) { } } j = 0; for (dlistelem = gt_dlist_first(dlist); dlistelem != NULL; dlistelem = gt_dlistelem_next(dlistelem)) { data = gt_dlistelem_get_data(dlistelem); ensure(had_err, *data == elems_backup[j]); j++; } /* test gt_dlist_find() */ for (j = 0; j < size; j++) { dlistelem = gt_dlist_find(dlist, elems_backup + j); ensure(had_err, dlistelem); ensure(had_err, *(int*) gt_dlistelem_get_data(dlistelem) == elems_backup[j]); } /* remove first element */ if (gt_dlist_size(dlist)) { gt_dlist_remove(dlist, gt_dlist_first(dlist)); if (gt_dlist_size(dlist)) { data = gt_dlistelem_get_data(gt_dlist_first(dlist)); ensure(had_err, *data == elems_backup[1]); } } /* remove last element */ if (gt_dlist_size(dlist)) { gt_dlist_remove(dlist, gt_dlist_last(dlist)); if (gt_dlist_size(dlist)) { data = gt_dlistelem_get_data(gt_dlist_last(dlist)); ensure(had_err, *data == elems_backup[size - 2]); } } /* XXX: fix this */ #if 0 /* remove middle element */ if (gt_dlist_size(dlist) >= 2) { dlistelem = gt_dlist_first(dlist); for (j = 1; j < gt_dlist_size(dlist) / 2; j++) dlistelem = gt_dlistelem_next(dlistelem); gt_dlist_remove(dlist, dlistelem); dlistelem = gt_dlist_first(dlist); for (j = 1; j < gt_dlist_size(dlist) / 2 + 1; j++) dlistelem = gt_dlistelem_next(dlistelem); data = gt_dlistelem_get_data(gt_dlist_last(dlist)); ensure(had_err, *data == elems_backup[size / 2 + 1]); } #endif gt_dlist_delete(dlist); /* test without compare function */ dlist = gt_dlist_new(NULL); ensure(had_err, !gt_dlist_size(dlist)); for (j = 0; j < size && !had_err; j++) { gt_dlist_add(dlist, elems + j); ensure(had_err, gt_dlist_size(dlist) == j+1); } j = 0; for (dlistelem = gt_dlist_first(dlist); dlistelem != NULL; dlistelem = gt_dlistelem_next(dlistelem)) { data = gt_dlistelem_get_data(dlistelem); ensure(had_err, *data == elems[j]); j++; } /* remove first element */ if (gt_dlist_size(dlist)) { gt_dlist_remove(dlist, gt_dlist_first(dlist)); if (gt_dlist_size(dlist)) { data = gt_dlistelem_get_data(gt_dlist_first(dlist)); ensure(had_err, *data == elems[1]); } } /* remove last element */ if (gt_dlist_size(dlist)) { gt_dlist_remove(dlist, gt_dlist_last(dlist)); if (gt_dlist_size(dlist)) { data = gt_dlistelem_get_data(gt_dlist_last(dlist)); ensure(had_err, *data == elems[size - 2]); } } gt_dlist_delete(dlist); } return had_err; }