-
Notifications
You must be signed in to change notification settings - Fork 3
/
selector.go
136 lines (109 loc) · 2.93 KB
/
selector.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package bond
// SelectorType is the type of selector.
type SelectorType uint8
const (
SelectorTypePoint SelectorType = iota
SelectorTypePoints
SelectorTypeRange
SelectorTypeRanges
)
// Selector is the interface for all selectors.
type Selector[T any] interface {
Type() SelectorType
}
// SelectorPoint is the interface for point selector.
type SelectorPoint[T any] interface {
Selector[T]
Point() T
SetPoint(p T)
}
// SelectorPoints is the interface for multi-point selector.
type SelectorPoints[T any] interface {
Selector[T]
Points() []T
SetPoints(p ...T)
}
// SelectorRange is the interface for range selector.
// The range is represented as a two-element slice.
// The first element is the start of the range, and the second element is the end of the range.
// The range is inclusive on both ends.
type SelectorRange[T any] interface {
Selector[T]
Range() (T, T)
SetRange(start, end T)
}
// SelectorRanges is the interface for multi-range selector.
// The ranges are represented as a slice of two-element slices.
// The first element of each slice is the start of the range, and the second element is the end of the range.
// The range is inclusive on both ends.
type SelectorRanges[T any] interface {
Selector[T]
Ranges() [][]T
SetRanges(ranges ...[]T)
}
type selectorPoint[T any] struct {
point T
}
// NewSelectorPoint creates a new point selector.
func NewSelectorPoint[T any](point T) SelectorPoint[T] {
return &selectorPoint[T]{point: point}
}
func (s *selectorPoint[T]) Type() SelectorType {
return SelectorTypePoint
}
func (s *selectorPoint[T]) Point() T {
return s.point
}
func (s *selectorPoint[T]) SetPoint(p T) {
s.point = p
}
type selectorPoints[T any] struct {
points []T
}
// NewSelectorPoints creates a new multi-point selector.
func NewSelectorPoints[T any](points ...T) SelectorPoints[T] {
return &selectorPoints[T]{points: points}
}
func (s *selectorPoints[T]) Type() SelectorType {
return SelectorTypePoints
}
func (s *selectorPoints[T]) Points() []T {
return s.points
}
func (s *selectorPoints[T]) SetPoints(p ...T) {
s.points = p
}
type selectorRange[T any] struct {
start T
end T
}
// NewSelectorRange creates a new range selector.
func NewSelectorRange[T any](start, end T) SelectorRange[T] {
return &selectorRange[T]{start: start, end: end}
}
func (s *selectorRange[T]) Type() SelectorType {
return SelectorTypeRange
}
func (s *selectorRange[T]) Range() (T, T) {
return s.start, s.end
}
func (s *selectorRange[T]) SetRange(start, end T) {
s.start = start
s.end = end
}
type selectorRanges[T any] struct {
ranges [][]T
}
// NewSelectorRanges creates a new multi-range selector.
func NewSelectorRanges[T any](ranges ...[]T) SelectorRanges[T] {
return &selectorRanges[T]{ranges: ranges}
}
func (s *selectorRanges[T]) Type() SelectorType {
return SelectorTypeRanges
}
func (s *selectorRanges[T]) Ranges() [][]T {
return s.ranges
}
func (s *selectorRanges[T]) SetRanges(ranges ...[]T) {
s.ranges = ranges
}