]> gitweb.factorcode.org Git - factor.git/blob - extra/tensors/tensor-slice/tensor-slice.factor
factor: trim using lists
[factor.git] / extra / tensors / tensor-slice / tensor-slice.factor
1 USING: accessors kernel math math.order sequences ;
2 IN: tensors.tensor-slice
3
4 TUPLE: step-slice
5     { from integer read-only initial: 0 }
6     { to integer read-only initial: 0 }
7     { seq read-only }
8     { step integer read-only } ;
9
10 :: <step-slice> ( from to step seq -- step-slice )
11     step zero? [ "can't be zero" throw ] when
12     seq length :> len
13     step 0 > [
14         from [ 0 ] unless*
15         to [ len ] unless*
16     ] [
17         from [ len ] unless*
18         to [ 0 ] unless*
19     ] if
20     [ dup 0 < [ len + ] when 0 len clamp ] bi@
21     ! FIXME: make this work with steps
22     seq dup slice? [ collapse-slice ] when
23     step step-slice boa ;
24
25 M: step-slice virtual-exemplar seq>> ; inline
26
27 M: step-slice virtual@
28     [ step>> * ] [ from>> + ] [ seq>> ] tri ; inline
29
30 M: step-slice length
31     [ to>> ] [ from>> - ] [ step>> ] tri
32     dup 0 < [ [ neg 0 max ] dip neg ] when /mod
33     zero? [ 1 + ] unless ; inline
34
35 INSTANCE: step-slice virtual-sequence