forked from quic-go/quic-go
implement generic Min and Max functions
This commit is contained in:
@@ -4,108 +4,26 @@ import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/internal/protocol"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
// InfDuration is a duration of infinite length
|
||||
const InfDuration = time.Duration(math.MaxInt64)
|
||||
|
||||
// MaxInt returns the maximum of two Ints
|
||||
func MaxInt(a, b int) int {
|
||||
func Max[T constraints.Ordered](a, b T) T {
|
||||
if a < b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// MaxUint32 returns the maximum of two uint32
|
||||
func MaxUint32(a, b uint32) uint32 {
|
||||
if a < b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// MaxUint64 returns the maximum of two uint64
|
||||
func MaxUint64(a, b uint64) uint64 {
|
||||
if a < b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// MinUint64 returns the maximum of two uint64
|
||||
func MinUint64(a, b uint64) uint64 {
|
||||
func Min[T constraints.Ordered](a, b T) T {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MinInt returns the minimum of two Ints
|
||||
func MinInt(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MinUint32 returns the maximum of two uint32
|
||||
func MinUint32(a, b uint32) uint32 {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MinInt64 returns the minimum of two int64
|
||||
func MinInt64(a, b int64) int64 {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MaxInt64 returns the minimum of two int64
|
||||
func MaxInt64(a, b int64) int64 {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MinByteCount returns the minimum of two ByteCounts
|
||||
func MinByteCount(a, b protocol.ByteCount) protocol.ByteCount {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MaxByteCount returns the maximum of two ByteCounts
|
||||
func MaxByteCount(a, b protocol.ByteCount) protocol.ByteCount {
|
||||
if a < b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// MaxDuration returns the max duration
|
||||
func MaxDuration(a, b time.Duration) time.Duration {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MinDuration returns the minimum duration
|
||||
func MinDuration(a, b time.Duration) time.Duration {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// MinNonZeroDuration return the minimum duration that's not zero.
|
||||
func MinNonZeroDuration(a, b time.Duration) time.Duration {
|
||||
if a == 0 {
|
||||
@@ -114,7 +32,7 @@ func MinNonZeroDuration(a, b time.Duration) time.Duration {
|
||||
if b == 0 {
|
||||
return a
|
||||
}
|
||||
return MinDuration(a, b)
|
||||
return Min(a, b)
|
||||
}
|
||||
|
||||
// AbsDuration returns the absolute value of a time duration
|
||||
@@ -152,19 +70,3 @@ func MaxTime(a, b time.Time) time.Time {
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MaxPacketNumber returns the max packet number
|
||||
func MaxPacketNumber(a, b protocol.PacketNumber) protocol.PacketNumber {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MinPacketNumber returns the min packet number
|
||||
func MinPacketNumber(a, b protocol.PacketNumber) protocol.PacketNumber {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user