Use binary-search instead of find-last for combinations
parent
678f603aa5
commit
78037d8d05
|
@ -1,7 +1,7 @@
|
||||||
! Copyright (c) 2007-2009 Slava Pestov, Doug Coleman, Aaron Schaefer.
|
! Copyright (c) 2007-2009 Slava Pestov, Doug Coleman, Aaron Schaefer.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors assocs fry kernel locals math math.order math.ranges mirrors
|
USING: accessors assocs binary-search fry kernel locals math math.order
|
||||||
namespaces sequences sorting ;
|
math.ranges mirrors namespaces sequences sorting ;
|
||||||
IN: math.combinatorics
|
IN: math.combinatorics
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
@ -74,8 +74,11 @@ C: <combo> combo
|
||||||
[ seq>> length ] [ k>> ] bi nCk ;
|
[ seq>> length ] [ k>> ] bi nCk ;
|
||||||
|
|
||||||
: largest-value ( a b x -- v )
|
: largest-value ( a b x -- v )
|
||||||
#! TODO: use a binary search instead of find-last
|
dup 0 = [
|
||||||
[ [0,b) ] 2dip '[ _ nCk _ <= ] find-last nip ;
|
drop 1 - nip
|
||||||
|
] [
|
||||||
|
[ [0,b) ] 2dip '[ _ nCk _ >=< ] search nip
|
||||||
|
] if ;
|
||||||
|
|
||||||
:: next-values ( a b x -- a' b' x' v )
|
:: next-values ( a b x -- a' b' x' v )
|
||||||
a b x largest-value dup :> v ! a'
|
a b x largest-value dup :> v ! a'
|
||||||
|
|
Loading…
Reference in New Issue