| 1 |
package pack |
| 2 |
|
| 3 |
import "fmt" |
| 4 |
|
| 5 |
// bounds encapsulates the window of search for a single iteration of binary |
| 6 |
// search. |
| 7 |
// |
| 8 |
// Callers may choose to treat the return values from Left() and Right() as |
| 9 |
// inclusive or exclusive. *bounds makes no assumptions on the inclusivity of |
| 10 |
// those values. |
| 11 |
// |
| 12 |
// See: *gitobj/pack.Index for more. |
| 13 |
type bounds struct { |
| 14 |
// left is the left or lower bound of the bounds. |
| 15 |
left int64 |
| 16 |
// right is the rightmost or upper bound of the bounds. |
| 17 |
right int64 |
| 18 |
} |
| 19 |
|
| 20 |
// newBounds returns a new *bounds instance with the given left and right |
| 21 |
// values. |
| 22 |
func newBounds(left, right int64) *bounds { |
| 23 |
return &bounds{ |
| 24 |
left: left, |
| 25 |
right: right, |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
// Left returns the leftmost value or lower bound of this *bounds instance. |
| 30 |
func (b *bounds) Left() int64 { |
| 31 |
return b.left |
| 32 |
} |
| 33 |
|
| 34 |
// right returns the rightmost value or upper bound of this *bounds instance. |
| 35 |
func (b *bounds) Right() int64 { |
| 36 |
return b.right |
| 37 |
} |
| 38 |
|
| 39 |
// WithLeft returns a new copy of this *bounds instance, replacing the left |
| 40 |
// value with the given argument. |
| 41 |
func (b *bounds) WithLeft(new int64) *bounds { |
| 42 |
return &bounds{ |
| 43 |
left: new, |
| 44 |
right: b.right, |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// WithRight returns a new copy of this *bounds instance, replacing the right |
| 49 |
// value with the given argument. |
| 50 |
func (b *bounds) WithRight(new int64) *bounds { |
| 51 |
return &bounds{ |
| 52 |
left: b.left, |
| 53 |
right: new, |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Equal returns whether or not the receiving *bounds instance is equal to the |
| 58 |
// given one: |
| 59 |
// |
| 60 |
// - If both the argument and receiver are nil, they are given to be equal. |
| 61 |
// - If both the argument and receiver are not nil, and they share the same |
| 62 |
// Left() and Right() values, they are equal. |
| 63 |
// - If both the argument and receiver are not nil, but they do not share the |
| 64 |
// same Left() and Right() values, they are not equal. |
| 65 |
// - If either the argument or receiver is nil, but the other is not, they are |
| 66 |
// not equal. |
| 67 |
func (b *bounds) Equal(other *bounds) bool { |
| 68 |
if b == nil { |
| 69 |
if other == nil { |
| 70 |
return true |
| 71 |
} |
| 72 |
return false |
| 73 |
} |
| 74 |
|
| 75 |
if other == nil { |
| 76 |
return false |
| 77 |
} |
| 78 |
|
| 79 |
return b.left == other.left && |
| 80 |
b.right == other.right |
| 81 |
} |
| 82 |
|
| 83 |
// String returns a string representation of this bounds instance, given as: |
| 84 |
// |
| 85 |
// [<left>,<right>] |
| 86 |
func (b *bounds) String() string { |
| 87 |
return fmt.Sprintf("[%d,%d]", b.Left(), b.Right()) |
| 88 |
} |