1 ! Copyright (C) 2008, 2009 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays fry kernel locals math namespaces
4 sequences sequences.private sorting ;
5 IN: splitting.monotonic
9 :: monotonic-split-impl ( seq quot slice-quot n -- pieces )
15 [ seq slice-quot call accum push ] keep dup
20 n = [ drop ] [ n seq slice-quot call accum push ] if
22 accum { } like ; inline
24 : (monotonic-split) ( seq quot slice-quot -- pieces )
25 pick length [ 3drop { } ] [ monotonic-split-impl ] if-zero ; inline
29 : monotonic-split ( seq quot: ( obj1 obj2 -- ? ) -- pieces )
30 [ subseq-unsafe ] (monotonic-split) ; inline
32 : monotonic-split-slice ( seq quot: ( obj1 obj2 -- ? ) -- pieces )
33 [ <slice-unsafe> ] (monotonic-split) ; inline
35 TUPLE: downward-slice < slice ;
36 TUPLE: stable-slice < slice ;
37 TUPLE: upward-slice < slice ;
39 : downward-slices ( seq -- slices )
40 [ > ] [ downward-slice boa ] (monotonic-split)
41 [ length 1 > ] filter ;
43 : stable-slices ( seq -- slices )
44 [ = ] [ stable-slice boa ] (monotonic-split)
45 [ length 1 > ] filter ;
47 : upward-slices ( seq -- slices )
48 [ < ] [ upward-slice boa ] (monotonic-split)
49 [ length 1 > ] filter ;
51 : trends ( seq -- slices )
56 [ upward-slices ] tri 3append [ from>> ] sort-with
58 zero? [ drop { } ] [ [ 0 1 ] dip stable-slice boa ] if