-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use randomly structured Set and Map in benchmarks
Trees constructed from [1..n] have a specific structure of perfect binary trees linked together. We shuffle the list so that the list is generated from repeated insertions instead, which forms a random structure that should be more representative of average use cases.
- Loading branch information
Showing
4 changed files
with
63 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Utils.Random | ||
( shuffle | ||
) where | ||
|
||
import Data.List (unfoldr) | ||
import qualified Data.Sequence as Seq | ||
import System.Random (StdGen, mkStdGen, randomR) | ||
|
||
-- O(n log n). Deterministic shuffle. Implements Fisher-Yates. | ||
shuffle :: [a] -> [a] | ||
shuffle xs0 = unfoldr f (gen, Seq.fromList xs0) | ||
where | ||
f (g, xs) | ||
| Seq.null xs = Nothing | ||
| otherwise = Just (x, (g', xs')) | ||
where | ||
(i, g') = randomR (0, Seq.length xs - 1) g | ||
x = Seq.index xs i | ||
xs' = Seq.deleteAt i xs | ||
{-# INLINABLE shuffle #-} | ||
|
||
gen :: StdGen | ||
gen = mkStdGen 42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters