File: bounds.go

package info (click to toggle)
golang-github-git-lfs-gitobj 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 432 kB
  • sloc: makefile: 2; sh: 1
file content (88 lines) | stat: -rw-r--r-- 2,299 bytes parent folder | download | duplicates (3)
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
package pack

import "fmt"

// bounds encapsulates the window of search for a single iteration of binary
// search.
//
// Callers may choose to treat the return values from Left() and Right() as
// inclusive or exclusive. *bounds makes no assumptions on the inclusivity of
// those values.
//
// See: *gitobj/pack.Index for more.
type bounds struct {
	// left is the left or lower bound of the bounds.
	left int64
	// right is the rightmost or upper bound of the bounds.
	right int64
}

// newBounds returns a new *bounds instance with the given left and right
// values.
func newBounds(left, right int64) *bounds {
	return &bounds{
		left:  left,
		right: right,
	}
}

// Left returns the leftmost value or lower bound of this *bounds instance.
func (b *bounds) Left() int64 {
	return b.left
}

// right returns the rightmost value or upper bound of this *bounds instance.
func (b *bounds) Right() int64 {
	return b.right
}

// WithLeft returns a new copy of this *bounds instance, replacing the left
// value with the given argument.
func (b *bounds) WithLeft(new int64) *bounds {
	return &bounds{
		left:  new,
		right: b.right,
	}
}

// WithRight returns a new copy of this *bounds instance, replacing the right
// value with the given argument.
func (b *bounds) WithRight(new int64) *bounds {
	return &bounds{
		left:  b.left,
		right: new,
	}
}

// Equal returns whether or not the receiving *bounds instance is equal to the
// given one:
//
//   - If both the argument and receiver are nil, they are given to be equal.
//   - If both the argument and receiver are not nil, and they share the same
//     Left() and Right() values, they are equal.
//   - If both the argument and receiver are not nil, but they do not share the
//     same Left() and Right() values, they are not equal.
//   - If either the argument or receiver is nil, but the other is not, they are
//     not equal.
func (b *bounds) Equal(other *bounds) bool {
	if b == nil {
		if other == nil {
			return true
		}
		return false
	}

	if other == nil {
		return false
	}

	return b.left == other.left &&
		b.right == other.right
}

// String returns a string representation of this bounds instance, given as:
//
//   [<left>,<right>]
func (b *bounds) String() string {
	return fmt.Sprintf("[%d,%d]", b.Left(), b.Right())
}