katsusan / gowiki

0 stars 0 forks source link

Go slice implementation #11

Open katsusan opened 3 years ago

katsusan commented 3 years ago

refer: https://docs.google.com/document/d/1GKKdiGYAghXRxC2BFrSEbHBZZgAGKQ-yXK-hRKBo0Kk/pub

1. slice struct

int cap;
int len;
T *array;

which means slice should be stored as the following layout:

//when three members of slice all on stack stack address comment
cap 0x7ffffe8 SP+offset+0x10
len 0x7ffffe0 SP+offset+0x8
array 0x7ffffd8 SP+offset

2. three-index slice definition

s[i:j:k] constructs a slice, means len=j-i, cap=k-i, starting at s[i], and must satisfy i<=j<=k<=cap(s).

3. defaults

x := make([]int, 5, 10)

//valid
x[i:j:k]
x[:j:k]

//invalid
x[i::] 
x[:j:]
x[::k]
x[i::k]
x[i:j:]