-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.go
581 lines (528 loc) · 14.6 KB
/
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright (c) 2018-2020, AT&T Intellectual Property.
// All rights reserved.
//
// SPDX-License-Identifier: MPL-2.0
package data
import (
"bytes"
"reflect"
"sort"
"github.com/danos/encoding/rfc7951"
"jsouthworth.net/go/immutable/vector"
)
// ArrayNew creates a new array and returns its abstract representation
func ArrayNew() *Array {
return arrayNew()
}
func arrayNew() *Array {
return &Array{
store: vector.Empty(),
}
}
// ArrayWith creates an array and initializes it with the provided elements
func ArrayWith(elements ...interface{}) *Array {
return ArrayNew().with(elements...)
}
// ArrayFrom creates an array and initializes it with the elements from the provided slice
func ArrayFrom(in interface{}) *Array {
return ArrayNew().from(in)
}
// Array is an RFC7159 array augmented for RFC7951 behaviors. The
// arrays are immutable, the mutation methods return new structurally
// shared copies of the original array with the changes. This provides
// cheap copies of the array and preserves the original allowing it to be
// easily shared.
type Array struct {
store *vector.Vector
module string
}
// from converts a go []interface{} to an Array.
func (arr *Array) from(ins interface{}) *Array {
val := reflect.ValueOf(ins)
vals := make([]*Value, val.Len())
for i := 0; i < val.Len(); i++ {
in := val.Index(i).Interface()
vals[i] = arr.adaptValue(ValueNew(in))
}
vec := vector.From(vals)
return &Array{
store: vec,
module: arr.module,
}
}
// with returns an Array containing the elements.
func (arr *Array) with(elements ...interface{}) *Array {
return arr.from(elements)
}
// At returns the value at the index of the array, if the index is out
// of bounds, nil is returned.
func (arr *Array) At(index int) *Value {
if index >= arr.store.Length() || index < 0 {
return nil
}
return arr.store.At(index).(*Value)
}
// Contains returns whether the index is in the bounds of the array.
func (arr *Array) Contains(index int) bool {
return index < arr.store.Length() && index >= 0
}
// Find returns the value at the index or nil if it doesn't exist and
// whether the index was in the array.
func (arr *Array) Find(index int) (*Value, bool) {
v, ok := arr.store.Find(index)
if !ok {
return nil, ok
}
return v.(*Value), ok
}
// Assoc associates the value with the index in the array. If the
// index is out of bounds the array is padded to that index and the value
// is associated.
func (arr *Array) Assoc(index int, value interface{}) *Array {
newStore := arr.store
if arr.Length() <= index {
for i := arr.Length(); i < index+1; i++ {
newStore = newStore.Append(nil)
}
}
newStore = newStore.Assoc(index, arr.adaptValue(ValueNew(value)))
return &Array{
store: newStore,
module: arr.module,
}
}
// Length returns the number of elements in the array.
func (arr *Array) Length() int {
return arr.store.Length()
}
// Append adds a new value to the end of the array.
func (arr *Array) Append(value interface{}) *Array {
newStore := arr.store.Append(arr.adaptValue(ValueNew(value)))
return &Array{
store: newStore,
module: arr.module,
}
}
// Delete removes an element at the supplied index from the array.
func (arr *Array) Delete(index int) *Array {
newStore := arr.store.Delete(index)
return &Array{
store: newStore,
module: arr.module,
}
}
func (arr *Array) detect(fn func(*Value) bool) *Value {
// TODO: is there a better name for this?
return arr.detectAndIfNone(fn, func() *Value { return nil })
}
func (arr *Array) detectAndIfNone(fn func(*Value) bool, ifNone func() *Value) *Value {
var out *Value
var found bool
arr.store.Range(func(_ int, v *Value) bool {
if fn(v) {
out = v
found = true
return false
}
return true
})
if found {
return out
}
return ifNone()
}
// Range iterates over the object's members. Range can take a set of functions
// matched by type. If the function returns a bool this is treated as a
// loop terminataion variable if false the loop will terminate.
//
// func(int, *Value) iterates over indicies and values.
// func(int, *Value) bool
// func(int) iterates over only the indicies
// func(int) bool
// func(*Value) iterates over only the values
// func(*Value bool
func (arr *Array) Range(fn interface{}) *Array {
switch f := fn.(type) {
case func(int, *Value):
case func(int, *Value) bool:
case func(*Value):
fn = func(idx int, val interface{}) bool {
f(val.(*Value))
return true
}
case func(*Value) bool:
fn = func(idx int, val interface{}) bool {
return f(val.(*Value))
}
case func(int):
fn = func(idx int, val interface{}) bool {
f(idx)
return true
}
case func(int) bool:
fn = func(idx int, val interface{}) bool {
return f(idx)
}
default:
panic("invalid range function")
}
arr.store.Range(fn)
return arr
}
func (arr *Array) selectItems(fn func(*Value) bool) *Array {
out := ArrayNew()
out.module = arr.module
out.store = out.store.Transform(
func(store *vector.TVector) *vector.TVector {
arr.Range(func(elem *Value) {
if fn(elem) {
elem = out.adaptValue(elem)
store = store.Append(elem)
}
})
return store
})
return out
}
// toNative returns a go native []interface{} from the object.
func (arr *Array) toNative() interface{} {
out := make([]interface{}, arr.Length())
arr.Range(func(idx int, value *Value) {
out[idx] = value.ToNative()
})
return out
}
// toData returns the contents of the array as a []*Value that
// can be used with things like text/template more easily.
func (arr *Array) toData() interface{} {
out := make([]*Value, arr.Length())
arr.Range(func(idx int, value *Value) {
out[idx] = value
})
return out
}
func (arr *Array) belongsTo(orig *Value, moduleName string) *Value {
if moduleName == arr.module {
return orig
}
out := arr.copy()
out.module = moduleName
out.store = out.store.Transform(
func(store *vector.TVector) *vector.TVector {
arr.Range(func(idx int, val *Value) {
adaptedValue := out.adaptValue(val)
if adaptedValue.equal(val) {
return
}
store = store.Assoc(idx, adaptedValue)
})
return store
})
return ValueNew(out)
}
func (arr *Array) adaptValue(val *Value) *Value {
return val.belongsTo(val, arr.module)
}
func (arr *Array) copy() *Array {
return &Array{
module: arr.module,
store: arr.store,
}
}
// merge merges one array with another. The returned array is the
// old array with any existing indicies replaced with counterparts from the
// new object and any new indicies added. Merge is accretive only and will
// not remove non-existant indicies.
func (arr *Array) merge(new *Value) *Value {
return new.Perform(func(n *Array) *Value {
out := arr.Transform(func(out *TArray) {
arr.Range(func(i int, v *Value) {
if n.Contains(i) {
out = out.Assoc(i,
v.Merge(n.At(i)))
}
})
n.Range(func(i int, v *Value) {
if !arr.Contains(i) {
out = out.Append(v)
}
})
})
return ValueNew(out)
}, func(_ interface{}) *Value {
// By default just return the original array; can't merge
// unlike types.
return ValueNew(arr)
}).(*Value)
}
// Equal implements equality for arrays. An array is equal to another
// array if all their values at each index is equal. Equality checks are linear
// with respect to the number of elements.
func (arr *Array) Equal(other interface{}) bool {
oa, isArray := other.(*Array)
return isArray &&
oa.module == arr.module &&
oa.store.Length() == arr.store.Length() &&
equal(oa.store, arr.store)
}
// String returns a string representation of the Array.
func (arr *Array) String() string {
var buf bytes.Buffer
arr.marshalRFC7951(&buf, arr.module)
return buf.String()
}
func (arr *Array) marshalRFC7951(buf *bytes.Buffer, module string) error {
buf.WriteByte('[')
arr.Range(func(i int, v *Value) {
v.marshalRFC7951(buf, module)
if i < arr.Length()-1 {
buf.WriteByte(',')
}
})
buf.WriteByte(']')
return nil
}
func (arr *Array) unmarshalRFC7951(
msg []byte, module string,
strs *stringInterner,
vals *valueInterner,
) error {
var a []rfc7951.RawMessage
rfc7951.Unmarshal(msg, &a)
arr.module = module
arr.store = arr.store.Transform(
func(store *vector.TVector) *vector.TVector {
for _, v := range a {
val := valueNew(nil)
val.unmarshalRFC7951(v, arr.module, strs, vals)
val = arr.adaptValue(val)
val = vals.Intern(val)
store = store.Append(val)
}
return store
})
return nil
}
func (arr *Array) diff(new *Value, path *InstanceID) []EditEntry {
out := []EditEntry{}
new.Perform(func(other *Array) {
arr.Range(func(i int, v *Value) {
if other.Contains(i) {
out = append(out,
v.diff(other.At(i),
path.addPosPredicate(i))...)
} else {
out = append(out,
EditEntry{
Action: EditDelete,
Path: path.addPosPredicate(i),
})
}
})
other.Range(func(i int, v *Value) {
if arr.Contains(i) {
return
}
out = append(out,
EditEntry{
Action: EditAssoc,
Path: path.addPosPredicate(i),
Value: v,
})
})
}, func(other interface{}) {
out = []EditEntry{
{Action: EditAssoc, Path: path, Value: ValueNew(new)},
}
})
return out
}
// Transform executes the provided function against a mutable
// transient array to provide a faster, less memory intensive, array
// editing mechanism.
func (arr *Array) Transform(fn func(*TArray)) *Array {
tarr := &TArray{
orig: arr,
store: arr.store.AsTransient(),
}
fn(tarr)
out := arr.copy()
out.store = tarr.store.AsPersistent()
return out
}
// Sort sorts an array returning a new array that is sorted.
// by default sort will use dyn.Compare as the comparison operator
// this may be overridden using the Compare option.
func (arr *Array) Sort(options ...SortOption) *Array {
var opts sortOpts
opts.compare = func(v1, v2 *Value) int {
return v1.Compare(v2)
}
for _, opt := range options {
opt(&opts)
}
out := arr.copy()
sorter := arraySorter{
array: out.store.AsTransient(),
opts: &opts,
}
sort.Sort(&sorter)
out.store = sorter.array.AsPersistent()
return out
}
type arraySorter struct {
array *vector.TVector
opts *sortOpts
}
func (s *arraySorter) Len() int {
return s.array.Length()
}
func (s *arraySorter) Less(i, j int) bool {
return s.opts.compare(s.array.At(i).(*Value),
s.array.At(j).(*Value)) < 0
}
func (s *arraySorter) Swap(i, j int) {
a, b := s.array.At(i), s.array.At(j)
s.array.Assoc(i, b)
s.array.Assoc(j, a)
}
type sortOpts struct {
compare func(v1, v2 *Value) int
}
// SortOption is an option to the Array.Sort function
type SortOption func(*sortOpts)
// Compare takes a comparison function and returns a sort option
// A compare function takes two values and returns a trinary state as
// an integer. Less than zero indicates the first was less than the last,
// zero indicates the two values were equal, and greater than zero
// indicates that the first was greater than the last.
func Compare(fn func(a, b *Value) int) SortOption {
return func(opts *sortOpts) {
opts.compare = fn
}
}
// TArray is a transient array that may be used to perform
// transformations on an array in a fast mutable fashion. This can
// only be accessed via the (*Array).Transform method. Care should be
// taken not to share this among threads as its values are mutable.
type TArray struct {
orig *Array
store *vector.TVector
}
// Assoc associates the value with the index in the array. If the
// index is out of bounds the array is padded to that index and the
// value is associated.
func (arr *TArray) Assoc(i int, v interface{}) *TArray {
arr.store = arr.store.Assoc(i, arr.orig.adaptValue(ValueNew(v)))
return arr
}
// Append adds a new value to the end of the array.
func (arr *TArray) Append(value interface{}) *TArray {
arr.store = arr.store.Append(arr.orig.adaptValue(ValueNew(value)))
return arr
}
// At returns the value at the index of the array, if the index is out
// of bounds, nil is returned.
func (arr *TArray) At(index int) *Value {
if index >= arr.store.Length() || index < 0 {
return nil
}
return arr.store.At(index).(*Value)
}
// Contains returns whether the index is in the bounds of the array.
func (arr *TArray) Contains(index int) bool {
return index < arr.store.Length() && index >= 0
}
// Delete removes an element at the supplied index from the array.
func (arr *TArray) Delete(index int) *TArray {
arr.store = arr.store.Delete(index)
return arr
}
// Find returns the value at the index or nil if it doesn't exist and
// whether the index was in the array.
func (arr *TArray) Find(index int) (*Value, bool) {
v, ok := arr.store.Find(index)
if !ok {
return nil, ok
}
return v.(*Value), ok
}
// Length returns the number of elements in the array.
func (arr *TArray) Length() int {
return arr.store.Length()
}
// Range iterates over the object's members. Range can take a set of functions
// matched by type. If the function returns a bool this is treated as a
// loop terminataion variable if false the loop will terminate.
//
// func(int, *Value) iterates over indicies and values.
// func(int, *Value) bool
// func(int) iterates over only the indicies
// func(int) bool
// func(*Value) iterates over only the values
// func(*Value bool
func (arr *TArray) Range(fn interface{}) {
// NOTE: this must be done inline to avoid needing a heap
// allocation for the generated closure.
switch f := fn.(type) {
case func(int, *Value):
case func(int, *Value) bool:
case func(*Value):
fn = func(idx int, val interface{}) bool {
f(val.(*Value))
return true
}
case func(*Value) bool:
fn = func(idx int, val interface{}) bool {
return f(val.(*Value))
}
case func(int):
fn = func(idx int, val interface{}) bool {
f(idx)
return true
}
case func(int) bool:
fn = func(idx int, val interface{}) bool {
return f(idx)
}
default:
panic("invalid range function")
}
arr.store.Range(fn)
}
// Sort sorts an array returning a new array that is sorted.
// by default sort will use dyn.Compare as the comparison operator
// this may be overridden using the Compare option.
func (arr *TArray) Sort(options ...SortOption) *TArray {
var opts sortOpts
opts.compare = func(v1, v2 *Value) int {
return v1.Compare(v2)
}
for _, opt := range options {
opt(&opts)
}
sorter := arraySorter{
array: arr.store,
opts: &opts,
}
sort.Sort(&sorter)
arr.store = sorter.array
return arr
}
// String returns a string representation of the Array.
func (arr *TArray) String() string {
var buf bytes.Buffer
arr.marshalRFC7951(&buf, arr.orig.module)
return buf.String()
}
func (arr *TArray) marshalRFC7951(buf *bytes.Buffer, module string) error {
buf.WriteByte('[')
arr.Range(func(i int, v *Value) {
v.marshalRFC7951(buf, module)
if i < arr.Length()-1 {
buf.WriteByte(',')
}
})
buf.WriteByte(']')
return nil
}