/
iterator.go
1795 lines (1516 loc) · 39.7 KB
/
iterator.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package core
import (
"bytes"
"math"
"reflect"
"slices"
"strconv"
"sync"
"github.com/bits-and-blooms/bitset"
"github.com/inoxlang/inox/internal/memds"
"github.com/inoxlang/inox/internal/utils"
"golang.org/x/exp/constraints"
)
var _ = []Iterable{
(*List)(nil), (*Tuple)(nil), (*Object)(nil), (*Record)(nil), (*OrderedPair)(nil), KeyList{},
FloatRange{}, IntRange{}, (*RuneRange)(nil),
Pattern(nil), EventSource(nil),
}
// An Iterable is a Value that provides an iterator.
// Patterns' implementations can either return an empty iterator or an iterator that loops over values matching the pattern.
type Iterable interface {
Value
//Iterator should return a new iterator that is not affected by mutations of the iterable.
//TODO: Update the implementations that currently do not meet this requirement (e.g. *Object).
Iterator(*Context, IteratorConfiguration) Iterator
}
type SerializableIterable interface {
Iterable
Serializable
}
type IteratorConfiguration struct {
KeyFilter Pattern
ValueFilter Pattern
KeysNeverRead bool //indicates that the Iterator.Key method will not be called.
}
// CreateIterator wraps an iterator in a filtering iterator if necessary
func (config IteratorConfiguration) CreateIterator(base Iterator) Iterator {
switch {
case config.KeyFilter == nil && config.ValueFilter == nil:
return base
case config.KeyFilter != nil && config.ValueFilter == nil:
return &KeyFilteredIterator{it: base, keyFilter: config.KeyFilter}
case config.KeyFilter == nil && config.ValueFilter != nil:
return &ValueFilteredIterator{it: base, valueFilter: config.ValueFilter}
default:
return &KeyValueFilteredIterator{it: base, keyFilter: config.KeyFilter, valueFilter: config.ValueFilter}
}
}
type Iterator interface {
Value
HasNext(*Context) bool
Next(*Context) bool
Key(*Context) Value
Value(*Context) Value
//TODO: Close(*Context)
}
type KeyFilteredIterator struct {
it Iterator
keyFilter Pattern
nextKey Value
nextValue Value
currentKey Value
currentValue Value
}
func (it *KeyFilteredIterator) HasNext(ctx *Context) bool {
if it.nextKey != nil {
return true
}
for {
if !it.it.HasNext(ctx) {
return false
}
it.it.Next(ctx)
key := it.it.Key(ctx)
if it.keyFilter.Test(ctx, key) {
it.nextKey = key
it.nextValue = it.it.Value(ctx)
break
}
it.nextKey = nil
it.nextValue = nil
}
return true
}
func (it *KeyFilteredIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.currentKey = it.nextKey
it.currentValue = it.nextValue
it.nextKey = nil
it.nextValue = nil
return true
}
func (it *KeyFilteredIterator) Key(ctx *Context) Value {
return it.currentKey
}
func (it *KeyFilteredIterator) Value(ctx *Context) Value {
return it.currentValue
}
type ValueFilteredIterator struct {
it Iterator
valueFilter Pattern
nextKey Value
nextValue Value
currentKey Value
currentValue Value
}
func (it *ValueFilteredIterator) HasNext(ctx *Context) bool {
if it.nextKey != nil {
return true
}
for {
if !it.it.HasNext(ctx) {
return false
}
it.it.Next(ctx)
value := it.it.Value(ctx)
if it.valueFilter.Test(ctx, value) {
it.nextValue = value
it.nextKey = it.it.Key(ctx)
break
}
it.nextKey = nil
it.nextValue = nil
}
return true
}
func (it *ValueFilteredIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.currentKey = it.nextKey
it.currentValue = it.nextValue
it.nextKey = nil
it.nextValue = nil
return true
}
func (it *ValueFilteredIterator) Key(ctx *Context) Value {
return it.currentKey
}
func (it *ValueFilteredIterator) Value(ctx *Context) Value {
return it.currentValue
}
type KeyValueFilteredIterator struct {
it Iterator
keyFilter Pattern
valueFilter Pattern
nextKey Value
nextValue Value
currentKey Value
currentValue Value
}
func (it *KeyValueFilteredIterator) HasNext(ctx *Context) bool {
if it.nextKey != nil {
return true
}
for {
if !it.it.HasNext(ctx) {
return false
}
it.it.Next(ctx)
key := it.it.Key(ctx)
value := it.it.Value(ctx)
if it.keyFilter.Test(ctx, key) && it.valueFilter.Test(ctx, value) {
it.nextKey = key
it.nextValue = value
break
}
it.nextKey = nil
it.nextValue = nil
}
return true
}
func (it *KeyValueFilteredIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.currentKey = it.nextKey
it.currentValue = it.nextValue
it.nextKey = nil
it.nextValue = nil
return true
}
func (it *KeyValueFilteredIterator) Key(ctx *Context) Value {
return it.currentKey
}
func (it *KeyValueFilteredIterator) Value(ctx *Context) Value {
return it.currentValue
}
// immutableSliceIterator iterates over an immutable slice.
type immutableSliceIterator[T Value] struct {
i int
elements []T
}
func (it *immutableSliceIterator[T]) HasNext(*Context) bool {
return it.i < len(it.elements)-1
}
func (it *immutableSliceIterator[T]) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.i++
return true
}
func (it *immutableSliceIterator[T]) Key(*Context) Value {
return Int(it.i)
}
func (it *immutableSliceIterator[T]) Value(ctx *Context) Value {
return it.elements[it.i]
}
type indexableIterator struct {
i int
len int
val Indexable
}
func (it *indexableIterator) HasNext(*Context) bool {
return it.i < it.len-1
}
func (it *indexableIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.i++
return true
}
func (it *indexableIterator) Key(*Context) Value {
return Int(it.i)
}
func (it *indexableIterator) Value(ctx *Context) Value {
return it.val.At(ctx, it.i)
}
func (s *ByteSlice) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&indexableIterator{
i: -1,
len: s.Len(),
val: s,
})
}
func (s *RuneSlice) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&indexableIterator{
i: -1,
len: s.Len(),
val: s,
})
}
func (s String) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&indexableIterator{
i: -1,
len: s.Len(),
val: s,
})
}
func (c *BytesConcatenation) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&indexableIterator{
i: -1,
len: c.Len(),
val: c,
})
}
func (c *StringConcatenation) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&indexableIterator{
i: -1,
len: c.Len(),
val: c,
})
}
type ValueListIterator struct {
list *ValueList
i int
}
func (it ValueListIterator) HasNext(*Context) bool {
return it.i < len(it.list.elements)-1
}
func (it *ValueListIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.i++
return true
}
func (it *ValueListIterator) Key(ctx *Context) Value {
return Int(it.i)
}
func (it *ValueListIterator) Value(*Context) Value {
return it.list.elements[it.i]
}
func (list *ValueList) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&ValueListIterator{list: list, i: -1})
}
func (list *List) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return list.underlyingList.Iterator(ctx, config)
}
type NumberListIterator[T interface {
constraints.Integer | constraints.Float
Serializable
}] struct {
list *NumberList[T]
i int
}
func (it NumberListIterator[T]) HasNext(*Context) bool {
return it.i < len(it.list.elements)-1
}
func (it *NumberListIterator[T]) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.i++
return true
}
func (it *NumberListIterator[T]) Key(ctx *Context) Value {
return Int(it.i)
}
func (it *NumberListIterator[T]) Value(*Context) Value {
return it.list.elements[it.i]
}
func (list *NumberList[T]) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&NumberListIterator[T]{list: list, i: -1})
}
type BitSetIterator struct {
set *bitset.BitSet
nextIndex uint //start at 0
}
func (it BitSetIterator) HasNext(*Context) bool {
return it.nextIndex < it.set.Len()
}
func (it *BitSetIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.nextIndex++
return true
}
func (it *BitSetIterator) Key(ctx *Context) Value {
return Int(it.nextIndex - 1)
}
func (it *BitSetIterator) Value(*Context) Value {
return Bool(it.set.Test(it.nextIndex - 1))
}
func (list *BoolList) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&BitSetIterator{set: list.elements, nextIndex: 0})
}
type StrListIterator struct {
list *StringList
i int
}
func (it StrListIterator) HasNext(*Context) bool {
return it.i < len(it.list.elements)-1
}
func (it *StrListIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.i++
return true
}
func (it *StrListIterator) Key(ctx *Context) Value {
return Int(it.i)
}
func (it *StrListIterator) Value(*Context) Value {
return it.list.elements[it.i]
}
func (list *StringList) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&StrListIterator{list: list, i: -1})
}
type TupleIterator struct {
tuple Tuple
i int
}
func (it TupleIterator) HasNext(*Context) bool {
return it.i < len(it.tuple.elements)-1
}
func (it *TupleIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.i++
return true
}
func (it *TupleIterator) Key(ctx *Context) Value {
return Int(it.i)
}
func (it *TupleIterator) Value(*Context) Value {
return it.tuple.elements[it.i]
}
func (tuple Tuple) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&TupleIterator{tuple: tuple, i: -1})
}
type indexedEntryIterator struct {
i int
len int
entries map[string]Value
currentValue Value
}
func (it *indexedEntryIterator) HasNext(*Context) bool {
return it.i < it.len
}
func (it *indexedEntryIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.currentValue = it.entries[strconv.Itoa(it.i)]
it.i++
return true
}
func (it *indexedEntryIterator) Key(*Context) Value {
return Int(it.i - 1)
}
func (it *indexedEntryIterator) Value(*Context) Value {
if it.currentValue == nil {
panic("no value")
}
return it.currentValue
}
type ArrayIterator struct {
elements []Value
i int
}
func (it ArrayIterator) HasNext(*Context) bool {
return it.i < len(it.elements)-1
}
func (it *ArrayIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.i++
return true
}
func (it *ArrayIterator) Key(ctx *Context) Value {
return Int(it.i)
}
func (it *ArrayIterator) Value(*Context) Value {
return it.elements[it.i]
}
func (a *Array) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&ArrayIterator{elements: *a, i: -1})
}
type IpropsIterator struct {
keys []string
values []Value
i int
}
// NewIpropsIterator creates an IpropsIterator, the provided keys slice and values slice should not be modified.
func NewIpropsIterator(ctx *Context, keys []string, values []Value, config IteratorConfiguration) Iterator {
it := &IpropsIterator{
i: -1,
keys: keys,
values: values,
}
return config.CreateIterator(it)
}
func (it *IpropsIterator) HasNext(*Context) bool {
return it.i < len(it.keys)-1
}
func (it *IpropsIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.i++
return true
}
func (it *IpropsIterator) Key(*Context) Value {
return String(it.keys[it.i])
}
func (it *IpropsIterator) Value(*Context) Value {
return it.values[it.i]
}
func (obj *Object) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
closestState := ctx.GetClosestState()
obj._lock(closestState)
defer obj._unlock(closestState)
values := make([]Value, len(obj.values))
for i, e := range obj.values {
values[i] = e
}
return NewIpropsIterator(ctx, slices.Clone(obj.keys), values, config)
}
func (rec *Record) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return NewIpropsIterator(ctx, rec.keys, ToValueList(rec.values), config)
}
type IntRangeIterator struct {
range_ IntRange
next int64
}
func (it *IntRangeIterator) HasNext(*Context) bool {
return it.next <= it.range_.end
}
func (it *IntRangeIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.next += 1
return true
}
func (it *IntRangeIterator) Key(ctx *Context) Value {
return Int(it.next - 1 - it.range_.start)
}
func (it *IntRangeIterator) Value(*Context) Value {
return Int(it.next - 1)
}
func (r IntRange) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
if r.unknownStart {
panic(ErrUnknownStartIntRange)
}
return config.CreateIterator(&IntRangeIterator{
range_: r,
next: r.start,
})
}
type FloatRangeIterator struct {
range_ FloatRange
}
func (it *FloatRangeIterator) HasNext(*Context) bool {
return false
}
func (it *FloatRangeIterator) Next(ctx *Context) bool {
return false
}
func (it *FloatRangeIterator) Key(ctx *Context) Value {
panic(ErrNotImplementedYet)
}
func (it *FloatRangeIterator) Value(*Context) Value {
panic(ErrNotImplementedYet)
}
func (r FloatRange) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
if r.unknownStart {
panic(ErrUnknownStartFloatRange)
}
return config.CreateIterator(&FloatRangeIterator{
range_: r,
})
}
type RuneRangeIterator struct {
range_ RuneRange
next rune
}
func (it *RuneRangeIterator) HasNext(*Context) bool {
return it.next <= it.range_.End
}
func (it *RuneRangeIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
it.next += 1
return true
}
func (it *RuneRangeIterator) Key(ctx *Context) Value {
return Rune(it.next - 1 - it.range_.Start)
}
func (it *RuneRangeIterator) Value(*Context) Value {
return Rune(it.next - 1)
}
func (r RuneRange) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return config.CreateIterator(&RuneRangeIterator{
range_: r,
next: r.Start,
})
}
type QuantityRangeIterator struct {
intNext, intEnd int64
floatNext, floatEnd float64
current reflect.Value
float bool
index int
}
func (it *QuantityRangeIterator) HasNext(*Context) bool {
if it.index == -1 {
return true
}
if it.float {
return it.floatNext <= it.floatEnd
}
return it.intNext <= it.intEnd
}
func (it *QuantityRangeIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
if it.index >= 0 {
if it.float {
it.current.SetFloat(it.floatNext)
it.floatNext += math.Nextafter(it.floatNext, math.MaxFloat64)
} else {
it.current.SetInt(it.intNext)
it.intNext++
}
}
it.index++
return true
}
func (it *QuantityRangeIterator) Key(ctx *Context) Value {
return Int(it.index)
}
func (it *QuantityRangeIterator) Value(*Context) Value {
return it.current.Interface().(Serializable)
}
func (r QuantityRange) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
it := &QuantityRangeIterator{
index: -1,
}
var startVal Serializable
if r.unknownStart {
startVal = getQuantityTypeStart(r.end)
} else {
startVal = r.start
}
start := reflect.ValueOf(startVal)
ptr := reflect.New(start.Type())
ptr.Elem().Set(start)
it.current = ptr.Elem()
if start.Kind() == reflect.Float64 {
it.float = true
it.floatEnd = reflect.ValueOf(r.InclusiveEnd()).Float()
it.floatNext = math.Nextafter(start.Float(), math.MaxFloat64)
} else {
it.intEnd = reflect.ValueOf(r.InclusiveEnd()).Int()
it.intNext = start.Int() + 1
}
return config.CreateIterator(it)
}
type PatternIterator struct {
hasNext func(*PatternIterator, *Context) bool
next func(*PatternIterator, *Context) bool
key func(*PatternIterator, *Context) Value
value func(*PatternIterator, *Context) Value
}
func (it *PatternIterator) HasNext(ctx *Context) bool {
return it.hasNext(it, ctx)
}
func (it *PatternIterator) Next(ctx *Context) bool {
if !it.HasNext(ctx) {
return false
}
return it.next(it, ctx)
}
func (it *PatternIterator) Key(ctx *Context) Value {
return it.key(it, ctx)
}
func (it *PatternIterator) Value(ctx *Context) Value {
return it.value(it, ctx)
}
func NewEmptyPatternIterator() *PatternIterator {
return &PatternIterator{
hasNext: func(pi *PatternIterator, ctx *Context) bool {
return false
},
next: func(pi *PatternIterator, ctx *Context) bool {
return false
},
}
}
//patterns
func (patt PathPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return nil
}
func (patt HostPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return nil
}
func (patt URLPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return nil
}
func (patt RegexPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return nil
}
func (patt TypePattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return nil
}
func (patt *NamedSegmentPathPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return nil
}
func (patt OptionPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return nil
}
func (patt ExactValuePattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
i := -1
return config.CreateIterator(&PatternIterator{
hasNext: func(pi *PatternIterator, ctx *Context) bool {
return i < 0
},
next: func(pi *PatternIterator, ctx *Context) bool {
i++
return true
},
key: func(pi *PatternIterator, ctx *Context) Value {
return Int(i)
},
value: func(pi *PatternIterator, ctx *Context) Value {
if i == 0 {
//TODO: clone ?
return patt.value
}
return nil
},
})
}
func (patt ExactStringPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
i := -1
return config.CreateIterator(&PatternIterator{
hasNext: func(pi *PatternIterator, ctx *Context) bool {
return i < 0
},
next: func(pi *PatternIterator, ctx *Context) bool {
i++
return true
},
key: func(pi *PatternIterator, ctx *Context) Value {
return Int(i)
},
value: func(pi *PatternIterator, ctx *Context) Value {
if i == 0 {
//TODO: clone ?
return patt.value
}
return nil
},
})
}
func (patt UnionPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
var iterator = patt.cases[0].Iterator(ctx, IteratorConfiguration{})
i := -1
caseIndex := 0
// TODO:
// If a value is present in two patterns in the union it will appear twice.
// Is it okay ? Is it possible to fix this without too much computation/memory ?
return config.CreateIterator(&PatternIterator{
hasNext: func(_ *PatternIterator, ctx *Context) bool {
return iterator.HasNext(ctx) || caseIndex < len(patt.cases)-1
},
next: func(it *PatternIterator, ctx *Context) bool {
if iterator.Next(ctx) {
i++
return true
}
caseIndex++
if caseIndex >= len(patt.cases) {
return false
}
iterator = patt.cases[caseIndex].Iterator(ctx, IteratorConfiguration{})
return it.next(it, ctx)
},
key: func(_ *PatternIterator, ctx *Context) Value {
return Int(i)
},
value: func(_ *PatternIterator, ctx *Context) Value {
return iterator.Value(ctx)
},
})
}
func (patt *IntersectionPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
baseIt := patt.cases[0].Iterator(ctx, IteratorConfiguration{})
if baseIt == nil {
return nil
}
var (
next, current Value
i = -1
)
return config.CreateIterator(&PatternIterator{
hasNext: func(pi *PatternIterator, ctx *Context) bool {
if next != nil {
return true
}
for {
if !baseIt.HasNext(ctx) {
return false
}
baseIt.Next(ctx)
next = baseIt.Value(ctx)
ok := true
for _, otherCases := range patt.cases[1:] {
if !otherCases.Test(ctx, next) {
ok = false
break
}
}
if ok {
break
}
next = nil
}
return true
},
next: func(pi *PatternIterator, ctx *Context) bool {
if !pi.hasNext(pi, ctx) {
return false
}
current = next
next = nil
i++
return true
},
key: func(pi *PatternIterator, ctx *Context) Value {
return Int(i)
},
value: func(pi *PatternIterator, ctx *Context) Value {
return current
},
})
}
func (patt LengthCheckingStringPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return NewEmptyPatternIterator()
}
func (patt SequenceStringPattern) Iterator(ctx *Context, config IteratorConfiguration) Iterator {
return createStringSequenceIterator(ctx, patt.elements, config)
}
func createStringSequenceIterator(ctx *Context, elements []StringPattern, config IteratorConfiguration) Iterator {
if len(elements) == 0 { // empty string
i := -1
return config.CreateIterator(&PatternIterator{
hasNext: func(pi *PatternIterator, ctx *Context) bool {
return i < 0
},
next: func(pi *PatternIterator, ctx *Context) bool {
i++
return true
},
key: func(pi *PatternIterator, ctx *Context) Value {
return Int(i)
},
value: func(pi *PatternIterator, ctx *Context) Value {
if i == 0 {
//TODO: clone ?
return String("")
}
return nil