Move updates into a separate module.

This commit is contained in:
Danila Fedorin 2019-05-28 19:37:22 -07:00
parent 2b00e5c7a5
commit ad3c1f38b0
5 changed files with 45 additions and 5 deletions

View File

@ -9,6 +9,13 @@ intMapGet i m =
(0, (x::xs)) -> Just x (0, (x::xs)) -> Just x
(n, (x::xs)) -> intMapGet (n-1) xs (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 : Int -> (a -> a) -> IntMap a -> IntMap a
intMapUpdate i f m = intMapUpdate i f m =
case (i, m) of case (i, m) of

View File

@ -5,5 +5,7 @@ type alias Model =
{ rawCacheModelHierarchy : RawCacheModelHierarchy { rawCacheModelHierarchy : RawCacheModelHierarchy
} }
type alias Flags = () type alias Flags = ()
type Msg = type Msg
UpdateRawModel Int (RawCacheModel -> RawCacheModel) = ChangeRawModel Int (RawCacheModel -> RawCacheModel)
| CreateRawModel
| DeleteRawModel Int

29
src/CacheSim/Update.elm Normal file
View File

@ -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)

View File

@ -18,7 +18,7 @@ viewRawCacheModel level rcm =
updateBlockSize s cm = { cm | blockSize = s} updateBlockSize s cm = { cm | blockSize = s}
updateSetCount s cm = { cm | setCount = s} updateSetCount s cm = { cm | setCount = s}
updateSetSize s cm = { cm | setSize = 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" ] params = div [ class "cache-model-params" ]
[ labeledInput "Block size" rcm.blockSize (wrapUpdate updateBlockSize) [ labeledInput "Block size" rcm.blockSize (wrapUpdate updateBlockSize)

View File

@ -1,6 +1,7 @@
import CacheSim.Model exposing (..) import CacheSim.Model exposing (..)
import CacheSim.Cache exposing (..) import CacheSim.Cache exposing (..)
import CacheSim.IntMap exposing (..) import CacheSim.IntMap exposing (..)
import CacheSim.Update exposing (..)
import CacheSim.Hierarchy exposing (..) import CacheSim.Hierarchy exposing (..)
import CacheSim.Raw exposing (..) import CacheSim.Raw exposing (..)
import CacheSim.View exposing (..) import CacheSim.View exposing (..)
@ -29,8 +30,9 @@ view m =
update : Msg -> Model -> (Model, Cmd Msg) update : Msg -> Model -> (Model, Cmd Msg)
update msg m = update msg m =
case msg of case msg of
UpdateRawModel l f -> ChangeRawModel l f -> updateChangeRawModel l f m
({ m | rawCacheModelHierarchy = intMapUpdate l f m.rawCacheModelHierarchy}, Cmd.none) CreateRawModel -> updateCreateRawModel m
DeleteRawModel i -> updateDeleteRawModel i m
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
subscriptions m = Sub.none subscriptions m = Sub.none