golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/slices/cmp.go (about) 1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package slices 6 7 import "golang.org/x/exp/constraints" 8 9 // min is a version of the predeclared function from the Go 1.21 release. 10 func min[T constraints.Ordered](a, b T) T { 11 if a < b || isNaN(a) { 12 return a 13 } 14 return b 15 } 16 17 // max is a version of the predeclared function from the Go 1.21 release. 18 func max[T constraints.Ordered](a, b T) T { 19 if a > b || isNaN(a) { 20 return a 21 } 22 return b 23 } 24 25 // cmpLess is a copy of cmp.Less from the Go 1.21 release. 26 func cmpLess[T constraints.Ordered](x, y T) bool { 27 return (isNaN(x) && !isNaN(y)) || x < y 28 } 29 30 // cmpCompare is a copy of cmp.Compare from the Go 1.21 release. 31 func cmpCompare[T constraints.Ordered](x, y T) int { 32 xNaN := isNaN(x) 33 yNaN := isNaN(y) 34 if xNaN && yNaN { 35 return 0 36 } 37 if xNaN || x < y { 38 return -1 39 } 40 if yNaN || x > y { 41 return +1 42 } 43 return 0 44 }