github.com/wzzhu/tensor@v0.9.24/slice.go (about) 1 package tensor 2 3 // A Slice represents a slicing operation for a Tensor. 4 type Slice interface { 5 Start() int 6 End() int 7 Step() int 8 } 9 10 type rs struct { 11 start, end, step int 12 } 13 14 func (s rs) Start() int { return s.start } 15 func (s rs) End() int { return s.end } 16 func (s rs) Step() int { return s.step } 17 18 // makeRS creates a ranged slice. It takes an optional step param. 19 func makeRS(start, end int, opts ...int) rs { 20 step := 1 21 if len(opts) > 0 { 22 step = opts[0] 23 } 24 return rs{ 25 start: start, 26 end: end, 27 step: step, 28 } 29 } 30 31 // ss is a single slice, representing this: [start:start+1:0] 32 type ss int 33 34 func (s ss) Start() int { return int(s) } 35 func (s ss) End() int { return int(s) + 1 } 36 func (s ss) Step() int { return 0 } 37 38 // sli is slice. It's named sli to prevent confusion over naming 39 type sli struct { 40 start, end, step int 41 } 42 43 // S creates a Slice. 44 // end is optional. It should be passed in as the first param of the optionals. 45 // step is optional. It should be passed in as the second param of the optionals. 46 // 47 // Default end is start+1. Default step is 1, unless end == step+1, then it defaults to 0 48 func S(start int, opt ...int) Slice { 49 var end, step int 50 if len(opt) > 0 { 51 end = opt[0] 52 } else { 53 end = start + 1 54 } 55 56 step = 1 57 if len(opt) > 1 { 58 step = opt[1] 59 } else if end == start+1 { 60 step = 0 61 } 62 63 return &sli{ 64 start: start, 65 end: end, 66 step: step, 67 } 68 } 69 70 func (s *sli) Start() int { return s.start } 71 func (s *sli) End() int { return s.end } 72 func (s *sli) Step() int { return s.step }