Skip to content

Commit 5d42113

Browse files
committed
[stdlib] Expand comments for _sort3
1 parent 7303125 commit 5d42113

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

stdlib/public/core/Sort.swift.gyb

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ func _insertionSort<C>(
7272
}
7373
}
7474

75-
/// Sorts the elements at indices `a`, `b`, and `c`. Stable.
75+
/// Sorts the elements at `elements[a]`, `elements[b]`, and `elements[c]`.
76+
/// Stable.
77+
///
78+
/// The indices passed as `a`, `b`, and `c` do not need to be consecutive, but
79+
/// must be in strict increasing order.
7680
///
7781
/// - Precondition: `a < b && b < c`
7882
/// - Postcondition: `elements[a] <= elements[b] && elements[b] <= elements[c]`
@@ -86,8 +90,25 @@ func _sort3<C>(
8690
C : MutableCollection & RandomAccessCollection
8791
${"" if p else ", C.Iterator.Element : Comparable"}
8892
{
89-
// Possible starting states for elements at a, b, and c:
90-
// 123, 132, 213, 231, 312, 321, 112, 121, 211, 122, 212, 221, 111
93+
// There are thirteen possible permutations for the original ordering of
94+
// the elements at indices `a`, `b`, and `c`. The comments in the code below
95+
// show the relative ordering of the three elements using a three-digit
96+
// number as shorthand for the position and comparative relationship of
97+
// each element. For example, "312" indicates that the element at `a` is the
98+
// largest of the three, the element at `b` is the smallest, and the element
99+
// at `c` is the median. This hypothetical input array has a 312 ordering for
100+
// `a`, `b`, and `c`:
101+
//
102+
// [ 7, 4, 3, 9, 2, 0, 3, 7, 6, 5 ]
103+
// ^ ^ ^
104+
// a b c
105+
//
106+
// - If each of the three elements is distinct, they could be ordered as any
107+
// of the permutations of 1, 2, and 3: 123, 132, 213, 231, 312, or 321.
108+
// - If two elements are equivalent and one is distinct, they could be
109+
// ordered as any permutation of 1, 1, and 2 or 1, 2, and 2: 112, 121, 211,
110+
// 122, 212, or 221.
111+
// - If all three elements are equivalent, they are already in order: 111.
91112

92113
switch (${cmp("elements[b]", "elements[a]", p)},
93114
${cmp("elements[c]", "elements[b]", p)}) {
@@ -96,24 +117,29 @@ func _sort3<C>(
96117
break
97118

98119
case (true, true):
99-
// 1 swap: 321->123
120+
// 1 swap: 321
121+
// swap(a, c): 312->123
100122
swap(&elements[a], &elements[c])
101123

102124
case (true, false):
103125
// 1 swap: 213, 212 --- 2 swaps: 312, 211
104-
// 213->123, 212->122 --- 312->132, 211->121
126+
// swap(a, b): 213->123, 212->122, 312->132, 211->121
105127
swap(&elements[a], &elements[b])
128+
106129
if ${cmp("elements[c]", "elements[b]", p)} {
107-
// 132->123, 121->112
130+
// 132 (started as 312), 121 (started as 211)
131+
// swap(b, c): 132->123, 121->112
108132
swap(&elements[b], &elements[c])
109133
}
110134

111135
case (false, true):
112136
// 1 swap: 132, 121 --- 2 swaps: 231, 221
113-
// 132->123, 121->112 --- 231->213, 221->212
137+
// swap(b, c): 132->123, 121->112, 231->213, 221->212
114138
swap(&elements[b], &elements[c])
139+
115140
if ${cmp("elements[b]", "elements[a]", p)} {
116-
// 213->123, 212->122
141+
// 213 (started as 231), 212 (started as 221)
142+
// swap(a, b): 213->123, 212->122
117143
swap(&elements[a], &elements[b])
118144
}
119145
}

0 commit comments

Comments
 (0)