1
1
mirror of https://github.com/wader/fq.git synced 2024-11-22 07:16:49 +03:00

generics: Use more from x/exp

This commit is contained in:
Mattias Wadman 2022-08-31 10:34:32 +02:00
parent e14cb49ba9
commit 226a9a3e08
13 changed files with 28 additions and 48 deletions

View File

@ -163,7 +163,10 @@
"tojvalue",
"torepr",
"tovalue",
"toxml",
"toxmlentities",
"traf",
"trak",
"trex",
"trun",
"TSSE",
@ -196,4 +199,4 @@
"files.trimTrailingWhitespace": true
},
"fracturedjsonvsc.MaxInlineLength": 160,
}
}

View File

@ -467,7 +467,7 @@ func frameDecode(d *decode.D, in any) any {
for j := 0; j < count; j++ {
high := d.Unary(0)
low := d.U(riceParameter)
samples[n] = mathex.ZigZag(high<<riceParameter | low)
samples[n] = mathex.ZigZag[uint64, int64](high<<riceParameter | low)
n++
}
samplesStop := d.Pos()

View File

@ -20,9 +20,9 @@ import (
"fmt"
"github.com/wader/fq/format"
"github.com/wader/fq/internal/sortex"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"golang.org/x/exp/slices"
)
//go:embed mp4.jq
@ -210,7 +210,7 @@ func mp4Tracks(d *decode.D, ctx *decodeContext) {
for _, t := range ctx.tracks {
sortedTracks = append(sortedTracks, t)
}
sortex.Slice(sortedTracks, func(a, b *track) bool { return a.id < b.id })
slices.SortFunc(sortedTracks, func(a, b *track) bool { return a.id < b.id })
d.FieldArray("tracks", func(d *decode.D) {
for _, t := range sortedTracks {

View File

@ -49,7 +49,7 @@ func uEV(d *decode.D) uint64 { return expGolomb(d) }
func sEV(d *decode.D) int64 {
v := expGolomb(d) + 1
return mathex.ZigZag(v) - -int64(v&1)
return mathex.ZigZag[uint64, int64](v) - -int64(v&1)
}
const (

View File

@ -70,7 +70,7 @@ func protobufDecodeField(d *decode.D, pbm *format.ProtoBufMessage) {
switch pbf.Type {
case format.ProtoBufTypeInt32, format.ProtoBufTypeInt64:
v := mathex.ZigZag(value)
v := mathex.ZigZag[uint64, int64](value)
d.FieldValueS("value", v)
if len(pbf.Enums) > 0 {
d.FieldValueStr("enum", pbf.Enums[uint64(v)])

View File

@ -25,6 +25,7 @@ import (
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
"golang.org/x/exp/slices"
)
//go:embed xml.jq
@ -396,7 +397,7 @@ func toXMLFromObject(c any, opts ToXMLOpts) any {
sortex.ProxySort(orderNames, n.Nodes, func(a, b string) bool { return a < b })
}
sortex.Slice(n.Attrs, func(a, b xml.Attr) bool { return xmlNameSort(a.Name, b.Name) })
slices.SortFunc(n.Attrs, func(a, b xml.Attr) bool { return xmlNameSort(a.Name, b.Name) })
return n, seq, hasSeq
}
@ -469,7 +470,7 @@ func toXMLFromArray(c any, opts ToXMLOpts) any {
}
}
sortex.Slice(n.Attrs, func(a, b xml.Attr) bool { return xmlNameSort(a.Name, b.Name) })
slices.SortFunc(n.Attrs, func(a, b xml.Attr) bool { return xmlNameSort(a.Name, b.Name) })
for _, c := range children {
c, ok := c.([]any)

View File

@ -37,7 +37,7 @@ import (
"strconv"
"unicode/utf8"
"github.com/wader/fq/internal/sortex"
"golang.org/x/exp/slices"
)
type Colors struct {
@ -239,7 +239,7 @@ func (e *Encoder) encodeMap(vs map[string]any) {
kvs[i] = keyVal{k, v}
i++
}
sortex.Slice(kvs, func(a, b keyVal) bool { return a.key < b.key })
slices.SortFunc(kvs, func(a, b keyVal) bool { return a.key < b.key })
for i, kv := range kvs {
if e.wErr != nil {
return

View File

@ -12,10 +12,6 @@ import (
"github.com/wader/fq/pkg/ranges"
)
type Number interface {
constraints.Integer | constraints.Float
}
var BasePrefixMap = map[int]string{
2: "0b",
8: "0o",
@ -59,7 +55,7 @@ func PadFormatBigInt(i *big.Int, base int, basePrefix bool, width int) string {
return padFormatNumber(i.Text(base), base, basePrefix, width)
}
func Max[T Number](v T, vs ...T) T {
func Max[T constraints.Ordered](v T, vs ...T) T {
m := v
for _, v := range vs {
if v > m {
@ -69,7 +65,7 @@ func Max[T Number](v T, vs ...T) T {
return m
}
func Min[T Number](v T, vs ...T) T {
func Min[T constraints.Ordered](v T, vs ...T) T {
m := v
for _, v := range vs {
if v < m {
@ -79,7 +75,7 @@ func Min[T Number](v T, vs ...T) T {
return m
}
func Clamp[T Number](min, max, v T) T {
func Clamp[T constraints.Ordered](min, max, v T) T {
return Max(min, Min(max, v))
}
@ -111,6 +107,6 @@ func TwosComplement(nBits int, n uint64) int64 {
// decode zigzag encoded integer
// https://developers.google.com/protocol-buffers/docs/encoding
func ZigZag(n uint64) int64 {
return int64(n>>1 ^ -(n & 1))
func ZigZag[U constraints.Unsigned, S constraints.Signed](n U) S {
return S(n>>1 ^ -(n & 1))
}

View File

@ -14,9 +14,9 @@ import (
"strings"
"github.com/wader/fq/internal/shquote"
"github.com/wader/fq/internal/sortex"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/interp"
"golang.org/x/exp/slices"
)
var unescapeRe = regexp.MustCompile(`\\(?:t|b|n|r|0(?:b[01]{8}|x[0-f]{2}))`)
@ -273,7 +273,7 @@ type Case struct {
func (c *Case) ToActual() string {
var partsLineSorted []part
partsLineSorted = append(partsLineSorted, c.Parts...)
sortex.Slice(partsLineSorted, func(a, b part) bool { return a.Line() < b.Line() })
slices.SortFunc(partsLineSorted, func(a, b part) bool { return a.Line() < b.Line() })
sb := &strings.Builder{}
for _, p := range partsLineSorted {

View File

@ -1,13 +0,0 @@
package sortex
import "sort"
// Slice same as sort.Slice but type safe and also indexes for you
func Slice[T any](s []T, less func(a, b T) bool) {
sort.Slice(s, func(i, j int) bool { return less(s[i], s[j]) })
}
// SliceStable same as sort.SliceStable but type safe and also indexes for you
func SliceStable[T any](s []T, less func(a, b T) bool) {
sort.SliceStable(s, func(i, j int) bool { return less(s[i], s[j]) })
}

View File

@ -3,10 +3,10 @@ package decode
import (
"errors"
"github.com/wader/fq/internal/sortex"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/ranges"
"github.com/wader/fq/pkg/scalar"
"golang.org/x/exp/slices"
)
type Compound struct {
@ -200,7 +200,7 @@ func (v *Value) postProcess() {
// TODO: really sort array? if sort it needs to be stable to keep the order
// of value with same range start, think null values etc
if vv.RangeSorted {
sortex.SliceStable(vv.Children, func(a, b *Value) bool { return a.Range.Start < b.Range.Start })
slices.SortFunc(vv.Children, func(a, b *Value) bool { return a.Range.Start < b.Range.Start })
}
v.Index = -1

View File

@ -7,8 +7,8 @@ import (
"sync"
"github.com/wader/fq/internal/gojqex"
"github.com/wader/fq/internal/sortex"
"github.com/wader/fq/pkg/decode"
"golang.org/x/exp/slices"
)
type EnvFuncFn func(env *Interp) gojqex.Function
@ -67,7 +67,7 @@ func (r *Registry) Func(funcFn EnvFuncFn) {
}
func sortFormats(g decode.Group) {
sortex.Slice(g, func(a, b decode.Format) bool {
slices.SortFunc(g, func(a, b decode.Format) bool {
if a.ProbeOrder == b.ProbeOrder {
return a.Name < b.Name
}

View File

@ -2,9 +2,10 @@ package ranges
import (
"fmt"
"sort"
"strconv"
"strings"
"golang.org/x/exp/slices"
)
func max(a, b int64) int64 {
@ -32,14 +33,6 @@ func (r Range) String() string { return fmt.Sprintf("%d:%d", r.Start, r.Len) }
func (r Range) IsZero() bool { return r.Start == 0 && r.Len == 0 }
type rangeStartSort []Range
func (p rangeStartSort) Len() int { return len(p) }
func (p rangeStartSort) Less(i, j int) bool { return p[i].Start < p[j].Start }
func (p rangeStartSort) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func RangeFromString(s string) Range {
ps := strings.Split(s, ":")
start, _ := strconv.Atoi(ps[0])
@ -70,7 +63,7 @@ func Gaps(total Range, ranges []Range) []Range {
return []Range{total}
}
sort.Sort(rangeStartSort(ranges))
slices.SortFunc(ranges, func(a, b Range) bool { return a.Start < b.Start })
// worst case ranges+1 gaps
merged := make([]Range, 0, len(ranges)+1)