io.random: bit faster using set-nth-unsafe.

db4
John Benediktsson 2012-10-23 19:30:20 -07:00
parent a0d1c036ea
commit f835bb01f4
1 changed files with 7 additions and 7 deletions

View File

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