gofront 1.7.0 → 1.9.0
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 +14 -14
- package/package.json +10 -38
- package/src/backend/js/expressions.js +2060 -389
- package/src/backend/js/index.js +586 -174
- package/src/backend/js/natives.js +383 -0
- package/src/backend/js/runtime.js +936 -1644
- package/src/backend/js/statements.js +1349 -132
- package/src/backend/js/stdlib/index.js +51 -94
- package/src/backend/js/templ.js +67 -118
- package/src/backend/js/type-info.js +436 -0
- package/src/backend/shared/fixed-float.js +20 -0
- package/src/backend/shared/fmt-types.js +80 -0
- package/src/backend/shared/host-unicode.js +36 -0
- package/src/backend/wasm/boundary.js +753 -872
- package/src/backend/wasm/emit-builtins.js +914 -237
- package/src/backend/wasm/emit-exprs.js +1456 -195
- package/src/backend/wasm/emit-maps.js +78 -380
- package/src/backend/wasm/emit-stmts.js +1659 -214
- package/src/backend/wasm/emit.js +614 -88
- package/src/backend/wasm/encode.js +43 -13
- package/src/backend/wasm/globals.js +491 -0
- package/src/backend/wasm/glue.js +2 -4
- package/src/backend/wasm/index.js +406 -3195
- package/src/backend/wasm/method-expr.js +392 -0
- package/src/backend/wasm/module-emitter.js +1615 -0
- package/src/backend/wasm/monomorph.js +164 -9
- package/src/backend/wasm/natives.js +596 -0
- package/src/backend/wasm/prescan.js +249 -0
- package/src/backend/wasm/prune-funcs.js +89 -0
- package/src/backend/wasm/stdlib/index.js +2 -37
- package/src/backend/wasm/stdlib/simd.js +293 -28
- package/src/backend/wasm/types.js +260 -27
- package/src/backend/wasm/variadic.js +93 -0
- package/src/backend/wasm/wat.js +47 -20
- package/src/{cli-core.js → cli/cli-core.js} +241 -11
- package/src/{test-runner.js → cli/test-runner.js} +188 -122
- package/src/{vendor.js → cli/vendor.js} +3 -4
- package/src/compiler.js +202 -386
- package/src/index.js +15 -169
- package/src/{lexer.js → lexer/lexer.js} +203 -62
- package/src/{templ-lexer.js → lexer/templ-lexer.js} +61 -18
- package/src/{tokens.js → lexer/tokens.js} +2 -0
- package/src/link/sourcemap.js +97 -0
- package/src/link/stdlib.js +737 -0
- package/src/link/type-names.js +148 -0
- package/src/link/wasm.js +318 -0
- package/src/lower/bounds.js +547 -0
- package/src/lower/boxing.js +1 -1
- package/src/lower/captures.js +267 -64
- package/src/lower/embedding.js +32 -3
- package/src/lower/errors-as.js +196 -0
- package/src/lower/fmt-formatters.js +905 -0
- package/src/lower/functions.js +2 -0
- package/src/lower/gen-types.js +274 -0
- package/src/lower/index.js +33 -19
- package/src/lower/json-codecs.js +1379 -0
- package/src/lower/ownership.js +47 -8
- package/src/lower/range.js +86 -1
- package/src/lower/reachability.js +150 -0
- package/src/lower/walk.js +12 -0
- package/src/parser/declarations.js +83 -15
- package/src/parser/expressions.js +110 -18
- package/src/parser/index.js +2 -1
- package/src/parser/statements.js +38 -22
- package/src/{templ-parser.js → parser/templ.js} +179 -17
- package/src/parser/types.js +70 -9
- package/src/resolver.js +21 -1
- package/src/typechecker/assignability.js +291 -19
- package/src/typechecker/const-fold.js +399 -0
- package/src/typechecker/constants.js +282 -0
- package/src/typechecker/expressions.js +1220 -183
- package/src/typechecker/index.js +659 -92
- package/src/typechecker/init-order.js +426 -0
- package/src/typechecker/operand-text.js +40 -0
- package/src/typechecker/resolve.js +613 -62
- package/src/typechecker/statements.js +212 -20
- package/src/typechecker/stdlib/excluded.js +902 -0
- package/src/typechecker/stdlib/index.js +38 -0
- package/src/typechecker/stdlib/simd.js +65 -15
- package/src/typechecker/termination.js +11 -2
- package/src/typechecker/types.js +268 -514
- package/stdlib/bytes/buffer.go +479 -0
- package/stdlib/bytes/bytes.go +1010 -0
- package/stdlib/bytes/reader.go +161 -0
- package/stdlib/cmp/cmp.go +65 -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/errors.go +31 -0
- package/stdlib/encoding/json/indent.go +71 -0
- package/stdlib/encoding/json/json.go +62 -0
- package/stdlib/encoding/json/number.go +12 -0
- package/stdlib/encoding/json/stream.go +173 -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/hash/hash.go +53 -0
- package/stdlib/html/html.go +251 -0
- package/stdlib/internal/fmtrt/fmtrt.go +163 -0
- package/stdlib/internal/jsonrt/dec.go +871 -0
- package/stdlib/internal/jsonrt/enc.go +516 -0
- package/stdlib/internal/jsonrt/indent.go +135 -0
- package/stdlib/internal/jsonrt/number.go +27 -0
- package/stdlib/internal/jsonrt/reader.go +73 -0
- package/stdlib/internal/jsonrt/scanner.go +658 -0
- package/stdlib/io/io.go +502 -0
- package/stdlib/iter/iter.go +18 -0
- package/stdlib/maps/maps.go +131 -0
- package/stdlib/math/bits/bits.go +507 -0
- package/stdlib/math/extra.go +272 -0
- 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 +500 -0
- 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 +526 -0
- 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 +250 -0
- package/stdlib/unicode/utf8/utf8.go +464 -0
- 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 -46
- package/src/backend/js/stdlib/builder.js +0 -63
- package/src/backend/js/stdlib/bytes.js +0 -122
- package/src/backend/js/stdlib/errors.js +0 -44
- package/src/backend/js/stdlib/fmt.js +0 -178
- 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 -972
- package/src/backend/js/stdlib/maps.js +0 -45
- package/src/backend/js/stdlib/math.js +0 -69
- 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 -24
- package/src/backend/js/stdlib/slices.js +0 -77
- package/src/backend/js/stdlib/sort.js +0 -30
- package/src/backend/js/stdlib/strconv.js +0 -74
- package/src/backend/js/stdlib/strings.js +0 -222
- package/src/backend/js/stdlib/sync.js +0 -13
- package/src/backend/js/stdlib/testing.js +0 -34
- package/src/backend/js/stdlib/time.js +0 -128
- package/src/backend/js/stdlib/unicode.js +0 -25
- package/src/backend/js/stdlib/utf8.js +0 -26
- package/src/backend/wasm/const-fold.js +0 -205
- package/src/backend/wasm/emit-stdlib.js +0 -13
- package/src/backend/wasm/stdlib/bytes.js +0 -5039
- package/src/backend/wasm/stdlib/core.js +0 -8903
- package/src/backend/wasm/stdlib/io.js +0 -2161
- package/src/backend/wasm/stdlib/packages.js +0 -2400
- package/src/backend/wasm/stdlib/pure.js +0 -720
- package/src/backend/wasm/stdlib/time.js +0 -1200
- package/src/typechecker/stdlib/core.js +0 -801
- package/src/typechecker/stdlib/extended.js +0 -914
- package/src/typechecker/stdlib.js +0 -22
- /package/src/{asset-manager.js → cli/asset-manager.js} +0 -0
- /package/src/{colors.js → cli/colors.js} +0 -0
- /package/src/{dev-server.js → cli/dev-server.js} +0 -0
- /package/src/{minifier.js → cli/minifier.js} +0 -0
- /package/src/{project-config.js → cli/project-config.js} +0 -0
- /package/src/{pwa.js → cli/pwa.js} +0 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
//go:build gofront
|
|
2
|
+
|
|
3
|
+
//gofront:target both
|
|
4
|
+
|
|
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.
|
|
10
|
+
package sort
|
|
11
|
+
|
|
12
|
+
import "slices"
|
|
13
|
+
|
|
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.
|
|
16
|
+
type Interface interface {
|
|
17
|
+
// Len is the number of elements in the collection.
|
|
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
|
|
37
|
+
Less(i, j int) bool
|
|
38
|
+
|
|
39
|
+
// Swap swaps the elements with indexes i and j.
|
|
40
|
+
Swap(i, j int)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Sort sorts data in ascending order as determined by the Less method.
|
|
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.
|
|
49
|
+
func Sort(data Interface) {
|
|
50
|
+
n := data.Len()
|
|
51
|
+
if n <= 1 {
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
limit := bitsLen(uint(n))
|
|
55
|
+
pdqsort(data, 0, n, limit)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type sortedHint int // hint for pdqsort when choosing the pivot
|
|
59
|
+
|
|
60
|
+
const (
|
|
61
|
+
unknownHint sortedHint = iota
|
|
62
|
+
increasingHint
|
|
63
|
+
decreasingHint
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
// xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf
|
|
67
|
+
type xorshift uint64
|
|
68
|
+
|
|
69
|
+
func (r *xorshift) Next() uint64 {
|
|
70
|
+
*r ^= *r << 13
|
|
71
|
+
*r ^= *r >> 7
|
|
72
|
+
*r ^= *r << 17
|
|
73
|
+
return uint64(*r)
|
|
74
|
+
}
|
|
75
|
+
|
|
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
|
|
82
|
+
}
|
|
83
|
+
return l
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
func nextPowerOfTwo(length int) uint {
|
|
87
|
+
shift := uint(bitsLen(uint(length)))
|
|
88
|
+
return uint(1 << shift)
|
|
89
|
+
}
|
|
90
|
+
|
|
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)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
type reverse struct {
|
|
100
|
+
// This embedded Interface permits Reverse to use the methods of
|
|
101
|
+
// another Interface implementation.
|
|
102
|
+
data Interface
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Len is the number of elements in the collection.
|
|
106
|
+
func (r *reverse) Len() int { return r.data.Len() }
|
|
107
|
+
|
|
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)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Swap swaps the elements with indexes i and j.
|
|
114
|
+
func (r *reverse) Swap(i, j int) { r.data.Swap(i, j) }
|
|
115
|
+
|
|
116
|
+
// Reverse returns the reverse order for data.
|
|
117
|
+
func Reverse(data Interface) Interface {
|
|
118
|
+
return &reverse{data: data}
|
|
119
|
+
}
|
|
120
|
+
|
|
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) {
|
|
129
|
+
return false
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return true
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Convenience types for common cases
|
|
136
|
+
|
|
137
|
+
// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
|
|
138
|
+
type IntSlice []int
|
|
139
|
+
|
|
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] }
|
|
143
|
+
|
|
144
|
+
// Sort is a convenience method: x.Sort() calls Sort(x).
|
|
145
|
+
func (x IntSlice) Sort() { Sort(x) }
|
|
146
|
+
|
|
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
|
|
150
|
+
|
|
151
|
+
func (x Float64Slice) Len() int { return len(x) }
|
|
152
|
+
|
|
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] }
|
|
161
|
+
|
|
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
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Sort is a convenience method: x.Sort() calls Sort(x).
|
|
168
|
+
func (x Float64Slice) Sort() { Sort(x) }
|
|
169
|
+
|
|
170
|
+
// StringSlice attaches the methods of Interface to []string, sorting in increasing order.
|
|
171
|
+
type StringSlice []string
|
|
172
|
+
|
|
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] }
|
|
176
|
+
|
|
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.
|
|
241
|
+
|
|
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())
|
|
252
|
+
}
|
|
253
|
+
|
|
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
|
|
365
|
+
} else {
|
|
366
|
+
j = h // preserves f(j) == true
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i.
|
|
370
|
+
return i
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Find uses binary search to find and return the smallest index i in [0, n)
|
|
374
|
+
// at which cmp(i) <= 0. If there is no such index i, Find returns i = n.
|
|
375
|
+
// The found result is true if i < n and cmp(i) == 0.
|
|
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
|
+
// }
|
|
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
|
|
401
|
+
i, j := 0, n
|
|
402
|
+
for i < j {
|
|
403
|
+
h := int(uint(i+j) >> 1) // avoid overflow when computing h
|
|
404
|
+
// i ≤ h < j
|
|
405
|
+
if cmp(h) > 0 {
|
|
406
|
+
i = h + 1 // preserves cmp(i-1) > 0
|
|
407
|
+
} else {
|
|
408
|
+
j = h // preserves cmp(j) <= 0
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
// i == j, cmp(i-1) > 0 and cmp(j) <= 0
|
|
412
|
+
return i, i < n && cmp(i) == 0
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Convenience wrappers for common cases.
|
|
416
|
+
|
|
417
|
+
// SearchInts searches for x in a sorted slice of ints and returns the index
|
|
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.
|
|
421
|
+
func SearchInts(a []int, x int) int {
|
|
422
|
+
return Search(len(a), func(i int) bool { return a[i] >= x })
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// SearchFloat64s searches for x in a sorted slice of float64s and returns the index
|
|
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.
|
|
429
|
+
func SearchFloat64s(a []float64, x float64) int {
|
|
430
|
+
return Search(len(a), func(i int) bool { return a[i] >= x })
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// SearchStrings searches for x in a sorted slice of strings and returns the index
|
|
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.
|
|
437
|
+
func SearchStrings(a []string, x string) int {
|
|
438
|
+
return Search(len(a), func(i int) bool { return a[i] >= x })
|
|
439
|
+
}
|
|
440
|
+
|
|
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
|
|
500
|
+
}
|