/
sequence.go
1340 lines (1285 loc) · 39.3 KB
/
sequence.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 internal
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"reflect"
)
// There are a *lot* of Sequence methods, and each one needs to be able to
// handle a dozen different data types, so the implementation is spread out
// among a few separate files:
//
// - sequence.go: The Sequence type itself and Go methods thereof.
// - sequence_immutable.go: Io methods for non-mutating Sequence methods.
// - sequence_mutable.go: Io methods for mutating Sequence methods.
// - sequence_string.go: Implementation of Sequence as a string type,
// including encodings and representation.
// - sequence_math.go: Mathematical methods and operations. Eventually,
// this should have different versions for different arches.
// A Sequence is a collection of data of one fixed-size type.
type Sequence struct {
Value interface{}
Mutable bool
Code string // encoding
}
// tagSequence is the Tag type for Sequence values.
type tagSequence struct{}
func (tagSequence) Activate(vm *VM, self, target, locals, context *Object, msg *Message) *Object {
return self
}
func (tagSequence) CloneValue(value interface{}) interface{} {
s := value.(Sequence)
ns := Sequence{
Mutable: s.Mutable,
Code: s.Code,
}
switch v := s.Value.(type) {
case []byte:
ns.Value = append([]byte{}, v...)
case []uint16:
ns.Value = append([]uint16{}, v...)
case []uint32:
ns.Value = append([]uint32{}, v...)
case []uint64:
ns.Value = append([]uint64{}, v...)
case []int8:
ns.Value = append([]int8{}, v...)
case []int16:
ns.Value = append([]int16{}, v...)
case []int32:
ns.Value = append([]int32{}, v...)
case []int64:
ns.Value = append([]int64{}, v...)
case []float32:
ns.Value = append([]float32{}, v...)
case []float64:
ns.Value = append([]float64{}, v...)
default:
panic(fmt.Sprintf("unknown sequence type %T", s.Value))
}
return ns
}
func (tagSequence) String() string {
return "Sequence"
}
// SequenceTag is the Tag for Sequence values. Activate returns self.
// CloneValue copies the sequence.
var SequenceTag tagSequence
// SeqKind represents a sequence data type.
type SeqKind struct {
kind reflect.Type
}
// SeqKind values.
var (
SeqU8 = SeqKind{seqU8}
SeqU16 = SeqKind{seqU16}
SeqU32 = SeqKind{seqU32}
SeqU64 = SeqKind{seqU64}
SeqS8 = SeqKind{seqS8}
SeqS16 = SeqKind{seqS16}
SeqS32 = SeqKind{seqS32}
SeqS64 = SeqKind{seqS64}
SeqF32 = SeqKind{seqF32}
SeqF64 = SeqKind{seqF64}
)
var (
seqU8 reflect.Type = reflect.TypeOf([]byte(nil))
seqU16 reflect.Type = reflect.TypeOf([]uint16(nil))
seqU32 reflect.Type = reflect.TypeOf([]uint32(nil))
seqU64 reflect.Type = reflect.TypeOf([]uint64(nil))
seqS8 reflect.Type = reflect.TypeOf([]int8(nil))
seqS16 reflect.Type = reflect.TypeOf([]int16(nil))
seqS32 reflect.Type = reflect.TypeOf([]int32(nil))
seqS64 reflect.Type = reflect.TypeOf([]int64(nil))
seqF32 reflect.Type = reflect.TypeOf([]float32(nil))
seqF64 reflect.Type = reflect.TypeOf([]float64(nil))
)
// SeqMaxItemSize is the maximum size in bytes of a single sequence element.
const SeqMaxItemSize = 8
// Encoding returns the suggested default encoding for the sequence kind. This
// is utf8 for uint8 kinds, utf16 for uint16, utf32 for int32, and number for
// all other kinds.
func (kind SeqKind) Encoding() string {
switch kind {
case SeqU8:
return "utf8"
case SeqU16:
return "utf16"
case SeqS32:
return "utf32"
}
return "number"
}
// ItemSize returns the number of bytes required to represent one element of
// the type represented by the SeqKind.
func (kind SeqKind) ItemSize() int {
switch kind {
case SeqU8, SeqS8:
return 1
case SeqU16, SeqS16:
return 2
case SeqU32, SeqS32, SeqF32:
return 4
case SeqU64, SeqS64, SeqF64:
return 8
default:
panic(fmt.Sprintf("unrecognized sequence element type %#v", kind.kind))
}
}
// copySeqVal creates a copy of a Sequence value slice. If the value is that of
// an existing sequence object and that sequence is mutable, then callers
// should hold the object's lock.
func copySeqVal(value interface{}) interface{} {
switch v := value.(type) {
case []byte:
return append([]byte{}, v...)
case []uint16:
return append([]uint16{}, v...)
case []uint32:
return append([]uint32{}, v...)
case []uint64:
return append([]uint64{}, v...)
case []int8:
return append([]int8{}, v...)
case []int16:
return append([]int16{}, v...)
case []int32:
return append([]int32{}, v...)
case []int64:
return append([]int64{}, v...)
case []float32:
return append([]float32{}, v...)
case []float64:
return append([]float64{}, v...)
default:
panic(fmt.Sprintf("unsupported value type %T; must be slice of basic fixed-size data type", value))
}
}
// NewSequence creates a new Sequence object with the given value and with the
// given encoding. The value must be a slice of a basic fixed-size data type,
// and it is copied. Panics if the encoding is not supported.
func (vm *VM) NewSequence(value interface{}, mutable bool, encoding string) *Object {
if !vm.CheckEncoding(encoding) {
panic(fmt.Sprintf("unsupported encoding %q", encoding))
}
seq := Sequence{
Value: copySeqVal(value),
Mutable: mutable,
Code: encoding,
}
return vm.ObjectWith(nil, vm.CoreProto("Sequence"), seq, SequenceTag)
}
// SequenceObject creates a new Sequence object with the given value directly.
func (vm *VM) SequenceObject(value Sequence) *Object {
return vm.ObjectWith(nil, vm.CoreProto("Sequence"), value, SequenceTag)
}
// SequenceFromBytes makes a mutable Sequence with the given type having the
// same bit pattern as the given bytes. If the length of b is not a multiple of
// the item size for the given kind, the extra bytes are ignored. The
// sequence's encoding will be number unless the kind is SeqU8, SeqU16, or
// SeqS32, in which cases the encoding will be utf8, utf16, or utf32,
// respectively.
func (vm *VM) SequenceFromBytes(b []byte, kind SeqKind) Sequence {
if kind == SeqU8 {
return Sequence{
Value: b,
Mutable: true,
Code: "utf8",
}
}
if kind == SeqF32 {
v := make([]float32, 0, len(b)/4)
for len(b) >= 4 {
c := binary.LittleEndian.Uint32(b)
v = append(v, math.Float32frombits(c))
b = b[4:]
}
return Sequence{
Value: v,
Mutable: true,
Code: "number",
}
}
if kind == SeqF64 {
v := make([]float64, 0, len(b)/8)
for len(b) >= 8 {
c := binary.LittleEndian.Uint64(b)
v = append(v, math.Float64frombits(c))
b = b[8:]
}
return Sequence{
Value: v,
Mutable: true,
Code: "number",
}
}
var v interface{}
switch kind {
case SeqU16:
v = make([]uint16, len(b)/2)
case SeqU32:
v = make([]uint32, len(b)/4)
case SeqU64:
v = make([]uint64, len(b)/8)
case SeqS8:
v = make([]int8, len(b))
case SeqS16:
v = make([]int16, len(b)/2)
case SeqS32:
v = make([]int32, len(b)/4)
case SeqS64:
v = make([]int64, len(b)/8)
default:
panic(fmt.Sprintf("unknown sequence kind %#v", kind))
}
binary.Read(bytes.NewReader(b), binary.LittleEndian, v)
return Sequence{
Value: v,
Mutable: true,
Code: kind.Encoding(),
}
}
// SequenceArgAt evaluates the nth argument and returns its value as a
// Sequence with its object. If a stop occurs during evaluation, the returned
// Sequence has nil value, and the stop result and status are returned. If the
// evaluated result is not a Sequence, the result has nil value, and an
// exception is returned with an ExceptionStop.
func (m *Message) SequenceArgAt(vm *VM, locals *Object, n int) (Sequence, *Object, Stop) {
v, s := m.EvalArgAt(vm, locals, n)
if s == NoStop {
v.Lock()
seq, ok := v.Value.(Sequence)
v.Unlock()
if ok {
return seq, v, NoStop
}
// Not the expected type, so return an error.
v = vm.NewExceptionf("argument %d to %s must be Sequence, not %s", n, m.Text, vm.TypeName(v))
s = ExceptionStop
}
return Sequence{}, v, s
}
// StringArgAt evaluates the nth argument, asserts that it is a Sequence, and
// returns its value as a string. If a stop occurs during evaluation, the
// returned string is empty, and the stop result and status are returned. If
// the evaluated result is not a Sequence, the result has nil value, and an
// exception is returned with an ExceptionStop.
func (m *Message) StringArgAt(vm *VM, locals *Object, n int) (string, *Object, Stop) {
v, s := m.EvalArgAt(vm, locals, n)
if s == NoStop {
v.Lock()
str, ok := v.Value.(Sequence)
if ok {
r := str.String()
v.Unlock()
return r, nil, NoStop
}
v.Unlock()
// Not the expected type, so return an error.
v = vm.NewExceptionf("argument %d to %s must be Sequence, not %s", n, m.Text, vm.TypeName(v))
s = ExceptionStop
}
return "", v, s
}
// AsStringArgAt evaluates the nth argument, then activates its asString slot
// for a string representation. If the result is not a string, then the result
// has nil value, and an exception object is returned with an ExceptionStop.
func (m *Message) AsStringArgAt(vm *VM, locals *Object, n int) (string, *Object, Stop) {
v, stop := m.EvalArgAt(vm, locals, n)
if stop != NoStop {
return "", v, stop
}
v, stop = vm.Perform(v, locals, vm.IdentMessage("asString"))
if stop == NoStop {
v.Lock()
str, ok := v.Value.(Sequence)
v.Unlock()
if ok {
return str.String(), nil, NoStop
}
v, stop = vm.NewExceptionf("argument %d to %s cannot be converted to string", n, m.Text), ExceptionStop
}
return "", v, stop
}
// Kind returns the SeqKind appropriate for this sequence. If the sequence is
// mutable, callers should hold its object's lock.
func (s Sequence) Kind() SeqKind {
switch s.Value.(type) {
case []byte:
return SeqU8
case []uint16:
return SeqU16
case []uint32:
return SeqU32
case []uint64:
return SeqU64
case []int8:
return SeqS8
case []int16:
return SeqS16
case []int32:
return SeqS32
case []int64:
return SeqS64
case []float32:
return SeqF32
case []float64:
return SeqF64
default:
panic(fmt.Sprintf("unknown sequence type %T", s.Value))
}
}
// IsMutable returns whether the sequence can be modified safely. Callers do
// not need to hold the object's lock, as mutability of a sequence should never
// change.
func (s Sequence) IsMutable() bool {
return s.Mutable
}
// IsFP returns whether the sequence has a float32 or float64 data type. If the
// sequence is mutable, callers should hold its object's lock.
func (s Sequence) IsFP() bool {
switch s.Value.(type) {
case []float32, []float64:
return true
}
return false
}
// SameType returns whether the sequence has the same data type as another. If
// either sequence is mutable, callers should hold their objects' locks.
func (s Sequence) SameType(as Sequence) bool {
return reflect.TypeOf(s.Value) == reflect.TypeOf(as.Value)
}
// Len returns the length of the sequence. If the sequence is mutable, callers
// should hold its object's lock.
func (s Sequence) Len() int {
switch v := s.Value.(type) {
case []byte:
return len(v)
case []uint16:
return len(v)
case []uint32:
return len(v)
case []uint64:
return len(v)
case []int8:
return len(v)
case []int16:
return len(v)
case []int32:
return len(v)
case []int64:
return len(v)
case []float32:
return len(v)
case []float64:
return len(v)
default:
panic(fmt.Sprintf("unknown sequence type %T", s.Value))
}
}
// FixIndex wraps an index into the sequence's size. If the sequence is
// mutable, callers should hold its object's lock.
func (s Sequence) FixIndex(i int) int {
n := s.Len()
if i >= n {
return n
}
if i < 0 {
i += n
if i < 0 {
return 0
}
}
return i
}
// ItemSize returns the number of bytes required to represent a single element
// of the sequence. If the sequence is mutable, callers should hold its
// object's lock.
func (s Sequence) ItemSize() int {
switch s.Value.(type) {
case []byte, []int8:
return 1
case []uint16, []int16:
return 2
case []uint32, []int32, []float32:
return 4
case []uint64, []int64, []float64:
return 8
}
panic(fmt.Sprintf("unknown sequencet type %T", s.Value))
}
// At returns the value of an item in the sequence as a float64. If the index
// is out of bounds, the second return value is false. Callers should hold the
// sequence object's lock if the sequence is mutable.
func (s Sequence) At(i int) (float64, bool) {
if i < 0 {
return 0, false
}
switch v := s.Value.(type) {
case []byte:
if i >= len(v) {
return 0, false
}
return float64(v[i]), true
case []uint16:
if i >= len(v) {
return 0, false
}
return float64(v[i]), true
case []uint32:
if i >= len(v) {
return 0, false
}
return float64(v[i]), true
case []uint64:
if i >= len(v) {
return 0, false
}
return float64(v[i]), true
case []int8:
if i >= len(v) {
return 0, false
}
return float64(v[i]), true
case []int16:
if i >= len(v) {
return 0, false
}
return float64(v[i]), true
case []int32:
if i >= len(v) {
return 0, false
}
return float64(v[i]), true
case []int64:
if i >= len(v) {
return 0, false
}
return float64(v[i]), true
case []float32:
if i >= len(v) {
return 0, false
}
return float64(v[i]), true
case []float64:
if i >= len(v) {
return 0, false
}
return v[i], true
default:
panic(fmt.Sprintf("unknown sequence type %T", s.Value))
}
}
// Convert changes the item type of the sequence. The conversion is such that
// the result keeps the same number of items. If the sequence is mutable,
// callers should hold its object's lock.
func (s Sequence) Convert(kind SeqKind) Sequence {
u := reflect.ValueOf(s.Value)
if u.Type() == kind.kind {
return Sequence{Value: copySeqVal(s.Value), Mutable: s.Mutable, Code: s.Code}
}
n := s.Len()
v := reflect.MakeSlice(kind.kind, n, n)
nt := kind.kind.Elem()
for i := 0; i < n; i++ {
v.Index(i).Set(u.Index(i).Convert(nt))
}
return Sequence{Value: v.Interface(), Mutable: s.Mutable, Code: s.Code}
}
// Append appends other's items to this sequence. If other has a larger item
// size than this sequence, then the result is converted to the item type of
// other. Callers should hold the sequence object's lock. Panics if this
// sequence is not mutable.
func (s Sequence) Append(other Sequence) Sequence {
if err := s.CheckMutable("Sequence.Append"); err != nil {
panic(err)
}
if s.SameType(other) {
return s.appendSameKind(other)
} else if s.ItemSize() >= other.ItemSize() {
return s.appendConvert(other)
} else {
return s.appendGrow(other)
}
}
func (s Sequence) appendSameKind(other Sequence) Sequence {
switch v := s.Value.(type) {
case []byte:
s.Value = append(v, other.Value.([]byte)...)
case []uint16:
s.Value = append(v, other.Value.([]uint16)...)
case []uint32:
s.Value = append(v, other.Value.([]uint32)...)
case []uint64:
s.Value = append(v, other.Value.([]uint64)...)
case []int8:
s.Value = append(v, other.Value.([]int8)...)
case []int16:
s.Value = append(v, other.Value.([]int16)...)
case []int32:
s.Value = append(v, other.Value.([]int32)...)
case []int64:
s.Value = append(v, other.Value.([]int64)...)
case []float32:
s.Value = append(v, other.Value.([]float32)...)
case []float64:
s.Value = append(v, other.Value.([]float64)...)
default:
panic(fmt.Sprintf("unknown sequence type %T", s.Value))
}
return s
}
func (s Sequence) appendConvert(other Sequence) Sequence {
a := reflect.ValueOf(s.Value)
b := reflect.ValueOf(other.Value)
t := a.Type().Elem()
for i := 0; i < b.Len(); i++ {
a = reflect.Append(a, b.Index(i).Convert(t))
}
s.Value = a.Interface()
return s
}
func (s Sequence) appendGrow(other Sequence) Sequence {
old := reflect.ValueOf(s.Value)
b := reflect.ValueOf(other.Value)
t := b.Type().Elem()
a := reflect.MakeSlice(b.Type(), 0, old.Len()+b.Len())
for i := 0; i < old.Len(); i++ {
a = reflect.Append(a, old.Index(i).Convert(t))
}
a = reflect.AppendSlice(a, b)
s.Value = a.Interface()
return s
}
// Insert inserts the elements of another sequence, converted to this
// sequence's type, at a given index. If the index is beyond the length of the
// sequence, then zeros are inserted as needed. Callers should hold the
// sequence object's lock. Panics if k < 0 or if s is immutable.
func (s Sequence) Insert(other Sequence, k int) Sequence {
if err := s.CheckMutable("Sequence.Insert"); err != nil {
panic(err)
}
if sl := s.Len(); k > sl {
s = s.extend(k)
}
if s.SameType(other) {
return s.insertSameKind(other, k)
}
return s.insertConvert(other, k)
}
func (s Sequence) extend(k int) Sequence {
s.Value = copySeqVal(s.Value)
switch v := s.Value.(type) {
case []byte:
if len(v) < k {
v = append(v, make([]byte, k-len(v))...)
}
s.Value = v
case []uint16:
if len(v) < k {
v = append(v, make([]uint16, k-len(v))...)
}
s.Value = v
case []uint32:
if len(v) < k {
v = append(v, make([]uint32, k-len(v))...)
}
s.Value = v
case []uint64:
if len(v) < k {
v = append(v, make([]uint64, k-len(v))...)
}
s.Value = v
case []int8:
if len(v) < k {
v = append(v, make([]int8, k-len(v))...)
}
s.Value = v
case []int16:
if len(v) < k {
v = append(v, make([]int16, k-len(v))...)
}
s.Value = v
case []int32:
if len(v) < k {
v = append(v, make([]int32, k-len(v))...)
}
s.Value = v
case []int64:
if len(v) < k {
v = append(v, make([]int64, k-len(v))...)
}
s.Value = v
case []float32:
if len(v) < k {
v = append(v, make([]float32, k-len(v))...)
}
s.Value = v
case []float64:
if len(v) < k {
v = append(v, make([]float64, k-len(v))...)
}
s.Value = v
default:
panic(fmt.Sprintf("unknown sequence kind %T", s.Value))
}
return s
}
func (s Sequence) insertSameKind(other Sequence, k int) Sequence {
s.Value = copySeqVal(s.Value)
switch v := s.Value.(type) {
case []byte:
w := other.Value.([]byte)
v = append(v, make([]byte, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
case []uint16:
w := other.Value.([]uint16)
v = append(v, make([]uint16, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
case []uint32:
w := other.Value.([]uint32)
v = append(v, make([]uint32, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
case []uint64:
w := other.Value.([]uint64)
v = append(v, make([]uint64, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
case []int8:
w := other.Value.([]int8)
v = append(v, make([]int8, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
case []int16:
w := other.Value.([]int16)
v = append(v, make([]int16, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
case []int32:
w := other.Value.([]int32)
v = append(v, make([]int32, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
case []int64:
w := other.Value.([]int64)
v = append(v, make([]int64, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
case []float32:
w := other.Value.([]float32)
v = append(v, make([]float32, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
case []float64:
w := other.Value.([]float64)
v = append(v, make([]float64, len(w))...)
copy(v[k+len(w):], v[k:])
copy(v[k:], w)
s.Value = v
default:
panic(fmt.Sprintf("unknown sequence type %T", s.Value))
}
return s
}
func (s Sequence) insertConvert(other Sequence, k int) Sequence {
s.Value = copySeqVal(s.Value)
a := reflect.ValueOf(s.Value)
b := reflect.ValueOf(other.Value)
al := a.Len()
bl := b.Len()
z := reflect.MakeSlice(a.Type(), bl, bl)
a = reflect.AppendSlice(a, z)
reflect.Copy(a.Slice(k+bl, al), a.Slice(k, al))
at := a.Type().Elem()
for i := 0; i < bl; i++ {
a.Index(k + i).Set(b.Index(i).Convert(at))
}
s.Value = a.Interface()
return s
}
// Find locates the first instance of other in the sequence following start.
// Comparison is done following conversion to the type of s. If there is no
// match, the result is -1. If s or other are mutable, then callers should hold
// their objects' locks.
func (s Sequence) Find(other Sequence, start int) int {
ol := other.Len()
if ol == 0 {
return start
}
m := reflect.ValueOf(s.Value)
o := reflect.ValueOf(other.Value)
mt := m.Type().Elem()
checks := s.Len() - ol + 1
switch s.Value.(type) {
case []byte, []uint16, []uint32, []uint64:
for i := start; i < checks; i++ {
if findUMatch(m, o, i, ol, mt) {
return i
}
}
case []int8, []int16, []int32, []int64:
for i := start; i < checks; i++ {
if findIMatch(m, o, i, ol, mt) {
return i
}
}
case []float32, []float64:
for i := start; i < checks; i++ {
if findFMatch(m, o, i, ol, mt) {
return i
}
}
}
return -1
}
// RFind locates the last instance of other in the sequence ending before end.
// Comparison is done following conversion to the type of s. If there is no
// match, the result is -1. If s or other are mutable, then callers should hold
// their objects' locks.
func (s Sequence) RFind(other Sequence, end int) int {
ol := other.Len()
if ol == 0 {
return end
}
if end > s.Len() {
end = s.Len()
}
m := reflect.ValueOf(s.Value)
o := reflect.ValueOf(other.Value)
mt := m.Type().Elem()
switch s.Value.(type) {
case []byte, []uint16, []uint32, []uint64:
for i := end - ol; i >= 0; i-- {
if findUMatch(m, o, i, ol, mt) {
return i
}
}
case []int8, []int16, []int32, []int64:
for i := end - ol; i >= 0; i-- {
if findIMatch(m, o, i, ol, mt) {
return i
}
}
case []float32, []float64:
for i := end - ol; i >= 0; i-- {
if findFMatch(m, o, i, ol, mt) {
return i
}
}
}
return -1
}
func findUMatch(m, o reflect.Value, i, ol int, mt reflect.Type) bool {
for k := 0; k < ol; k++ {
x := m.Index(i + k).Uint()
y := o.Index(k).Convert(mt).Uint()
if x != y {
return false
}
}
return true
}
func findIMatch(m, o reflect.Value, i, ol int, mt reflect.Type) bool {
for k := 0; k < ol; k++ {
x := m.Index(i + k).Int()
y := o.Index(k).Convert(mt).Int()
if x != y {
return false
}
}
return true
}
func findFMatch(m, o reflect.Value, i, ol int, mt reflect.Type) bool {
for k := 0; k < ol; k++ {
x := m.Index(i + k).Float()
y := o.Index(k).Convert(mt).Float()
if x != y {
return false
}
}
return true
}
// Compare finds the lexicographical ordering between s and other in the
// element-wise sense, returning -1 if s < other, 1 if s > other, and 0 if
// s == other. If s or other are mutable, then callers should hold their
// objects' locks.
func (s Sequence) Compare(other Sequence) int {
sl := s.Len()
ol := other.Len()
n := sl
if sl > ol {
n = ol
}
m := reflect.ValueOf(s.Value)
o := reflect.ValueOf(other.Value)
mt := m.Type().Elem()
switch s.Value.(type) {
case []byte, []uint16, []uint32, []uint64:
for i := 0; i < n; i++ {
x := m.Index(i).Uint()
y := o.Index(i).Convert(mt).Uint()
if x < y {
return -1
}
if x > y {
return 1
}
}
case []int8, []int16, []int32, []int64:
for i := 0; i < n; i++ {
x := m.Index(i).Int()
y := o.Index(i).Convert(mt).Int()
if x < y {
return -1
}
if x > y {
return 1
}
}
case []float32, []float64:
for i := 0; i < n; i++ {
x := m.Index(i).Float()
y := o.Index(i).Convert(mt).Float()
if x < y {
return -1
}
if x > y {
return 1
}
}
}
if sl < ol {
return -1
}
if sl > ol {
return 1
}
return 0
}
// Slice selects a linear portion of the sequence. Callers should hold the
// sequence object's lock if it is mutable.
func (s Sequence) Slice(start, stop, step int) Sequence {
if !s.IsMutable() {
panic("cannot slice immutable sequence")
}
l := s.Len()
start = fixSliceIndex(start, step, l)
stop = fixSliceIndex(stop, step, l)
if step > 0 {
return s.sliceForward(start, stop, step)
} else if step < 0 {
return s.sliceBackward(start, stop, step)
} else {
panic("cannot slice with zero step")
}
}
func (s Sequence) sliceForward(start, stop, step int) Sequence {
switch v := s.Value.(type) {
case []byte:
w := []byte{}
for start < stop {
w = append(w, v[start])
start += step
}
s.Value = w
case []uint16:
w := []uint16{}
for start < stop {
w = append(w, v[start])
start += step
}
s.Value = w
case []uint32:
w := []uint32{}
for start < stop {
w = append(w, v[start])
start += step
}
s.Value = w
case []uint64:
w := []uint64{}
for start < stop {
w = append(w, v[start])
start += step
}
s.Value = w
case []int8:
w := []int8{}
for start < stop {
w = append(w, v[start])
start += step
}
s.Value = w
case []int16:
w := []int16{}
for start < stop {
w = append(w, v[start])
start += step
}
s.Value = w
case []int32:
w := []int32{}
for start < stop {
w = append(w, v[start])
start += step
}
s.Value = w
case []int64:
w := []int64{}
for start < stop {
w = append(w, v[start])
start += step
}
s.Value = w
case []float32:
w := []float32{}
for start < stop {
w = append(w, v[start])
start += step
}
s.Value = w
case []float64: