sequences.windowed: faster windows and rolling words.

db4
John Benediktsson 2013-05-01 22:18:26 -07:00
parent 05ea081cd0
commit b78e32733e
1 changed files with 34 additions and 13 deletions

View File

@ -1,25 +1,46 @@
! Copyright (C) 2012 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel math math.order sequences ;
USING: accessors fry kernel math math.order math.statistics
sequences sequences.private ;
IN: sequences.windowed
TUPLE: windowed-sequence { sequence sequence read-only } { n integer } ;
TUPLE: windowed-sequence
{ sequence sequence read-only }
{ n integer } ;
INSTANCE: windowed-sequence sequence
C: <windowed-sequence> windowed-sequence
M: windowed-sequence nth-unsafe
[ 1 + ] dip [ n>> dupd [-] swap ] [ sequence>> ] bi <slice> ; inline
M: windowed-sequence length
sequence>> length ; inline
: in-bound ( n sequence -- n' )
[ drop 0 ] [ length ] bi clamp ; inline
: in-bounds ( a b sequence -- a' b' sequence )
[ nip in-bound ]
[ [ nip ] dip in-bound ]
[ 2nip ] 3tri ;
M: windowed-sequence nth
[ [ 1 + ] dip n>> [ - ] [ drop ] 2bi ]
[ nip sequence>> in-bounds <slice> ] 2bi ;
M: windowed-sequence length
sequence>> length ;
[ nip in-bound ] [ [ nip ] dip in-bound ] [ 2nip ] 3tri ;
: rolling-map ( seq n quot: ( slice -- elt ) -- newseq )
[ <windowed-sequence> ] [ map ] bi* ; inline
: rolling-sum ( seq n -- newseq )
[ sum ] rolling-map ;
: rolling-mean ( seq n -- newseq )
[ mean ] rolling-map ;
: rolling-median ( seq n -- newseq )
[ median ] rolling-map ;
: rolling-supremum ( seq n -- newseq )
[ supremum ] rolling-map ;
: rolling-infimum ( seq n -- newseq )
[ infimum ] rolling-map ;
: rolling-count ( ... u n quot: ( ... elt -- ... ? ) -- ... v )
'[ _ count ] rolling-map ; inline