1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
package counts
import (
"math"
)
// A count of something, capped at math.MaxUint32.
type Count32 uint32
func NewCount32(n uint64) Count32 {
if n > math.MaxUint32 {
return Count32(math.MaxUint32)
}
return Count32(n)
}
func (n Count32) ToUint64() (uint64, bool) {
return uint64(n), n == math.MaxUint32
}
// Return the sum of two Count32s, capped at math.MaxUint32.
func (n1 Count32) Plus(n2 Count32) Count32 {
n := n1 + n2
if n < n1 {
// Overflow
return math.MaxUint32
}
return n
}
// Increment `*n1` by `n2`, capped at math.MaxUint32.
func (n1 *Count32) Increment(n2 Count32) {
*n1 = n1.Plus(n2)
}
// AdjustMaxIfNecessary adjusts `*n1` to be `max(*n1, n2)`. Return
// true iff `n2` was greater than `*n1`.
func (n1 *Count32) AdjustMaxIfNecessary(n2 Count32) bool {
if n2 > *n1 {
*n1 = n2
return true
} else {
return false
}
}
// AdjustMaxIfPossible adjusts `*n1` to be `max(*n1, n2)`. Return true
// iff `n2` was greater than or equal to `*n1`.
func (n1 *Count32) AdjustMaxIfPossible(n2 Count32) bool {
if n2 >= *n1 {
*n1 = n2
return true
} else {
return false
}
}
// A count of something, capped at math.MaxUint64.
type Count64 uint64
func NewCount64(n uint64) Count64 {
return Count64(n)
}
func (n Count64) ToUint64() (uint64, bool) {
return uint64(n), n == math.MaxUint64
}
// Return the sum of two Count64s, capped at math.MaxUint64.
func (n1 Count64) Plus(n2 Count64) Count64 {
n := n1 + n2
if n < n1 {
// Overflow
return math.MaxUint64
}
return n
}
// Increment `*n1` by `n2`, capped at math.MaxUint64.
func (n1 *Count64) Increment(n2 Count64) {
*n1 = n1.Plus(n2)
}
// AdjustMaxIfNecessary adjusts `*n1` to be `max(*n1, n2)`. Return
// true iff `n2` was greater than `*n1`.
func (n1 *Count64) AdjustMaxIfNecessary(n2 Count64) bool {
if n2 > *n1 {
*n1 = n2
return true
} else {
return false
}
}
// AdjustMaxIfPossible adjusts `*n1` to be `max(*n1, n2)`. Return true
// iff `n2` was greater than or equal to `*n1`.
func (n1 *Count64) AdjustMaxIfPossible(n2 Count64) bool {
if n2 > *n1 {
*n1 = n2
return true
} else {
return false
}
}
|