@@ -97,21 +97,51 @@ def Coll.node [V: Type] (m: Coll.IMap[V], k: I64): Coll.IMap[V] = m match
9797 case Coll.Bin(_, k2, _, l, r2) => if k < k2 then Coll.node(l, k) else if k > k2 then Coll.node(r2, k) else m
9898
9999// b4 batch input: key/value pairs, strictly ascending by key. Only Coll.sortDedup builds a Sorted (no sealing in s1 yet:
100- // the order is a producer invariant, not a type one; n is the length of xs )
100+ // the order is a producer invariant, not a type one)
101101enum Coll.KV [V: Type]:
102102 case Coll.KV(k: I64, v: V)
103103
104104enum Coll.Sorted [V: Type]:
105- case Coll.Sorted(n: I64, xs: Coll.List[Coll.KV[V]])
105+ case Coll.Sorted(xs: Coll.List[Coll.KV[V]])
106106
107107def Coll.kvKey [V: Type] (e: Coll.KV[V]): I64 = e match
108108 case Coll.KV(k, _) => k
109109
110- def Coll.singles [V: Type] {r: Region} (xs: Coll.List[Coll.KV[V]], acc: Coll.List[Coll.List[Coll.KV[V]]]): Coll.List[Coll.List[Coll.KV[V]]] = xs match
111- case Coll.Nil => acc
112- case Coll.Cons(h, t) => Coll.singles(t, Coll.Cons(Coll.Cons(h, Coll.Nil), acc))
110+ // the input order of a natural run: Up = non-decreasing (consed, the run is strictly descending), Down = strictly
111+ // decreasing (consed, the run is ascending until it is closed)
112+ enum Coll.Dir:
113+ case Coll.Up
114+ case Coll.Down
115+
116+ // the run Cons(h, rt) as a strictly descending run
117+ def Coll.closeRun [V: Type] {r: Region} (d: Coll.Dir, h: Coll.KV[V], rt: Coll.List[Coll.KV[V]]): Coll.List[Coll.KV[V]] = d match
118+ case Coll.Up => Coll.Cons(h, rt)
119+ case Coll.Down => Coll.revOnto(rt, Coll.Cons(h, Coll.Nil))
113120
114- // merge of two ascending runs onto acc, reversed; equal keys: the element of b (the later one) wins
121+ // the natural runs of xs, each strictly descending, the latest run first. In an Up run an equal key replaces the run
122+ // head (the later binding wins); a one-element Up run turns Down when the next key is smaller.
123+ def Coll.runs [V: Type] {r: Region} (xs: Coll.List[Coll.KV[V]]): Coll.List[Coll.List[Coll.KV[V]]] = xs match
124+ case Coll.Nil => Coll.Nil
125+ case Coll.Cons(x, t) => Coll.runStep(t, Coll.Up, x, Coll.Nil, Coll.Nil)
126+
127+ // h is the last element taken into the current run Cons(h, rt)
128+ def Coll.runStep [V: Type] {r: Region} (xs: Coll.List[Coll.KV[V]], d: Coll.Dir, h: Coll.KV[V], rt: Coll.List[Coll.KV[V]], acc: Coll.List[Coll.List[Coll.KV[V]]]): Coll.List[Coll.List[Coll.KV[V]]] = xs match
129+ case Coll.Nil => Coll.Cons(Coll.closeRun(d, h, rt), acc)
130+ case Coll.Cons(y, t) =>
131+ val ky = Coll.kvKey(y)
132+ val kh = Coll.kvKey(h)
133+ d match
134+ case Coll.Up =>
135+ if ky > kh then Coll.runStep(t, d, y, Coll.Cons(h, rt), acc)
136+ else if ky == kh then Coll.runStep(t, d, y, rt, acc)
137+ else rt match
138+ case Coll.Nil => Coll.runStep(t, Coll.Down, y, Coll.Cons(h, rt), acc)
139+ case Coll.Cons(_, _) => Coll.runStep(t, Coll.Up, y, Coll.Nil, Coll.Cons(Coll.Cons(h, rt), acc))
140+ case Coll.Down =>
141+ if ky < kh then Coll.runStep(t, d, y, Coll.Cons(h, rt), acc)
142+ else Coll.runStep(t, Coll.Up, y, Coll.Nil, Coll.Cons(Coll.closeRun(d, h, rt), acc))
143+
144+ // merge of two ascending runs onto acc, giving a descending run; equal keys: the element of b (the later run) wins
115145def Coll.mergeRev [V: Type] {r: Region} (a: Coll.List[Coll.KV[V]], b: Coll.List[Coll.KV[V]], acc: Coll.List[Coll.KV[V]]): Coll.List[Coll.KV[V]] = a match
116146 case Coll.Nil => Coll.revOnto(b, acc)
117147 case Coll.Cons(x, ta) => b match
@@ -123,20 +153,44 @@ def Coll.mergeRev [V: Type] {r: Region} (a: Coll.List[Coll.KV[V]], b: Coll.List[
123153 else if kx == ky then Coll.mergeRev(ta, tb, Coll.Cons(y, acc))
124154 else Coll.mergeRev(a, tb, Coll.Cons(y, acc))
125155
126- // adjacent pairs of runs merged, result reversed
127- def Coll.mergePairs [V: Type] {r: Region} (runs: Coll.List[Coll.List[Coll.KV[V]]], acc: Coll.List[Coll.List[Coll.KV[V]]]): Coll.List[Coll.List[Coll.KV[V]]] = runs match
156+ // merge of two descending runs onto acc, giving an ascending run; equal keys: the element of a (the later run) wins
157+ def Coll.mergeRevD [V: Type] {r: Region} (a: Coll.List[Coll.KV[V]], b: Coll.List[Coll.KV[V]], acc: Coll.List[Coll.KV[V]]): Coll.List[Coll.KV[V]] = a match
158+ case Coll.Nil => Coll.revOnto(b, acc)
159+ case Coll.Cons(x, ta) => b match
160+ case Coll.Nil => Coll.revOnto(a, acc)
161+ case Coll.Cons(y, tb) =>
162+ val kx = Coll.kvKey(x)
163+ val ky = Coll.kvKey(y)
164+ if kx > ky then Coll.mergeRevD(ta, b, Coll.Cons(x, acc))
165+ else if kx == ky then Coll.mergeRevD(ta, tb, Coll.Cons(x, acc))
166+ else Coll.mergeRevD(a, tb, Coll.Cons(y, acc))
167+
168+ // one pass over ascending runs, earliest first: descending runs, latest first (a last odd run is reversed)
169+ def Coll.passA [V: Type] {r: Region} (runs: Coll.List[Coll.List[Coll.KV[V]]], acc: Coll.List[Coll.List[Coll.KV[V]]]): Coll.List[Coll.List[Coll.KV[V]]] = runs match
170+ case Coll.Nil => acc
171+ case Coll.Cons(a, t) => t match
172+ case Coll.Nil => Coll.Cons(Coll.rev(a), acc)
173+ case Coll.Cons(b, t2) => Coll.passA(t2, Coll.Cons(Coll.mergeRev(a, b, Coll.Nil), acc))
174+
175+ // one pass over descending runs, latest first: ascending runs, earliest first (a last odd run is reversed)
176+ def Coll.passD [V: Type] {r: Region} (runs: Coll.List[Coll.List[Coll.KV[V]]], acc: Coll.List[Coll.List[Coll.KV[V]]]): Coll.List[Coll.List[Coll.KV[V]]] = runs match
128177 case Coll.Nil => acc
129178 case Coll.Cons(a, t) => t match
130- case Coll.Nil => Coll.Cons(a , acc)
131- case Coll.Cons(b, t2) => Coll.mergePairs (t2, Coll.Cons(Coll.rev(Coll.mergeRev( a, b, Coll.Nil) ), acc))
179+ case Coll.Nil => Coll.Cons(Coll.rev(a) , acc)
180+ case Coll.Cons(b, t2) => Coll.passD (t2, Coll.Cons(Coll.mergeRevD( a, b, Coll.Nil), acc))
132181
133- def Coll.mergeAll [V: Type] {r: Region} (runs: Coll.List[Coll.List[Coll.KV[V]]]): Coll.List[Coll.KV[V]] = runs match
182+ // the run order is in the function: mergeAllA gets ascending runs earliest first, mergeAllD descending runs latest first
183+ def Coll.mergeAllA [V: Type] {r: Region} (runs: Coll.List[Coll.List[Coll.KV[V]]]): Coll.List[Coll.KV[V]] = runs match
134184 case Coll.Nil => Coll.Nil
135185 case Coll.Cons(a, t) => t match
136186 case Coll.Nil => a
137- case Coll.Cons(_, _) => Coll.mergeAll(Coll.rev(Coll.mergePairs(runs, Coll.Nil)))
187+ case Coll.Cons(_, _) => Coll.mergeAllD(Coll.passA(runs, Coll.Nil))
188+
189+ def Coll.mergeAllD [V: Type] {r: Region} (runs: Coll.List[Coll.List[Coll.KV[V]]]): Coll.List[Coll.KV[V]] = runs match
190+ case Coll.Nil => Coll.Nil
191+ case Coll.Cons(a, t) => t match
192+ case Coll.Nil => Coll.rev(a)
193+ case Coll.Cons(_, _) => Coll.mergeAllA(Coll.passD(runs, Coll.Nil))
138194
139- // bottom-up merge sort, O(n log n); a duplicate key keeps the last binding of the input
140- def Coll.sortDedup [V: Type] {r: Region} (xs: Coll.List[Coll.KV[V]]): Coll.Sorted[V] =
141- val ys = Coll.mergeAll(Coll.rev(Coll.singles(xs, Coll.Nil)))
142- Coll.Sorted(Coll.length(ys), ys)
195+ // natural bottom-up merge sort, O(n log r) for r runs; a duplicate key keeps the last binding of the input
196+ def Coll.sortDedup [V: Type] {r: Region} (xs: Coll.List[Coll.KV[V]]): Coll.Sorted[V] = Coll.Sorted(Coll.mergeAllD(Coll.runs(xs)))
0 commit comments