-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_cut.go
executable file
·256 lines (223 loc) · 5.66 KB
/
range_cut.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
import (
"fmt"
)
// RangeCut represents a position on the line of all possible values.
type RangeCut interface {
// Compare returns an integer stating the relative position of the calling RangeCut to the given RangeCut.
Compare(RangeCut, Type) (int, error)
// String returns the RangeCut as a string for display purposes.
String() string
// TypeAsLowerBound returns the bound type if the calling RangeCut is the lower bound of a range.
TypeAsLowerBound() RangeBoundType
// TypeAsUpperBound returns the bound type if the calling RangeCut is the upper bound of a range.
TypeAsUpperBound() RangeBoundType
}
// RangeBoundType is the bound of the RangeCut.
type RangeBoundType int
const (
// Open bounds represent exclusion.
Open RangeBoundType = iota
// Closed bounds represent inclusion.
Closed
)
// Inclusive returns whether the bound represents inclusion.
func (bt RangeBoundType) Inclusive() bool {
return bt == Closed
}
// GetRangeCutKey returns the inner value from the given RangeCut.
func GetRangeCutKey(c RangeCut) interface{} {
switch c := c.(type) {
case Below:
return c.key
case Above:
return c.key
default:
panic(fmt.Errorf("need to check the RangeCut type before calling GetRangeCutKey, used on `%T`", c))
}
}
// GetRangeCutMax returns the RangeCut with the highest value.
func GetRangeCutMax(typ Type, cuts ...RangeCut) (RangeCut, error) {
i := 0
var maxCut RangeCut
for ; i < len(cuts); i++ {
if cuts[i] != nil {
maxCut = cuts[i]
i++
break
}
}
for ; i < len(cuts); i++ {
if cuts[i] == nil {
continue
}
comp, err := maxCut.Compare(cuts[i], typ)
if err != nil {
return maxCut, err
}
if comp == -1 {
maxCut = cuts[i]
}
}
return maxCut, nil
}
// GetRangeCutMin returns the RangeCut with the lowest value.
func GetRangeCutMin(typ Type, cuts ...RangeCut) (RangeCut, error) {
i := 0
var minCut RangeCut
for ; i < len(cuts); i++ {
if cuts[i] != nil {
minCut = cuts[i]
i++
break
}
}
for ; i < len(cuts); i++ {
if cuts[i] == nil {
continue
}
comp, err := minCut.Compare(cuts[i], typ)
if err != nil {
return minCut, err
}
if comp == 1 {
minCut = cuts[i]
}
}
return minCut, nil
}
// Above represents the position immediately above the contained key.
type Above struct {
key interface{}
}
var _ RangeCut = Above{}
// Compare implements RangeCut.
func (a Above) Compare(c RangeCut, typ Type) (int, error) {
switch c := c.(type) {
case AboveAll:
return -1, nil
case BelowAll:
return 1, nil
case Above:
return typ.Compare(a.key, c.key)
case Below:
cmp, err := typ.Compare(a.key, c.key)
if err != nil {
return 0, err
}
if cmp == -1 {
return -1, nil
}
return 1, nil
default:
panic(fmt.Errorf("unrecognized RangeCut type '%T'", c))
}
}
// String implements RangeCut.
func (a Above) String() string {
return fmt.Sprintf("Above[%v]", a.key)
}
// TypeAsLowerBound implements RangeCut.
func (Above) TypeAsLowerBound() RangeBoundType {
return Open
}
// TypeAsUpperBound implements RangeCut.
func (Above) TypeAsUpperBound() RangeBoundType {
return Closed
}
// AboveAll represents the position beyond the maximum possible value.
type AboveAll struct{}
var _ RangeCut = AboveAll{}
// Compare implements RangeCut.
func (AboveAll) Compare(c RangeCut, typ Type) (int, error) {
if _, ok := c.(AboveAll); ok {
return 0, nil
}
return 1, nil
}
// String implements RangeCut.
func (AboveAll) String() string {
return "AboveAll"
}
// TypeAsLowerBound implements RangeCut.
func (AboveAll) TypeAsLowerBound() RangeBoundType {
return Open
}
// TypeAsUpperBound implements RangeCut.
func (AboveAll) TypeAsUpperBound() RangeBoundType {
return Open
}
// Below represents the position immediately below the contained key.
type Below struct {
key interface{}
}
var _ RangeCut = Below{}
// Compare implements RangeCut.
func (b Below) Compare(c RangeCut, typ Type) (int, error) {
switch c := c.(type) {
case AboveAll:
return -1, nil
case BelowAll:
return 1, nil
case Below:
return typ.Compare(b.key, c.key)
case Above:
cmp, err := typ.Compare(c.key, b.key)
if err != nil {
return 0, err
}
if cmp == -1 {
return 1, nil
}
return -1, nil
default:
panic(fmt.Errorf("unrecognized RangeCut type '%T'", c))
}
}
// String implements RangeCut.
func (b Below) String() string {
return fmt.Sprintf("Below[%v]", b.key)
}
// TypeAsLowerBound implements RangeCut.
func (Below) TypeAsLowerBound() RangeBoundType {
return Closed
}
// TypeAsUpperBound implements RangeCut.
func (Below) TypeAsUpperBound() RangeBoundType {
return Open
}
// BelowAll represents the position beyond the minimum possible value.
type BelowAll struct{}
var _ RangeCut = BelowAll{}
// Compare implements RangeCut.
func (BelowAll) Compare(c RangeCut, typ Type) (int, error) {
if _, ok := c.(BelowAll); ok {
return 0, nil
}
return -1, nil
}
// String implements RangeCut.
func (BelowAll) String() string {
return "BelowAll"
}
// TypeAsLowerBound implements RangeCut.
func (BelowAll) TypeAsLowerBound() RangeBoundType {
return Open
}
// TypeAsUpperBound implements RangeCut.
func (BelowAll) TypeAsUpperBound() RangeBoundType {
return Open
}