github.com/ooni/psiphon/tunnel-core@v0.0.0-20230105123940-fe12a24c96ee/oovendor/quic-go/internal/utils/minmax.go (about)

     1  package utils
     2  
     3  import (
     4  	"math"
     5  	"time"
     6  
     7  	"github.com/ooni/psiphon/tunnel-core/oovendor/quic-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  // MinNonZeroDuration return the minimum duration that's not zero.
   110  func MinNonZeroDuration(a, b time.Duration) time.Duration {
   111  	if a == 0 {
   112  		return b
   113  	}
   114  	if b == 0 {
   115  		return a
   116  	}
   117  	return MinDuration(a, b)
   118  }
   119  
   120  // AbsDuration returns the absolute value of a time duration
   121  func AbsDuration(d time.Duration) time.Duration {
   122  	if d >= 0 {
   123  		return d
   124  	}
   125  	return -d
   126  }
   127  
   128  // MinTime returns the earlier time
   129  func MinTime(a, b time.Time) time.Time {
   130  	if a.After(b) {
   131  		return b
   132  	}
   133  	return a
   134  }
   135  
   136  // MinNonZeroTime returns the earlist time that is not time.Time{}
   137  // If both a and b are time.Time{}, it returns time.Time{}
   138  func MinNonZeroTime(a, b time.Time) time.Time {
   139  	if a.IsZero() {
   140  		return b
   141  	}
   142  	if b.IsZero() {
   143  		return a
   144  	}
   145  	return MinTime(a, b)
   146  }
   147  
   148  // MaxTime returns the later time
   149  func MaxTime(a, b time.Time) time.Time {
   150  	if a.After(b) {
   151  		return a
   152  	}
   153  	return b
   154  }
   155  
   156  // MaxPacketNumber returns the max packet number
   157  func MaxPacketNumber(a, b protocol.PacketNumber) protocol.PacketNumber {
   158  	if a > b {
   159  		return a
   160  	}
   161  	return b
   162  }
   163  
   164  // MinPacketNumber returns the min packet number
   165  func MinPacketNumber(a, b protocol.PacketNumber) protocol.PacketNumber {
   166  	if a < b {
   167  		return a
   168  	}
   169  	return b
   170  }