1import core/cmp
2
3-- Restore the max-heap property in values[0..<count] below root.
4-- A parent has a child exactly when at < count / 2, so the child-index
5-- arithmetic cannot overflow even for a maximum-length view.
6sift_down: (item: type is cmp.ordered, values: []mut item,
7 root: usize, count: usize) -> none =
8 mut at := root
9 while at < count / 2 do
10 mut child: usize = at * 2 + 1
11 if child + 1 < count and item.less(values[child], values[child + 1]) then
12 inc child
13 end if
14 if not item.less(values[at], values[child]) then
15 return
16 end if
17 saved: item = values[at]
18 values[at] = values[child]
19 values[child] = saved
20 at = child
21 end while
22end sift_down
23
24--- Sort an initialized mutable slice in place using heapsort: worst-case
25--- O(n log n) comparisons, constant auxiliary storage, bounded call depth and
26--- no allocation. The `cmp.ordered` evidence must be a strict weak ordering;
27--- equal items need not keep their order.
28public sort: (item: type is cmp.ordered, values: []mut item) -> none =
29 count: usize = lenof values
30 mut start: usize = count / 2
31 while start > 0 do
32 dec start
33 sift_down(values, start, count)
34 end while
35 mut remaining := count
36 zero: usize = 0
37 while remaining > 1 do
38 dec remaining
39 saved: item = values[zero]
40 values[zero] = values[remaining]
41 values[remaining] = saved
42 sift_down(values, zero, remaining)
43 end while
44end sort
45
46--- Sort in place using selection sort: exactly n(n-1)/2 comparisons, at most
47--- n-1 swaps, constant storage, bounded call depth and no allocation. Useful
48--- for tiny or swap-expensive views; it is not stable.
49public sort_selection: (item: type is cmp.ordered, values: []mut item) -> none =
50 mut at: usize = 0
51 while at < lenof values do
52 mut least := at
53 mut next := at + 1
54 while next < lenof values do
55 if item.less(values[next], values[least]) then
56 least = next
57 end if
58 inc next
59 end while
60 if least <> at then
61 saved: item = values[at]
62 values[at] = values[least]
63 values[least] = saved
64 end if
65 inc at
66 end while
67end sort_selection
68
69-- Restore the max-heap property below root as `sift_down` does, ordering
70-- by `less` with `by` passed to every comparison.
71sift_down_by: (item: type, context: type, values: []mut item, root: usize,
72 count: usize, by: context,
73 less: (by: context, left: item, right: item) -> (yes: bool))
74 -> none =
75 mut at := root
76 while at < count / 2 do
77 mut child: usize = at * 2 + 1
78 if child + 1 < count and less(by, values[child], values[child + 1]) then
79 inc child
80 end if
81 if not less(by, values[at], values[child]) then
82 return
83 end if
84 saved: item = values[at]
85 values[at] = values[child]
86 values[child] = saved
87 at = child
88 end while
89end sift_down_by
90
91--- Sort an initialized mutable slice in place by a comparison that is
92--- given a context: `less(by, left, right)` says whether `left` goes before
93--- `right`, and `by` is passed to every call unchanged, so items that refer
94--- into shared storage, such as offsets into one pool of names, are
95--- ordered by what they refer to. Heapsort, as `sort` is: worst-case
96--- O(n log n) comparisons, constant auxiliary storage, bounded call depth
97--- and no allocation. `less` must be a strict weak ordering for as long as
98--- the sort runs; equal items need not keep their order.
99public sort_by: (item: type, context: type, values: []mut item, by: context,
100 less: (by: context, left: item, right: item) -> (yes: bool))
101 -> none =
102 count: usize = lenof values
103 mut start: usize = count / 2
104 while start > 0 do
105 dec start
106 sift_down_by(values, start, count, by, less)
107 end while
108 mut remaining := count
109 zero: usize = 0
110 while remaining > 1 do
111 dec remaining
112 saved: item = values[zero]
113 values[zero] = values[remaining]
114 values[remaining] = saved
115 sift_down_by(values, zero, remaining, by, less)
116 end while
117end sort_by