-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
210 lines (190 loc) · 4.02 KB
/
set.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package interval
import (
"fmt"
"sort"
"strings"
"github.com/Merovius/AdventOfCode/internal/math"
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
)
// Set is a set of T, built out of intervals. The zero value is an empty set.
type Set[I Interval[T], T constraints.Signed] struct {
set set[T]
}
// Contains returns whether s contains v.
func (s *Set[I, T]) Contains(v T) bool {
return s.set.Contains(v)
}
// Len returns the number of elements in s.
func (s *Set[I, T]) Len() int {
return s.set.Len()
}
// Add adds the interval i to s.
func (s *Set[I, T]) Add(i I) {
s.set.Add(i.toCO())
}
// Intersect intersects i into s.
func (s *Set[I, T]) Intersect(i I) {
s.set.Intersect(i.toCO())
}
// Continuous returns whether s is representable by a single interval. The
// empty set is considered continuous.
func (s *Set[I, T]) Continuous() bool {
return s.set.Continuous()
}
// Intervals returns a copy of the intervals in s.
func (s *Set[I, T]) Intervals() []I {
out := make([]I, 0, len(s.set.s))
for _, i := range s.set.s {
out = append(out, Convert[I](i))
}
return out
}
// String implements fmt.Stringer.
func (s Set[I, T]) String() string {
var (
parts = make([]string, 0, len(s.set.s))
)
for _, i := range s.set.s {
parts = append(parts, any(Convert[I](i)).(fmt.Stringer).String())
}
return "{" + strings.Join(parts, ", ") + "}"
}
// set is a set of T, built out of intervals.
type set[T constraints.Signed] struct {
// the slice is in canonical form, that is all intervals are not empty and
// s[i+1].Min > s[i].Max
s []CO[T]
}
// Contains returns whether s contains v.
func (s *set[T]) Contains(v T) bool {
n, ok := search(s.s, v, func(i CO[T], v T) int {
return math.Cmp(i.Min, v)
})
if ok {
return true
}
if n == 0 {
return false
}
return v < s.s[n-1].Max
}
// Len returns the number of elements in s.
func (s *set[T]) Len() int {
var n int
for _, i := range s.s {
n += i.Len()
}
return n
}
// Add adds the interval i to s.
func (s *set[T]) Add(i CO[T]) {
if i.Empty() {
return
}
lo, lok := search(s.s, i.Min, func(j CO[T], v T) int {
if v > j.Max {
return -1
}
if v+1 < j.Min {
return 1
}
return 0
})
hi, hok := search(s.s, i.Max, func(j CO[T], v T) int {
if v > j.Max {
return -1
}
if v+1 < j.Min {
return 1
}
return 0
})
if lok {
i.Min = math.Min(i.Min, s.s[lo].Min)
}
if hok {
i.Max = math.Max(i.Max, s.s[hi].Max)
hi++
}
if hi < lo {
panic("hi < lo")
}
if hi == lo {
s.s = append(s.s, CO[T]{})
copy(s.s[hi+1:], s.s[hi:])
hi++
}
s.s = append(append(s.s[:lo], i), s.s[hi:]...)
}
// Intersect intersects i into s.
func (s *set[T]) Intersect(i CO[T]) {
lo, lok := search(s.s, i.Min, func(j CO[T], v T) int {
if v >= j.Max {
return -1
}
if v < j.Min {
return 1
}
return 0
})
if lok {
s.s[lo].Min = math.Max(s.s[lo].Min, i.Min)
if s.s[lo].Empty() {
lo++
}
}
s.s = s.s[lo:]
hi, hok := search(s.s, i.Max, func(j CO[T], v T) int {
if v >= j.Max {
return -1
}
if v < j.Min {
return 1
}
return 0
})
if hok {
s.s[hi].Max = math.Min(s.s[hi].Max, i.Max)
if !s.s[hi].Empty() {
hi++
}
}
s.s = s.s[:hi]
}
// Continuous returns whether s is representable by a single interval. The
// empty set is considered continuous.
func (s *set[T]) Continuous() bool {
return len(s.s) <= 1
}
// Intervals returns a copy of the intervals in s.
func (s *set[T]) Intervals() []CO[T] {
return slices.Clone(s.s)
}
func (s *set[T]) check() {
if len(s.s) == 0 {
return
}
i := s.s[0]
if !i.valid() {
panic(fmt.Errorf("invalid interval %v in set", i))
}
for _, j := range s.s[1:] {
if !j.valid() {
panic(fmt.Errorf("invalid interval %v in set", i))
}
if j.Min <= i.Max || j.Max <= i.Min {
panic(fmt.Errorf("invalid successive pair %v and %v in set", i, j))
}
i = j
}
}
func search[A, B any](x []A, target B, cmp func(A, B) int) (int, bool) {
pos := sort.Search(len(x), func(i int) bool {
return cmp(x[i], target) >= 0
})
if pos >= len(x) || cmp(x[pos], target) != 0 {
return pos, false
}
return pos, true
}