         seq increment-last
     ] if* seq ;
 
+HINTS: next-combination array fixnum ;
+
 :: combinations-quot ( seq k quot -- seq quot' )
     seq length :> n
     n k nCk iota k iota >array seq quot n

! See http://factorcode.org/license.txt for BSD license.

USING: accessors arrays assocs binary-search classes.tuple fry
kernel locals math math.order math.ranges memoize namespaces
sequences sequences.private sorting ;
FROM: sequences => change-nth ;
IN: math.combinatorics


<PRIVATE

: cut-point ( seq -- n )
    [ last ] keep [ [ > ] keep swap ] find-last drop nip ; inline

: greater-from-last ( n seq -- i )
    [ nip ] [ nth ] 2bi [ > ] curry find-last drop ; inline

: reverse-tail! ( n seq -- seq )
    [ swap 1 + tail-slice reverse! drop ] keep ; inline

: (next-permutation) ( seq -- seq )
    dup cut-point [
        swap [ greater-from-last ] 2keep
        [ exchange ] [ reverse-tail! nip ] 3bi
    ] [ reverse! ] if* ;

HINTS: (next-permutation) array ;

PRIVATE>

