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;
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 *);
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;
{
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);
struct bt bt;
size_t record_cap;
casenumber idx;
+
+ sort_distinct_combine_func *combine;
+ void *aux;
};
struct pqueue_record
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;
bt_init (&pq->bt, compare_pqueue_records, &pq->ordering);
pq->idx = 0;
+ pq->combine = combine;
+ pq->aux = aux;
+
return pq;
}
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 *