|
module Main exposing |
|
( runOnArray |
|
, runOnDict |
|
, runOnIntDict |
|
, runOnList |
|
, runOnPreallocatedArray |
|
, runOnPreallocatedDict |
|
, runOnPreallocatedIntDict |
|
, runOnPreallocatedList |
|
) |
|
|
|
import Array |
|
import Array.Extra |
|
import Dict |
|
import IntDict |
|
import List.Extra |
|
import Random |
|
import Random.List |
|
|
|
|
|
runOnArray : Int -> Int -> Int -> () |
|
runOnArray seed0 insertedItems accessedItems = |
|
run |
|
{ init = \_ -> Array.empty |
|
, insert = |
|
\i v acc -> |
|
case Array.get i acc of |
|
Nothing -> |
|
acc |
|
|> Array.Extra.resizelRepeat (i + 1) 0 |
|
|> Array.set i v |
|
|
|
Just _ -> |
|
Array.set i v acc |
|
, access = Array.get |
|
} |
|
seed0 |
|
insertedItems |
|
accessedItems |
|
|
|
|
|
runOnPreallocatedArray : Int -> Int -> Int -> () |
|
runOnPreallocatedArray seed0 insertedItems accessedItems = |
|
run |
|
{ init = \n -> Array.repeat n 0 |
|
, insert = Array.set |
|
, access = Array.get |
|
} |
|
seed0 |
|
insertedItems |
|
accessedItems |
|
|
|
|
|
runOnDict : Int -> Int -> Int -> () |
|
runOnDict seed0 insertedItems accessedItems = |
|
run |
|
{ init = \_ -> Dict.empty |
|
, insert = Dict.insert |
|
, access = Dict.get |
|
} |
|
seed0 |
|
insertedItems |
|
accessedItems |
|
|
|
|
|
runOnPreallocatedDict : Int -> Int -> Int -> () |
|
runOnPreallocatedDict seed0 insertedItems accessedItems = |
|
run |
|
{ init = \n -> List.range 1 n |> List.map (\x -> ( x, x )) |> Dict.fromList |
|
, insert = Dict.insert |
|
, access = Dict.get |
|
} |
|
seed0 |
|
insertedItems |
|
accessedItems |
|
|
|
|
|
runOnIntDict : Int -> Int -> Int -> () |
|
runOnIntDict seed0 insertedItems accessedItems = |
|
run |
|
{ init = \_ -> IntDict.empty |
|
, insert = IntDict.insert |
|
, access = IntDict.get |
|
} |
|
seed0 |
|
insertedItems |
|
accessedItems |
|
|
|
|
|
runOnPreallocatedIntDict : Int -> Int -> Int -> () |
|
runOnPreallocatedIntDict seed0 insertedItems accessedItems = |
|
run |
|
{ init = \n -> List.range 1 n |> List.map (\x -> ( x, x )) |> IntDict.fromList |
|
, insert = IntDict.insert |
|
, access = IntDict.get |
|
} |
|
seed0 |
|
insertedItems |
|
accessedItems |
|
|
|
|
|
runOnList : Int -> Int -> Int -> () |
|
runOnList seed0 insertedItems accessedItems = |
|
run |
|
{ init = \_ -> [] |
|
, insert = |
|
\i v acc -> |
|
let |
|
len = |
|
List.length acc |
|
|
|
missing = |
|
i - len + 1 |
|
in |
|
if missing > 0 then |
|
(acc ++ List.repeat missing 0) |
|
|> List.Extra.setAt i v |
|
|
|
else |
|
List.Extra.setAt i v acc |
|
, access = List.Extra.getAt |
|
} |
|
seed0 |
|
insertedItems |
|
accessedItems |
|
|
|
|
|
runOnPreallocatedList : Int -> Int -> Int -> () |
|
runOnPreallocatedList seed0 insertedItems accessedItems = |
|
run |
|
{ init = \n -> List.range 1 n |
|
, insert = List.Extra.setAt |
|
, access = List.Extra.getAt |
|
} |
|
seed0 |
|
insertedItems |
|
accessedItems |
|
|
|
|
|
type alias RunConfig a = |
|
{ init : Int -> a |
|
, insert : Int -> Int -> a -> a |
|
, access : Int -> a -> Maybe Int |
|
} |
|
|
|
|
|
run : RunConfig a -> Int -> Int -> Int -> () |
|
run config seed insertedItems accessedItems = |
|
let |
|
seed0 = |
|
Random.initialSeed seed |
|
|
|
rawInsertIndices = |
|
List.range 0 (insertedItems - 1) |
|
|
|
( shuffledInsertIndices, seed1 ) = |
|
Random.step (Random.List.shuffle rawInsertIndices) seed0 |
|
|
|
insertIndices = |
|
shuffledInsertIndices |
|
|> List.indexedMap (\v i -> ( i, v )) |
|
|
|
rawAccessIndices = |
|
List.Extra.cycle accessedItems rawInsertIndices |
|
|
|
( shuffledAccessIndices, _ ) = |
|
Random.step (Random.List.shuffle rawAccessIndices) seed1 |
|
|
|
insertMany : List ( Int, Int ) -> a -> a |
|
insertMany indices acc = |
|
case indices of |
|
[] -> |
|
acc |
|
|
|
( i, v ) :: rest -> |
|
insertMany rest (config.insert i v acc) |
|
|
|
accessMany : List Int -> a -> () |
|
accessMany indices acc = |
|
case indices of |
|
[] -> |
|
() |
|
|
|
index :: rest -> |
|
let |
|
_ = |
|
config.access index acc |
|
in |
|
accessMany rest acc |
|
in |
|
config.init insertedItems |
|
|> insertMany insertIndices |
|
|> accessMany shuffledAccessIndices |