1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
// Package xsort contains extensions to the standard library package sort.
package xsort
import (
"sort"
"github.com/bradenaw/juniper/internal/heap"
"github.com/bradenaw/juniper/iterator"
"github.com/bradenaw/juniper/xslices"
)
// Returns true if a is less than b. Must follow the same rules as sort.Interface.Less.
type Less[T any] func(a, b T) bool
// Compile-time assert the types match.
var _ Less[int] = OrderedLess[int]
// Greater returns true if a > b according to less.
func Greater[T any](less Less[T], a T, b T) bool {
return less(b, a)
}
// LessOrEqual returns true if a <= b according to less.
func LessOrEqual[T any](less Less[T], a T, b T) bool {
// a <= b
// !(a > b)
// !(b < a)
return !less(b, a)
}
// LessOrEqual returns true if a >= b according to less.
func GreaterOrEqual[T any](less Less[T], a T, b T) bool {
// a >= b
// !(a < b)
return !less(a, b)
}
// Equal returns true if a == b according to less.
func Equal[T any](less Less[T], a T, b T) bool {
return !less(a, b) && !less(b, a)
}
// Reverse returns a Less that orders elements in the opposite order of the provided less.
func Reverse[T any](less Less[T]) Less[T] {
return func(a, b T) bool {
return less(b, a)
}
}
// Slice sorts x in-place using the given less function to compare items.
//
// Follows the same rules as sort.Slice.
//
// Deprecated: slices.SortFunc is in the standard library as of Go 1.21.
func Slice[T any](x []T, less Less[T]) {
sort.Slice(x, func(i, j int) bool {
return less(x[i], x[j])
})
}
// SliceStable stably sorts x in-place using the given less function to compare items.
//
// Follows the same rules as sort.SliceStable.
//
// Deprecated: slices.SortStableFunc is in the standard library as of Go 1.21.
func SliceStable[T any](x []T, less Less[T]) {
sort.SliceStable(x, func(i, j int) bool {
return less(x[i], x[j])
})
}
// SliceIsSorted returns true if x is in sorted order according to the given less function.
//
// Follows the same rules as sort.SliceIsSorted.
//
// Deprecated: slices.IsSortedFunc is in the standard library as of Go 1.21.
func SliceIsSorted[T any](x []T, less Less[T]) bool {
return sort.SliceIsSorted(x, func(i, j int) bool {
return less(x[i], x[j])
})
}
// Search searches for item in x, assumed sorted according to less, and returns the index. The
// return value is the index to insert item at if it is not present (it could be len(a)).
//
// Deprecated: slices.BinarySearchFunc is in the standard library as of Go 1.21.
func Search[T any](x []T, less Less[T], item T) int {
return sort.Search(len(x), func(i int) bool {
return less(item, x[i]) || !less(x[i], item)
})
}
type valueAndSource[T any] struct {
value T
source int
}
type mergeIterator[T any] struct {
in []iterator.Iterator[T]
h heap.Heap[valueAndSource[T]]
}
func (iter *mergeIterator[T]) Next() (T, bool) {
if iter.h.Len() == 0 {
var zero T
return zero, false
}
item := iter.h.Pop()
nextItem, ok := iter.in[item.source].Next()
if ok {
iter.h.Push(valueAndSource[T]{nextItem, item.source})
}
return item.value, true
}
// Merge returns an iterator that yields all items from in in sorted order.
//
// The results are undefined if the in iterators do not yield items in sorted order according to
// less.
//
// The time complexity of Next() is O(log(k)) where k is len(in).
func Merge[T any](less Less[T], in ...iterator.Iterator[T]) iterator.Iterator[T] {
initial := make([]valueAndSource[T], 0, len(in))
for i := range in {
item, ok := in[i].Next()
if !ok {
continue
}
initial = append(initial, valueAndSource[T]{item, i})
}
h := heap.New(
func(a, b valueAndSource[T]) bool {
return less(a.value, b.value)
},
func(a valueAndSource[T], i int) {},
initial,
)
return &mergeIterator[T]{
in: in,
h: h,
}
}
// MergeSlices merges the already-sorted slices of in. Optionally, a pre-allocated out slice can be
// provided to store the result into.
//
// The results are undefined if the in slices are not already sorted.
//
// The time complexity is O(n * log(k)) where n is the total number of items and k is len(in).
func MergeSlices[T any](less Less[T], out []T, in ...[]T) []T {
n := 0
for i := range in {
n += len(in[i])
}
out = xslices.Grow(out[:0], n)
iter := Merge(less, xslices.Map(in, iterator.Slice[T])...)
for {
item, ok := iter.Next()
if !ok {
break
}
out = append(out, item)
}
return out
}
// MinK returns the k minimum items according to less from iter in sorted order. If iter yields
// fewer than k items, MinK returns all of them.
func MinK[T any](less Less[T], iter iterator.Iterator[T], k int) []T {
h := heap.New[T](heap.Less[T](Reverse(less)), func(a T, i int) {}, nil)
for {
item, ok := iter.Next()
if !ok {
break
}
h.Push(item)
if h.Len() > k {
h.Pop()
}
}
out := make([]T, h.Len())
for i := len(out) - 1; i >= 0; i-- {
out[i] = h.Pop()
}
return out
}
|