Skip to content

Commit

Permalink
Use cmp package added in Go 1.21 (#4871)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwt authored Nov 9, 2023
1 parent 2a2d359 commit 7604371
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 55 deletions.
47 changes: 7 additions & 40 deletions runtime/expr/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package expr

import (
"bytes"
"cmp"
"fmt"
"math"
"slices"
Expand Down Expand Up @@ -326,24 +327,12 @@ func LookupCompare(typ zed.Type) comparefn {

case zed.IDInt16, zed.IDInt32, zed.IDInt64:
return func(a, b zcode.Bytes) int {
va, vb := zed.DecodeInt(a), zed.DecodeInt(b)
if va < vb {
return -1
} else if va > vb {
return 1
}
return 0
return cmp.Compare(zed.DecodeInt(a), zed.DecodeInt(b))
}

case zed.IDUint16, zed.IDUint32, zed.IDUint64:
return func(a, b zcode.Bytes) int {
va, vb := zed.DecodeUint(a), zed.DecodeUint(b)
if va < vb {
return -1
} else if va > vb {
return 1
}
return 0
return cmp.Compare(zed.DecodeUint(a), zed.DecodeUint(b))
}

case zed.IDFloat16, zed.IDFloat32, zed.IDFloat64:
Expand All @@ -352,41 +341,19 @@ func LookupCompare(typ zed.Type) comparefn {
aNaN, bNaN := math.IsNaN(va), math.IsNaN(vb)
if aNaN && bNaN {
// Order different NaNs so ZNG sets have a canonical form.
aBits, bBits := math.Float64bits(va), math.Float64bits(vb)
if aBits < bBits {
return -1
} else if aBits > bBits {
return 1
}
return 0
} else if aNaN || va < vb {
return -1
} else if bNaN || va > vb {
return 1
cmp.Compare(math.Float64bits(va), math.Float64bits(vb))
}
return 0
return cmp.Compare(va, vb)
}

case zed.IDTime:
return func(a, b zcode.Bytes) int {
va, vb := zed.DecodeTime(a), zed.DecodeTime(b)
if va < vb {
return -1
} else if va > vb {
return 1
}
return 0
return cmp.Compare(zed.DecodeTime(a), zed.DecodeTime(b))
}

case zed.IDDuration:
return func(a, b zcode.Bytes) int {
va, vb := zed.DecodeDuration(a), zed.DecodeDuration(b)
if va < vb {
return -1
} else if va > vb {
return 1
}
return 0
return cmp.Compare(zed.DecodeDuration(a), zed.DecodeDuration(b))
}

case zed.IDIP:
Expand Down
22 changes: 7 additions & 15 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package zed

import (
"cmp"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -411,17 +412,17 @@ func CompareTypes(a, b Type) int {
// a == b
return 0
}
if cmp := compareInts(int(a.Kind()), int(b.Kind())); cmp != 0 {
if cmp := cmp.Compare(a.Kind(), b.Kind()); cmp != 0 {
return cmp
}
switch a.Kind() {
case PrimitiveKind:
return compareInts(aID, bID)
return cmp.Compare(aID, bID)
case RecordKind:
ra, rb := TypeRecordOf(a), TypeRecordOf(b)
// First compare number of fields.
if len(ra.Fields) != len(rb.Fields) {
return compareInts(len(ra.Fields), len(rb.Fields))
if cmp := cmp.Compare(len(ra.Fields), len(rb.Fields)); cmp != 0 {
return cmp
}
// Second compare field names.
for i := 0; i < len(ra.Fields); i++ {
Expand All @@ -447,7 +448,7 @@ func CompareTypes(a, b Type) int {
return CompareTypes(ma.ValType, mb.ValType)
case UnionKind:
ua, ub := a.(*TypeUnion), b.(*TypeUnion)
if cmp := compareInts(len(ua.Types), len(ub.Types)); cmp != 0 {
if cmp := cmp.Compare(len(ua.Types), len(ub.Types)); cmp != 0 {
return cmp
}
for i := 0; i < len(ua.Types); i++ {
Expand All @@ -458,7 +459,7 @@ func CompareTypes(a, b Type) int {
return 0
case EnumKind:
ea, eb := a.(*TypeEnum), b.(*TypeEnum)
if cmp := compareInts(len(ea.Symbols), len(eb.Symbols)); cmp != 0 {
if cmp := cmp.Compare(len(ea.Symbols), len(eb.Symbols)); cmp != 0 {
return cmp
}
for i := 0; i < len(ea.Symbols); i++ {
Expand All @@ -474,15 +475,6 @@ func CompareTypes(a, b Type) int {
return 0
}

func compareInts(a, b int) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}

type TypeOfType struct{}

func (t *TypeOfType) ID() int {
Expand Down

0 comments on commit 7604371

Please sign in to comment.