forked from capnproto/go-capnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
1120 lines (990 loc) · 27 KB
/
list.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 capnp
import (
"bytes"
"errors"
"fmt"
"math"
"strconv"
"capnproto.org/go/capnp/v3/exc"
"capnproto.org/go/capnp/v3/internal/str"
"capnproto.org/go/capnp/v3/internal/strquote"
)
// A List is a reference to an array of values.
type List ListKind
// The underlying type of List. We expose this so that
// we can use ~ListKind as a constraint in generics to
// capture any list type.
type ListKind = struct {
seg *Segment
off address // at beginning of elements (past composite list tag word)
length int32 // always [0, 1<<29)
size ObjectSize // always valid
depthLimit uint
flags listFlags
}
// newPrimitiveList allocates a new list of primitive values, preferring placement in s.
func newPrimitiveList(s *Segment, sz Size, n int32) (List, error) {
if n < 0 || n >= 1<<29 {
return List{}, errors.New("new list: length out of range")
}
// sz is [0, 8] and n is [0, 1<<29).
// Range is [0, maxSegmentSize], thus there will never be overflow.
total := sz.timesUnchecked(n)
s, addr, err := alloc(s, total)
if err != nil {
return List{}, exc.WrapError("new list", err)
}
return List{
seg: s,
off: addr,
length: n,
size: ObjectSize{DataSize: sz},
depthLimit: maxDepth,
}, nil
}
// NewCompositeList creates a new composite list, preferring placement
// in s.
func NewCompositeList(s *Segment, sz ObjectSize, n int32) (List, error) {
if !sz.isValid() {
return List{}, errors.New("new composite list: invalid element size")
}
if n < 0 || n >= 1<<29 {
return List{}, errors.New("new composite list: length out of range")
}
sz.DataSize = sz.DataSize.padToWord()
total, ok := sz.totalSize().times(n)
if !ok || total > maxSegmentSize-wordSize {
return List{}, errors.New("new composite list: size overflow")
}
s, addr, err := alloc(s, wordSize+total)
if err != nil {
return List{}, exc.WrapError("new composite list", err)
}
// Add tag word
s.writeRawPointer(addr, rawStructPointer(pointerOffset(n), sz))
return List{
seg: s,
off: addr.addSizeUnchecked(wordSize),
length: n,
size: sz,
flags: isCompositeList,
depthLimit: maxDepth,
}, nil
}
// ToPtr converts the list to a generic pointer.
func (p List) ToPtr() Ptr {
return Ptr{
seg: p.seg,
off: p.off,
lenOrCap: uint32(p.length),
size: p.size,
depthLimit: p.depthLimit,
flags: listPtrFlag(p.flags),
}
}
// Segment returns the segment the referenced list is stored in or
// nil if the pointer is invalid.
func (p List) Segment() *Segment {
return p.seg
}
// Message returns the message the referenced list is stored in or nil
// if the pointer is invalid.
func (p List) Message() *Message {
if p.seg == nil {
return nil
}
return p.seg.msg
}
// IsValid returns whether the list is valid.
func (p List) IsValid() bool {
return p.seg != nil
}
// readSize returns the list's size for the purposes of read limit
// accounting.
func (p List) readSize() Size {
if p.seg == nil {
return 0
}
e := p.size.totalSize()
if e == 0 {
e = wordSize
}
sz, ok := e.times(p.length)
if !ok {
return maxSegmentSize
}
return sz
}
// allocSize returns the list's size for the purpose of copying the list
// to a different message.
func (p List) allocSize() Size {
if p.seg == nil {
return 0
}
if p.flags&isBitList != 0 {
return bitListSize(p.length)
}
sz, _ := p.size.totalSize().times(p.length) // size has already been validated
if p.flags&isCompositeList == 0 {
return sz
}
return sz + wordSize
}
// raw returns the equivalent raw list pointer with a zero offset.
func (p List) raw() rawPointer {
if p.seg == nil {
return 0
}
if p.flags&isCompositeList != 0 {
return rawListPointer(0, compositeList, p.length*p.size.totalWordCount())
}
if p.flags&isBitList != 0 {
return rawListPointer(0, bit1List, p.length)
}
if p.size.PointerCount == 1 && p.size.DataSize == 0 {
return rawListPointer(0, pointerList, p.length)
}
if p.size.PointerCount != 0 {
panic("invalid list size")
}
switch p.size.DataSize {
case 0:
return rawListPointer(0, voidList, p.length)
case 1:
return rawListPointer(0, byte1List, p.length)
case 2:
return rawListPointer(0, byte2List, p.length)
case 4:
return rawListPointer(0, byte4List, p.length)
case 8:
return rawListPointer(0, byte8List, p.length)
default:
panic("invalid list size")
}
}
// Len returns the length of the list.
func (p List) Len() int {
if p.seg == nil {
return 0
}
return int(p.length)
}
// primitiveElem returns the address of the segment data for a list element.
// Calling this on a bit list returns an error.
func (p List) primitiveElem(i int, expectedSize ObjectSize) (address, error) {
if p.seg == nil || i < 0 || i >= int(p.length) {
// This is programmer error, not input error.
panic("list element out of bounds")
}
if p.flags&isBitList != 0 ||
p.flags&isCompositeList == 0 && p.size != expectedSize ||
p.flags&isCompositeList != 0 &&
(p.size.DataSize < expectedSize.DataSize ||
p.size.PointerCount < expectedSize.PointerCount) {
return 0, errors.New("mismatched list element size")
}
addr, ok := p.off.element(int32(i), p.size.totalSize())
if !ok {
return 0, errors.New("read list element " + str.Itod(i) + ": address overflow")
}
return addr, nil
}
// Struct returns the i'th element as a struct.
func (p List) Struct(i int) Struct {
if p.seg == nil || i < 0 || i >= int(p.length) {
// This is programmer error, not input error.
panic("list element out of bounds")
}
if p.flags&isBitList != 0 {
return Struct{}
}
addr, ok := p.off.element(int32(i), p.size.totalSize())
if !ok {
return Struct{}
}
return Struct{
seg: p.seg,
off: addr,
size: p.size,
flags: isListMember,
depthLimit: p.depthLimit - 1,
}
}
// SetStruct set the i'th element to the value in s.
func (p List) SetStruct(i int, s Struct) error {
if p.flags&isBitList != 0 {
return errors.New("SetStruct called on bit list")
}
if err := copyStruct(p.Struct(i), s); err != nil {
return exc.WrapError("set list element "+str.Itod(i), err)
}
return nil
}
// l.EncodeAsPtr is equivalent to l.ToPtr(); for implementing TypeParam.
// The segment argument is ignored.
func (l List) EncodeAsPtr(*Segment) Ptr { return l.ToPtr() }
// DecodeFromPtr(p) is equivalent to p.List; for implementing TypeParam.
func (List) DecodeFromPtr(p Ptr) List { return p.List() }
var _ TypeParam[List] = List{}
// A BitList is a reference to a list of booleans.
type BitList List
var _ TypeParam[BitList] = BitList{}
// NewBitList creates a new bit list, preferring placement in s.
func NewBitList(s *Segment, n int32) (BitList, error) {
if n < 0 || n >= 1<<29 {
return BitList{}, errors.New("new bit list: length out of range")
}
s, addr, err := alloc(s, bitListSize(n))
if err != nil {
return BitList{}, exc.WrapError("new "+str.Itod(n)+"-element bit list", err)
}
return BitList{
seg: s,
off: addr,
length: n,
flags: isBitList,
depthLimit: maxDepth,
}, nil
}
// bitListSize returns the number of bytes needed for a bit list with n
// elements. It is only defined for n in [0, 1<<29).
func bitListSize(n int32) Size {
return Size((n + 7) / 8)
}
// At returns the i'th bit.
func (p BitList) At(i int) bool {
if p.seg == nil || i < 0 || i >= int(p.length) {
// This is programmer error, not input error.
panic("list element out of bounds")
}
if p.flags&isBitList == 0 {
return false
}
bit := BitOffset(i)
addr := p.off.addOffset(bit.offset())
return p.seg.readUint8(addr)&bit.mask() != 0
}
// Set sets the i'th bit to v.
func (p BitList) Set(i int, v bool) {
if p.seg == nil || i < 0 || i >= int(p.length) {
// This is programmer error, not input error.
panic("list element out of bounds")
}
if p.flags&isBitList == 0 {
// Again, programmer error. Should have used NewBitList.
panic("BitList.Set called on a non-bit list")
}
bit := BitOffset(i)
addr := p.off.addOffset(bit.offset())
b := p.seg.slice(addr, 1)
if v {
b[0] |= bit.mask()
} else {
b[0] &^= bit.mask()
}
}
// String returns the list in Cap'n Proto schema format (e.g. "[true, false]").
func (p BitList) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < p.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
if p.At(i) {
buf = append(buf, "true"...)
} else {
buf = append(buf, "false"...)
}
}
buf = append(buf, ']')
return string(buf)
}
// A PointerList is a reference to an array of pointers.
type PointerList List
var _ TypeParam[PointerList] = PointerList{}
// NewPointerList allocates a new list of pointers, preferring placement in s.
func NewPointerList(s *Segment, n int32) (PointerList, error) {
total, ok := wordSize.times(n)
if !ok {
return PointerList{}, errors.New("new pointer list: size overflow")
}
s, addr, err := alloc(s, total)
if err != nil {
return PointerList{}, exc.WrapError("new "+str.Itod(n)+"-element pointer list", err)
}
return PointerList{
seg: s,
off: addr,
length: n,
size: ObjectSize{PointerCount: 1},
depthLimit: maxDepth,
}, nil
}
// At returns the i'th pointer in the list.
func (p PointerList) At(i int) (Ptr, error) {
addr, err := p.primitiveElem(i, ObjectSize{PointerCount: 1})
if err != nil {
return Ptr{}, err
}
addr += address(p.size.DataSize)
return p.seg.readPtr(addr, p.depthLimit)
}
// Set sets the i'th pointer in the list to v.
func (p PointerList) Set(i int, v Ptr) error {
addr, err := p.primitiveElem(i, ObjectSize{PointerCount: 1})
if err != nil {
return err
}
return p.seg.writePtr(addr, v, false)
}
// TextList is an array of pointers to strings.
type TextList List
var _ TypeParam[TextList] = TextList{}
// NewTextList allocates a new list of text pointers, preferring placement in s.
func NewTextList(s *Segment, n int32) (TextList, error) {
pl, err := NewPointerList(s, n)
if err != nil {
return TextList{}, err
}
return TextList(pl), nil
}
// At returns the i'th string in the list.
func (l TextList) At(i int) (string, error) {
addr, err := l.primitiveElem(i, ObjectSize{PointerCount: 1})
if err != nil {
return "", err
}
p, err := l.seg.readPtr(addr, l.depthLimit)
if err != nil {
return "", err
}
return p.Text(), nil
}
// BytesAt returns the i'th element in the list as a byte slice.
// The underlying array of the slice is the segment data.
func (l TextList) BytesAt(i int) ([]byte, error) {
addr, err := l.primitiveElem(i, ObjectSize{PointerCount: 1})
if err != nil {
return nil, err
}
p, err := l.seg.readPtr(addr, l.depthLimit)
if err != nil {
return nil, err
}
return p.TextBytes(), nil
}
// Set sets the i'th string in the list to v.
func (l TextList) Set(i int, v string) error {
addr, err := l.primitiveElem(i, ObjectSize{PointerCount: 1})
if err != nil {
return err
}
if v == "" {
return l.seg.writePtr(addr, Ptr{}, false)
}
p, err := NewText(l.seg, v)
if err != nil {
return err
}
return l.seg.writePtr(addr, p.ToPtr(), false)
}
// String returns the list in Cap'n Proto schema format (e.g. `["foo", "bar"]`).
func (l TextList) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
s, err := l.BytesAt(i)
if err != nil {
buf = append(buf, "<error>"...)
continue
}
buf = strquote.Append(buf, s)
}
buf = append(buf, ']')
return string(buf)
}
// DataList is an array of pointers to data.
type DataList List
var _ TypeParam[DataList] = DataList{}
// NewDataList allocates a new list of data pointers, preferring placement in s.
func NewDataList(s *Segment, n int32) (DataList, error) {
pl, err := NewPointerList(s, n)
if err != nil {
return DataList{}, err
}
return DataList(pl), nil
}
// At returns the i'th data in the list.
func (l DataList) At(i int) ([]byte, error) {
addr, err := l.primitiveElem(i, ObjectSize{PointerCount: 1})
if err != nil {
return nil, err
}
p, err := l.seg.readPtr(addr, l.depthLimit)
if err != nil {
return nil, err
}
return p.Data(), nil
}
// Set sets the i'th data in the list to v.
func (l DataList) Set(i int, v []byte) error {
addr, err := l.primitiveElem(i, ObjectSize{PointerCount: 1})
if err != nil {
return err
}
if len(v) == 0 {
return l.seg.writePtr(addr, Ptr{}, false)
}
p, err := NewData(l.seg, v)
if err != nil {
return err
}
return l.seg.writePtr(addr, p.ToPtr(), false)
}
// String returns the list in Cap'n Proto schema format (e.g. `["foo", "bar"]`).
func (l DataList) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
s, err := l.At(i)
if err != nil {
buf = append(buf, "<error>"...)
continue
}
buf = strquote.Append(buf, s)
}
buf = append(buf, ']')
return string(buf)
}
// A VoidList is a list of zero-sized elements.
type VoidList List
var _ TypeParam[VoidList] = VoidList{}
// NewVoidList creates a list of voids. No allocation is performed;
// s is only used for Segment()'s return value.
func NewVoidList(s *Segment, n int32) VoidList {
if n < 0 || n >= 1<<29 {
panic("list length overflow")
}
return VoidList{
seg: s,
length: n,
depthLimit: maxDepth,
}
}
// String returns the list in Cap'n Proto schema format (e.g. "[void, void, void]").
func (l VoidList) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = append(buf, "void"...)
}
buf = append(buf, ']')
return string(buf)
}
// A UInt8List is an array of UInt8 values.
type UInt8List List
var _ TypeParam[UInt8List] = UInt8List{}
// NewUInt8List creates a new list of UInt8, preferring placement in s.
func NewUInt8List(s *Segment, n int32) (UInt8List, error) {
l, err := newPrimitiveList(s, 1, n)
if err != nil {
return UInt8List{}, err
}
return UInt8List(l), nil
}
// NewText creates a new list of UInt8 from a string.
func NewText(s *Segment, v string) (UInt8List, error) {
// TODO(light): error if v is too long
l, err := NewUInt8List(s, int32(len(v)+1))
if err != nil {
return UInt8List{}, err
}
copy(l.seg.slice(l.off, Size(len(v))), v)
return l, nil
}
// NewTextFromBytes creates a NUL-terminated list of UInt8 from a byte slice.
func NewTextFromBytes(s *Segment, v []byte) (UInt8List, error) {
// TODO(light): error if v is too long
l, err := NewUInt8List(s, int32(len(v)+1))
if err != nil {
return UInt8List{}, err
}
copy(l.seg.slice(l.off, Size(len(v))), v)
return l, nil
}
// NewData creates a new list of UInt8 from a byte slice.
func NewData(s *Segment, v []byte) (UInt8List, error) {
// TODO(light): error if v is too long
l, err := NewUInt8List(s, int32(len(v)))
if err != nil {
return UInt8List{}, err
}
copy(l.seg.slice(l.off, Size(len(v))), v)
return l, nil
}
func isOneByteList(p Ptr) bool {
return p.seg != nil && p.flags.ptrType() == listPtrType && p.size.isOneByte() && p.flags.listFlags()&isCompositeList == 0
}
// At returns the i'th element.
func (l UInt8List) At(i int) uint8 {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 1})
if err != nil {
return 0
}
return l.seg.readUint8(addr)
}
// Set sets the i'th element to v.
func (l UInt8List) Set(i int, v uint8) {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 1})
if err != nil {
panic(err)
}
l.seg.writeUint8(addr, v)
}
// String returns the list in Cap'n Proto schema format (e.g. "[1, 2, 3]").
func (l UInt8List) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = strconv.AppendUint(buf, uint64(l.At(i)), 10)
}
buf = append(buf, ']')
return string(buf)
}
// Int8List is an array of Int8 values.
type Int8List List
var _ TypeParam[Int8List] = Int8List{}
// NewInt8List creates a new list of Int8, preferring placement in s.
func NewInt8List(s *Segment, n int32) (Int8List, error) {
l, err := newPrimitiveList(s, 1, n)
if err != nil {
return Int8List{}, err
}
return Int8List(l), nil
}
// At returns the i'th element.
func (l Int8List) At(i int) int8 {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 1})
if err != nil {
return 0
}
return int8(l.seg.readUint8(addr))
}
// Set sets the i'th element to v.
func (l Int8List) Set(i int, v int8) {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 1})
if err != nil {
panic(err)
}
l.seg.writeUint8(addr, uint8(v))
}
// String returns the list in Cap'n Proto schema format (e.g. "[1, 2, 3]").
func (l Int8List) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = strconv.AppendInt(buf, int64(l.At(i)), 10)
}
buf = append(buf, ']')
return string(buf)
}
// A UInt16List is an array of UInt16 values.
type UInt16List List
var _ TypeParam[UInt16List] = UInt16List{}
// NewUInt16List creates a new list of UInt16, preferring placement in s.
func NewUInt16List(s *Segment, n int32) (UInt16List, error) {
l, err := newPrimitiveList(s, 2, n)
if err != nil {
return UInt16List{}, err
}
return UInt16List(l), nil
}
// At returns the i'th element.
func (l UInt16List) At(i int) uint16 {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 2})
if err != nil {
return 0
}
return l.seg.readUint16(addr)
}
// Set sets the i'th element to v.
func (l UInt16List) Set(i int, v uint16) {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 2})
if err != nil {
panic(err)
}
l.seg.writeUint16(addr, v)
}
// String returns the list in Cap'n Proto schema format (e.g. "[1, 2, 3]").
func (l UInt16List) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = strconv.AppendUint(buf, uint64(l.At(i)), 10)
}
buf = append(buf, ']')
return string(buf)
}
// Int16List is an array of Int16 values.
type Int16List List
var _ TypeParam[Int16List] = Int16List{}
// NewInt16List creates a new list of Int16, preferring placement in s.
func NewInt16List(s *Segment, n int32) (Int16List, error) {
l, err := newPrimitiveList(s, 2, n)
if err != nil {
return Int16List{}, err
}
return Int16List(l), nil
}
// At returns the i'th element.
func (l Int16List) At(i int) int16 {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 2})
if err != nil {
return 0
}
return int16(l.seg.readUint16(addr))
}
// Set sets the i'th element to v.
func (l Int16List) Set(i int, v int16) {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 2})
if err != nil {
panic(err)
}
l.seg.writeUint16(addr, uint16(v))
}
// String returns the list in Cap'n Proto schema format (e.g. "[1, 2, 3]").
func (l Int16List) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = strconv.AppendInt(buf, int64(l.At(i)), 10)
}
buf = append(buf, ']')
return string(buf)
}
// UInt32List is an array of UInt32 values.
type UInt32List List
var _ TypeParam[UInt32List] = UInt32List{}
// NewUInt32List creates a new list of UInt32, preferring placement in s.
func NewUInt32List(s *Segment, n int32) (UInt32List, error) {
l, err := newPrimitiveList(s, 4, n)
if err != nil {
return UInt32List{}, err
}
return UInt32List(l), nil
}
// At returns the i'th element.
func (l UInt32List) At(i int) uint32 {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 4})
if err != nil {
return 0
}
return l.seg.readUint32(addr)
}
// Set sets the i'th element to v.
func (l UInt32List) Set(i int, v uint32) {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 4})
if err != nil {
panic(err)
}
l.seg.writeUint32(addr, v)
}
// String returns the list in Cap'n Proto schema format (e.g. "[1, 2, 3]").
func (l UInt32List) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = strconv.AppendUint(buf, uint64(l.At(i)), 10)
}
buf = append(buf, ']')
return string(buf)
}
// Int32List is an array of Int32 values.
type Int32List List
var _ TypeParam[Int32List] = Int32List{}
// NewInt32List creates a new list of Int32, preferring placement in s.
func NewInt32List(s *Segment, n int32) (Int32List, error) {
l, err := newPrimitiveList(s, 4, n)
if err != nil {
return Int32List{}, err
}
return Int32List(l), nil
}
// At returns the i'th element.
func (l Int32List) At(i int) int32 {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 4})
if err != nil {
return 0
}
return int32(l.seg.readUint32(addr))
}
// Set sets the i'th element to v.
func (l Int32List) Set(i int, v int32) {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 4})
if err != nil {
panic(err)
}
l.seg.writeUint32(addr, uint32(v))
}
// String returns the list in Cap'n Proto schema format (e.g. "[1, 2, 3]").
func (l Int32List) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = strconv.AppendInt(buf, int64(l.At(i)), 10)
}
buf = append(buf, ']')
return string(buf)
}
// UInt64List is an array of UInt64 values.
type UInt64List List
var _ TypeParam[UInt64List] = UInt64List{}
// NewUInt64List creates a new list of UInt64, preferring placement in s.
func NewUInt64List(s *Segment, n int32) (UInt64List, error) {
l, err := newPrimitiveList(s, 8, n)
if err != nil {
return UInt64List{}, err
}
return UInt64List(l), nil
}
// At returns the i'th element.
func (l UInt64List) At(i int) uint64 {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 8})
if err != nil {
return 0
}
return l.seg.readUint64(addr)
}
// Set sets the i'th element to v.
func (l UInt64List) Set(i int, v uint64) {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 8})
if err != nil {
panic(err)
}
l.seg.writeUint64(addr, v)
}
// String returns the list in Cap'n Proto schema format (e.g. "[1, 2, 3]").
func (l UInt64List) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = strconv.AppendUint(buf, l.At(i), 10)
}
buf = append(buf, ']')
return string(buf)
}
// Int64List is an array of Int64 values.
type Int64List List
var _ TypeParam[Int64List] = Int64List{}
// NewInt64List creates a new list of Int64, preferring placement in s.
func NewInt64List(s *Segment, n int32) (Int64List, error) {
l, err := newPrimitiveList(s, 8, n)
if err != nil {
return Int64List{}, err
}
return Int64List(l), nil
}
// At returns the i'th element.
func (l Int64List) At(i int) int64 {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 8})
if err != nil {
return 0
}
return int64(l.seg.readUint64(addr))
}
// Set sets the i'th element to v.
func (l Int64List) Set(i int, v int64) {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 8})
if err != nil {
panic(err)
}
l.seg.writeUint64(addr, uint64(v))
}
// String returns the list in Cap'n Proto schema format (e.g. "[1, 2, 3]").
func (l Int64List) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = strconv.AppendInt(buf, l.At(i), 10)
}
buf = append(buf, ']')
return string(buf)
}
// Float32List is an array of Float32 values.
type Float32List List
var _ TypeParam[Float32List] = Float32List{}
// NewFloat32List creates a new list of Float32, preferring placement in s.
func NewFloat32List(s *Segment, n int32) (Float32List, error) {
l, err := newPrimitiveList(s, 4, n)
if err != nil {
return Float32List{}, err
}
return Float32List(l), nil
}
// At returns the i'th element.
func (l Float32List) At(i int) float32 {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 4})
if err != nil {
return 0
}
return math.Float32frombits(l.seg.readUint32(addr))
}
// Set sets the i'th element to v.
func (l Float32List) Set(i int, v float32) {
addr, err := l.primitiveElem(i, ObjectSize{DataSize: 4})
if err != nil {
panic(err)
}
l.seg.writeUint32(addr, math.Float32bits(v))
}
// String returns the list in Cap'n Proto schema format (e.g. "[1, 2, 3]").
func (l Float32List) String() string {
var buf []byte
buf = append(buf, '[')
for i := 0; i < l.Len(); i++ {
if i > 0 {
buf = append(buf, ", "...)
}
buf = strconv.AppendFloat(buf, float64(l.At(i)), 'g', -1, 32)
}
buf = append(buf, ']')
return string(buf)
}
// Float64List is an array of Float64 values.
type Float64List List
var _ TypeParam[Float64List] = Float64List{}