Skip to content

Commit

Permalink
Use stable sort on various slices to make data model and zap outputs …
Browse files Browse the repository at this point in the history
…more deterministic
  • Loading branch information
hasty committed Sep 19, 2024
1 parent 9626107 commit fe1d017
Show file tree
Hide file tree
Showing 16 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion compare/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Entities(spec *spec.Specification, specEntities map[string][]types.Entity,
for _, path := range missingZapEntities {
slog.Warn("missing from spec entities", slog.String("path", path))
}
slices.SortFunc(diffs, func(a, b *ClusterDifferences) int {
slices.SortStableFunc(diffs, func(a, b *ClusterDifferences) int {
return strings.Compare(a.Name, b.Name)
})
return
Expand Down
2 changes: 1 addition & 1 deletion dm/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func renderAttributes(doc *spec.Doc, cluster *matter.Cluster, c *etree.Element)
}
as := make([]*matter.Field, len(cluster.Attributes))
copy(as, cluster.Attributes)
slices.SortFunc(as, func(a, b *matter.Field) int {
slices.SortStableFunc(as, func(a, b *matter.Field) int {
return a.ID.Compare(b.ID)
})
attributes := c.CreateElement("attributes")
Expand Down
2 changes: 1 addition & 1 deletion dm/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func renderBitmaps(doc *spec.Doc, cluster *matter.Cluster, dt *etree.Element) (err error) {
bitmaps := make([]*matter.Bitmap, len(cluster.Bitmaps))
copy(bitmaps, cluster.Bitmaps)
slices.SortFunc(bitmaps, func(a, b *matter.Bitmap) int {
slices.SortStableFunc(bitmaps, func(a, b *matter.Bitmap) int {
return strings.Compare(a.Name, b.Name)
})
for _, bm := range bitmaps {
Expand Down
2 changes: 1 addition & 1 deletion dm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func renderCommands(doc *spec.Doc, cluster *matter.Cluster, c *etree.Element) (e
cmds = append(cmds, c)
}

slices.SortFunc(cmds, func(a, b *matter.Command) int {
slices.SortStableFunc(cmds, func(a, b *matter.Command) int {
if a.ID.Equals(b.ID) {
if a.Direction == b.Direction {
return 0
Expand Down
2 changes: 1 addition & 1 deletion dm/devicetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func renderDeviceType(doc *spec.Doc, deviceTypes []*matter.DeviceType) (output s
cx := c.CreateElement("clusters")
reqs := make([]*matter.ClusterRequirement, len(deviceType.ClusterRequirements))
copy(reqs, deviceType.ClusterRequirements)
slices.SortFunc(reqs, func(a, b *matter.ClusterRequirement) int {
slices.SortStableFunc(reqs, func(a, b *matter.ClusterRequirement) int {
cmp := a.ID.Compare(b.ID)
if cmp != 0 {
return cmp
Expand Down
2 changes: 1 addition & 1 deletion dm/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func renderEnums(doc *spec.Doc, cluster *matter.Cluster, dt *etree.Element) (err error) {
enums := make([]*matter.Enum, len(cluster.Enums))
copy(enums, cluster.Enums)
slices.SortFunc(enums, func(a, b *matter.Enum) int {
slices.SortStableFunc(enums, func(a, b *matter.Enum) int {
return strings.Compare(a.Name, b.Name)
})
for _, e := range enums {
Expand Down
2 changes: 1 addition & 1 deletion dm/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func renderEvents(doc *spec.Doc, cluster *matter.Cluster, c *etree.Element) (err
evs = append(evs, e)
}

slices.SortFunc(evs, func(a, b *matter.Event) int {
slices.SortStableFunc(evs, func(a, b *matter.Event) int {
return a.ID.Compare(b.ID)
})
events := c.CreateElement("events")
Expand Down
2 changes: 1 addition & 1 deletion dm/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func renderStructs(doc *spec.Doc, cluster *matter.Cluster, dt *etree.Element) (err error) {
structs := make([]*matter.Struct, len(cluster.Structs))
copy(structs, cluster.Structs)
slices.SortFunc(structs, func(a, b *matter.Struct) int {
slices.SortStableFunc(structs, func(a, b *matter.Struct) int {
return strings.Compare(a.Name, b.Name)
})
for _, s := range structs {
Expand Down
2 changes: 1 addition & 1 deletion internal/pipeline/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewData[T any](path string, content T) *Data[T] {
}

func SortData[T any](data []*Data[T]) {
slices.SortFunc[[]*Data[T], *Data[T]](data, func(a *Data[T], b *Data[T]) int {
slices.SortStableFunc[[]*Data[T], *Data[T]](data, func(a *Data[T], b *Data[T]) int {
return strings.Compare(a.Path, b.Path)
})
}
Expand Down
2 changes: 1 addition & 1 deletion matter/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (bm *Bitmap) Inherit(parent *Bitmap) error {
if len(bm.Description) == 0 {
bm.Description = parent.Description
}
slices.SortFunc(mergedBits, func(a, b Bit) int {
slices.SortStableFunc(mergedBits, func(a, b Bit) int {
return strings.Compare(a.Bit(), b.Bit())
})
bm.Bits = mergedBits
Expand Down
2 changes: 1 addition & 1 deletion matter/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (e *Enum) Inherit(parent *Enum) error {
if len(e.Description) == 0 {
e.Description = parent.Description
}
slices.SortFunc(mergedValues, func(a, b *EnumValue) int {
slices.SortStableFunc(mergedValues, func(a, b *EnumValue) int {
return a.Value.Compare(b.Value)
})
e.Values = mergedValues
Expand Down
2 changes: 1 addition & 1 deletion matter/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func ContainsNumber(s []*Number, n *Number) bool {
}

func SortNumbers(s []*Number) {
slices.SortFunc[[]*Number, *Number](s, func(a *Number, b *Number) int {
slices.SortStableFunc[[]*Number, *Number](s, func(a *Number, b *Number) int {
av := a.Value()
bv := b.Value()
if av < bv {
Expand Down
4 changes: 2 additions & 2 deletions tests/tools/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
cc.elapsed += count.elapsed
}

slices.SortFunc(rcs, func(a, b *ruleCollection) int {
slices.SortStableFunc(rcs, func(a, b *ruleCollection) int {
if a.elapsed < b.elapsed {
return -1
} else if a.elapsed > b.elapsed {
Expand All @@ -73,7 +73,7 @@ func main() {

}
fmt.Printf("\tPaths: %d: Count: %d Elapsed: %s (avg. %s\n", len(rc.calls), totalCount, totalElapsed.String(), (totalElapsed / time.Duration(totalCount)).String())
slices.SortFunc(calls, func(a, b *callCount) int {
slices.SortStableFunc(calls, func(a, b *callCount) int {
if a.elapsed < b.elapsed {
return 1
} else if a.elapsed > b.elapsed {
Expand Down
2 changes: 1 addition & 1 deletion zap/generate/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func generateCommands(commands map[*matter.Command][]*matter.Number, docPath str
for command := range commands {
remainingCommands = append(remainingCommands, command)
}
slices.SortFunc(remainingCommands, func(a, b *matter.Command) int { return strings.Compare(a.Name, b.Name) })
slices.SortStableFunc(remainingCommands, func(a, b *matter.Command) int { return strings.Compare(a.Name, b.Name) })

for _, command := range remainingCommands {

Expand Down
2 changes: 1 addition & 1 deletion zap/generate/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func generateEnums(enums map[*matter.Enum][]*matter.Number, sourcePath string, c
return
}

slices.SortFunc(remainingEnums, func(a, b *matter.Enum) int { return strings.Compare(a.Name, b.Name) })
slices.SortStableFunc(remainingEnums, func(a, b *matter.Enum) int { return strings.Compare(a.Name, b.Name) })

for _, en := range remainingEnums {
bme := etree.NewElement("enum")
Expand Down
2 changes: 1 addition & 1 deletion zap/generate/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func generateStructs(structs map[*matter.Struct][]*matter.Number, docPath string
remainingStructs = append(remainingStructs, s)
}

slices.SortFunc(remainingStructs, func(a, b *matter.Struct) int {
slices.SortStableFunc(remainingStructs, func(a, b *matter.Struct) int {
return strings.Compare(a.Name, b.Name)
})

Expand Down

0 comments on commit fe1d017

Please sign in to comment.