aboutsummaryrefslogtreecommitdiffstats
path: root/lib/iterator/index.go
blob: dd660052b22e1c5787d6da1b079a80996fdc1699 (plain) (blame)
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
package iterator

// IndexProvider implements a subset of the Interator interface
type IndexProvider interface {
	StartIndex() int
	EndIndex() int
}

// FixBounds will force the index i to either its lower- or upper-bound value
// if out-of-bound
func FixBounds(i, lower, upper int) int {
	switch {
	case i > upper:
		i = upper
	case i < lower:
		i = lower
	}
	return i
}

// WrapBounds will wrap the index i around its upper- or lower-bound if
// out-of-bound
func WrapBounds(i, lower, upper int) int {
	switch {
	case i > upper:
		i = lower + (i-upper-1)%upper
	case i < lower:
		i = upper - (lower-i-1)%upper
	}
	return i
}

type BoundsCheckFunc func(int, int, int) int

// MoveIndex moves the index variable idx forward by delta steps and ensures
// that the boundary policy as defined by the CheckBoundsFunc is enforced.
//
// If CheckBoundsFunc is nil, fix boundary checks are performed.
func MoveIndex(idx, delta int, indexer IndexProvider, cb BoundsCheckFunc) int {
	lower, upper := indexer.StartIndex(), indexer.EndIndex()
	sign := 1
	if upper < lower {
		lower, upper = upper, lower
		sign = -1
	}
	result := idx + sign*delta
	if cb == nil {
		return FixBounds(result, lower, upper)
	}
	return cb(result, lower, upper)
}