minmax.go 2.6 KB

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