-
Notifications
You must be signed in to change notification settings - Fork 15
/
value_set_array.go
451 lines (404 loc) · 9.25 KB
/
value_set_array.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package rel
import (
"fmt"
"math"
"reflect"
"strings"
"github.com/arr-ai/frozen"
"github.com/arr-ai/wbnf/parser"
)
// Array is an ordered collection of Values.
type Array struct {
values []Value
offset int
count int
}
// NewArray constructs an array as a relation.
func NewArray(values ...Value) Set {
return NewOffsetArray(0, values...)
}
// NewArray constructs an array as a relation.
func NewOffsetArray(offset int, values ...Value) Set {
// Trim holes from both ends.
for i, v := range values {
if v != nil {
if i > 0 {
offset += i
values = values[i:]
}
break
}
}
for i := len(values) - 1; i >= 0; i-- {
if values[i] != nil {
if i < len(values)-1 {
values = values[:i+1]
}
break
}
}
if len(values) == 0 {
return None
}
// Count non-holes.
n := 0
for _, v := range values {
if v != nil {
n++
}
}
return Array{values: values, offset: offset, count: n}
}
func AsArray(v Value) (Array, bool) {
switch v := v.(type) {
case Array:
return v, true
case Set:
return Array{}, !v.IsTrue()
}
return Array{}, false
}
func asArray(s Set) (Array, bool) {
if s.Count() == 0 {
return Array{}, true
}
minIndex := math.MaxInt32
maxIndex := math.MinInt32
i := s.Enumerator()
for i.MoveNext() {
t, is := i.Current().(ArrayItemTuple)
if !is {
return Array{}, false
}
if t.at < minIndex {
minIndex = t.at
}
if t.at > maxIndex {
maxIndex = t.at
}
}
items := make([]Value, maxIndex-minIndex+1)
i = s.Enumerator()
for i.MoveNext() {
t := i.Current().(ArrayItemTuple)
items[t.at-minIndex] = t.item
}
return Array{
values: items,
offset: minIndex,
count: s.Count(),
}, true
}
func (a Array) clone() Array {
values := make([]Value, len(a.values))
copy(values, a.values)
a.values = values
return a
}
// Values returns the slice of values in the array. Holes in the indices are
// represented by nil elements.
//
// Callers must not reassign elements of the returned slice.
func (a Array) Values() []Value {
return a.values
}
// Hash computes a hash for a Array.
func (a Array) Hash(seed uintptr) uintptr {
h := seed
for e := a.Enumerator(); e.MoveNext(); {
h ^= e.Current().Hash(seed)
}
return h
}
// Equal tests two Sets for equality. Any other type returns false.
func (a Array) Equal(v interface{}) bool {
switch x := v.(type) {
case Array:
if len(a.values) != len(x.values) || a.offset != x.offset || a.count != x.count {
return false
}
for i, c := range a.values {
if (c != nil) != (x.values[i] != nil) || c != nil && !c.Equal(x.values[i]) {
return false
}
}
return true
}
return false
}
// String returns a string representation of an Array.
func (a Array) String() string {
var sb strings.Builder
if a.offset != 0 {
fmt.Fprintf(&sb, `%d\`, a.offset)
}
sb.WriteRune('[')
for i, v := range a.values {
if i > 0 {
sb.WriteString(", ")
}
if v != nil {
sb.WriteString(v.String())
}
}
sb.WriteRune(']')
return sb.String()
}
// Shift increments the Array's offset
func (a Array) Shift(offset int) Array {
a.offset += offset
return a
}
// Eval returns the string.
func (a Array) Eval(_ Scope) (Value, error) {
return a, nil
}
// Source returns a scanner locating the Array's source code.
func (a Array) Source() parser.Scanner {
return *parser.NewScanner("")
}
var arrayKind = registerKind(208, reflect.TypeOf(Array{}))
// Kind returns a number that is unique for each major kind of Value.
func (a Array) Kind() int {
return arrayKind
}
// IsTrue returns true if the tuple has attributes.
func (a Array) IsTrue() bool {
return a.count > 0
}
// Less returns true iff v is not a number or tuple, or v is a tuple and t
// precedes v in a lexicographical comparison of their name/value pairs.
func (a Array) Less(v Value) bool {
if a.Kind() != v.Kind() {
return a.Kind() < v.Kind()
}
b := v.(Array)
if a.offset != b.offset {
return a.offset < b.offset
}
n := len(a.values)
if n > len(b.values) {
n = len(b.values)
}
for i, av := range a.values[:n] {
bv := b.values[i]
if bv == nil {
return av != nil
}
if av == nil {
return false
}
if av.Less(bv) {
return true
}
if bv.Less(av) {
return false
}
}
return len(a.values) < len(b.values)
}
// Negate returns {@neg: a}.
func (a Array) Negate() Value {
return NewTuple(NewAttr(negateTag, a))
}
// Export exports an Array as a slice.
func (a Array) Export() interface{} {
result := make([]interface{}, 0, a.Count())
for _, v := range a.values {
if v != nil {
result = append(result, v.Export())
} else {
result = append(result, nil)
}
}
return result
}
// Count returns the number of elements in the Array.
func (a Array) Count() int {
return a.count
}
// Has returns true iff the given Value is in the Array.
func (a Array) Has(value Value) bool {
if t, ok := value.(ArrayItemTuple); ok {
if a.offset <= t.at && t.at < a.offset+len(a.values) {
v := a.values[t.at-a.offset]
return v != nil && v.Equal(t.item)
}
}
return false
}
func (a Array) withItem(index int, item Value) Set {
b := a
index -= a.offset
switch {
case index < 0:
b.values = make([]Value, len(a.values)-index)
copy(b.values[-index:], a.values)
b.offset += index
index = 0
case index < len(a.values):
b.values = make([]Value, len(a.values))
copy(b.values, a.values)
default:
b.values = make([]Value, index+1)
copy(b.values, a.values)
}
if b.values[index] != nil {
panic("superimposed array items not supported yet")
}
b.values[index] = item
b.count++
return b
}
// With returns the original Array with given value added. Iff the value was
// already present, the original Array is returned.
func (a Array) With(value Value) Set {
if t, ok := value.(ArrayItemTuple); ok {
return a.withItem(t.at, t.item)
}
return newSetFromSet(a).With(value)
}
// Without returns the original Array without the given value. Iff the value
// was already absent, the original Array is returned.
func (a Array) Without(value Value) Set {
if t, ok := value.(ArrayItemTuple); ok {
if i := t.at - a.offset; 0 <= i && i < len(a.values) {
v := a.values[i]
if v != nil && v.Equal(t.item) {
if t.at == a.offset {
return Array{
values: a.values[1:],
offset: a.offset + 1,
count: a.count - 1,
}
}
if t.at == a.offset+len(a.values)-1 {
return Array{
values: a.values[:len(a.values)-1],
offset: a.offset,
count: a.count - 1,
}
}
result := a.clone()
result.values[i] = nil
result.count--
}
}
}
return a
}
// Map maps values per f.
func (a Array) Map(f func(v Value) Value) Set {
var values []Value
for e := a.Enumerator(); e.MoveNext(); {
values = append(values, f(e.Current()))
}
return NewSet(values...)
}
// Where returns a new Array with all the Values satisfying predicate p.
func (a Array) Where(p func(v Value) (bool, error)) (Set, error) {
result := a.clone()
for i, v := range a.values {
if v != nil {
match, err := p(NewArrayItemTuple(a.offset+i, v))
if err != nil {
return nil, err
}
if !match {
result.values[i] = nil
result.count--
}
}
}
if result.count == 0 {
return None, nil
}
// Trim leading nils.
for i, v := range result.values {
if v != nil {
if i > 0 {
result.values = result.values[i:]
result.offset += i
}
break
}
}
// Trim trailing nils.
for i := len(result.values) - 1; i >= 0; i-- {
if v := result.values[i]; v != nil {
if i < len(result.values)-1 {
result.values = result.values[:i+1]
}
break
}
}
return result, nil
}
func (a Array) CallAll(arg Value) (Set, error) {
i := int(arg.(Number).Float64()) - a.offset
if i < 0 || i >= len(a.values) {
return None, nil
}
if v := a.values[i]; v != nil {
return NewSet(v), nil
}
return None, nil
}
// Enumerator returns an enumerator over the Values in the Array.
func (a Array) Enumerator() ValueEnumerator {
return &arrayValueEnumerator{a: a, i: -1}
}
func (a Array) ArrayEnumerator() (OffsetValueEnumerator, bool) {
return &arrayOffsetValueEnumerator{arrayValueEnumerator{a: a, i: -1}}, true
}
// arrayValueEnumerator represents an enumerator over a Array.
type arrayValueEnumerator struct {
a Array
i int
}
// MoveNext moves the enumerator to the next Value.
func (e *arrayValueEnumerator) MoveNext() bool {
if e.i >= len(e.a.values)-1 {
return false
}
for {
e.i++
if e.i < len(e.a.values) && e.a.values[e.i] != nil {
break
}
}
return e.i < len(e.a.values)
}
// Current returns the enumerator's current Value.
func (e *arrayValueEnumerator) Current() Value {
return NewArrayItemTuple(e.a.offset+e.i, e.a.values[e.i])
}
// arrayOffsetValueEnumerator represents an enumerator over a Array.
type arrayOffsetValueEnumerator struct {
arrayValueEnumerator
}
// Current returns the enumerator's current Value.
func (e *arrayOffsetValueEnumerator) Current() Value {
return e.a.values[e.i]
}
// Current returns the offset of the enumerator's current Value.
func (e *arrayOffsetValueEnumerator) Offset() int {
return e.a.offset + e.i
}
type arrayEnumerator struct {
i frozen.Iterator
t Tuple
}
func (e *arrayEnumerator) MoveNext() bool {
if e.i.Next() {
e.t = e.i.Value().(Tuple)
return true
}
return false
}
func (e *arrayEnumerator) Current() Value {
return e.t.MustGet(ArrayItemAttr)
}
func (e *arrayEnumerator) Offset() int {
return int(e.t.MustGet("@").(Number).Float64())
}