-
Notifications
You must be signed in to change notification settings - Fork 4
/
IdentityList.hs
177 lines (129 loc) · 6.25 KB
/
IdentityList.hs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
{-
******************************************************************************
* I N V A D E R S *
* *
* Module: IdentityList *
* Purpose: Association list with automatic key assignment and *
* identity-preserving map and filter operations. *
* Author: Henrik Nilsson *
* *
* Copyright (c) Yale University, 2003 *
* *
******************************************************************************
-}
module IdentityList (
ILKey, -- Identity-list key type
IL, -- Identity-list, abstract. Instance of functor.
emptyIL, -- :: IL a
insertIL_, -- :: a -> IL a -> IL a
insertIL, -- :: a -> IL a -> (ILKey, IL a)
listToIL, -- :: [a] -> IL a
keysIL, -- :: IL a -> [ILKey]
elemsIL, -- :: IL a -> [a]
assocsIL, -- :: IL a -> [(ILKey, a)]
deleteIL, -- :: ILKey -> IL a -> IL a
updateIL, -- :: ILKey -> a -> IL a -> IL a
updateILWith, -- :: ILKey -> (a -> a) -> IL a -> IL a
mapIL, -- :: ((ILKey, a) -> b) -> IL a -> IL b
filterIL, -- :: ((ILKey, a) -> Bool) -> IL a -> IL a
mapFilterIL, -- :: ((ILKey, a) -> Maybe b) -> IL a -> IL b
lookupIL, -- :: ILKey -> IL a -> Maybe a
findIL, -- :: ((ILKey, a) -> Bool) -> IL a -> Maybe a
mapFindIL, -- :: ((ILKey, a) -> Maybe b) -> IL a -> Maybe b
findAllIL, -- :: ((ILKey, a) -> Bool) -> IL a -> [a]
mapFindAllIL -- :: ((ILKey, a) -> Maybe b) -> IL a -> [b]
) where
import Data.List (find)
------------------------------------------------------------------------------
-- Data type definitions
------------------------------------------------------------------------------
type ILKey = Int
-- Invariants:
-- * Sorted in descending key order. (We don't worry about
-- key wrap around).
-- * Keys are NOT reused
data IL a = IL { ilNextKey :: ILKey, ilAssocs :: [(ILKey, a)] }
------------------------------------------------------------------------------
-- Class instances
------------------------------------------------------------------------------
instance Functor IL where
fmap f (IL {ilNextKey = nk, ilAssocs = kas}) =
IL {ilNextKey = nk, ilAssocs = [ (i, f a) | (i, a) <- kas ]}
------------------------------------------------------------------------------
-- Constructors
------------------------------------------------------------------------------
emptyIL :: IL a
emptyIL = IL {ilNextKey = 0, ilAssocs = []}
insertIL_ :: a -> IL a -> IL a
insertIL_ a il = snd (insertIL a il)
insertIL :: a -> IL a -> (ILKey, IL a)
insertIL a (IL {ilNextKey = k, ilAssocs = kas}) = (k, il') where
il' = IL {ilNextKey = k + 1, ilAssocs = (k, a) : kas}
listToIL :: [a] -> IL a
listToIL as = IL {ilNextKey = length as,
ilAssocs = reverse (zip [0..] as)} -- Maintain invariant!
------------------------------------------------------------------------------
-- Additional selectors
------------------------------------------------------------------------------
assocsIL :: IL a -> [(ILKey, a)]
assocsIL = ilAssocs
keysIL :: IL a -> [ILKey]
keysIL = map fst . ilAssocs
elemsIL :: IL a -> [a]
elemsIL = map snd . ilAssocs
------------------------------------------------------------------------------
-- Mutators
------------------------------------------------------------------------------
deleteIL :: ILKey -> IL a -> IL a
deleteIL k (IL {ilNextKey = nk, ilAssocs = kas}) =
IL {ilNextKey = nk, ilAssocs = deleteHlp kas}
where
deleteHlp [] = []
deleteHlp kakas@(ka@(k', _) : kas) | k > k' = kakas
| k == k' = kas
| otherwise = ka : deleteHlp kas
updateIL :: ILKey -> a -> IL a -> IL a
updateIL k v l = updateILWith k (const v) l
updateILWith :: ILKey -> (a -> a) -> IL a -> IL a
updateILWith k f l = mapIL g l
where g (k',v') | k == k' = f v'
| otherwise = v'
------------------------------------------------------------------------------
-- Filter and map operations
------------------------------------------------------------------------------
-- These are "identity-preserving", i.e. the key associated with an element
-- in the result is the same as the key of the element from which the
-- result element was derived.
mapIL :: ((ILKey, a) -> b) -> IL a -> IL b
mapIL f (IL {ilNextKey = nk, ilAssocs = kas}) =
IL {ilNextKey = nk, ilAssocs = [(k, f ka) | ka@(k,_) <- kas]}
filterIL :: ((ILKey, a) -> Bool) -> IL a -> IL a
filterIL p (IL {ilNextKey = nk, ilAssocs = kas}) =
IL {ilNextKey = nk, ilAssocs = filter p kas}
mapFilterIL :: ((ILKey, a) -> Maybe b) -> IL a -> IL b
mapFilterIL p (IL {ilNextKey = nk, ilAssocs = kas}) =
IL {
ilNextKey = nk,
ilAssocs = [(k, b) | ka@(k, _) <- kas, Just b <- [p ka]]
}
------------------------------------------------------------------------------
-- Lookup operations
------------------------------------------------------------------------------
lookupIL :: ILKey -> IL a -> Maybe a
lookupIL k il = lookup k (ilAssocs il)
findIL :: ((ILKey, a) -> Bool) -> IL a -> Maybe a
findIL p (IL {ilAssocs = kas}) = findHlp kas
where
findHlp [] = Nothing
findHlp (ka@(_, a) : kas) = if p ka then Just a else findHlp kas
mapFindIL :: ((ILKey, a) -> Maybe b) -> IL a -> Maybe b
mapFindIL p (IL {ilAssocs = kas}) = mapFindHlp kas
where
mapFindHlp [] = Nothing
mapFindHlp (ka : kas) = case p ka of
Nothing -> mapFindHlp kas
jb@(Just _) -> jb
findAllIL :: ((ILKey, a) -> Bool) -> IL a -> [a]
findAllIL p (IL {ilAssocs = kas}) = [ a | ka@(_, a) <- kas, p ka ]
mapFindAllIL:: ((ILKey, a) -> Maybe b) -> IL a -> [b]
mapFindAllIL p (IL {ilAssocs = kas}) = [ b | ka <- kas, Just b <- [p ka] ]