Debian Codegraph @

slices/delegated_sort.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package slices
2
3 import (
4 "golang.org/x/exp/constraints"
5 "golang.org/x/exp/slices"
6 )
7
8 // This file delegates to the official slices package, so that we end up with a superset of the official API.
9
10 // Sort sorts a slice of any ordered type in ascending order.
11 func Sort[E constraints.Ordered](x []E) {
12 slices.Sort(x)
13 }
14
15 // Sort sorts the slice x in ascending order as determined by the less function.
16 // This sort is not guaranteed to be stable.
17 func SortFunc[E any](x []E, less func(a, b E) bool) {
18 slices.SortFunc(x, less)
19 }
20
21 // SortStable sorts the slice x while keeping the original order of equal
22 // elements, using less to compare elements.
23 func SortStableFunc[E any](x []E, less func(a, b E) bool) {
24 slices.SortStableFunc(x, less)
25 }
26
27 // IsSorted reports whether x is sorted in ascending order.
28 func IsSorted[E constraints.Ordered](x []E) bool {
29 return slices.IsSorted(x)
30 }
31
32 // IsSortedFunc reports whether x is sorted in ascending order, with less as the
33 // comparison function.
34 func IsSortedFunc[E any](x []E, less func(a, b E) bool) bool {
35 return slices.IsSortedFunc(x, less)
36 }
37
38 // BinarySearch searches for target in a sorted slice and returns the smallest
39 // index at which target is found. If the target is not found, the index at
40 // which it could be inserted into the slice is returned; therefore, if the
41 // intention is to find target itself a separate check for equality with the
42 // element at the returned index is required.
43 func BinarySearch[E constraints.Ordered](x []E, target E) int {
44 return slices.BinarySearch(x, target)
45 }
46
47 // BinarySearchFunc uses binary search to find and return the smallest index i
48 // in [0, n) at which ok(i) is true, assuming that on the range [0, n),
49 // ok(i) == true implies ok(i+1) == true. That is, BinarySearchFunc requires
50 // that ok is false for some (possibly empty) prefix of the input range [0, n)
51 // and then true for the (possibly empty) remainder; BinarySearchFunc returns
52 // the first true index. If there is no such index, BinarySearchFunc returns n.
53 // (Note that the "not found" return value is not -1 as in, for instance,
54 // strings.Index.) Search calls ok(i) only for i in the range [0, n).
55 func BinarySearchFunc[E any](x []E, ok func(E) bool) int {
56 return slices.BinarySearchFunc(x, ok)
57 }