io.random: much faster version of random-lines for large n.

db4
John Benediktsson 2012-10-23 19:03:34 -07:00
parent 954e5ff5bb
commit a0d1c036ea
1 changed files with 8 additions and 8 deletions

View File

@ -1,8 +1,8 @@
! Copyright (C) 2012 John Benediktsson ! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license ! See http://factorcode.org/license.txt for BSD license
USING: fry io kernel locals math random sequences USING: combinators fry io kernel locals math math.order random
sequences.extras ; sequences ;
IN: io.random IN: io.random
@ -19,10 +19,10 @@ PRIVATE>
:: random-lines ( n -- lines ) :: random-lines ( n -- lines )
V{ } clone :> accum V{ } clone :> accum
[| line line# | [| line line# |
line# random :> r line# n <=> {
r n < [ { +lt+ [ line accum push ] }
line# n <= { +eq+ [ line accum [ push ] [ randomize drop ] bi ] }
[ line r accum insert-nth! ] { +gt+ [ line# random :> r
[ line r accum set-nth ] if r n < [ line r accum set-nth ] when ] }
] when } case
] each-numbered-line accum ; ] each-numbered-line accum ;