forked from phR0ze/n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice_ref.go
1217 lines (1087 loc) · 31.5 KB
/
slice_ref.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 n
import (
"fmt"
"reflect"
"strings"
"github.com/pkg/errors"
)
// RefSlice implements the Slice interface providing a generic way to work with slice types
// including convenience methods on par with rapid development languages. This type
// incurs the typical 10x reflection overhead costs. For high performance use the Slice
// implementation matching the type your working with or implement a new type that satisfies
// the Slice interface.
type RefSlice struct {
k reflect.Kind
v *reflect.Value
}
// NewRefSlice uses reflection to encapsulate the given Go slice type inside a new *RefSlice.
// Expects a Go slice type to be provided and will create an empty *RefSlice if nothing valid
// is given.
func NewRefSlice(slice interface{}) (new *RefSlice) {
new = &RefSlice{}
v := reflect.ValueOf(slice)
k := v.Kind()
x, interfaceSliceType := slice.([]interface{})
switch {
// Return the NSlice.Nil
case k == reflect.Invalid:
// Iterate over array and append
case k == reflect.Array:
new = newEmptySlice(slice)
for i := 0; i < v.Len(); i++ {
*new.v = reflect.Append(*new.v, v.Index(i))
}
// Convert []interface to slice of elem type
case interfaceSliceType:
new = NewRefSliceV(x...)
// Slice of distinct type can be used directly
case k == reflect.Slice:
new.v = &v
new.k = k
// Append single items
default:
new = newEmptySlice(slice)
*new.v = reflect.Append(*new.v, v)
}
return
}
// NewRefSliceV creates a new *RefSlice from the given variadic elements. Always returns
// at least a reference to an empty RefSlice.
func NewRefSliceV(elems ...interface{}) (new *RefSlice) {
new = &RefSlice{}
// Return RefSlice.Nil if nothing given
if len(elems) == 0 {
return
}
// Create new slice from the type of the first non Invalid element
var slice *reflect.Value
for i := range elems {
// Create target slice from first Valid element
if slice == nil && reflect.ValueOf(elems[i]).IsValid() {
typ := reflect.SliceOf(reflect.TypeOf(elems[i]))
v := reflect.MakeSlice(typ, 0, 10)
slice = &v
}
// Append element to slice
if slice != nil {
elem := reflect.ValueOf(elems[i])
*slice = reflect.Append(*slice, elem)
}
}
if slice != nil {
new.v = slice
new.k = slice.Kind()
}
return
}
// create a new empty slice of the given type or element type if a slice/array
// want to return a new Slice so that we can use this in the AppendX functions
// to defer creating an underlying slice type until we have an actual type to work with.
func newEmptySlice(elems interface{}) (new *RefSlice) {
new = &RefSlice{}
v := reflect.ValueOf(elems)
typ := reflect.TypeOf([]interface{}{})
k := v.Kind()
switch k {
// Use a new generic slice for nils
case reflect.Invalid:
// Use the element type of slice/arrays
case reflect.Slice, reflect.Array:
// Use slice type if not generic
if _, ok := elems.([]interface{}); !ok {
typ = reflect.SliceOf(v.Type().Elem())
} else {
// For generics try to find actual element type
if v.Len() != 0 {
elem := v.Index(0).Interface()
if elem != nil {
typ = reflect.SliceOf(reflect.TypeOf(elem))
}
}
}
default:
typ = reflect.SliceOf(v.Type())
}
// Create new slice with type of the element
slice := reflect.MakeSlice(typ, 0, 10)
new.v = &slice
new.k = k
return
}
// A is an alias to String for brevity
func (p *RefSlice) A() string {
return p.String()
}
// All tests if this Slice is not empty or optionally if it contains
// all of the given variadic elements; Incompatible types will return false.
func (p *RefSlice) All(elems ...interface{}) bool {
// No elements
if p.Nil() || p.Len() == 0 {
return false
}
// Not looking for anything
if len(elems) == 0 {
return true
}
// Looking for something specific returns false if incompatible type
for i := range elems {
x := reflect.ValueOf(elems[i])
if p.v.Type().Elem() != x.Type() {
break
} else {
for j := 0; j < p.v.Len(); j++ {
if p.v.Index(j).Interface() == x.Interface() {
return true
}
}
}
}
return false
}
// AllS tests if this Slice contains all of the given Slice's elements;
// Incompatible types will return false;
// Supports RefSlice, *RefSlice, Slice and Go slice types
func (p *RefSlice) AllS(slice interface{}) bool {
// No elements
if p.Nil() || p.Len() == 0 {
return false
}
// Handle supported types
var v reflect.Value
if x, ok := slice.(RefSlice); ok {
if !x.Nil() {
v = *(x.v)
}
} else if x, ok := slice.(*RefSlice); ok {
if !x.Nil() {
v = *(x.v)
}
} else if x, ok := slice.(ISlice); ok {
if !x.Nil() {
v = reflect.ValueOf(x.O())
}
} else {
v = reflect.ValueOf(slice)
}
if !v.IsValid() {
return false
}
if p.v.Type() == v.Type() {
for i := 0; i < v.Len(); i++ {
for j := 0; j < p.v.Len(); j++ {
if p.v.Index(j).Interface() == v.Index(i).Interface() {
return true
}
}
}
}
return false
}
// Any tests if this Slice is not empty or optionally if it contains
// any of the given variadic elements. Incompatible types will return false.
func (p *RefSlice) Any(elems ...interface{}) bool {
// No elements
if p.Nil() || p.Len() == 0 {
return false
}
// Not looking for anything
if len(elems) == 0 {
return true
}
// Looking for something specific returns false if incompatible type
for i := range elems {
x := reflect.ValueOf(elems[i])
if p.v.Type().Elem() != x.Type() {
break
} else {
for j := 0; j < p.v.Len(); j++ {
if p.v.Index(j).Interface() == x.Interface() {
return true
}
}
}
}
return false
}
// AnyS tests if this Slice contains any of the given Slice's elements.
// Incompatible types will return false.
// Supports RefSlice, *RefSlice, Slice and Go slice types
func (p *RefSlice) AnyS(slice interface{}) bool {
// No elements
if p.Nil() || p.Len() == 0 {
return false
}
// Handle supported types
var v reflect.Value
if x, ok := slice.(RefSlice); ok {
if !x.Nil() {
v = *(x.v)
}
} else if x, ok := slice.(*RefSlice); ok {
if !x.Nil() {
v = *(x.v)
}
} else if x, ok := slice.(ISlice); ok {
if !x.Nil() {
v = reflect.ValueOf(x.O())
}
} else {
v = reflect.ValueOf(slice)
}
if !v.IsValid() {
return false
}
if p.v.Type() == v.Type() {
for i := 0; i < v.Len(); i++ {
for j := 0; j < p.v.Len(); j++ {
if p.v.Index(j).Interface() == v.Index(i).Interface() {
return true
}
}
}
}
return false
}
// AnyW tests if this Slice contains any that match the lambda selector.
func (p *RefSlice) AnyW(sel func(O) bool) bool {
return p.CountW(sel) != 0
}
// Append an element to the end of this Slice and returns a reference to this Slice.
func (p *RefSlice) Append(elem interface{}) ISlice {
if p.Nil() {
if p == nil {
p = newEmptySlice(elem)
} else {
*p = *(newEmptySlice(elem))
}
}
x := reflect.ValueOf(elem)
if p.v.Type().Elem() != x.Type() {
panic(fmt.Sprintf("can't append type '%v' to '%v'", x.Type(), p.v.Type()))
} else {
*p.v = reflect.Append(*p.v, x)
}
return p
}
// AppendV appends the variadic elements to the end of this Slice and returns a reference to this Slice.
func (p *RefSlice) AppendV(elems ...interface{}) ISlice {
if p.Nil() {
if p == nil {
p = newEmptySlice(elems)
} else {
*p = *(newEmptySlice(elems))
}
}
for _, elem := range elems {
p.Append(elem)
}
return p
}
// At returns the element at the given index location. Allows for negative notation.
func (p *RefSlice) At(i int) (elem *Object) {
elem = &Object{}
if p.Nil() {
return
}
if i = absIndex(p.Len(), i); i == -1 {
return
}
elem.o = p.v.Index(i).Interface()
return
}
// Clear modifies this Slice to clear out all elements and returns a reference to this Slice.
func (p *RefSlice) Clear() ISlice {
if p.Nil() {
if p == nil {
p = NewRefSliceV()
} else {
*p = *NewRefSliceV()
}
} else {
p.Drop()
}
return p
}
// Concat returns a new Slice by appending the given Slice to this Slice using variadic expansion.
// Supports RefSlice, *RefSlice, []int or *[]int
func (p *RefSlice) Concat(slice interface{}) (new ISlice) {
return p.Copy().ConcatM(slice)
}
// ConcatM modifies this Slice by appending the given Slice using variadic expansion and returns a reference to this Slice.
// Supports RefSlice, *RefSlice, Slice and Go slice types
func (p *RefSlice) ConcatM(slice interface{}) ISlice {
// Handle supported types
var v reflect.Value
if x, ok := slice.(RefSlice); ok {
if !x.Nil() {
v = *(x.v)
}
} else if x, ok := slice.(*RefSlice); ok {
if !x.Nil() {
v = *(x.v)
}
} else if x, ok := slice.(ISlice); ok {
if !x.Nil() {
v = reflect.ValueOf(x.O())
}
} else {
v = reflect.ValueOf(slice)
}
if !v.IsValid() {
return p
}
// Nothing in this slice so return new slice from given
if p.Nil() {
if p == nil {
p = newEmptySlice(v.Interface())
} else {
*p = *(newEmptySlice(v.Interface()))
}
}
// Concat the two slices
if p.v.Type() != v.Type() {
panic(fmt.Sprintf("can't concat type '%v' with '%v'", v.Type(), p.v.Type()))
} else {
*p.v = reflect.AppendSlice(*p.v, v)
}
return p
}
// Copy returns a new Slice with the indicated range of elements copied from this Slice.
// Expects nothing, in which case everything is copied, or two indices i and j, in which
// case positive and negative notation is supported and uses an inclusive behavior such
// that Slice(0, -1) includes index -1 as opposed to Go's exclusive behavior. Out of
// bounds indices will be moved within bounds.
//
// An empty Slice is returned if indicies are mutually exclusive or nothing can be returned.
func (p *RefSlice) Copy(indices ...int) (new ISlice) {
if p.Nil() {
return NewRefSliceV()
}
// Handle index manipulation
i, j, err := absIndices(p.Len(), indices...)
if err != nil {
return newEmptySlice(p.O())
}
// Copy elements over to new Slice
x := reflect.MakeSlice(p.v.Type(), j-i, j-i)
reflect.Copy(x, p.v.Slice(i, j))
new = &RefSlice{v: &x, k: x.Kind()}
return
}
// Count the number of elements in this Slice equal to the given element.
func (p *RefSlice) Count(elem interface{}) (cnt int) {
l := p.Len()
if p.Nil() || l == 0 {
return
}
x := reflect.ValueOf(elem)
if p.v.Type().Elem() == x.Type() {
for i := 0; i < l; i++ {
if p.v.Index(i).Interface() == x.Interface() {
cnt++
}
}
}
return
}
// CountW counts the number of elements in this Slice that match the lambda selector.
func (p *RefSlice) CountW(sel func(O) bool) (cnt int) {
l := p.Len()
if p.Nil() || l == 0 {
return
}
for i := 0; i < l; i++ {
if sel(p.v.Index(i).Interface()) {
cnt++
}
}
return
}
// Drop modifies this Slice to delete the indicated range of elements and returns a referece to this Slice.
// Expects nothing, in which case everything is dropped, or two indices i and j, in which case positive and
// negative notation is supported and uses an inclusive behavior such that DropAt(0, -1) includes index -1
// as opposed to Go's exclusive behavior. Out of bounds indices will be moved within bounds.
func (p *RefSlice) Drop(indices ...int) ISlice {
l := p.Len()
if p == nil || l == 0 {
return p
}
// Handle index manipulation
i, j, err := absIndices(l, indices...)
if err != nil {
return p
}
// Execute
n := j - i
if i+n < l {
*p.v = reflect.AppendSlice(p.v.Slice(0, i), p.v.Slice(i+n, p.v.Len()))
} else {
*p.v = p.v.Slice(0, i)
}
return p
}
// DropAt modifies this Slice to delete the element at the given index location. Allows for negative notation.
// Returns a reference to this Slice.
func (p *RefSlice) DropAt(i int) ISlice {
return p.Drop(i, i)
}
// DropFirst modifies this Slice to delete the first element and returns a reference to this Slice.
func (p *RefSlice) DropFirst() ISlice {
return p.Drop(0, 0)
}
// DropFirstN modifies this Slice to delete the first n elements and returns a reference to this Slice.
func (p *RefSlice) DropFirstN(n int) ISlice {
if n == 0 {
return p
}
return p.Drop(0, abs(n)-1)
}
// DropLast modifies this Slice to delete the last element and returns a reference to this Slice.
func (p *RefSlice) DropLast() ISlice {
return p.Drop(-1, -1)
}
// DropLastN modifies thi Slice to delete the last n elements and returns a reference to this Slice.
func (p *RefSlice) DropLastN(n int) ISlice {
if n == 0 {
return p
}
return p.Drop(absNeg(n), -1)
}
// DropW modifies this Slice to delete the elements that match the lambda selector and returns a reference to this Slice.
// The slice is updated instantly when lambda expression is evaluated not after DropW completes.
func (p *RefSlice) DropW(sel func(O) bool) ISlice {
l := p.Len()
if p.Nil() || l == 0 {
return p
}
for i := 0; i < l; i++ {
if sel(p.v.Index(i).Interface()) {
p.DropAt(i)
l--
i--
}
}
return p
}
// Each calls the given lambda once for each element in this Slice, passing in that element
// as a parameter. Returns a reference to this Slice
func (p *RefSlice) Each(action func(O)) ISlice {
if p.Nil() {
return p
}
for i := 0; i < p.Len(); i++ {
action(p.v.Index(i).Interface())
}
return p
}
// EachE calls the given lambda once for each element in this Slice, passing in that element
// as a parameter. Returns a reference to this Slice and any error from the lambda.
func (p *RefSlice) EachE(action func(O) error) (ISlice, error) {
var err error
if p.Nil() {
return p, err
}
for i := 0; i < p.Len(); i++ {
if err = action(p.v.Index(i).Interface()); err != nil {
return p, err
}
}
return p, err
}
// EachI calls the given lambda once for each element in this Slice, passing in the index and element
// as a parameter. Returns a reference to this Slice
func (p *RefSlice) EachI(action func(int, O)) ISlice {
if p.Nil() {
return p
}
for i := 0; i < p.Len(); i++ {
action(i, p.v.Index(i).Interface())
}
return p
}
// EachIE calls the given lambda once for each element in this Slice, passing in the index and element
// as a parameter. Returns a reference to this Slice and any error from the lambda.
func (p *RefSlice) EachIE(action func(int, O) error) (ISlice, error) {
var err error
if p.Nil() {
return p, err
}
for i := 0; i < p.Len(); i++ {
if err = action(i, p.v.Index(i).Interface()); err != nil {
return p, err
}
}
return p, err
}
// EachR calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Returns a reference to this Slice
func (p *RefSlice) EachR(action func(O)) ISlice {
if p.Nil() {
return p
}
for i := p.Len() - 1; i >= 0; i-- {
action(p.v.Index(i).Interface())
}
return p
}
// EachRE calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Returns a reference to this Slice and any error from the lambda.
func (p *RefSlice) EachRE(action func(O) error) (ISlice, error) {
var err error
if p.Nil() {
return p, err
}
for i := p.Len() - 1; i >= 0; i-- {
if err = action(p.v.Index(i).Interface()); err != nil {
return p, err
}
}
return p, err
}
// EachRI calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Returns a reference to this Slice
func (p *RefSlice) EachRI(action func(int, O)) ISlice {
if p.Nil() {
return p
}
for i := p.Len() - 1; i >= 0; i-- {
action(i, p.v.Index(i).Interface())
}
return p
}
// EachRIE calls the given lambda once for each element in this Slice in reverse, passing in that element
// as a parameter. Returns a reference to this Slice and any error from the lambda.
func (p *RefSlice) EachRIE(action func(int, O) error) (ISlice, error) {
var err error
if p.Nil() {
return p, err
}
for i := p.Len() - 1; i >= 0; i-- {
if err = action(i, p.v.Index(i).Interface()); err != nil {
return p, err
}
}
return p, err
}
// Empty tests if this Slice is empty.
func (p *RefSlice) Empty() bool {
if p.Nil() || p.Len() == 0 {
return true
}
return false
}
// First returns the first element in this Slice as Object.
// Object.Nil() == true will be returned when there are no elements in the slice.
func (p *RefSlice) First() (elem *Object) {
return p.At(0)
}
// FirstN returns the first n elements in this slice as a Slice reference to the original.
// Best effort is used such that as many as can be will be returned up until the request is satisfied.
func (p *RefSlice) FirstN(n int) ISlice {
if p.Nil() {
return NewRefSliceV()
}
if n == 0 {
return newEmptySlice(p.O())
}
return p.Slice(0, abs(n)-1)
}
// Index returns the index of the first element in this Slice where element == elem
// Returns a -1 if the element was not not found.
func (p *RefSlice) Index(elem interface{}) (loc int) {
loc = -1
l := p.Len()
if p.Nil() || l == 0 {
return
}
for i := 0; i < l; i++ {
if elem == p.v.Index(i).Interface() {
return i
}
}
return
}
// Insert modifies this Slice to insert the given element before the element with the given index.
// Negative indices count backwards from the end of the slice, where -1 is the last element. If a
// negative index is used, the given element will be inserted after that element, so using an index
// of -1 will insert the element at the end of the slice. If a Slice is given all elements will be
// inserted starting from the beging until the end. Slice is returned for chaining. Invalid
// index locations will not change the slice.
func (p *RefSlice) Insert(i int, elem interface{}) ISlice {
l := p.Len()
if p.Nil() || l == 0 {
return p.Append(elem)
}
// Insert the item before j if pos and after j if neg
j := i
if j = absIndex(l, j); j == -1 {
return p
}
if i < 0 {
j++
}
x := reflect.ValueOf(elem)
if p.v.Type().Elem() != x.Type() {
panic(fmt.Sprintf("can't insert type '%v' into '%v'", x.Type(), p.v.Type()))
} else {
if j == 0 {
*p.v = reflect.Append(*p.v, x)
reflect.Copy(p.v.Slice(1, p.v.Len()), p.v.Slice(0, p.v.Len()-1))
p.v.Index(0).Set(x)
} else if j < l {
*p.v = reflect.Append(*p.v, x)
reflect.Copy(p.v.Slice(j+1, p.v.Len()), p.v.Slice(j, p.v.Len()))
p.v.Index(j).Set(x)
} else {
*p.v = reflect.Append(*p.v, x)
}
}
return p
}
// InsertS modifies this Slice to insert the given elements before the element with the given index.
// Negative indices count backwards from the end of the slice, where -1 is the last element. If a
// negative index is used, the given element will be inserted after that element, so using an index
// of -1 will insert the element at the end of the slice. If a Slice is given all elements will be
// inserted starting from the beging until the end. Slice is returned for chaining. Invalid
// index locations will not change the slice.
func (p *RefSlice) InsertS(i int, slice interface{}) ISlice {
l := p.Len()
if p.Nil() || l == 0 {
return p.ConcatM(slice)
}
// // Insert the item before j if pos and after j if neg
j := i
if j = absIndex(l, j); j == -1 {
return p
}
if i < 0 {
j++
}
// x := reflect.ValueOf(elem)
// if p.v.Type().Elem() != x.Type() {
// panic(fmt.Sprintf("can't insert type '%v' into '%v'", x.Type(), p.v.Type()))
// } else {
// if j == 0 {
// *p.v = reflect.Append(*p.v, x)
// reflect.Copy(p.v.Slice(1, p.v.Len()), p.v.Slice(0, p.v.Len()-1))
// p.v.Index(0).Set(x)
// } else if j < l {
// *p.v = reflect.Append(*p.v, x)
// reflect.Copy(p.v.Slice(j+1, p.v.Len()), p.v.Slice(j, p.v.Len()))
// p.v.Index(j).Set(x)
// } else {
// *p.v = reflect.Append(*p.v, x)
// }
// }
return p
}
// InterSlice returns true if the underlying implementation is a RefSlice
func (p *RefSlice) InterSlice() bool {
return false
}
// Join converts each element into a string then joins them together using the given separator or comma by default.
func (p *RefSlice) Join(separator ...string) (str *Object) {
l := p.Len()
if p.Nil() || l == 0 {
str = &Object{""}
return
}
sep := ","
if len(separator) > 0 {
sep = separator[0]
}
var builder strings.Builder
for i := 0; i < l; i++ {
builder.WriteString(Obj(p.v.Index(i).Interface()).ToString())
if i+1 < l {
builder.WriteString(sep)
}
}
str = &Object{builder.String()}
return
}
// Last returns the last element in this Slice as an Object.
// Object.Nil() == true will be returned if there are no elements in the slice.
func (p *RefSlice) Last() (elem *Object) {
return p.At(-1)
}
// LastN returns the last n elements in this Slice as a Slice reference to the original.
// Best effort is used such that as many as can be will be returned up until the request is satisfied.
func (p *RefSlice) LastN(n int) ISlice {
if p.Nil() {
return NewRefSliceV()
}
if n == 0 {
return newEmptySlice(p.O())
}
return p.Slice(absNeg(n), -1)
}
// Len returns the number of elements in this Slice
func (p *RefSlice) Len() int {
if p.Nil() {
return 0
}
return p.v.Len()
}
// Less returns true if the element indexed by i is less than the element indexed by j.
// Supports optimized Slice types or Go types that can be converted into an optimized Slice type.
func (p *RefSlice) Less(i, j int) bool {
l := p.Len()
if p.Nil() || l < 2 || i < 0 || j < 0 || i >= l || j >= l {
return false
}
// Handle supported types
slice := Slice(p.v.Interface())
if !slice.RefSlice() {
return slice.Less(i, j)
}
panic(fmt.Sprintf("unsupported comparable type '%v'", p.v.Type()))
}
// Map creates a new slice with the modified elements from the lambda.
func (p *RefSlice) Map(mod func(O) O) ISlice {
l := p.Len()
var slice ISlice
if p == nil || l == 0 {
return NewRefSliceV()
}
for i := 0; i < l; i++ {
v := mod(p.v.Index(i).Interface())
if slice == nil {
slice = Slice(v)
} else {
slice.Append(v)
}
}
return slice
}
// Nil tests if this Slice is nil
func (p *RefSlice) Nil() bool {
if p == nil || p.v == nil {
return true
}
return false
}
// O returns the underlying data structure as is
func (p *RefSlice) O() interface{} {
if p.Nil() {
return nil
}
return p.v.Interface()
}
// Pair simply returns the first and second Slice elements as Objects
func (p *RefSlice) Pair() (first, second *Object) {
l := p.Len()
first, second = &Object{}, &Object{}
if l > 0 {
first = p.At(0)
}
if l > 1 {
second = p.At(1)
}
return
}
// Pop modifies this Slice to remove the last element and returns the removed element as an Object.
func (p *RefSlice) Pop() (elem *Object) {
elem = p.Last()
p.DropLast()
return
}
// PopN modifies this Slice to remove the last n elements and returns the removed elements as a new Slice.
func (p *RefSlice) PopN(n int) (new ISlice) {
if p.Nil() {
return NewRefSliceV()
}
if n == 0 {
return newEmptySlice(p.O())
}
new = p.Copy(absNeg(n), -1)
p.DropLastN(n)
return
}
// Prepend modifies this Slice to add the given element at the begining and returns a reference to this Slice.
func (p *RefSlice) Prepend(elem interface{}) ISlice {
return p.Insert(0, elem)
}
// RefSlice returns true if the underlying implementation is a RefSlice
func (p *RefSlice) RefSlice() bool {
return true
}
// Reverse returns a new Slice with the order of the elements reversed.
func (p *RefSlice) Reverse() (new ISlice) {
if p.Nil() || p.Len() < 2 {
return p.Copy()
}
return p.Copy().ReverseM()
}
// ReverseM modifies this Slice reversing the order of the elements and returns a reference to this Slice.
func (p *RefSlice) ReverseM() ISlice {
l := p.Len()
if p.Nil() || l == 0 {
return p
}
for i, j := 0, l-1; i < j; i, j = i+1, j-1 {
p.Swap(i, j)
}
return p
}
// S is an alias to ToStringSlice
func (p *RefSlice) S() (slice *StringSlice) {
return ToStringSlice(p.O())
}
// Select creates a new slice with the elements that match the lambda selector.
func (p *RefSlice) Select(sel func(O) bool) (new ISlice) {
l := p.Len()
slice := NewRefSliceV()
if p.Nil() || l == 0 {
return slice
}
for i := 0; i < l; i++ {
obj := p.v.Index(i).Interface()
if sel(obj) {
slice.Append(obj)
}
}
return slice
}
// Set the element at the given index location to the given element. Allows for negative notation.
// Returns a reference to this Slice and swallows any errors.
func (p *RefSlice) Set(i int, elem interface{}) ISlice {
slice, _ := p.SetE(i, elem)
return slice
}
// SetE the element at the given index location to the given element. Allows for negative notation.
// Returns a referenc to this Slice and an error if out of bounds or elem is the wrong type.
func (p *RefSlice) SetE(i int, elem interface{}) (ISlice, error) {
var err error
if p.Nil() {
return p, err
}
if i = absIndex(p.Len(), i); i == -1 {
err = errors.Errorf("slice assignment is out of bounds")
return p, err
}
x := reflect.ValueOf(elem)
if p.v.Type().Elem() != x.Type() {
err = errors.Errorf("can't set type '%v' in '%v'", x.Type(), p.v.Type())
} else {
p.v.Index(i).Set(x)
}
return p, err
}
// Shift modifies this Slice to remove the first element and returns the removed element as an Object.
func (p *RefSlice) Shift() (elem *Object) {
elem = p.First()
p.DropFirst()
return
}
// ShiftN modifies this Slice to remove the first n elements and returns the removed elements as a new Slice.
func (p *RefSlice) ShiftN(n int) (new ISlice) {
if p.Nil() {
return NewRefSliceV()
}
if n == 0 {
return newEmptySlice(p.O())
}
new = p.Copy(0, abs(n)-1)
p.DropFirstN(n)
return
}
// Single reports true if there is only one element in this Slice.
func (p *RefSlice) Single() bool {
return p.Len() == 1
}
// Slice returns a range of elements from this Slice as a Slice reference to the original. Allows for negative notation.
// Expects nothing, in which case everything is included, or two indices i and j, in which case an inclusive behavior