Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
alexandria
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alexander Fedorov
alexandria
Commits
8beef2dc
Commit
8beef2dc
authored
4 years ago
by
death
Committed by
Michał Herda
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Use linear-time SHUFFLE if supplied with many list items to shuffle
parent
f35e232c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
alexandria-1/sequences.lisp
+16
-7
16 additions, 7 deletions
alexandria-1/sequences.lisp
with
16 additions
and
7 deletions
alexandria-1/sequences.lisp
+
16
−
7
View file @
8beef2dc
...
@@ -81,19 +81,28 @@ share structure with it."
...
@@ -81,19 +81,28 @@ share structure with it."
(
defun
shuffle
(
sequence
&key
(
start
0
)
end
)
(
defun
shuffle
(
sequence
&key
(
start
0
)
end
)
"Returns a random permutation of SEQUENCE bounded by START and END.
"Returns a random permutation of SEQUENCE bounded by START and END.
Original sequence may be destructively modified, and (if it contains
Original sequence may be destructively modified.
CONS or lists themselv) share storage with the original one.
Signals an error if SEQUENCE is not a proper sequence."
Signals an error if SEQUENCE is not a proper sequence."
(
declare
(
type
fixnum
start
)
(
declare
(
type
fixnum
start
)
(
type
(
or
fixnum
null
)
end
))
(
type
(
or
fixnum
null
)
end
))
(
etypecase
sequence
(
etypecase
sequence
(
list
(
list
(
let*
((
end
(
or
end
(
proper-list-length
sequence
)))
(
let*
((
end
(
or
end
(
proper-list-length
sequence
)))
(
n
(
-
end
start
)))
(
n
(
-
end
start
))
(
do
((
tail
(
nthcdr
start
sequence
)
(
cdr
tail
)))
(
sublist
(
nthcdr
start
sequence
))
((
zerop
n
))
(
small-enough-threshold
100
))
(
rotatef
(
car
tail
)
(
car
(
nthcdr
(
random
n
)
tail
)))
;; It's fine to use a pure list shuffle if the number of items
(
decf
n
))))
;; 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
(
vector
(
let
((
end
(
or
end
(
length
sequence
))))
(
let
((
end
(
or
end
(
length
sequence
))))
(
loop
for
i
from
start
below
end
(
loop
for
i
from
start
below
end
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment