X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Fsort.c;fp=src%2Fmath%2Fsort.c;h=9c2118985c59583bfe42d66ed5152452e3417d3b;hb=86189d786fc34c9da174b5018bec27640f7062e4;hp=77d9ea5cc190e42bed25d79a0f8c904e5874362b;hpb=7c96e98d5ee26ca08d9d31e84b08e79f1183663d;p=pspp diff --git a/src/math/sort.c b/src/math/sort.c index 77d9ea5cc1..9c2118985c 100644 --- a/src/math/sort.c +++ b/src/math/sort.c @@ -46,6 +46,10 @@ struct sort_writer struct merge *merge; struct pqueue *pqueue; + sort_distinct_combine_func *combine; + sort_distinct_destroy_func *destroy; + void *aux; + struct casewriter *run; casenumber run_id; struct ccase *run_end; @@ -54,7 +58,8 @@ struct sort_writer static struct casewriter_class sort_casewriter_class; static struct pqueue *pqueue_create (const struct subcase *, - const struct caseproto *); + const struct caseproto *, + sort_distinct_combine_func *, void *aux); static void pqueue_destroy (struct pqueue *); static bool pqueue_is_full (const struct pqueue *); static bool pqueue_is_empty (const struct pqueue *); @@ -66,14 +71,29 @@ static void output_record (struct sort_writer *); struct casewriter * sort_create_writer (const struct subcase *ordering, const struct caseproto *proto) +{ + return sort_distinct_create_writer (ordering, proto, NULL, NULL, NULL); +} + +struct casewriter * +sort_distinct_create_writer (const struct subcase *ordering, + const struct caseproto *proto, + sort_distinct_combine_func *combine, + sort_distinct_destroy_func *destroy, + void *aux) { struct sort_writer *sort; sort = xmalloc (sizeof *sort); sort->proto = caseproto_ref (proto); subcase_clone (&sort->ordering, ordering); - sort->merge = merge_create (ordering, proto); - sort->pqueue = pqueue_create (ordering, proto); + sort->merge = merge_create (ordering, proto, combine, aux); + sort->pqueue = pqueue_create (ordering, proto, combine, aux); + + sort->combine = combine; + sort->destroy = destroy; + sort->aux = aux; + sort->run = NULL; sort->run_id = 0; sort->run_end = NULL; @@ -102,6 +122,9 @@ sort_casewriter_destroy (struct casewriter *writer UNUSED, void *sort_) { struct sort_writer *sort = sort_; + if (sort->destroy != NULL) + sort->destroy (sort->aux); + subcase_destroy (&sort->ordering); merge_destroy (sort->merge); pqueue_destroy (sort->pqueue); @@ -200,6 +223,9 @@ struct pqueue struct bt bt; size_t record_cap; casenumber idx; + + sort_distinct_combine_func *combine; + void *aux; }; struct pqueue_record @@ -215,7 +241,8 @@ static int compare_pqueue_records (const struct bt_node *a, const void *ordering); static struct pqueue * -pqueue_create (const struct subcase *ordering, const struct caseproto *proto) +pqueue_create (const struct subcase *ordering, const struct caseproto *proto, + sort_distinct_combine_func *combine, void *aux) { struct pqueue *pq; @@ -229,6 +256,9 @@ pqueue_create (const struct subcase *ordering, const struct caseproto *proto) bt_init (&pq->bt, compare_pqueue_records, &pq->ordering); pq->idx = 0; + pq->combine = combine; + pq->aux = aux; + return pq; } @@ -272,6 +302,23 @@ pqueue_push (struct pqueue *pq, struct ccase *c, casenumber id) r->c = c; r->idx = pq->idx++; bt_insert (&pq->bt, &r->bt_node); + + if (pq->combine != NULL) + { + struct bt_node *q_ = bt_prev (&pq->bt, &r->bt_node); + if (q_ != NULL) + { + struct pqueue_record *q = bt_data (q_, struct pqueue_record, + bt_node); + if (q->id == r->id && subcase_equal (&pq->ordering, q->c, + &pq->ordering, r->c)) + { + bt_delete (&pq->bt, &r->bt_node); + free (r); + q->c = pq->combine (q->c, r->c, pq->aux); + } + } + } } static struct ccase *