forked from github/git-sizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounts.go
111 lines (92 loc) · 2.44 KB
/
counts.go
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
105
106
107
108
109
110
111
package counts
import (
"math"
)
// Count32 is a count of something, capped at math.MaxUint32.
type Count32 uint32
// NewCount32 initializes a Count32 from a uint64, capped at
// math.MaxUint32.
func NewCount32(n uint64) Count32 {
if n > math.MaxUint32 {
return Count32(math.MaxUint32)
}
return Count32(n)
}
// ToUint64 returns the value of `n` as a `uint64`. If the value has
// overflowed, it returns `(math.MaxUint32, true)`.
func (n Count32) ToUint64() (uint64, bool) {
return uint64(n), n == math.MaxUint32
}
// Plus returns 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 increases `*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 {
return false
}
*n1 = n2
return true
}
// 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 {
return false
}
*n1 = n2
return true
}
// Count64 is a count of something, capped at math.MaxUint64.
type Count64 uint64
// NewCount64 initializes a Count64 from a uint64.
func NewCount64(n uint64) Count64 {
return Count64(n)
}
// ToUint64 returns the value of `n` as a `uint64`. If the value has
// overflowed, it returns `(math.MaxUint64, true)`.
func (n Count64) ToUint64() (uint64, bool) {
return uint64(n), n == math.MaxUint64
}
// Plus returns 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 increases `*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 {
return false
}
*n1 = n2
return true
}
// 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 {
return false
}
*n1 = n2
return true
}