Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
alexandria
alexandria
Commits
8beef2dc
Commit
8beef2dc
authored
Jan 07, 2021
by
death
Committed by
Michał Herda
Mar 13, 2021
Browse files
Use linear-time SHUFFLE if supplied with many list items to shuffle
parent
f35e232c
Changes
1
Hide whitespace changes
Inline
Side-by-side
alexandria-1/sequences.lisp
View file @
8beef2dc
...
...
@@ -81,19 +81,28 @@ share structure with it."
(
defun
shuffle
(
sequence
&key
(
start
0
)
end
)
"Returns a random permutation of SEQUENCE bounded by START and END.
Original sequence may be destructively modified, and (if it contains
CONS or lists themselv) share storage with the original one.
Original sequence may be destructively modified.
Signals an error if SEQUENCE is not a proper sequence."
(
declare
(
type
fixnum
start
)
(
type
(
or
fixnum
null
)
end
))
(
etypecase
sequence
(
list
(
let*
((
end
(
or
end
(
proper-list-length
sequence
)))
(
n
(
-
end
start
)))
(
do
((
tail
(
nthcdr
start
sequence
)
(
cdr
tail
)))
((
zerop
n
))
(
rotatef
(
car
tail
)
(
car
(
nthcdr
(
random
n
)
tail
)))
(
decf
n
))))
(
n
(
-
end
start
))
(
sublist
(
nthcdr
start
sequence
))
(
small-enough-threshold
100
))
;; It's fine to use a pure list shuffle if the number of items
;; to shuffle is small enough, but otherwise it's more
;; time-efficient to create an intermediate vector to work on.
;; I picked the threshold based on rudimentary benchmarks on my
;; machine, where both branches take about the same time.
(
if
(
<
n
small-enough-threshold
)
(
do
((
tail
sublist
(
cdr
tail
)))
((
zerop
n
))
(
rotatef
(
car
tail
)
(
car
(
nthcdr
(
random
n
)
tail
)))
(
decf
n
))
(
let
((
intermediate-vector
(
replace
(
make-array
n
)
sublist
)))
(
replace
sublist
(
shuffle
intermediate-vector
))))))
(
vector
(
let
((
end
(
or
end
(
length
sequence
))))
(
loop
for
i
from
start
below
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment