// Copyright 2018 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.
// Package fmtsort provides a general stable ordering mechanism// for maps, on behalf of the fmt and text/template packages.// It is not guaranteed to be efficient and works only for types// that are valid map keys.
package fmtsortimport ()// Note: Throughout this package we avoid calling reflect.Value.Interface as// it is not always legal to do so and it's easier to avoid the issue than to face it.// SortedMap is a slice of KeyValue pairs that simplifies sorting// and iterating over map entries.//// Each KeyValue pair contains a map key and its corresponding value.typeSortedMap []KeyValue// KeyValue holds a single key and value pair found in a map.typeKeyValuestruct { Key, Value reflect.Value}// Sort accepts a map and returns a SortedMap that has the same keys and// values but in a stable sorted order according to the keys, modulo issues// raised by unorderable key values such as NaNs.//// The ordering rules are more general than with Go's < operator://// - when applicable, nil compares low// - ints, floats, and strings order by <// - NaN compares less than non-NaN floats// - bool compares false before true// - complex compares real, then imag// - pointers compare by machine address// - channel values compare by machine address// - structs compare each field in turn// - arrays compare each element in turn.// Otherwise identical arrays compare by length.// - interface values compare first by reflect.Type describing the concrete type// and then by concrete value as described in the previous rules.func ( reflect.Value) SortedMap {if .Type().Kind() != reflect.Map {returnnil }// Note: this code is arranged to not panic even in the presence // of a concurrent map update. The runtime is responsible for // yelling loudly if that happens. See issue 33275. := .Len() := make(SortedMap, 0, ) := .MapRange()for .Next() { = append(, KeyValue{.Key(), .Value()}) }slices.SortStableFunc(, func(, KeyValue) int {returncompare(.Key, .Key) })return}// compare compares two values of the same type. It returns -1, 0, 1// according to whether a > b (1), a == b (0), or a < b (-1).// If the types differ, it returns -1.// See the comment on Sort for the comparison rules.func compare(, reflect.Value) int { , := .Type(), .Type()if != {return -1// No good answer possible, but don't return 0: they're not equal. }switch .Kind() {casereflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:returncmp.Compare(.Int(), .Int())casereflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:returncmp.Compare(.Uint(), .Uint())casereflect.String:returncmp.Compare(.String(), .String())casereflect.Float32, reflect.Float64:returncmp.Compare(.Float(), .Float())casereflect.Complex64, reflect.Complex128: , := .Complex(), .Complex()if := cmp.Compare(real(), real()); != 0 {return }returncmp.Compare(imag(), imag())casereflect.Bool: , := .Bool(), .Bool()switch {case == :return0case :return1default:return -1 }casereflect.Pointer, reflect.UnsafePointer:returncmp.Compare(.Pointer(), .Pointer())casereflect.Chan:if , := nilCompare(, ); {return }returncmp.Compare(.Pointer(), .Pointer())casereflect.Struct:for := 0; < .NumField(); ++ {if := (.Field(), .Field()); != 0 {return } }return0casereflect.Array:for := 0; < .Len(); ++ {if := (.Index(), .Index()); != 0 {return } }return0casereflect.Interface:if , := nilCompare(, ); {return } := (reflect.ValueOf(.Elem().Type()), reflect.ValueOf(.Elem().Type()))if != 0 {return }return (.Elem(), .Elem())default:// Certain types cannot appear as keys (maps, funcs, slices), but be explicit.panic("bad type in compare: " + .String()) }}// nilCompare checks whether either value is nil. If not, the boolean is false.// If either value is nil, the boolean is true and the integer is the comparison// value. The comparison is defined to be 0 if both are nil, otherwise the one// nil value compares low. Both arguments must represent a chan, func,// interface, map, pointer, or slice.func nilCompare(, reflect.Value) (int, bool) {if .IsNil() {if .IsNil() {return0, true }return -1, true }if .IsNil() {return1, true }return0, false}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.