gofront 1.8.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/package.json +6 -5
- package/src/backend/js/expressions.js +1157 -408
- package/src/backend/js/index.js +327 -153
- package/src/backend/js/natives.js +383 -0
- package/src/backend/js/runtime.js +559 -2552
- package/src/backend/js/statements.js +670 -158
- package/src/backend/js/stdlib/index.js +45 -93
- package/src/backend/js/templ.js +32 -45
- package/src/backend/js/type-info.js +298 -19
- package/src/backend/shared/fixed-float.js +20 -0
- package/src/backend/shared/fmt-types.js +91 -170
- package/src/backend/shared/host-unicode.js +36 -0
- package/src/backend/wasm/boundary.js +490 -965
- package/src/backend/wasm/emit-builtins.js +884 -256
- package/src/backend/wasm/emit-exprs.js +185 -77
- package/src/backend/wasm/emit-maps.js +17 -367
- package/src/backend/wasm/emit-shared.js +4 -1
- package/src/backend/wasm/emit-stmts.js +524 -240
- package/src/backend/wasm/emit.js +176 -38
- package/src/backend/wasm/globals.js +48 -8
- package/src/backend/wasm/glue.js +2 -4
- package/src/backend/wasm/index.js +44 -530
- package/src/backend/wasm/method-expr.js +12 -1
- package/src/backend/wasm/module-emitter.js +299 -770
- package/src/backend/wasm/monomorph.js +139 -8
- package/src/backend/wasm/natives.js +600 -0
- package/src/backend/wasm/prescan.js +5 -153
- package/src/backend/wasm/prune-funcs.js +89 -0
- package/src/backend/wasm/stdlib/index.js +2 -32
- package/src/backend/wasm/types.js +97 -20
- package/src/backend/wasm/variadic.js +9 -4
- package/src/cli/cli-core.js +5 -1
- package/src/cli/test-runner.js +115 -342
- package/src/compiler.js +113 -130
- package/src/lexer/lexer.js +106 -11
- package/src/lexer/templ-lexer.js +61 -18
- package/src/lexer/tokens.js +1 -0
- package/src/link/stdlib.js +355 -132
- package/src/link/type-names.js +204 -0
- package/src/link/wasm.js +30 -6
- package/src/lower/bounds.js +153 -0
- package/src/lower/captures.js +92 -33
- package/src/lower/embedding.js +62 -5
- package/src/lower/errors-as.js +196 -0
- package/src/lower/fmt-formatters.js +929 -0
- package/src/lower/functions.js +225 -7
- package/src/lower/gen-types.js +293 -0
- package/src/lower/index.js +14 -2
- package/src/lower/json-codecs.js +334 -245
- package/src/lower/ownership.js +25 -7
- package/src/lower/reachability.js +69 -51
- package/src/lower/walk.js +12 -0
- package/src/parser/declarations.js +30 -5
- package/src/parser/expressions.js +111 -57
- package/src/parser/statements.js +108 -0
- package/src/parser/templ.js +44 -5
- package/src/parser/types.js +12 -0
- package/src/resolver.js +1 -2
- package/src/typechecker/assignability.js +214 -19
- package/src/typechecker/const-fold.js +70 -27
- package/src/typechecker/constants.js +156 -8
- package/src/typechecker/expressions.js +490 -108
- package/src/typechecker/index.js +346 -42
- package/src/typechecker/init-order.js +136 -40
- package/src/typechecker/operand-text.js +41 -0
- package/src/typechecker/resolve.js +490 -65
- package/src/typechecker/statements.js +288 -27
- package/src/typechecker/stdlib/excluded.js +9 -5
- package/src/typechecker/stdlib/index.js +25 -9
- package/src/typechecker/termination.js +10 -1
- package/src/typechecker/types.js +287 -575
- package/stdlib/bytes/buffer.go +479 -0
- package/stdlib/bytes/bytes.go +1010 -0
- package/stdlib/bytes/reader.go +161 -0
- package/stdlib/context/context.go +115 -0
- package/stdlib/crypto/rand/rand.go +34 -0
- package/stdlib/crypto/sha256/sha256.go +249 -0
- package/stdlib/encoding/base64/base64.go +331 -0
- package/stdlib/encoding/binary/binary.go +332 -0
- package/stdlib/encoding/hex/hex.go +140 -0
- package/stdlib/encoding/json/json.go +62 -0
- package/stdlib/errors/errors.go +174 -0
- package/stdlib/fmt/errors.go +110 -0
- package/stdlib/fmt/format.go +732 -0
- package/stdlib/fmt/print.go +1232 -0
- package/stdlib/gom/gom.go +293 -0
- package/stdlib/html/html.go +251 -0
- package/stdlib/internal/fmtrt/fmtrt.go +163 -0
- package/stdlib/internal/jsonrt/dec.go +171 -38
- package/stdlib/internal/jsonrt/enc.go +186 -36
- package/stdlib/internal/jsonrt/scanner.go +17 -8
- package/stdlib/io/io.go +157 -1
- package/stdlib/maps/maps.go +131 -0
- package/stdlib/math/bits/bits.go +507 -0
- package/stdlib/math/extra.go +16 -14
- package/stdlib/math/math.go +439 -0
- package/stdlib/math/rand/rand.go +297 -0
- package/stdlib/os/os.go +93 -0
- package/stdlib/path/filepath/filepath.go +47 -0
- package/stdlib/path/path.go +394 -0
- package/stdlib/regexp/regexp.go +231 -0
- package/stdlib/slices/slices.go +526 -0
- package/stdlib/slices/sort.go +209 -0
- package/stdlib/slices/zsort_cmp.go +480 -0
- package/stdlib/slices/zsort_ordered.go +482 -0
- package/stdlib/sort/sort.go +391 -452
- package/stdlib/sort/zsort_func.go +480 -0
- package/stdlib/sort/zsort_iface.go +480 -0
- package/stdlib/strconv/float.go +1699 -0
- package/stdlib/strconv/isprint.go +752 -0
- package/stdlib/strconv/quote.go +235 -50
- package/stdlib/strconv/strconv.go +616 -0
- package/stdlib/strings/builder.go +95 -0
- package/stdlib/strings/reader.go +163 -0
- package/stdlib/strings/replacer.go +102 -0
- package/stdlib/strings/strings.go +807 -0
- package/stdlib/sync/sync.go +268 -0
- package/stdlib/testing/async_js.go +61 -0
- package/stdlib/testing/benchmark.go +297 -0
- package/stdlib/testing/testing.go +523 -0
- package/stdlib/time/duration.go +233 -0
- package/stdlib/time/format.go +745 -0
- package/stdlib/time/parse.go +790 -0
- package/stdlib/time/sleep_js.go +63 -0
- package/stdlib/time/time.go +834 -0
- package/stdlib/unicode/unicode.go +141 -3
- package/stdlib/unicode/utf8/utf8.go +464 -0
- package/src/backend/js/fmt-runtime.js +0 -573
- package/src/backend/js/stdlib/base64.js +0 -12
- package/src/backend/js/stdlib/binary.js +0 -102
- package/src/backend/js/stdlib/bits.js +0 -119
- package/src/backend/js/stdlib/builder.js +0 -98
- package/src/backend/js/stdlib/bytes.js +0 -368
- package/src/backend/js/stdlib/errors.js +0 -45
- package/src/backend/js/stdlib/fmt.js +0 -517
- package/src/backend/js/stdlib/gom.js +0 -155
- package/src/backend/js/stdlib/hex.js +0 -17
- package/src/backend/js/stdlib/html.js +0 -20
- package/src/backend/js/stdlib/io.js +0 -137
- package/src/backend/js/stdlib/json.js +0 -1167
- package/src/backend/js/stdlib/maps.js +0 -37
- package/src/backend/js/stdlib/math.js +0 -85
- package/src/backend/js/stdlib/os.js +0 -13
- package/src/backend/js/stdlib/path.js +0 -48
- package/src/backend/js/stdlib/rand.js +0 -32
- package/src/backend/js/stdlib/regexp.js +0 -54
- package/src/backend/js/stdlib/sha256.js +0 -12
- package/src/backend/js/stdlib/shared.js +0 -17
- package/src/backend/js/stdlib/simd.js +0 -34
- package/src/backend/js/stdlib/slices.js +0 -107
- package/src/backend/js/stdlib/sort.js +0 -30
- package/src/backend/js/stdlib/strconv.js +0 -79
- package/src/backend/js/stdlib/strings.js +0 -301
- package/src/backend/js/stdlib/sync.js +0 -13
- package/src/backend/js/stdlib/testing.js +0 -41
- package/src/backend/js/stdlib/time.js +0 -152
- package/src/backend/js/stdlib/unicode.js +0 -25
- package/src/backend/js/stdlib/utf8.js +0 -26
- package/src/backend/wasm/stdlib/bytes.js +0 -6716
- package/src/backend/wasm/stdlib/core.js +0 -10278
- package/src/backend/wasm/stdlib/io.js +0 -3578
- package/src/backend/wasm/stdlib/packages.js +0 -2402
- package/src/backend/wasm/stdlib/pure.js +0 -720
- package/src/backend/wasm/stdlib/time.js +0 -2229
- package/src/typechecker/stdlib/core.js +0 -1079
- package/src/typechecker/stdlib/extended.js +0 -1210
- package/stdlib/bytes/iter.go +0 -192
- package/stdlib/bytes/search.go +0 -240
- package/stdlib/bytes/split.go +0 -218
- package/stdlib/errors/overlay.go +0 -12
- package/stdlib/maps/iter.go +0 -65
- package/stdlib/slices/extra.go +0 -73
- package/stdlib/slices/iter.go +0 -113
- package/stdlib/strings/extra.go +0 -35
- package/stdlib/strings/iter.go +0 -122
- package/stdlib/sync/extra.go +0 -105
- package/stdlib/unicode/utf8/bytes.go +0 -239
package/stdlib/sort/sort.go
CHANGED
|
@@ -2,28 +2,50 @@
|
|
|
2
2
|
|
|
3
3
|
//gofront:target both
|
|
4
4
|
|
|
5
|
-
//
|
|
6
|
-
//
|
|
5
|
+
// Copyright 2009 The Go Authors. All rights reserved.
|
|
6
|
+
// Use of this source code is governed by a BSD-style
|
|
7
|
+
// license that can be found in the LICENSE file.
|
|
8
|
+
|
|
9
|
+
// Package sort provides primitives for sorting slices and user-defined collections.
|
|
7
10
|
package sort
|
|
8
11
|
|
|
12
|
+
import "slices"
|
|
13
|
+
|
|
9
14
|
// An implementation of Interface can be sorted by the routines in this package.
|
|
15
|
+
// The methods refer to elements of the underlying collection by integer index.
|
|
10
16
|
type Interface interface {
|
|
17
|
+
// Len is the number of elements in the collection.
|
|
11
18
|
Len() int
|
|
19
|
+
|
|
20
|
+
// Less reports whether the element with index i
|
|
21
|
+
// must sort before the element with index j.
|
|
22
|
+
//
|
|
23
|
+
// If both Less(i, j) and Less(j, i) are false,
|
|
24
|
+
// then the elements at index i and j are considered equal.
|
|
25
|
+
// Sort may place equal elements in any order in the final result,
|
|
26
|
+
// while Stable preserves the original input order of equal elements.
|
|
27
|
+
//
|
|
28
|
+
// Less must describe a [Strict Weak Ordering]. For example:
|
|
29
|
+
// - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well.
|
|
30
|
+
// - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.
|
|
31
|
+
//
|
|
32
|
+
// Note that floating-point comparison (the < operator on float32 or float64 values)
|
|
33
|
+
// is not a strict weak ordering when not-a-number (NaN) values are involved.
|
|
34
|
+
// See Float64Slice.Less for a correct implementation for floating-point values.
|
|
35
|
+
//
|
|
36
|
+
// [Strict Weak Ordering]: https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings
|
|
12
37
|
Less(i, j int) bool
|
|
13
|
-
Swap(i, j int)
|
|
14
|
-
}
|
|
15
38
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
for n > 0 {
|
|
19
|
-
l++
|
|
20
|
-
n >>= 1
|
|
21
|
-
}
|
|
22
|
-
return l
|
|
39
|
+
// Swap swaps the elements with indexes i and j.
|
|
40
|
+
Swap(i, j int)
|
|
23
41
|
}
|
|
24
42
|
|
|
25
43
|
// Sort sorts data in ascending order as determined by the Less method.
|
|
26
|
-
// It
|
|
44
|
+
// It makes one call to data.Len to determine n and O(n*log(n)) calls to
|
|
45
|
+
// data.Less and data.Swap. The sort is not guaranteed to be stable.
|
|
46
|
+
//
|
|
47
|
+
// Note: in many situations, the newer [slices.SortFunc] function is more
|
|
48
|
+
// ergonomic and runs faster.
|
|
27
49
|
func Sort(data Interface) {
|
|
28
50
|
n := data.Len()
|
|
29
51
|
if n <= 1 {
|
|
@@ -33,45 +55,7 @@ func Sort(data Interface) {
|
|
|
33
55
|
pdqsort(data, 0, n, limit)
|
|
34
56
|
}
|
|
35
57
|
|
|
36
|
-
|
|
37
|
-
// while keeping the original order of equal elements.
|
|
38
|
-
func Stable(data Interface) {
|
|
39
|
-
stable(data, data.Len())
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// IsSorted reports whether data is sorted.
|
|
43
|
-
func IsSorted(data Interface) bool {
|
|
44
|
-
n := data.Len()
|
|
45
|
-
for i := n - 1; i > 0; i-- {
|
|
46
|
-
if data.Less(i, i-1) {
|
|
47
|
-
return false
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return true
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
type reverse struct {
|
|
54
|
-
data Interface
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
func (r *reverse) Len() int {
|
|
58
|
-
return r.data.Len()
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
func (r *reverse) Less(i, j int) bool {
|
|
62
|
-
return r.data.Less(j, i)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
func (r *reverse) Swap(i, j int) {
|
|
66
|
-
r.data.Swap(i, j)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Reverse returns the reverse order for data.
|
|
70
|
-
func Reverse(data Interface) Interface {
|
|
71
|
-
return &reverse{data: data}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
type sortedHint int
|
|
58
|
+
type sortedHint int // hint for pdqsort when choosing the pivot
|
|
75
59
|
|
|
76
60
|
const (
|
|
77
61
|
unknownHint sortedHint = iota
|
|
@@ -79,14 +63,7 @@ const (
|
|
|
79
63
|
decreasingHint
|
|
80
64
|
)
|
|
81
65
|
|
|
82
|
-
|
|
83
|
-
maxInsertion = 12
|
|
84
|
-
maxSteps = 5
|
|
85
|
-
shortestShifting = 50
|
|
86
|
-
shortestNinther = 50
|
|
87
|
-
maxSwaps = 4 * 3
|
|
88
|
-
)
|
|
89
|
-
|
|
66
|
+
// xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf
|
|
90
67
|
type xorshift uint64
|
|
91
68
|
|
|
92
69
|
func (r *xorshift) Next() uint64 {
|
|
@@ -96,466 +73,428 @@ func (r *xorshift) Next() uint64 {
|
|
|
96
73
|
return uint64(*r)
|
|
97
74
|
}
|
|
98
75
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
for i := a + 1; i < b; i++ {
|
|
106
|
-
for j := i; j > a && data.Less(j, j-1); j-- {
|
|
107
|
-
data.Swap(j, j-1)
|
|
108
|
-
}
|
|
76
|
+
// bitsLen is math/bits.Len for a uint.
|
|
77
|
+
func bitsLen(n uint) int {
|
|
78
|
+
var l int
|
|
79
|
+
for n > 0 {
|
|
80
|
+
l++
|
|
81
|
+
n >>= 1
|
|
109
82
|
}
|
|
83
|
+
return l
|
|
110
84
|
}
|
|
111
85
|
|
|
112
|
-
func
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
child := 2*root + 1
|
|
116
|
-
if child >= hi {
|
|
117
|
-
break
|
|
118
|
-
}
|
|
119
|
-
if child+1 < hi && data.Less(first+child, first+child+1) {
|
|
120
|
-
child++
|
|
121
|
-
}
|
|
122
|
-
if !data.Less(first+root, first+child) {
|
|
123
|
-
return
|
|
124
|
-
}
|
|
125
|
-
data.Swap(first+root, first+child)
|
|
126
|
-
root = child
|
|
127
|
-
}
|
|
86
|
+
func nextPowerOfTwo(length int) uint {
|
|
87
|
+
shift := uint(bitsLen(uint(length)))
|
|
88
|
+
return uint(1 << shift)
|
|
128
89
|
}
|
|
129
90
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
siftDown(data, i, hi, first)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
for i := hi - 1; i >= 0; i-- {
|
|
140
|
-
data.Swap(first, first+i)
|
|
141
|
-
siftDown(data, lo, i, first)
|
|
142
|
-
}
|
|
91
|
+
// lessSwap is a pair of Less and Swap function for use with the
|
|
92
|
+
// auto-generated func-optimized variant of sort.go in
|
|
93
|
+
// zfuncversion.go.
|
|
94
|
+
type lessSwap struct {
|
|
95
|
+
Less func(i, j int) bool
|
|
96
|
+
Swap func(i, j int)
|
|
143
97
|
}
|
|
144
98
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
for {
|
|
152
|
-
length := b - a
|
|
153
|
-
|
|
154
|
-
if length <= maxInsertion {
|
|
155
|
-
insertionSort(data, a, b)
|
|
156
|
-
return
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if limit == 0 {
|
|
160
|
-
heapSort(data, a, b)
|
|
161
|
-
return
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if !wasBalanced {
|
|
165
|
-
breakPatterns(data, a, b)
|
|
166
|
-
limit--
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
pivot, hint := choosePivot(data, a, b)
|
|
170
|
-
if hint == decreasingHint {
|
|
171
|
-
reverseRange(data, a, b)
|
|
172
|
-
pivot = (b - 1) - (pivot - a)
|
|
173
|
-
hint = increasingHint
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if wasBalanced && wasPartitioned && hint == increasingHint {
|
|
177
|
-
if partialInsertionSort(data, a, b) {
|
|
178
|
-
return
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if a > 0 && !data.Less(a-1, pivot) {
|
|
183
|
-
mid := partitionEqual(data, a, b, pivot)
|
|
184
|
-
a = mid
|
|
185
|
-
continue
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
mid, alreadyPartitioned := partition(data, a, b, pivot)
|
|
189
|
-
wasPartitioned = alreadyPartitioned
|
|
190
|
-
|
|
191
|
-
leftLen, rightLen := mid-a, b-mid
|
|
192
|
-
balanceThreshold := length / 8
|
|
193
|
-
if leftLen < rightLen {
|
|
194
|
-
wasBalanced = leftLen >= balanceThreshold
|
|
195
|
-
pdqsort(data, a, mid, limit)
|
|
196
|
-
a = mid + 1
|
|
197
|
-
} else {
|
|
198
|
-
wasBalanced = rightLen >= balanceThreshold
|
|
199
|
-
pdqsort(data, mid+1, b, limit)
|
|
200
|
-
b = mid
|
|
201
|
-
}
|
|
202
|
-
}
|
|
99
|
+
type reverse struct {
|
|
100
|
+
// This embedded Interface permits Reverse to use the methods of
|
|
101
|
+
// another Interface implementation.
|
|
102
|
+
data Interface
|
|
203
103
|
}
|
|
204
104
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
i, j := a+1, b-1
|
|
208
|
-
|
|
209
|
-
for i <= j && data.Less(i, a) {
|
|
210
|
-
i++
|
|
211
|
-
}
|
|
212
|
-
for i <= j && !data.Less(j, a) {
|
|
213
|
-
j--
|
|
214
|
-
}
|
|
215
|
-
if i > j {
|
|
216
|
-
data.Swap(j, a)
|
|
217
|
-
return j, true
|
|
218
|
-
}
|
|
219
|
-
data.Swap(i, j)
|
|
220
|
-
i++
|
|
221
|
-
j--
|
|
105
|
+
// Len is the number of elements in the collection.
|
|
106
|
+
func (r *reverse) Len() int { return r.data.Len() }
|
|
222
107
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
for i <= j && !data.Less(j, a) {
|
|
228
|
-
j--
|
|
229
|
-
}
|
|
230
|
-
if i > j {
|
|
231
|
-
break
|
|
232
|
-
}
|
|
233
|
-
data.Swap(i, j)
|
|
234
|
-
i++
|
|
235
|
-
j--
|
|
236
|
-
}
|
|
237
|
-
data.Swap(j, a)
|
|
238
|
-
return j, false
|
|
108
|
+
// Less returns the opposite of the embedded implementation's Less method.
|
|
109
|
+
func (r *reverse) Less(i, j int) bool {
|
|
110
|
+
return r.data.Less(j, i)
|
|
239
111
|
}
|
|
240
112
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
i, j := a+1, b-1
|
|
113
|
+
// Swap swaps the elements with indexes i and j.
|
|
114
|
+
func (r *reverse) Swap(i, j int) { r.data.Swap(i, j) }
|
|
244
115
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
for i <= j && data.Less(a, j) {
|
|
250
|
-
j--
|
|
251
|
-
}
|
|
252
|
-
if i > j {
|
|
253
|
-
break
|
|
254
|
-
}
|
|
255
|
-
data.Swap(i, j)
|
|
256
|
-
i++
|
|
257
|
-
j--
|
|
258
|
-
}
|
|
259
|
-
return i
|
|
116
|
+
// Reverse returns the reverse order for data.
|
|
117
|
+
func Reverse(data Interface) Interface {
|
|
118
|
+
return &reverse{data: data}
|
|
260
119
|
}
|
|
261
120
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if i
|
|
270
|
-
return true
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if b-a < shortestShifting {
|
|
121
|
+
// IsSorted reports whether data is sorted.
|
|
122
|
+
//
|
|
123
|
+
// Note: in many situations, the newer [slices.IsSortedFunc] function is more
|
|
124
|
+
// ergonomic and runs faster.
|
|
125
|
+
func IsSorted(data Interface) bool {
|
|
126
|
+
n := data.Len()
|
|
127
|
+
for i := n - 1; i > 0; i-- {
|
|
128
|
+
if data.Less(i, i-1) {
|
|
274
129
|
return false
|
|
275
130
|
}
|
|
276
|
-
|
|
277
|
-
data.Swap(i, i-1)
|
|
278
|
-
|
|
279
|
-
if i-a >= 2 {
|
|
280
|
-
for j := i - 1; j >= 1; j-- {
|
|
281
|
-
if !data.Less(j, j-1) {
|
|
282
|
-
break
|
|
283
|
-
}
|
|
284
|
-
data.Swap(j, j-1)
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
if b-i >= 2 {
|
|
288
|
-
for j := i + 1; j < b; j++ {
|
|
289
|
-
if !data.Less(j, j-1) {
|
|
290
|
-
break
|
|
291
|
-
}
|
|
292
|
-
data.Swap(j, j-1)
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
return false
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
func breakPatterns(data Interface, a, b int) {
|
|
300
|
-
length := b - a
|
|
301
|
-
if length >= 8 {
|
|
302
|
-
random := xorshift(length)
|
|
303
|
-
modulus := nextPowerOfTwo(length)
|
|
304
|
-
|
|
305
|
-
for idx := a + (length/4)*2 - 1; idx <= a+(length/4)*2+1; idx++ {
|
|
306
|
-
other := int(uint(random.Next()) & (modulus - 1))
|
|
307
|
-
if other >= length {
|
|
308
|
-
other -= length
|
|
309
|
-
}
|
|
310
|
-
data.Swap(idx, a+other)
|
|
311
|
-
}
|
|
312
131
|
}
|
|
132
|
+
return true
|
|
313
133
|
}
|
|
314
134
|
|
|
315
|
-
|
|
316
|
-
l := b - a
|
|
317
|
-
|
|
318
|
-
var (
|
|
319
|
-
swaps int
|
|
320
|
-
i = a + l/4*1
|
|
321
|
-
j = a + l/4*2
|
|
322
|
-
k = a + l/4*3
|
|
323
|
-
)
|
|
324
|
-
|
|
325
|
-
if l >= 8 {
|
|
326
|
-
if l >= shortestNinther {
|
|
327
|
-
i = medianAdjacent(data, i, &swaps)
|
|
328
|
-
j = medianAdjacent(data, j, &swaps)
|
|
329
|
-
k = medianAdjacent(data, k, &swaps)
|
|
330
|
-
}
|
|
331
|
-
j = median(data, i, j, k, &swaps)
|
|
332
|
-
}
|
|
135
|
+
// Convenience types for common cases
|
|
333
136
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
return j, increasingHint
|
|
337
|
-
case maxSwaps:
|
|
338
|
-
return j, decreasingHint
|
|
339
|
-
default:
|
|
340
|
-
return j, unknownHint
|
|
341
|
-
}
|
|
342
|
-
}
|
|
137
|
+
// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
|
|
138
|
+
type IntSlice []int
|
|
343
139
|
|
|
344
|
-
func
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
return b, a
|
|
348
|
-
}
|
|
349
|
-
return a, b
|
|
350
|
-
}
|
|
140
|
+
func (x IntSlice) Len() int { return len(x) }
|
|
141
|
+
func (x IntSlice) Less(i, j int) bool { return x[i] < x[j] }
|
|
142
|
+
func (x IntSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
351
143
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
b, c = order2(data, b, c, swaps)
|
|
355
|
-
a, b = order2(data, a, b, swaps)
|
|
356
|
-
return b
|
|
357
|
-
}
|
|
144
|
+
// Sort is a convenience method: x.Sort() calls Sort(x).
|
|
145
|
+
func (x IntSlice) Sort() { Sort(x) }
|
|
358
146
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
147
|
+
// Float64Slice implements Interface for a []float64, sorting in increasing order,
|
|
148
|
+
// with not-a-number (NaN) values ordered before other values.
|
|
149
|
+
type Float64Slice []float64
|
|
362
150
|
|
|
363
|
-
func
|
|
364
|
-
i := a
|
|
365
|
-
j := b - 1
|
|
366
|
-
for i < j {
|
|
367
|
-
data.Swap(i, j)
|
|
368
|
-
i++
|
|
369
|
-
j--
|
|
370
|
-
}
|
|
371
|
-
}
|
|
151
|
+
func (x Float64Slice) Len() int { return len(x) }
|
|
372
152
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
153
|
+
// Less reports whether x[i] should be ordered before x[j], as required by the sort Interface.
|
|
154
|
+
// Note that floating-point comparison by itself is not a transitive relation: it does not
|
|
155
|
+
// report a consistent ordering for not-a-number (NaN) values.
|
|
156
|
+
// This implementation of Less places NaN values before any others, by using:
|
|
157
|
+
//
|
|
158
|
+
// x[i] < x[j] || (math.IsNaN(x[i]) && !math.IsNaN(x[j]))
|
|
159
|
+
func (x Float64Slice) Less(i, j int) bool { return x[i] < x[j] || (isNaN(x[i]) && !isNaN(x[j])) }
|
|
160
|
+
func (x Float64Slice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
378
161
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
for b <= n {
|
|
383
|
-
insertionSort(data, a, b)
|
|
384
|
-
a = b
|
|
385
|
-
b += blockSize
|
|
386
|
-
}
|
|
387
|
-
insertionSort(data, a, n)
|
|
388
|
-
|
|
389
|
-
for blockSize < n {
|
|
390
|
-
a, b = 0, 2*blockSize
|
|
391
|
-
for b <= n {
|
|
392
|
-
symMerge(data, a, a+blockSize, b)
|
|
393
|
-
a = b
|
|
394
|
-
b += 2 * blockSize
|
|
395
|
-
}
|
|
396
|
-
if m := a + blockSize; m < n {
|
|
397
|
-
symMerge(data, a, m, n)
|
|
398
|
-
}
|
|
399
|
-
blockSize *= 2
|
|
400
|
-
}
|
|
162
|
+
// isNaN is a copy of math.IsNaN to avoid a dependency on the math package.
|
|
163
|
+
func isNaN(f float64) bool {
|
|
164
|
+
return f != f
|
|
401
165
|
}
|
|
402
166
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
i := m
|
|
406
|
-
j := b
|
|
407
|
-
for i < j {
|
|
408
|
-
h := int(uint(i+j) >> 1)
|
|
409
|
-
if data.Less(h, a) {
|
|
410
|
-
i = h + 1
|
|
411
|
-
} else {
|
|
412
|
-
j = h
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
for k := a; k < i-1; k++ {
|
|
416
|
-
data.Swap(k, k+1)
|
|
417
|
-
}
|
|
418
|
-
return true
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
if b-m == 1 {
|
|
422
|
-
i := a
|
|
423
|
-
j := m
|
|
424
|
-
for i < j {
|
|
425
|
-
h := int(uint(i+j) >> 1)
|
|
426
|
-
if !data.Less(m, h) {
|
|
427
|
-
i = h + 1
|
|
428
|
-
} else {
|
|
429
|
-
j = h
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
for k := m; k > i; k-- {
|
|
433
|
-
data.Swap(k, k-1)
|
|
434
|
-
}
|
|
435
|
-
return true
|
|
436
|
-
}
|
|
437
|
-
return false
|
|
438
|
-
}
|
|
167
|
+
// Sort is a convenience method: x.Sort() calls Sort(x).
|
|
168
|
+
func (x Float64Slice) Sort() { Sort(x) }
|
|
439
169
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
return
|
|
443
|
-
}
|
|
170
|
+
// StringSlice attaches the methods of Interface to []string, sorting in increasing order.
|
|
171
|
+
type StringSlice []string
|
|
444
172
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
if m > mid {
|
|
449
|
-
start = n - b
|
|
450
|
-
r = mid
|
|
451
|
-
} else {
|
|
452
|
-
start = a
|
|
453
|
-
r = m
|
|
454
|
-
}
|
|
455
|
-
p := n - 1
|
|
173
|
+
func (x StringSlice) Len() int { return len(x) }
|
|
174
|
+
func (x StringSlice) Less(i, j int) bool { return x[i] < x[j] }
|
|
175
|
+
func (x StringSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
456
176
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
177
|
+
// Sort is a convenience method: x.Sort() calls Sort(x).
|
|
178
|
+
func (x StringSlice) Sort() { Sort(x) }
|
|
179
|
+
|
|
180
|
+
// Convenience wrappers for common cases
|
|
181
|
+
|
|
182
|
+
// Ints sorts a slice of ints in increasing order.
|
|
183
|
+
//
|
|
184
|
+
// Note: as of Go 1.22, this function simply calls [slices.Sort].
|
|
185
|
+
//gofront:native sort.Ints
|
|
186
|
+
func Ints(x []int) { slices.Sort(x) }
|
|
187
|
+
|
|
188
|
+
// Float64s sorts a slice of float64s in increasing order.
|
|
189
|
+
// Not-a-number (NaN) values are ordered before other values.
|
|
190
|
+
//
|
|
191
|
+
// Note: as of Go 1.22, this function simply calls [slices.Sort].
|
|
192
|
+
func Float64s(x []float64) { slices.Sort(x) }
|
|
193
|
+
|
|
194
|
+
// Strings sorts a slice of strings in increasing order.
|
|
195
|
+
//
|
|
196
|
+
// Note: as of Go 1.22, this function simply calls [slices.Sort].
|
|
197
|
+
//gofront:native sort.Strings
|
|
198
|
+
func Strings(x []string) { slices.Sort(x) }
|
|
199
|
+
|
|
200
|
+
// IntsAreSorted reports whether the slice x is sorted in increasing order.
|
|
201
|
+
//
|
|
202
|
+
// Note: as of Go 1.22, this function simply calls [slices.IsSorted].
|
|
203
|
+
func IntsAreSorted(x []int) bool { return slices.IsSorted(x) }
|
|
204
|
+
|
|
205
|
+
// Float64sAreSorted reports whether the slice x is sorted in increasing order,
|
|
206
|
+
// with not-a-number (NaN) values before any other values.
|
|
207
|
+
//
|
|
208
|
+
// Note: as of Go 1.22, this function simply calls [slices.IsSorted].
|
|
209
|
+
func Float64sAreSorted(x []float64) bool { return slices.IsSorted(x) }
|
|
210
|
+
|
|
211
|
+
// StringsAreSorted reports whether the slice x is sorted in increasing order.
|
|
212
|
+
//
|
|
213
|
+
// Note: as of Go 1.22, this function simply calls [slices.IsSorted].
|
|
214
|
+
func StringsAreSorted(x []string) bool { return slices.IsSorted(x) }
|
|
215
|
+
|
|
216
|
+
// Notes on stable sorting:
|
|
217
|
+
// The used algorithms are simple and provably correct on all input and use
|
|
218
|
+
// only logarithmic additional stack space. They perform well if compared
|
|
219
|
+
// experimentally to other stable in-place sorting algorithms.
|
|
220
|
+
//
|
|
221
|
+
// Remarks on other algorithms evaluated:
|
|
222
|
+
// - GCC's 4.6.3 stable_sort with merge_without_buffer from libstdc++:
|
|
223
|
+
// Not faster.
|
|
224
|
+
// - GCC's __rotate for block rotations: Not faster.
|
|
225
|
+
// - "Practical in-place mergesort" from Jyrki Katajainen, Tomi A. Pasanen
|
|
226
|
+
// and Jukka Teuhola; Nordic Journal of Computing 3,1 (1996), 27-40:
|
|
227
|
+
// The given algorithms are in-place, number of Swap and Assignments
|
|
228
|
+
// grow as n log n but the algorithm is not stable.
|
|
229
|
+
// - "Fast Stable In-Place Sorting with O(n) Data Moves" J.I. Munro and
|
|
230
|
+
// V. Raman in Algorithmica (1996) 16, 115-160:
|
|
231
|
+
// This algorithm either needs additional 2n bits or works only if there
|
|
232
|
+
// are enough different elements available to encode some permutations
|
|
233
|
+
// which have to be undone later (so not stable on any input).
|
|
234
|
+
// - All the optimal in-place sorting/merging algorithms I found are either
|
|
235
|
+
// unstable or rely on enough different elements in each step to encode the
|
|
236
|
+
// performed block rearrangements. See also "In-Place Merging Algorithms",
|
|
237
|
+
// Denham Coates-Evely, Department of Computer Science, Kings College,
|
|
238
|
+
// January 2004 and the references in there.
|
|
239
|
+
// - Often "optimal" algorithms are optimal in the number of assignments
|
|
240
|
+
// but Interface has only Swap as operation.
|
|
465
241
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
242
|
+
// Stable sorts data in ascending order as determined by the Less method,
|
|
243
|
+
// while keeping the original order of equal elements.
|
|
244
|
+
//
|
|
245
|
+
// It makes one call to data.Len to determine n, O(n*log(n)) calls to
|
|
246
|
+
// data.Less and O(n*log(n)*log(n)) calls to data.Swap.
|
|
247
|
+
//
|
|
248
|
+
// Note: in many situations, the newer slices.SortStableFunc function is more
|
|
249
|
+
// ergonomic and runs faster.
|
|
250
|
+
func Stable(data Interface) {
|
|
251
|
+
stable(data, data.Len())
|
|
476
252
|
}
|
|
477
253
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
254
|
+
/*
|
|
255
|
+
Complexity of Stable Sorting
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
Complexity of block swapping rotation
|
|
259
|
+
|
|
260
|
+
Each Swap puts one new element into its correct, final position.
|
|
261
|
+
Elements which reach their final position are no longer moved.
|
|
262
|
+
Thus block swapping rotation needs |u|+|v| calls to Swaps.
|
|
263
|
+
This is best possible as each element might need a move.
|
|
264
|
+
|
|
265
|
+
Pay attention when comparing to other optimal algorithms which
|
|
266
|
+
typically count the number of assignments instead of swaps:
|
|
267
|
+
E.g. the optimal algorithm of Dudzinski and Dydek for in-place
|
|
268
|
+
rotations uses O(u + v + gcd(u,v)) assignments which is
|
|
269
|
+
better than our O(3 * (u+v)) as gcd(u,v) <= u.
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
Stable sorting by SymMerge and BlockSwap rotations
|
|
273
|
+
|
|
274
|
+
SymMerg complexity for same size input M = N:
|
|
275
|
+
Calls to Less: O(M*log(N/M+1)) = O(N*log(2)) = O(N)
|
|
276
|
+
Calls to Swap: O((M+N)*log(M)) = O(2*N*log(N)) = O(N*log(N))
|
|
277
|
+
|
|
278
|
+
(The following argument does not fuzz over a missing -1 or
|
|
279
|
+
other stuff which does not impact the final result).
|
|
280
|
+
|
|
281
|
+
Let n = data.Len(). Assume n = 2^k.
|
|
282
|
+
|
|
283
|
+
Plain merge sort performs log(n) = k iterations.
|
|
284
|
+
On iteration i the algorithm merges 2^(k-i) blocks, each of size 2^i.
|
|
285
|
+
|
|
286
|
+
Thus iteration i of merge sort performs:
|
|
287
|
+
Calls to Less O(2^(k-i) * 2^i) = O(2^k) = O(2^log(n)) = O(n)
|
|
288
|
+
Calls to Swap O(2^(k-i) * 2^i * log(2^i)) = O(2^k * i) = O(n*i)
|
|
289
|
+
|
|
290
|
+
In total k = log(n) iterations are performed; so in total:
|
|
291
|
+
Calls to Less O(log(n) * n)
|
|
292
|
+
Calls to Swap O(n + 2*n + 3*n + ... + (k-1)*n + k*n)
|
|
293
|
+
= O((k/2) * k * n) = O(n * k^2) = O(n * log^2(n))
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
Above results should generalize to arbitrary n = 2^k + p
|
|
297
|
+
and should not be influenced by the initial insertion sort phase:
|
|
298
|
+
Insertion sort is O(n^2) on Swap and Less, thus O(bs^2) per block of
|
|
299
|
+
size bs at n/bs blocks: O(bs*n) Swaps and Less during insertion sort.
|
|
300
|
+
Merge sort iterations start at i = log(bs). With t = log(bs) constant:
|
|
301
|
+
Calls to Less O((log(n)-t) * n + bs*n) = O(log(n)*n + (bs-t)*n)
|
|
302
|
+
= O(n * log(n))
|
|
303
|
+
Calls to Swap O(n * log^2(n) - (t^2+t)/2*n) = O(n * log^2(n))
|
|
304
|
+
|
|
305
|
+
*/
|
|
306
|
+
|
|
307
|
+
// Search uses binary search to find and return the smallest index i
|
|
308
|
+
// in [0, n) at which f(i) is true, assuming that on the range [0, n),
|
|
309
|
+
// f(i) == true implies f(i+1) == true. That is, Search requires that
|
|
310
|
+
// f is false for some (possibly empty) prefix of the input range [0, n)
|
|
311
|
+
// and then true for the (possibly empty) remainder; Search returns
|
|
312
|
+
// the first true index. If there is no such index, Search returns n.
|
|
313
|
+
// (Note that the "not found" return value is not -1 as in, for instance,
|
|
314
|
+
// strings.Index.)
|
|
315
|
+
// Search calls f(i) only for i in the range [0, n).
|
|
316
|
+
//
|
|
317
|
+
// A common use of Search is to find the index i for a value x in
|
|
318
|
+
// a sorted, indexable data structure such as an array or slice.
|
|
319
|
+
// In this case, the argument f, typically a closure, captures the value
|
|
320
|
+
// to be searched for, and how the data structure is indexed and
|
|
321
|
+
// ordered.
|
|
322
|
+
//
|
|
323
|
+
// For instance, given a slice data sorted in ascending order,
|
|
324
|
+
// the call Search(len(data), func(i int) bool { return data[i] >= 23 })
|
|
325
|
+
// returns the smallest index i such that data[i] >= 23. If the caller
|
|
326
|
+
// wants to find whether 23 is in the slice, it must test data[i] == 23
|
|
327
|
+
// separately.
|
|
328
|
+
//
|
|
329
|
+
// Searching data sorted in descending order would use the <=
|
|
330
|
+
// operator instead of the >= operator.
|
|
331
|
+
//
|
|
332
|
+
// To complete the example above, the following code tries to find the value
|
|
333
|
+
// x in an integer slice data sorted in ascending order:
|
|
334
|
+
//
|
|
335
|
+
// x := 23
|
|
336
|
+
// i := sort.Search(len(data), func(i int) bool { return data[i] >= x })
|
|
337
|
+
// if i < len(data) && data[i] == x {
|
|
338
|
+
// // x is present at data[i]
|
|
339
|
+
// } else {
|
|
340
|
+
// // x is not present in data,
|
|
341
|
+
// // but i is the index where it would be inserted.
|
|
342
|
+
// }
|
|
343
|
+
//
|
|
344
|
+
// As a more whimsical example, this program guesses your number:
|
|
345
|
+
//
|
|
346
|
+
// func GuessingGame() {
|
|
347
|
+
// var s string
|
|
348
|
+
// fmt.Printf("Pick an integer from 0 to 100.\n")
|
|
349
|
+
// answer := sort.Search(100, func(i int) bool {
|
|
350
|
+
// fmt.Printf("Is your number <= %d? ", i)
|
|
351
|
+
// fmt.Scanf("%s", &s)
|
|
352
|
+
// return s != "" && s[0] == 'y'
|
|
353
|
+
// })
|
|
354
|
+
// fmt.Printf("Your number is %d.\n", answer)
|
|
355
|
+
// }
|
|
356
|
+
func Search(n int, f func(int) bool) int {
|
|
357
|
+
// Define f(-1) == false and f(n) == true.
|
|
358
|
+
// Invariant: f(i-1) == false, f(j) == true.
|
|
359
|
+
i, j := 0, n
|
|
360
|
+
for i < j {
|
|
361
|
+
h := int(uint(i+j) >> 1) // avoid overflow when computing h
|
|
362
|
+
// i ≤ h < j
|
|
363
|
+
if !f(h) {
|
|
364
|
+
i = h + 1 // preserves f(i-1) == false
|
|
486
365
|
} else {
|
|
487
|
-
|
|
488
|
-
j -= i
|
|
366
|
+
j = h // preserves f(j) == true
|
|
489
367
|
}
|
|
490
368
|
}
|
|
491
|
-
|
|
369
|
+
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i.
|
|
370
|
+
return i
|
|
492
371
|
}
|
|
493
372
|
|
|
494
373
|
// Find uses binary search to find and return the smallest index i in [0, n)
|
|
495
374
|
// at which cmp(i) <= 0. If there is no such index i, Find returns i = n.
|
|
496
375
|
// The found result is true if i < n and cmp(i) == 0.
|
|
497
376
|
// Find calls cmp(i) only for i in the range [0, n).
|
|
377
|
+
//
|
|
378
|
+
// To permit binary search, Find requires that cmp(i) > 0 for a leading
|
|
379
|
+
// prefix of the range, cmp(i) == 0 in the middle, and cmp(i) < 0 for
|
|
380
|
+
// the final suffix of the range. (Each subrange could be empty.)
|
|
381
|
+
// The usual way to establish this condition is to interpret cmp(i)
|
|
382
|
+
// as a comparison of a desired target value t against entry i in an
|
|
383
|
+
// underlying indexed data structure x, returning <0, 0, and >0
|
|
384
|
+
// when t < x[i], t == x[i], and t > x[i], respectively.
|
|
385
|
+
//
|
|
386
|
+
// For example, to look for a particular string in a sorted, random-access
|
|
387
|
+
// list of strings:
|
|
388
|
+
//
|
|
389
|
+
// i, found := sort.Find(x.Len(), func(i int) int {
|
|
390
|
+
// return strings.Compare(target, x.At(i))
|
|
391
|
+
// })
|
|
392
|
+
// if found {
|
|
393
|
+
// fmt.Printf("found %s at entry %d\n", target, i)
|
|
394
|
+
// } else {
|
|
395
|
+
// fmt.Printf("%s not found, would insert at %d", target, i)
|
|
396
|
+
// }
|
|
498
397
|
func Find(n int, cmp func(int) int) (i int, found bool) {
|
|
398
|
+
// The invariants here are similar to the ones in Search.
|
|
399
|
+
// Define cmp(-1) > 0 and cmp(n) <= 0
|
|
400
|
+
// Invariant: cmp(i-1) > 0, cmp(j) <= 0
|
|
499
401
|
i, j := 0, n
|
|
500
402
|
for i < j {
|
|
501
|
-
h := int(uint(i+j) >> 1)
|
|
403
|
+
h := int(uint(i+j) >> 1) // avoid overflow when computing h
|
|
404
|
+
// i ≤ h < j
|
|
502
405
|
if cmp(h) > 0 {
|
|
503
|
-
i = h + 1
|
|
406
|
+
i = h + 1 // preserves cmp(i-1) > 0
|
|
504
407
|
} else {
|
|
505
|
-
j = h
|
|
408
|
+
j = h // preserves cmp(j) <= 0
|
|
506
409
|
}
|
|
507
410
|
}
|
|
411
|
+
// i == j, cmp(i-1) > 0 and cmp(j) <= 0
|
|
508
412
|
return i, i < n && cmp(i) == 0
|
|
509
413
|
}
|
|
510
414
|
|
|
415
|
+
// Convenience wrappers for common cases.
|
|
416
|
+
|
|
511
417
|
// SearchInts searches for x in a sorted slice of ints and returns the index
|
|
512
|
-
// as specified by Search.
|
|
418
|
+
// as specified by [Search]. The return value is the index to insert x if x is
|
|
419
|
+
// not present (it could be len(a)).
|
|
420
|
+
// The slice must be sorted in ascending order.
|
|
513
421
|
func SearchInts(a []int, x int) int {
|
|
514
422
|
return Search(len(a), func(i int) bool { return a[i] >= x })
|
|
515
423
|
}
|
|
516
424
|
|
|
517
425
|
// SearchFloat64s searches for x in a sorted slice of float64s and returns the index
|
|
518
|
-
// as specified by Search.
|
|
426
|
+
// as specified by [Search]. The return value is the index to insert x if x is not
|
|
427
|
+
// present (it could be len(a)).
|
|
428
|
+
// The slice must be sorted in ascending order.
|
|
519
429
|
func SearchFloat64s(a []float64, x float64) int {
|
|
520
430
|
return Search(len(a), func(i int) bool { return a[i] >= x })
|
|
521
431
|
}
|
|
522
432
|
|
|
523
433
|
// SearchStrings searches for x in a sorted slice of strings and returns the index
|
|
524
|
-
// as specified by Search.
|
|
434
|
+
// as specified by Search. The return value is the index to insert x if x is not
|
|
435
|
+
// present (it could be len(a)).
|
|
436
|
+
// The slice must be sorted in ascending order.
|
|
525
437
|
func SearchStrings(a []string, x string) int {
|
|
526
438
|
return Search(len(a), func(i int) bool { return a[i] >= x })
|
|
527
439
|
}
|
|
528
440
|
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
func (
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
//
|
|
552
|
-
//
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
func (x
|
|
556
|
-
|
|
557
|
-
|
|
441
|
+
// Search returns the result of applying [SearchInts] to the receiver and x.
|
|
442
|
+
func (p IntSlice) Search(x int) int { return SearchInts(p, x) }
|
|
443
|
+
|
|
444
|
+
// Search returns the result of applying [SearchFloat64s] to the receiver and x.
|
|
445
|
+
func (p Float64Slice) Search(x float64) int { return SearchFloat64s(p, x) }
|
|
446
|
+
|
|
447
|
+
// Search returns the result of applying [SearchStrings] to the receiver and x.
|
|
448
|
+
func (p StringSlice) Search(x string) int { return SearchStrings(p, x) }
|
|
449
|
+
|
|
450
|
+
// Slice, SliceStable and SliceIsSorted take the slice as a type parameter
|
|
451
|
+
// (S ~[]E) where Go takes `x any` and swaps through package reflect, which
|
|
452
|
+
// GoFront does not have. Every call that is valid in Go (a slice argument)
|
|
453
|
+
// infers the same way; an interface value holding a slice is not accepted.
|
|
454
|
+
|
|
455
|
+
// Slice sorts the slice x given the provided less function.
|
|
456
|
+
// It panics if x is not a slice.
|
|
457
|
+
//
|
|
458
|
+
// The sort is not guaranteed to be stable: equal elements
|
|
459
|
+
// may be reversed from their original order.
|
|
460
|
+
// For a stable sort, use [SliceStable].
|
|
461
|
+
//
|
|
462
|
+
// The less function must satisfy the same requirements as
|
|
463
|
+
// the Interface type's Less method.
|
|
464
|
+
//
|
|
465
|
+
// Note: in many situations, the newer [slices.SortFunc] function is more
|
|
466
|
+
// ergonomic and runs faster.
|
|
467
|
+
func Slice[S ~[]E, E any](x S, less func(i, j int) bool) {
|
|
468
|
+
swap := func(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
469
|
+
length := len(x)
|
|
470
|
+
limit := bitsLen(uint(length))
|
|
471
|
+
pdqsort_func(lessSwap{less, swap}, 0, length, limit)
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// SliceStable sorts the slice x using the provided less
|
|
475
|
+
// function, keeping equal elements in their original order.
|
|
476
|
+
// It panics if x is not a slice.
|
|
477
|
+
//
|
|
478
|
+
// The less function must satisfy the same requirements as
|
|
479
|
+
// the Interface type's Less method.
|
|
480
|
+
//
|
|
481
|
+
// Note: in many situations, the newer [slices.SortStableFunc] function is more
|
|
482
|
+
// ergonomic and runs faster.
|
|
483
|
+
func SliceStable[S ~[]E, E any](x S, less func(i, j int) bool) {
|
|
484
|
+
swap := func(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
485
|
+
stable_func(lessSwap{less, swap}, len(x))
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// SliceIsSorted reports whether the slice x is sorted according to the provided less function.
|
|
489
|
+
// It panics if x is not a slice.
|
|
490
|
+
//
|
|
491
|
+
// Note: in many situations, the newer [slices.IsSortedFunc] function is more
|
|
492
|
+
// ergonomic and runs faster.
|
|
493
|
+
func SliceIsSorted[S ~[]E, E any](x S, less func(i, j int) bool) bool {
|
|
494
|
+
for i := len(x) - 1; i > 0; i-- {
|
|
495
|
+
if less(i, i-1) {
|
|
496
|
+
return false
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return true
|
|
558
500
|
}
|
|
559
|
-
func (x Float64Slice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
|
560
|
-
func (x Float64Slice) Sort() { Sort(x) }
|
|
561
|
-
func (x Float64Slice) Search(target float64) int { return SearchFloat64s(x, target) }
|