sort: Add support for combining cases with identical sort criteria.
[pspp] / src / math / sort.c
index bfc27fd7d899f98465ae77fd1fc2640bccacb76f..61256ccdbc6d1f8dd19e284232778263a585ed41 100644 (file)
@@ -47,6 +47,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;
@@ -55,7 +59,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 *);
@@ -67,14 +72,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;
@@ -103,6 +123,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);
@@ -203,6 +226,9 @@ struct pqueue
     struct bt bt;
     size_t record_max;
     casenumber idx;
+
+    sort_distinct_combine_func *combine;
+    void *aux;
   };
 
 struct pqueue_record
@@ -218,7 +244,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;
 
@@ -232,6 +259,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;
 }
 
@@ -275,6 +305,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);
+              q->c = pq->combine (q->c, r->c, pq->aux);
+              free (r);
+            }
+        }
+    }
 }
 
 static struct ccase *