CacheSim/src/CacheSim/IntMap.elm

38 lines
969 B
Elm

module CacheSim.IntMap exposing (..)
type alias IntMap a = List a
intMapGet : Int -> IntMap a -> Maybe a
intMapGet i m =
case (i, m) of
(_, []) -> Nothing
(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
(_, []) -> []
(0, (x::xs)) -> (f x)::xs
(n, (x::xs)) -> x :: intMapUpdate (n-1) f xs
intMapPut : Int -> a -> IntMap a -> IntMap a
intMapPut i v = intMapUpdate i (\_ -> v)
intMapFind : (a -> Bool) -> IntMap a -> Maybe Int
intMapFind f m =
let
findHelper n l =
case l of
[] -> Nothing
(x::xs) -> if f x then Just n else findHelper (n + 1) xs
in
findHelper 0 m