From 281c589dc6aa4e85df265db6789a61abb7992d0e Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 29 May 2019 23:48:02 -0700 Subject: [PATCH] Allow accessing several numbers. --- elm.json | 25 +++++++++++++++++++++++++ src/CacheSim/AccessView.elm | 5 +++++ src/CacheSim/Model.elm | 4 ++-- src/CacheSim/Update.elm | 24 ++++++++++++++++-------- src/CacheSim/View.elm | 20 ++++++++++++++++++-- 5 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 elm.json diff --git a/elm.json b/elm.json new file mode 100644 index 0000000..c8dc8a5 --- /dev/null +++ b/elm.json @@ -0,0 +1,25 @@ +{ + "type": "application", + "source-directories": [ + "src" + ], + "elm-version": "0.19.0", + "dependencies": { + "direct": { + "elm/browser": "1.0.1", + "elm/core": "1.0.2", + "elm/html": "1.0.0", + "elm/parser": "1.1.0" + }, + "indirect": { + "elm/json": "1.1.3", + "elm/time": "1.0.0", + "elm/url": "1.0.0", + "elm/virtual-dom": "1.0.2" + } + }, + "test-dependencies": { + "direct": {}, + "indirect": {} + } +} \ No newline at end of file diff --git a/src/CacheSim/AccessView.elm b/src/CacheSim/AccessView.elm index c52232d..7e9ae1f 100644 --- a/src/CacheSim/AccessView.elm +++ b/src/CacheSim/AccessView.elm @@ -35,6 +35,11 @@ accessViewBack (l, ap) = (l, accessPositionBack (List.length l) ap) accessViewDone : AccessView -> Bool accessViewDone (_, ap) = accessPositionDone ap +finalCacheHierarchy : CacheHierarchy -> AccessView -> CacheHierarchy +finalCacheHierarchy ch (l, ap) = + List.map .output l ++ List.drop (List.length l) ch + + effectiveCacheHierarchy : CacheHierarchy -> AccessView -> CacheHierarchy effectiveCacheHierarchy c (l, ap) = let diff --git a/src/CacheSim/Model.elm b/src/CacheSim/Model.elm index 1bf47a8..f1b8f24 100644 --- a/src/CacheSim/Model.elm +++ b/src/CacheSim/Model.elm @@ -6,7 +6,7 @@ import CacheSim.AccessView exposing (..) type alias Model = { rawHierarchy : RawCacheModelHierarchy , hierarchy : Maybe CacheHierarchy - , accessView : Maybe AccessView + , accessView : Maybe (List AccessView) , accessInput : String } type alias Flags = () @@ -15,7 +15,7 @@ type Msg | CreateRawModel | DeleteRawModel Int | UseHierarchy (Maybe CacheModelHierarchy) - | Access Int + | Access (List Int) | ChangeAccessInput String | AccessViewForward | AccessViewBack diff --git a/src/CacheSim/Update.elm b/src/CacheSim/Update.elm index bc9d7fb..28b6445 100644 --- a/src/CacheSim/Update.elm +++ b/src/CacheSim/Update.elm @@ -38,11 +38,19 @@ updateUseHierarchy cmh m = in (newModel, cmd) -updateAccess : Int -> Model -> (Model, Cmd Msg) -updateAccess i m = +updateAccess : List Int -> Model -> (Model, Cmd Msg) +updateAccess li m = let - accessResult = Maybe.andThen (Result.toMaybe << accessCacheHierarchy i) m.hierarchy - newModel = { m | accessView = Maybe.map (\ar -> (ar, Down 0)) accessResult } + process c xs = + case xs of + [] -> Ok [] + (i::t) -> + case accessCacheHierarchy i c of + Ok av -> Result.map ((::) (av, Down 0)) <| process (finalCacheHierarchy c (av, Done)) t + Err s -> Err s + + accessResult = Maybe.andThen (\h -> Result.toMaybe <| process h li) m.hierarchy + newModel = { m | accessView = accessResult } cmd = Cmd.none in (newModel, cmd) @@ -50,11 +58,11 @@ updateAccess i m = updateAccessViewForward : Model -> (Model, Cmd Msg) updateAccessViewForward m = let - afterStep = Maybe.map accessViewForward m.accessView - replaceHierarchy avs h = List.map .output avs ++ List.drop (List.length avs) h + afterStep = Maybe.map (intMapUpdate 0 accessViewForward) m.accessView (newHierarchy, newAccessView) = case afterStep of - Just (avs, Done) -> (Maybe.map (replaceHierarchy avs) m.hierarchy, Nothing) + Just ((avs, Done)::[]) -> (Maybe.map (\h -> finalCacheHierarchy h (avs, Done)) m.hierarchy, Nothing) + Just ((avs, Done)::xs) -> (Maybe.map (\h -> finalCacheHierarchy h (avs, Done)) m.hierarchy, Just xs) as_ -> (m.hierarchy, as_) newModel = { m | accessView = newAccessView, hierarchy = newHierarchy } cmd = Cmd.none @@ -64,7 +72,7 @@ updateAccessViewForward m = updateAccessViewBack : Model -> (Model, Cmd Msg) updateAccessViewBack m = let - afterStep = Maybe.map accessViewBack m.accessView + afterStep = Maybe.map (intMapUpdate 0 accessViewBack) m.accessView newModel = { m | accessView = afterStep } cmd = Cmd.none in diff --git a/src/CacheSim/View.elm b/src/CacheSim/View.elm index f2e3710..28b8640 100644 --- a/src/CacheSim/View.elm +++ b/src/CacheSim/View.elm @@ -4,6 +4,7 @@ import CacheSim.Model exposing (..) import CacheSim.Cache exposing (..) import CacheSim.AccessView exposing (..) import CacheSim.Hierarchy exposing (..) +import Parser exposing ((|.)) import Html exposing (Html, Attribute, input, text, div, label, span, h2, h3, table, tr, td, th, p, h1) import Html.Attributes exposing (type_, class, value, for, classList, disabled, colspan, hidden) import Html.Events exposing (onInput, onClick) @@ -198,13 +199,28 @@ viewAccessLog (aes, ap) = viewAccessInput : Model -> Html Msg viewAccessInput m = let - accessButton = maybeButton (String.toInt m.accessInput) "Access address" Access + parser = + Parser.sequence + { start = "" + , end = "" + , separator = "," + , spaces = Parser.spaces + , item = Parser.int + , trailing = Parser.Optional + } + parseResult = Parser.run (parser |. Parser.end) m.accessInput + accessButton = maybeButton (Result.toMaybe parseResult) "Access address" Access + errorHtml = + case parseResult of + Ok _ -> viewError True "" + Err lde -> viewError False "Unable to parse input. Please enter a sequence of numbers separated by commas." editHierarchyButton = button "Edit hierarchy" (UseHierarchy Nothing) in div [] [ h2 [] [ text "Run access simulation" ] , labeledInput "Access address" m.accessInput ChangeAccessInput , buttonWrapper [ accessButton, editHierarchyButton ] + , errorHtml ] viewDescription : Html Msg @@ -241,7 +257,7 @@ viewBase m = Maybe.withDefault [] <| Maybe.map (List.singleton << viewCacheHierarchy) <| m.hierarchy Just _ -> [] - accessView = Maybe.withDefault [] <| Maybe.map (List.singleton << viewAccessView m) <| m.accessView + accessView = Maybe.withDefault [] <| Maybe.map (List.singleton << viewAccessView m) <| Maybe.andThen (List.head) <| m.accessView in div [ class "container" ] <| [ viewDescription] ++ rawView ++ accessInputView ++ accessView ++ cacheView