-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimulator.elm
66 lines (50 loc) · 1.78 KB
/
Simulator.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Simulator where
import Schedule(..)
import Util(enumerate,repeatN)
main =
let mem = 6
stations = 8
schedule = scanline stations
cstate = map snd <| cacheState mem <| schedule
cstateElem = map asText cstate
misses = cacheMisses mem <| schedule
cumulMisses = scanl1 (+) misses
missesElem = map asText cumulMisses
scheduleElem = map asText <| schedule
noJobs = length schedule
in flow down
[ flow right <| zipWith above scheduleElem <| zipWith above cstateElem missesElem
, asText <| isCompleteSched stations schedule
, asText <| sum misses
--, drawSchedule 10 32 (hindex 3)
--, asText <| (hindex 2)
]
----
-- simulation
cacheState : Int -> Schedule -> [Cache]
cacheState mem sched =
let loadJob (i,j) = load i << load j
in scanl loadJob (emptyCache mem) sched
cacheMisses : Int -> Schedule -> [Int]
cacheMisses mem sched =
let caches = cacheState mem sched
in zipWith (uncurry jobCost) sched caches
jobCost : Int -> Int -> Cache -> Int
jobCost i j cache =
let toInt b = if b then 0 else 1
in toInt (cache `contains` i) + toInt (i == j || contains cache j)
totalCacheMisses : Int -> Schedule -> Int
totalCacheMisses mem = sum << cacheMisses mem
----
-- the cache
type Cache = (Int, [Int])
emptyCache size = (size, [])
contains : Cache -> Int -> Bool
contains (s,contents) i = any (\j -> i == j) contents
remove : Int -> Cache -> Cache
remove i (s,contents) = (s,filter (\j -> i /= j) contents)
load : Int -> Cache -> Cache
load i (s,contents) =
if | (s,contents) `contains` i -> (s, i :: filter (\j -> i/=j) contents)
| length contents < s -> (s, i :: contents)
| otherwise -> (s, i :: take (s-1) contents)