-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.hs
More file actions
37 lines (24 loc) · 906 Bytes
/
Heap.hs
File metadata and controls
37 lines (24 loc) · 906 Bytes
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
module Heap where
import AssocList
type Heap a = (Int, [Addr], [(Addr, a)])
hInitial :: Heap a
hInitial = (0, map Addr [1..], [])
hAlloc :: Heap a -> a -> (Heap a, Addr)
hAlloc (size, next : free, cts) n = ((size+1, free, (next, n) : cts), next)
hUpdate :: Heap a -> Addr -> a -> Heap a
hUpdate (size, free, cts) a n = (size, free, (a, n) : remove cts a)
hFree :: Heap a -> Addr -> Heap a
hFree (size, free, cts) a = (size-1, a:free, remove cts a)
hLookup :: Heap a -> Addr -> a
hLookup (size,free,cts) a = aLookup cts a $ error msg
where msg = "can't find node " ++ show a ++ " in heap"
hAddresses :: Heap a -> [Addr]
hAddresses (size,free,cts) = map fst cts
hSize :: Heap a -> Int
hSize (size,free,cts) = size
hNull :: Addr
hNull = Addr 0
hIsnull :: Addr -> Bool
hIsnull (Addr a) = a == 0
newtype Addr = Addr Int deriving (Eq, Ord)
instance Show Addr where show (Addr a) = '#' : show a