erigon-pulse/erigon-lib/common/cmp/cmp.go
Jason Yellick 5654ba07c9
Upgrade libp2p (enables go 1.21 support) (#8288)
Closes #8078 

This change is primarily intended to support go 1.21, but as a
side-effect requires updating libp2p, which in turn triggers an update
of golang.org/x/exp which creates quite a bit of (simple) churn in the
slice sorting.

This change introduces a new `cmp.Compare` function which can be used to
return an integer satisfying the compare interface for slice sorting.

In order to continue to support mplex for libp2p, the change references
github.com/libp2p/go-libp2p-mplex instead. Please see the PR at
https://github.com/libp2p/go-libp2p/pull/2498 for the official usptream
comment that indicates official support for mplex being moved to this
location.

Co-authored-by: Jason Yellick <jason@enya.ai>
2023-09-29 22:11:13 +02:00

58 lines
1.1 KiB
Go

/*
Copyright 2021 Erigon contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmp
import (
"golang.org/x/exp/constraints"
)
// InRange - ensure val is in [min,max] range
func InRange[T constraints.Ordered](min, max, val T) T {
if min >= val {
return min
}
if max <= val {
return max
}
return val
}
func Min[T constraints.Ordered](a, b T) T {
if a <= b {
return a
}
return b
}
func Max[T constraints.Ordered](a, b T) T {
if a >= b {
return a
}
return b
}
func Compare[T constraints.Ordered](a, b T) int {
switch {
case a < b:
return -1
case a == b:
return 0
default:
return 1
}
}