1 ! Copyright (C) 2007 Robbert van Dalen.
2 ! See http://factorcode.org/license.txt for BSD license.
4 IN: isequences.ops.maximum
5 USING: generic kernel math sequences isequences.interface isequences.base ;
9 TUPLE: imax left right ;
11 : imax-unpack ( imax -- left right )
12 dup imax-left swap imax-right ; inline
15 i-length over i-length - dup 0 <= [ drop ] [ ++ ] if ; inline
17 : <i-max> ( s1 s2 -- imax )
18 dup i-length pick swap nmax -rot swap nmax <imax> ; inline
20 : min## ( s1 s2 -- minimum )
21 [ i-length ] 2apply min ; inline
23 : ||g++ ( s1 s2 -- imax )
24 2dup [ i-length ] 2apply zero? [ 2drop ] [ zero? [ nip ] [ <i-max> ] if ] if ; inline
26 : ||g-+ ( s1 s2 -- imax )
27 swap -- `` swap 2dup min## -rot || swap ihead ; inline
29 : ||g+- ( s1 s2 -- imax )
30 -- `` 2dup min## -rot || swap ihead ; inline
32 : ||g-- ( s1 s2 -- imax )
33 [ -- `` ] 2apply 2dup min## -rot || swap ihead `` -- ; inline
35 : mcut-point ( imax -- i )
36 imax-unpack [ ileft i-length ] 2apply 2dup < [ drop ] [ nip ] if ; inline
38 : imax-ileft ( imax -- imax )
41 [ dup mcut-point swap imax-unpack pick ihead -rot swap ihead swap || ]
44 : imax-iright ( imax -- imax )
47 [ dup mcut-point swap imax-unpack pick itail -rot swap itail swap || ]
52 2dup [ neg? ] 2apply [ [ ||g-- ] [ ||g+- ] if ]
53 [ [ ||g-+ ] [ ||g++ ] if ] if ; inline
57 ! double dispatch integer/||
58 GENERIC: integer/|| ( s1 s2 -- v )
59 M: object integer/|| swap ||g ;
60 M: integer || swap integer/|| ;
61 ! integer optimization
62 M: integer integer/|| max ;
64 M: imax i-at swap imax-unpack pick i-at -rot swap i-at swap ++ ;
65 M: imax i-length imax-left i-length ;
66 M: imax ileft imax-ileft ;
67 M: imax iright imax-iright ;
68 M: imax ihead (ihead) ;
69 M: imax itail (itail) ;
70 M: imax $$ imax-unpack [ $$ -2 shift ] 2apply quick-hash ;