-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cmp package from go 1.21 to have cmp.Compare to make sort easier
- Loading branch information
Showing
10 changed files
with
88 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2023 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 cmp provides types and functions related to comparing | ||
// ordered values. | ||
package cmpex | ||
|
||
// Ordered is a constraint that permits any ordered type: any type | ||
// that supports the operators < <= >= >. | ||
// If future releases of Go add new ordered types, | ||
// this constraint will be modified to include them. | ||
// | ||
// Note that floating-point types may contain NaN ("not-a-number") values. | ||
// An operator such as == or < will always report false when | ||
// comparing a NaN value with any other value, NaN or not. | ||
// See the [Compare] function for a consistent way to compare NaN values. | ||
type Ordered interface { | ||
~int | ~int8 | ~int16 | ~int32 | ~int64 | | ||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | | ||
~float32 | ~float64 | | ||
~string | ||
} | ||
|
||
// Less reports whether x is less than y. | ||
// For floating-point types, a NaN is considered less than any non-NaN, | ||
// and -0.0 is not less than (is equal to) 0.0. | ||
func Less[T Ordered](x, y T) bool { | ||
return (isNaN(x) && !isNaN(y)) || x < y | ||
} | ||
|
||
// Compare returns | ||
// | ||
// -1 if x is less than y, | ||
// 0 if x equals y, | ||
// +1 if x is greater than y. | ||
// | ||
// For floating-point types, a NaN is considered less than any non-NaN, | ||
// a NaN is considered equal to a NaN, and -0.0 is equal to 0.0. | ||
func Compare[T Ordered](x, y T) int { | ||
xNaN := isNaN(x) | ||
yNaN := isNaN(y) | ||
if xNaN && yNaN { | ||
return 0 | ||
} | ||
if xNaN || x < y { | ||
return -1 | ||
} | ||
if yNaN || x > y { | ||
return +1 | ||
} | ||
return 0 | ||
} | ||
|
||
// isNaN reports whether x is a NaN without requiring the math package. | ||
// This will always return false if T is not floating-point. | ||
func isNaN[T Ordered](x T) bool { | ||
return x != x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters