From ad3c1f38b000decfe5bcd5611858cea8eac08284 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 28 May 2019 19:37:22 -0700 Subject: [PATCH] Move updates into a separate module. --- src/CacheSim/IntMap.elm | 7 +++++++ src/CacheSim/Model.elm | 6 ++++-- src/CacheSim/Update.elm | 29 +++++++++++++++++++++++++++++ src/CacheSim/View.elm | 2 +- src/Main.elm | 6 ++++-- 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/CacheSim/Update.elm diff --git a/src/CacheSim/IntMap.elm b/src/CacheSim/IntMap.elm index b3b4095..3503671 100644 --- a/src/CacheSim/IntMap.elm +++ b/src/CacheSim/IntMap.elm @@ -9,6 +9,13 @@ intMapGet i m = (0, (x::xs)) -> Just x (n, (x::xs)) -> intMapGet (n-1) xs +intMapDelete : Int -> IntMap a -> IntMap a +intMapDelete i m = + case (i, m) of + (_, []) -> [] + (0, x::xs) -> xs + (n, x::xs) -> x :: intMapDelete (n-1) xs + intMapUpdate : Int -> (a -> a) -> IntMap a -> IntMap a intMapUpdate i f m = case (i, m) of diff --git a/src/CacheSim/Model.elm b/src/CacheSim/Model.elm index e862002..0ef4c58 100644 --- a/src/CacheSim/Model.elm +++ b/src/CacheSim/Model.elm @@ -5,5 +5,7 @@ type alias Model = { rawCacheModelHierarchy : RawCacheModelHierarchy } type alias Flags = () -type Msg = - UpdateRawModel Int (RawCacheModel -> RawCacheModel) +type Msg + = ChangeRawModel Int (RawCacheModel -> RawCacheModel) + | CreateRawModel + | DeleteRawModel Int diff --git a/src/CacheSim/Update.elm b/src/CacheSim/Update.elm new file mode 100644 index 0000000..bd94456 --- /dev/null +++ b/src/CacheSim/Update.elm @@ -0,0 +1,29 @@ +module CacheSim.Update exposing (..) +import CacheSim.Model exposing (..) +import CacheSim.Raw exposing (..) +import CacheSim.IntMap exposing (..) + +updateChangeRawModel : Int -> (RawCacheModel -> RawCacheModel) -> Model -> (Model, Cmd Msg) +updateChangeRawModel l f m = + let + newModel = { m | rawCacheModelHierarchy = intMapUpdate l f m.rawCacheModelHierarchy } + cmd = Cmd.none + in + (newModel, cmd) + +updateCreateRawModel : Model -> (Model, Cmd Msg) +updateCreateRawModel m = + let + freshRawModel = { blockSize = "", setCount = "", setSize = "" } + newModel = { m | rawCacheModelHierarchy = m.rawCacheModelHierarchy ++ [ freshRawModel ] } + cmd = Cmd.none + in + (newModel, cmd) + +updateDeleteRawModel : Int -> Model -> (Model, Cmd Msg) +updateDeleteRawModel l m = + let + newModel = { m | rawCacheModelHierarchy = intMapDelete l m.rawCacheModelHierarchy } + cmd = Cmd.none + in + (newModel, cmd) diff --git a/src/CacheSim/View.elm b/src/CacheSim/View.elm index a6206ba..3030ae6 100644 --- a/src/CacheSim/View.elm +++ b/src/CacheSim/View.elm @@ -18,7 +18,7 @@ viewRawCacheModel level rcm = updateBlockSize s cm = { cm | blockSize = s} updateSetCount s cm = { cm | setCount = s} updateSetSize s cm = { cm | setSize = s} - wrapUpdate f s = UpdateRawModel level (f s) + wrapUpdate f s = ChangeRawModel level (f s) params = div [ class "cache-model-params" ] [ labeledInput "Block size" rcm.blockSize (wrapUpdate updateBlockSize) diff --git a/src/Main.elm b/src/Main.elm index d52b58e..407b766 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -1,6 +1,7 @@ import CacheSim.Model exposing (..) import CacheSim.Cache exposing (..) import CacheSim.IntMap exposing (..) +import CacheSim.Update exposing (..) import CacheSim.Hierarchy exposing (..) import CacheSim.Raw exposing (..) import CacheSim.View exposing (..) @@ -29,8 +30,9 @@ view m = update : Msg -> Model -> (Model, Cmd Msg) update msg m = case msg of - UpdateRawModel l f -> - ({ m | rawCacheModelHierarchy = intMapUpdate l f m.rawCacheModelHierarchy}, Cmd.none) + ChangeRawModel l f -> updateChangeRawModel l f m + CreateRawModel -> updateCreateRawModel m + DeleteRawModel i -> updateDeleteRawModel i m subscriptions : Model -> Sub Msg subscriptions m = Sub.none