github.com/psiphon-labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/quic/gquic-go/internal/utils/minmax.go (about)

     1  package utils
     2  
     3  import (
     4  	"math"
     5  	"time"
     6  
     7  	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/quic/gquic-go/internal/protocol"
     8  )
     9  
    10  // InfDuration is a duration of infinite length
    11  const InfDuration = time.Duration(math.MaxInt64)
    12  
    13  // Max returns the maximum of two Ints
    14  func Max(a, b int) int {
    15  	if a < b {
    16  		return b
    17  	}
    18  	return a
    19  }
    20  
    21  // MaxUint32 returns the maximum of two uint32
    22  func MaxUint32(a, b uint32) uint32 {
    23  	if a < b {
    24  		return b
    25  	}
    26  	return a
    27  }
    28  
    29  // MaxUint64 returns the maximum of two uint64
    30  func MaxUint64(a, b uint64) uint64 {
    31  	if a < b {
    32  		return b
    33  	}
    34  	return a
    35  }
    36  
    37  // MinUint64 returns the maximum of two uint64
    38  func MinUint64(a, b uint64) uint64 {
    39  	if a < b {
    40  		return a
    41  	}
    42  	return b
    43  }
    44  
    45  // Min returns the minimum of two Ints
    46  func Min(a, b int) int {
    47  	if a < b {
    48  		return a
    49  	}
    50  	return b
    51  }
    52  
    53  // MinUint32 returns the maximum of two uint32
    54  func MinUint32(a, b uint32) uint32 {
    55  	if a < b {
    56  		return a
    57  	}
    58  	return b
    59  }
    60  
    61  // MinInt64 returns the minimum of two int64
    62  func MinInt64(a, b int64) int64 {
    63  	if a < b {
    64  		return a
    65  	}
    66  	return b
    67  }
    68  
    69  // MaxInt64 returns the minimum of two int64
    70  func MaxInt64(a, b int64) int64 {
    71  	if a > b {
    72  		return a
    73  	}
    74  	return b
    75  }
    76  
    77  // MinByteCount returns the minimum of two ByteCounts
    78  func MinByteCount(a, b protocol.ByteCount) protocol.ByteCount {
    79  	if a < b {
    80  		return a
    81  	}
    82  	return b
    83  }
    84  
    85  // MaxByteCount returns the maximum of two ByteCounts
    86  func MaxByteCount(a, b protocol.ByteCount) protocol.ByteCount {
    87  	if a < b {
    88  		return b
    89  	}
    90  	return a
    91  }
    92  
    93  // MaxDuration returns the max duration
    94  func MaxDuration(a, b time.Duration) time.Duration {
    95  	if a > b {
    96  		return a
    97  	}
    98  	return b
    99  }
   100  
   101  // MinDuration returns the minimum duration
   102  func MinDuration(a, b time.Duration) time.Duration {
   103  	if a > b {
   104  		return b
   105  	}
   106  	return a
   107  }
   108  
   109  // AbsDuration returns the absolute value of a time duration
   110  func AbsDuration(d time.Duration) time.Duration {
   111  	if d >= 0 {
   112  		return d
   113  	}
   114  	return -d
   115  }
   116  
   117  // MinTime returns the earlier time
   118  func MinTime(a, b time.Time) time.Time {
   119  	if a.After(b) {
   120  		return b
   121  	}
   122  	return a
   123  }
   124  
   125  // MaxTime returns the later time
   126  func MaxTime(a, b time.Time) time.Time {
   127  	if a.After(b) {
   128  		return a
   129  	}
   130  	return b
   131  }
   132  
   133  // MaxPacketNumber returns the max packet number
   134  func MaxPacketNumber(a, b protocol.PacketNumber) protocol.PacketNumber {
   135  	if a > b {
   136  		return a
   137  	}
   138  	return b
   139  }
   140  
   141  // MinPacketNumber returns the min packet number
   142  func MinPacketNumber(a, b protocol.PacketNumber) protocol.PacketNumber {
   143  	if a < b {
   144  		return a
   145  	}
   146  	return b
   147  }