-
Notifications
You must be signed in to change notification settings - Fork 1
/
Map.lhs
225 lines (197 loc) · 7.86 KB
/
Map.lhs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
% -*- LaTeX -*-
% $Id: Map.lhs 1744 2005-08-23 16:17:12Z wlux $
%
% Copyright (c) 1999-2002, Wolfgang Lux
% See LICENSE for the full license.
%
\nwfilename{Map.lhs}
\section{Maps}
The module \texttt{Map} implements finite maps using 2-3 trees.
\begin{verbatim}
> module Map(FM, nullFM, zeroFM, unitFM, addToFM, deleteFromFM,
> lookupFM, fromListFM, toListFM) where
> import List
\end{verbatim}
A 2-3 tree is either empty or a node with either two or three children
that are themselves 2-3 trees of the same height. Thus, a 2-3 is
always balanced.
\begin{verbatim}
> data FM a b =
> Empty
> | Node2 (FM a b) (a,b) (FM a b)
> | Node3 (FM a b) (a,b) (FM a b) (a,b) (FM a b)
> nullFM :: Ord a => FM a b -> Bool
> nullFM Empty = True
> nullFM _ = False
> zeroFM :: Ord a => FM a b
> zeroFM = Empty
> unitFM :: Ord a => a -> b -> FM a b
> unitFM x y = Node2 Empty (x,y) Empty
\end{verbatim}
Insertion into the map is performed with the help of an auxiliary
function. This function returns either the updated node or a triple of
a left and right subtree together with the element between them
if the height of the tree must be changed.
\begin{verbatim}
> addToFM :: Ord a => a -> b -> FM a b -> FM a b
> addToFM x y xys =
> case insertNode x y xys of
> Left xys' -> xys'
> Right (l,x,r) -> Node2 l x r
> fromListFM :: Ord a => [(a,b)] -> FM a b
> fromListFM = foldr (uncurry addToFM) zeroFM
> insertNode :: Ord a => a -> b -> FM a b
> -> Either (FM a b) ((FM a b),(a,b),(FM a b))
> insertNode k x Empty = Right (Empty,(k,x),Empty)
> insertNode k x (Node2 a y b) =
> Left (case compareKey k y of
> LT -> balanceL (insertNode k x a) y b
> EQ -> Node2 a (k,x) b
> GT -> balanceR a y (insertNode k x b))
> where balanceL (Left a) x b = Node2 a x b
> balanceL (Right (a,x,b)) y c = Node3 a x b y c
> balanceR a x (Left b) = Node2 a x b
> balanceR a x (Right (b,y,c)) = Node3 a x b y c
> insertNode k x (Node3 a y b z c) =
> case compareKey k y of
> LT -> balanceL (insertNode k x a) y b z c
> EQ -> Left (Node3 a (k,x) b z c)
> GT ->
> case compareKey k z of
> LT -> balanceM a y (insertNode k x b) z c
> EQ -> Left (Node3 a y b (k,x) c)
> GT -> balanceR a y b z (insertNode k x c)
> where balanceL (Left a) x b y c = Left (Node3 a x b y c)
> balanceL (Right (a,x,b)) y c z d = Right (Node2 a x b,y,Node2 c z d)
> balanceM a x (Left b) y c = Left (Node3 a x b y c)
> balanceM a x (Right (b,y,c)) z d = Right (Node2 a x b,y,Node2 c z d)
> balanceR a x b y (Left c) = Left (Node3 a x b y c)
> balanceR a x b y (Right (c,z,d)) = Right (Node2 a x b,y,Node2 c z d)
> compareKey :: Ord a => a -> (a,b) -> Ordering
> compareKey k1 (k2,_) = compare k1 k2
\end{verbatim}
Deletion also uses an auxiliary function. This function returns the
new node after the element has been deleted together with a boolean
flag that indicates whether the height was decremented.
\begin{verbatim}
> deleteFromFM :: Ord a => a -> FM a b -> FM a b
> deleteFromFM x xys = snd (deleteNode x xys)
> deleteNode :: Ord a => a -> FM a b -> (Bool,FM a b)
> deleteNode _ Empty = (False,Empty)
> deleteNode x (Node2 a y b) =
> case compareKey x y of
> LT -> balanceL (deleteNode x a) y b
> EQ
> | nullFM a -> (True,b)
> | otherwise -> balanceR a u (deleteNode (fst u) b)
> where u = findMin b
> GT -> balanceR a y (deleteNode x b)
> where balanceL (False,a) x b = (False,Node2 a x b)
> balanceL (True,a) x (Node2 b y c) = (True,Node3 a x b y c)
> balanceL (True,a) x (Node3 b y c z d) =
> (False,Node2 (Node2 a x b) y (Node2 c z d))
> balanceR a x (False,b) = (False,Node2 a x b)
> balanceR (Node2 a x b) y (True,c) = (True,Node3 a x b y c)
> balanceR (Node3 a x b y c) z (True,d) =
> (False,Node2 (Node2 a x b) y (Node2 c z d))
> deleteNode x (Node3 a y b z c) =
> (False,
> case compareKey x y of
> LT -> balanceL (deleteNode x a) y b z c
> EQ
> | nullFM a -> Node2 b z c
> | otherwise -> balanceM a u (deleteNode (fst u) b) z c
> where u = findMin b
> GT ->
> case compareKey x z of
> LT -> balanceM a y (deleteNode x b) z c
> EQ
> | nullFM c -> Node2 a y b
> | otherwise -> balanceR a y b u (deleteNode (fst u) c)
> where u = findMin c
> GT -> balanceR a y b z (deleteNode x c))
> where balanceL (False,a) x b y c = Node3 a x b y c
> balanceL (True,a) x (Node2 b y c) z d = Node2 (Node3 a x b y c) z d
> balanceL (True,a) w (Node3 b x c y d) z e =
> Node3 (Node2 a w b) x (Node2 c y d) z e
> balanceM a x (False,b) y c = Node3 a x b y c
> balanceM a x (True,b) y (Node2 c z d) = Node2 a x (Node3 b y c z d)
> balanceM a w (True,b) x (Node3 c y d z e) =
> Node3 a w (Node2 b x c) y (Node2 d z e)
> balanceR a x b y (False,c) = Node3 a x b y c
> balanceR a x (Node2 b y c) z (True,d) = Node2 a x (Node3 b y c z d)
> balanceR a w (Node3 b x c y d) z (True,e) =
> Node3 a w (Node2 b x c) y (Node2 d z e)
> findMin :: Ord a => FM a b -> (a,b)
> findMin (Node2 a x _)
> | nullFM a = x
> | otherwise = findMin a
> findMin (Node3 a x _ _ _)
> | nullFM a = x
> | otherwise = findMin a
\end{verbatim}
Looking up an element is trivial.
\begin{verbatim}
> lookupFM :: Ord a => a -> FM a b -> Maybe b
> lookupFM _ Empty = Nothing
> lookupFM x (Node2 a y b) =
> case compareKey x y of
> LT -> lookupFM x a
> EQ -> Just (snd y)
> GT -> lookupFM x b
> lookupFM x (Node3 a y b z c) =
> case compareKey x y of
> LT -> lookupFM x a
> EQ -> Just (snd y)
> GT -> lookupFM x (Node2 b z c)
\end{verbatim}
The function \texttt{toListFM} returns an association list of all
elements in the map. We use a functional difference list approach
similar to \texttt{show} in order to achieve an efficiency which is
linear in the number of elements in the finite map.
\begin{verbatim}
> toListFM :: Ord a => FM a b -> [(a,b)]
> toListFM = flip elems []
> where elems Empty xs = xs
> elems (Node2 a x b) xs = elems a (x : elems b xs)
> elems (Node3 a x b y c) xs = elems a (x : elems b (y : elems c xs))
\end{verbatim}
Two finite maps are considered equal if they contain the same
elements. Note that the representation trees of the two maps may be
different. Therefore we must use the list of elements in order to
compare the maps.
\begin{verbatim}
> instance (Ord a,Eq b) => Eq (FM a b) where
> xys1 == xys2 = toListFM xys1 == toListFM xys2
\end{verbatim}
When we display a finite map we will show only its semantic
information not the underlying tree representation.
\begin{verbatim}
> instance (Ord a,Show a,Show b) => Show (FM a b) where
> showsPrec p xys =
> showChar '{' . showList (map showAssoc (toListFM xys)) . showChar '}'
> where showList = flip (foldr ($)) . intersperse (showChar ',') -- $
> showAssoc (x,y) = showsPrec 0 x . showString "|->" . showsPrec 0 y
\end{verbatim}
A finite map is a functor with respect to its data argument.
\begin{verbatim}
> instance Ord a => Functor (FM a) where
> fmap f Empty = Empty
> fmap f (Node2 a (k,x) b) = Node2 (fmap f a) (k,f x) (fmap f b)
> fmap f (Node3 a (k,x) b (l,y) c) =
> Node3 (fmap f a) (k,f x) (fmap f b) (l,f y) (fmap f c)
\end{verbatim}
The function \texttt{checkTree} verifies that a 2-3 tree is actually
balanced. The function returns the height of the tree.
\begin{verbatim}
> checkTree :: Ord a => FM a b -> Int
> checkTree Empty = 0
> checkTree (Node2 a _ b)
> | h == checkTree b = h + 1
> | otherwise = error "checkTree: unbalanced 2-3 tree"
> where h = checkTree a
> checkTree (Node3 a _ b _ c)
> | h == checkTree b && h == checkTree c = h + 1
> | otherwise = error "checkTree: unbalanced 2-3 tree"
> where h = checkTree a
\end{verbatim}