Skip to content

Commit 94b6043

Browse files
committed
s2/coll: sortDedup v2: natural runs, merge direction alternates per pass, Sorted holds only the list
Coll.Sorted drops its cached length: it could disagree with the list. sortDedup splits the input into natural runs in one self-recursive step (Coll.Dir Up | Down): a non-decreasing stretch is consed into a strictly descending run, an equal key replaces the run head (the later binding wins), a strictly decreasing stretch is reversed once when it ends. Passes alternate: mergeAllD takes descending runs, latest first, mergeAllA ascending runs, earliest first, so no pass reverses every run. Only an odd last run and the final result are reversed. tests/s2/sorted: oracle against a BMap built from the same input (scrambled with duplicates, ascending with adjacent duplicates, descending), 20000 elements each. 100k elements, C -O1 instr:u: scrambled 102.4M -> 53.4M, descending 91.7M -> 9.9M, ascending 87.1M -> 7.7M; peak 130 MB -> 55/17/14 MB.
1 parent e9ddc9f commit 94b6043

3 files changed

Lines changed: 120 additions & 22 deletions

File tree

‎s2/coll.srs‎

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
101101
enum Coll.KV [V: Type]:
102102
case Coll.KV(k: I64, v: V)
103103

104104
enum 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

107107
def 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
115145
def 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)))

‎tests/s2/sorted.out‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
1
55
100000
66
0
7+
1 0
8+
1 0
9+
1 0

‎tests/s2/sorted.srs‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,69 @@ def T.scr {r: Region} (i: I64, n: I64, acc: Coll.List[Coll.KV[I64]]): Coll.List[
2222
def T.none(): Coll.List[Coll.KV[I64]] = Coll.Nil
2323

2424
def T.val(s: Coll.Sorted[I64]): Coll.List[Coll.KV[I64]] = s match
25-
case Coll.Sorted(_, xs) => xs
25+
case Coll.Sorted(xs) => xs
2626

27-
def T.cnt(s: Coll.Sorted[I64]): I64 = s match
28-
case Coll.Sorted(n, _) => n
27+
// the input lists of the oracle, in list order: keys (i * 7919) % 1009 (many duplicates, short runs both ways),
28+
// keys i / 2 ascending (one run, adjacent duplicates), keys n - 1 down to 0 (one descending run)
29+
def T.genMod {r: Region} (i: I64, n: I64, acc: Coll.List[Coll.KV[I64]]): Coll.List[Coll.KV[I64]] =
30+
if i == n then acc else T.genMod(i + 1, n, Coll.Cons(Coll.KV((i * 7919) % 1009, i), acc))
31+
32+
def T.genUp {r: Region} (i: I64, acc: Coll.List[Coll.KV[I64]]): Coll.List[Coll.KV[I64]] =
33+
if i == 0 then acc else T.genUp(i - 1, Coll.Cons(Coll.KV((i - 1) / 2, i - 1), acc))
34+
35+
def T.genDown {r: Region} (i: I64, n: I64, acc: Coll.List[Coll.KV[I64]]): Coll.List[Coll.KV[I64]] =
36+
if i == n then acc else T.genDown(i + 1, n, Coll.Cons(Coll.KV(i, i), acc))
37+
38+
// the second path: the same list put into a BMap in list order (a later binding replaces)
39+
def T.bm {r: Region} (xs: Coll.List[Coll.KV[I64]], m: Coll.BMap[I64]): Coll.BMap[I64] = xs match
40+
case Coll.Nil => m
41+
case Coll.Cons(e, t) => T.bmKV(e, t, m)
42+
43+
def T.bmKV {r: Region} (e: Coll.KV[I64], t: Coll.List[Coll.KV[I64]], m: Coll.BMap[I64]): Coll.BMap[I64] = e match
44+
case Coll.KV(k, v) => T.bm(t, Coll.bInsert(m, k, v))
45+
46+
def T.is (o: Coll.Option[I64], v: I64): Bool = o match
47+
case Coll.None => false
48+
case Coll.Some(w) => w == v
49+
50+
// keys strictly ascend above prev and every value is the one m binds
51+
def T.agrees {r: Region} (xs: Coll.List[Coll.KV[I64]], m: Coll.BMap[I64], prev: I64): Bool = xs match
52+
case Coll.Nil => true
53+
case Coll.Cons(e, t) => T.agreeKV(e, t, m, prev)
54+
55+
def T.agreeKV {r: Region} (e: Coll.KV[I64], t: Coll.List[Coll.KV[I64]], m: Coll.BMap[I64], prev: I64): Bool = e match
56+
case Coll.KV(k, v) => if k > prev && T.is(Coll.bLookup(m, k), v) then T.agrees(t, m, k) else false
57+
58+
// "1 0" when sortDedup agrees with the map and has its size
59+
def T.oracle {r: Region} (xs: Coll.List[Coll.KV[I64]]): I64 =
60+
val ys = T.val(Coll.sortDedup(xs))
61+
val m = T.bm(xs, Coll.bEmpty[I64]())
62+
rt_print_i64(if T.agrees(ys, m, -1) then 1 else 0)
63+
rt_print(" ")
64+
rt_print_i64(Coll.length(ys) - Coll.bSize(m))
65+
rt_print("\n")
66+
0
2967

3068
def main(): I64 =
3169
handle r = Arena()
3270
val l = Coll.Cons(Coll.KV(5, 1), Coll.Cons(Coll.KV(3, 2), Coll.Cons(Coll.KV(9, 3), Coll.Cons(Coll.KV(5, 4), Coll.Cons(Coll.KV(1, 5), Coll.Cons(Coll.KV(3, 6), Coll.Cons(Coll.KV(7, 7), Coll.Cons(Coll.KV(5, 8), Coll.Cons(Coll.KV(0, 9), Coll.Cons(Coll.KV(8, 10), Coll.Nil))))))))))
3371
val s = Coll.sortDedup(l)
3472
T.show(T.val(s))
3573
rt_print("\n")
36-
rt_print_i64(T.cnt(s))
74+
rt_print_i64(Coll.length(T.val(s)))
3775
rt_print("\n")
3876
val w = Coll.sortDedup(Coll.Cons(Coll.KV(4, 100), Coll.Cons(Coll.KV(4, 200), Coll.Nil)))
3977
T.show(T.val(w))
4078
rt_print("\n")
4179
val big = Coll.sortDedup(T.scr(0, 100000, Coll.Nil))
4280
rt_print_i64(T.asc(T.val(big), -1))
4381
rt_print("\n")
44-
rt_print_i64(T.cnt(big) + Coll.length(T.val(big)) - 100000)
82+
rt_print_i64(Coll.length(T.val(big)))
4583
rt_print("\n")
4684
val e = Coll.sortDedup(T.none())
47-
rt_print_i64(T.cnt(e))
85+
rt_print_i64(Coll.length(T.val(e)))
4886
rt_print("\n")
87+
T.oracle(T.genMod(0, 20000, Coll.Nil))
88+
T.oracle(T.genUp(20000, Coll.Nil))
89+
T.oracle(T.genDown(0, 20000, Coll.Nil))
4990
0

0 commit comments

Comments
 (0)