[project @ 1996-06-26 10:26:00 by partain]
[ghc-hetmet.git] / ghc / compiler / utils / FiniteMap.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4 \section[FiniteMap]{An implementation of finite maps}
5
6 ``Finite maps'' are the heart of the compiler's
7 lookup-tables/environments and its implementation of sets.  Important
8 stuff!
9
10 This code is derived from that in the paper:
11 \begin{display}
12         S Adams
13         "Efficient sets: a balancing act"
14         Journal of functional programming 3(4) Oct 1993, pp553-562
15 \end{display}
16
17 The code is SPECIALIZEd to various highly-desirable types (e.g., Id)
18 near the end (only \tr{#ifdef COMPILING_GHC}).
19
20 \begin{code}
21 #ifdef COMPILING_GHC
22 #include "HsVersions.h"
23 #define IF_NOT_GHC(a) {--}
24 #else
25 #define ASSERT(e) {--}
26 #define IF_NOT_GHC(a) a
27 #define COMMA ,
28 #endif
29
30 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)/* NB NB NB */
31 #define OUTPUTABLE_key , Outputable key
32 #else
33 #define OUTPUTABLE_key {--}
34 #endif
35
36 module FiniteMap (
37         FiniteMap,              -- abstract type
38
39         emptyFM, unitFM, listToFM,
40
41         addToFM,
42         addToFM_C,
43         addListToFM,
44         addListToFM_C,
45         IF_NOT_GHC(delFromFM COMMA)
46         delListFromFM,
47
48         plusFM,
49         plusFM_C,
50         minusFM,
51         foldFM,
52
53         IF_NOT_GHC(intersectFM COMMA)
54         IF_NOT_GHC(intersectFM_C COMMA)
55         IF_NOT_GHC(mapFM COMMA filterFM COMMA)
56
57         sizeFM, isEmptyFM, elemFM, lookupFM, lookupWithDefaultFM,
58
59         fmToList, keysFM, eltsFM
60
61 #ifdef COMPILING_GHC
62         , bagToFM
63         , SYN_IE(FiniteSet), emptySet, mkSet, isEmptySet
64         , elementOf, setToList, union, minusSet
65 #endif
66     ) where
67
68 import Maybes
69
70 #ifdef COMPILING_GHC
71 IMP_Ubiq(){-uitous-}
72 # ifdef DEBUG
73 import Pretty
74 # endif
75 import Bag      ( foldBag )
76 import {-hide from mkdependHS-}
77         Name    ( RdrName, OrigName )   -- specialising only
78
79 # if ! OMIT_NATIVE_CODEGEN
80 #  define IF_NCG(a) a
81 # else
82 #  define IF_NCG(a) {--}
83 # endif
84 #endif
85
86 -- SIGH: but we use unboxed "sizes"...
87 #if __GLASGOW_HASKELL__
88 #define IF_GHC(a,b) a
89 #else /* not GHC */
90 #define IF_GHC(a,b) b
91 #endif /* not GHC */
92 \end{code}
93
94
95 %************************************************************************
96 %*                                                                      *
97 \subsection{The signature of the module}
98 %*                                                                      *
99 %************************************************************************
100
101 \begin{code}
102 --      BUILDING
103 emptyFM         :: FiniteMap key elt
104 unitFM          :: key -> elt -> FiniteMap key elt
105 listToFM        :: (Ord key OUTPUTABLE_key) => [(key,elt)] -> FiniteMap key elt
106                         -- In the case of duplicates, the last is taken
107 #ifdef COMPILING_GHC
108 bagToFM         :: (Ord key OUTPUTABLE_key) => Bag (key,elt) -> FiniteMap key elt
109                         -- In the case of duplicates, who knows which is taken
110 #endif
111
112 --      ADDING AND DELETING
113                    -- Throws away any previous binding
114                    -- In the list case, the items are added starting with the
115                    -- first one in the list
116 addToFM         :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> elt  -> FiniteMap key elt
117 addListToFM     :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [(key,elt)] -> FiniteMap key elt
118
119                    -- Combines with previous binding
120 addToFM_C       :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
121                            -> FiniteMap key elt -> key -> elt
122                            -> FiniteMap key elt
123 addListToFM_C   :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
124                            -> FiniteMap key elt -> [(key,elt)]
125                            -> FiniteMap key elt
126
127                    -- Deletion doesn't complain if you try to delete something
128                    -- which isn't there
129 delFromFM       :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key   -> FiniteMap key elt
130 delListFromFM   :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [key] -> FiniteMap key elt
131
132 --      COMBINING
133                    -- Bindings in right argument shadow those in the left
134 plusFM          :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
135                            -> FiniteMap key elt
136
137                    -- Combines bindings for the same thing with the given function
138 plusFM_C        :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
139                            -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
140
141 minusFM         :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
142                    -- (minusFM a1 a2) deletes from a1 any bindings which are bound in a2
143
144 intersectFM     :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
145 intersectFM_C   :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
146                            -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
147
148 --      MAPPING, FOLDING, FILTERING
149 foldFM          :: (key -> elt -> a -> a) -> a -> FiniteMap key elt -> a
150 mapFM           :: (key -> elt1 -> elt2) -> FiniteMap key elt1 -> FiniteMap key elt2
151 filterFM        :: (Ord key OUTPUTABLE_key) => (key -> elt -> Bool)
152                            -> FiniteMap key elt -> FiniteMap key elt
153
154 --      INTERROGATING
155 sizeFM          :: FiniteMap key elt -> Int
156 isEmptyFM       :: FiniteMap key elt -> Bool
157
158 elemFM          :: (Ord key OUTPUTABLE_key) => key -> FiniteMap key elt -> Bool
159 lookupFM        :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> Maybe elt
160 lookupWithDefaultFM
161                 :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> elt -> key -> elt
162                 -- lookupWithDefaultFM supplies a "default" elt
163                 -- to return for an unmapped key
164
165 --      LISTIFYING
166 fmToList        :: FiniteMap key elt -> [(key,elt)]
167 keysFM          :: FiniteMap key elt -> [key]
168 eltsFM          :: FiniteMap key elt -> [elt]
169 \end{code}
170
171 %************************************************************************
172 %*                                                                      *
173 \subsection{The @FiniteMap@ data type, and building of same}
174 %*                                                                      *
175 %************************************************************************
176
177 Invariants about @FiniteMap@:
178 \begin{enumerate}
179 \item
180 all keys in a FiniteMap are distinct
181 \item
182 all keys in left  subtree are $<$ key in Branch and
183 all keys in right subtree are $>$ key in Branch
184 \item
185 size field of a Branch gives number of Branch nodes in the tree
186 \item
187 size of left subtree is differs from size of right subtree by a
188 factor of at most \tr{sIZE_RATIO}
189 \end{enumerate}
190
191 \begin{code}
192 data FiniteMap key elt
193   = EmptyFM
194   | Branch key elt              -- Key and elt stored here
195     IF_GHC(Int#,Int{-STRICT-})  -- Size >= 1
196     (FiniteMap key elt)         -- Children
197     (FiniteMap key elt)
198 \end{code}
199
200 \begin{code}
201 emptyFM = EmptyFM
202 {-
203 emptyFM
204   = Branch bottom bottom IF_GHC(0#,0) bottom bottom
205   where
206     bottom = panic "emptyFM"
207 -}
208
209 -- #define EmptyFM (Branch _ _ IF_GHC(0#,0) _ _)
210
211 unitFM key elt = Branch key elt IF_GHC(1#,1) emptyFM emptyFM
212
213 listToFM = addListToFM emptyFM
214
215 #ifdef COMPILING_GHC
216 bagToFM = foldBag plusFM (\ (k,v) -> unitFM k v) emptyFM
217 #endif
218 \end{code}
219
220 %************************************************************************
221 %*                                                                      *
222 \subsection{Adding to and deleting from @FiniteMaps@}
223 %*                                                                      *
224 %************************************************************************
225
226 \begin{code}
227 addToFM fm key elt = addToFM_C (\ old new -> new) fm key elt
228
229 addToFM_C combiner EmptyFM key elt = unitFM key elt
230 addToFM_C combiner (Branch key elt size fm_l fm_r) new_key new_elt
231 #ifdef __GLASGOW_HASKELL__
232   = case _tagCmp new_key key of
233         _LT -> mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
234         _GT -> mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
235         _EQ -> Branch new_key (combiner elt new_elt) size fm_l fm_r
236 #else
237   | new_key < key = mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
238   | new_key > key = mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
239   | otherwise     = Branch new_key (combiner elt new_elt) size fm_l fm_r
240 #endif
241
242 addListToFM fm key_elt_pairs = addListToFM_C (\ old new -> new) fm key_elt_pairs
243
244 addListToFM_C combiner fm key_elt_pairs
245   = foldl add fm key_elt_pairs  -- foldl adds from the left
246   where
247     add fmap (key,elt) = addToFM_C combiner fmap key elt
248 \end{code}
249
250 \begin{code}
251 delFromFM EmptyFM del_key = emptyFM
252 delFromFM (Branch key elt size fm_l fm_r) del_key
253 #ifdef __GLASGOW_HASKELL__
254   = case _tagCmp del_key key of
255         _GT -> mkBalBranch key elt fm_l (delFromFM fm_r del_key)
256         _LT -> mkBalBranch key elt (delFromFM fm_l del_key) fm_r
257         _EQ -> glueBal fm_l fm_r
258 #else
259   | del_key > key
260   = mkBalBranch key elt fm_l (delFromFM fm_r del_key)
261
262   | del_key < key
263   = mkBalBranch key elt (delFromFM fm_l del_key) fm_r
264
265   | key == del_key
266   = glueBal fm_l fm_r
267 #endif
268
269 delListFromFM fm keys = foldl delFromFM fm keys
270 \end{code}
271
272 %************************************************************************
273 %*                                                                      *
274 \subsection{Combining @FiniteMaps@}
275 %*                                                                      *
276 %************************************************************************
277
278 \begin{code}
279 plusFM_C combiner EmptyFM fm2 = fm2
280 plusFM_C combiner fm1 EmptyFM = fm1
281 plusFM_C combiner fm1 (Branch split_key elt2 _ left right)
282   = mkVBalBranch split_key new_elt
283                  (plusFM_C combiner lts left)
284                  (plusFM_C combiner gts right)
285   where
286     lts     = splitLT fm1 split_key
287     gts     = splitGT fm1 split_key
288     new_elt = case lookupFM fm1 split_key of
289                 Nothing   -> elt2
290                 Just elt1 -> combiner elt1 elt2
291
292 -- It's worth doing plusFM specially, because we don't need
293 -- to do the lookup in fm1.
294
295 plusFM EmptyFM fm2 = fm2
296 plusFM fm1 EmptyFM = fm1
297 plusFM fm1 (Branch split_key elt1 _ left right)
298   = mkVBalBranch split_key elt1 (plusFM lts left) (plusFM gts right)
299   where
300     lts     = splitLT fm1 split_key
301     gts     = splitGT fm1 split_key
302
303 minusFM EmptyFM fm2 = emptyFM
304 minusFM fm1 EmptyFM = fm1
305 minusFM fm1 (Branch split_key elt _ left right)
306   = glueVBal (minusFM lts left) (minusFM gts right)
307         -- The two can be way different, so we need glueVBal
308   where
309     lts = splitLT fm1 split_key         -- NB gt and lt, so the equal ones
310     gts = splitGT fm1 split_key         -- are not in either.
311
312 intersectFM fm1 fm2 = intersectFM_C (\ left right -> right) fm1 fm2
313
314 intersectFM_C combiner fm1 EmptyFM = emptyFM
315 intersectFM_C combiner EmptyFM fm2 = emptyFM
316 intersectFM_C combiner fm1 (Branch split_key elt2 _ left right)
317
318   | maybeToBool maybe_elt1      -- split_elt *is* in intersection
319   = mkVBalBranch split_key (combiner elt1 elt2) (intersectFM_C combiner lts left)
320                                                 (intersectFM_C combiner gts right)
321
322   | otherwise                   -- split_elt is *not* in intersection
323   = glueVBal (intersectFM_C combiner lts left) (intersectFM_C combiner gts right)
324
325   where
326     lts = splitLT fm1 split_key         -- NB gt and lt, so the equal ones
327     gts = splitGT fm1 split_key         -- are not in either.
328
329     maybe_elt1 = lookupFM fm1 split_key
330     Just elt1  = maybe_elt1
331 \end{code}
332
333 %************************************************************************
334 %*                                                                      *
335 \subsection{Mapping, folding, and filtering with @FiniteMaps@}
336 %*                                                                      *
337 %************************************************************************
338
339 \begin{code}
340 foldFM k z EmptyFM = z
341 foldFM k z (Branch key elt _ fm_l fm_r)
342   = foldFM k (k key elt (foldFM k z fm_r)) fm_l
343
344 mapFM f EmptyFM = emptyFM
345 mapFM f (Branch key elt size fm_l fm_r)
346   = Branch key (f key elt) size (mapFM f fm_l) (mapFM f fm_r)
347
348 filterFM p EmptyFM = emptyFM
349 filterFM p (Branch key elt _ fm_l fm_r)
350   | p key elt           -- Keep the item
351   = mkVBalBranch key elt (filterFM p fm_l) (filterFM p fm_r)
352
353   | otherwise           -- Drop the item
354   = glueVBal (filterFM p fm_l) (filterFM p fm_r)
355 \end{code}
356
357 %************************************************************************
358 %*                                                                      *
359 \subsection{Interrogating @FiniteMaps@}
360 %*                                                                      *
361 %************************************************************************
362
363 \begin{code}
364 --{-# INLINE sizeFM #-}
365 sizeFM EmptyFM               = 0
366 sizeFM (Branch _ _ size _ _) = IF_GHC(I# size, size)
367
368 isEmptyFM fm = sizeFM fm == 0
369
370 lookupFM EmptyFM key = Nothing
371 lookupFM (Branch key elt _ fm_l fm_r) key_to_find
372 #ifdef __GLASGOW_HASKELL__
373   = case _tagCmp key_to_find key of
374         _LT -> lookupFM fm_l key_to_find
375         _GT -> lookupFM fm_r key_to_find
376         _EQ -> Just elt
377 #else
378   | key_to_find < key = lookupFM fm_l key_to_find
379   | key_to_find > key = lookupFM fm_r key_to_find
380   | otherwise     = Just elt
381 #endif
382
383 key `elemFM` fm
384   = case (lookupFM fm key) of { Nothing -> False; Just elt -> True }
385
386 lookupWithDefaultFM fm deflt key
387   = case (lookupFM fm key) of { Nothing -> deflt; Just elt -> elt }
388 \end{code}
389
390 %************************************************************************
391 %*                                                                      *
392 \subsection{Listifying @FiniteMaps@}
393 %*                                                                      *
394 %************************************************************************
395
396 \begin{code}
397 fmToList fm = foldFM (\ key elt rest -> (key,elt) : rest) [] fm
398 keysFM fm   = foldFM (\ key elt rest -> key : rest)       [] fm
399 eltsFM fm   = foldFM (\ key elt rest -> elt : rest)       [] fm
400 \end{code}
401
402
403 %************************************************************************
404 %*                                                                      *
405 \subsection{The implementation of balancing}
406 %*                                                                      *
407 %************************************************************************
408
409 %************************************************************************
410 %*                                                                      *
411 \subsubsection{Basic construction of a @FiniteMap@}
412 %*                                                                      *
413 %************************************************************************
414
415 @mkBranch@ simply gets the size component right.  This is the ONLY
416 (non-trivial) place the Branch object is built, so the ASSERTion
417 recursively checks consistency.  (The trivial use of Branch is in
418 @unitFM@.)
419
420 \begin{code}
421 sIZE_RATIO :: Int
422 sIZE_RATIO = 5
423
424 mkBranch :: (Ord key OUTPUTABLE_key)            -- Used for the assertion checking only
425          => Int
426          -> key -> elt
427          -> FiniteMap key elt -> FiniteMap key elt
428          -> FiniteMap key elt
429
430 mkBranch which key elt fm_l fm_r
431   = --ASSERT( left_ok && right_ok && balance_ok )
432 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
433     if not ( left_ok && right_ok && balance_ok ) then
434         pprPanic ("mkBranch:"++show which) (ppAboves [ppr PprDebug [left_ok, right_ok, balance_ok],
435                                        ppr PprDebug key,
436                                        ppr PprDebug fm_l,
437                                        ppr PprDebug fm_r])
438     else
439 #endif
440     let
441         result = Branch key elt (unbox (1 + left_size + right_size)) fm_l fm_r
442     in
443 --    if sizeFM result <= 8 then
444         result
445 --    else
446 --      pprTrace ("mkBranch:"++(show which)) (ppr PprDebug result) (
447 --      result
448 --      )
449   where
450     left_ok  = case fm_l of
451                 EmptyFM                  -> True
452                 Branch left_key _ _ _ _  -> let
453                                                 biggest_left_key = fst (findMax fm_l)
454                                             in
455                                             biggest_left_key < key
456     right_ok = case fm_r of
457                 EmptyFM                  -> True
458                 Branch right_key _ _ _ _ -> let
459                                                 smallest_right_key = fst (findMin fm_r)
460                                             in
461                                             key < smallest_right_key
462     balance_ok = True -- sigh
463 {- LATER:
464     balance_ok
465       = -- Both subtrees have one or no elements...
466         (left_size + right_size <= 1)
467 -- NO         || left_size == 0  -- ???
468 -- NO         || right_size == 0 -- ???
469         -- ... or the number of elements in a subtree does not exceed
470         -- sIZE_RATIO times the number of elements in the other subtree
471       || (left_size  * sIZE_RATIO >= right_size &&
472           right_size * sIZE_RATIO >= left_size)
473 -}
474
475     left_size  = sizeFM fm_l
476     right_size = sizeFM fm_r
477
478 #ifdef __GLASGOW_HASKELL__
479     unbox :: Int -> Int#
480     unbox (I# size) = size
481 #else
482     unbox :: Int -> Int
483     unbox x = x
484 #endif
485 \end{code}
486
487 %************************************************************************
488 %*                                                                      *
489 \subsubsection{{\em Balanced} construction of a @FiniteMap@}
490 %*                                                                      *
491 %************************************************************************
492
493 @mkBalBranch@ rebalances, assuming that the subtrees aren't too far
494 out of whack.
495
496 \begin{code}
497 mkBalBranch :: (Ord key OUTPUTABLE_key)
498             => key -> elt
499             -> FiniteMap key elt -> FiniteMap key elt
500             -> FiniteMap key elt
501
502 mkBalBranch key elt fm_L fm_R
503
504   | size_l + size_r < 2
505   = mkBranch 1{-which-} key elt fm_L fm_R
506
507   | size_r > sIZE_RATIO * size_l        -- Right tree too big
508   = case fm_R of
509         Branch _ _ _ fm_rl fm_rr
510                 | sizeFM fm_rl < 2 * sizeFM fm_rr -> single_L fm_L fm_R
511                 | otherwise                       -> double_L fm_L fm_R
512         -- Other case impossible
513
514   | size_l > sIZE_RATIO * size_r        -- Left tree too big
515   = case fm_L of
516         Branch _ _ _ fm_ll fm_lr
517                 | sizeFM fm_lr < 2 * sizeFM fm_ll -> single_R fm_L fm_R
518                 | otherwise                       -> double_R fm_L fm_R
519         -- Other case impossible
520
521   | otherwise                           -- No imbalance
522   = mkBranch 2{-which-} key elt fm_L fm_R
523
524   where
525     size_l   = sizeFM fm_L
526     size_r   = sizeFM fm_R
527
528     single_L fm_l (Branch key_r elt_r _ fm_rl fm_rr)
529         = mkBranch 3{-which-} key_r elt_r (mkBranch 4{-which-} key elt fm_l fm_rl) fm_rr
530
531     double_L fm_l (Branch key_r elt_r _ (Branch key_rl elt_rl _ fm_rll fm_rlr) fm_rr)
532         = mkBranch 5{-which-} key_rl elt_rl (mkBranch 6{-which-} key   elt   fm_l   fm_rll)
533                                  (mkBranch 7{-which-} key_r elt_r fm_rlr fm_rr)
534
535     single_R (Branch key_l elt_l _ fm_ll fm_lr) fm_r
536         = mkBranch 8{-which-} key_l elt_l fm_ll (mkBranch 9{-which-} key elt fm_lr fm_r)
537
538     double_R (Branch key_l elt_l _ fm_ll (Branch key_lr elt_lr _ fm_lrl fm_lrr)) fm_r
539         = mkBranch 10{-which-} key_lr elt_lr (mkBranch 11{-which-} key_l elt_l fm_ll  fm_lrl)
540                                  (mkBranch 12{-which-} key   elt   fm_lrr fm_r)
541 \end{code}
542
543
544 \begin{code}
545 mkVBalBranch :: (Ord key OUTPUTABLE_key)
546              => key -> elt
547              -> FiniteMap key elt -> FiniteMap key elt
548              -> FiniteMap key elt
549
550 -- Assert: in any call to (mkVBalBranch_C comb key elt l r),
551 --         (a) all keys in l are < all keys in r
552 --         (b) all keys in l are < key
553 --         (c) all keys in r are > key
554
555 mkVBalBranch key elt EmptyFM fm_r = addToFM fm_r key elt
556 mkVBalBranch key elt fm_l EmptyFM = addToFM fm_l key elt
557
558 mkVBalBranch key elt fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
559                      fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
560   | sIZE_RATIO * size_l < size_r
561   = mkBalBranch key_r elt_r (mkVBalBranch key elt fm_l fm_rl) fm_rr
562
563   | sIZE_RATIO * size_r < size_l
564   = mkBalBranch key_l elt_l fm_ll (mkVBalBranch key elt fm_lr fm_r)
565
566   | otherwise
567   = mkBranch 13{-which-} key elt fm_l fm_r
568
569   where
570     size_l = sizeFM fm_l
571     size_r = sizeFM fm_r
572 \end{code}
573
574 %************************************************************************
575 %*                                                                      *
576 \subsubsection{Gluing two trees together}
577 %*                                                                      *
578 %************************************************************************
579
580 @glueBal@ assumes its two arguments aren't too far out of whack, just
581 like @mkBalBranch@.  But: all keys in first arg are $<$ all keys in
582 second.
583
584 \begin{code}
585 glueBal :: (Ord key OUTPUTABLE_key)
586         => FiniteMap key elt -> FiniteMap key elt
587         -> FiniteMap key elt
588
589 glueBal EmptyFM fm2 = fm2
590 glueBal fm1 EmptyFM = fm1
591 glueBal fm1 fm2
592         -- The case analysis here (absent in Adams' program) is really to deal
593         -- with the case where fm2 is a singleton. Then deleting the minimum means
594         -- we pass an empty tree to mkBalBranch, which breaks its invariant.
595   | sizeFM fm2 > sizeFM fm1
596   = mkBalBranch mid_key2 mid_elt2 fm1 (deleteMin fm2)
597
598   | otherwise
599   = mkBalBranch mid_key1 mid_elt1 (deleteMax fm1) fm2
600   where
601     (mid_key1, mid_elt1) = findMax fm1
602     (mid_key2, mid_elt2) = findMin fm2
603 \end{code}
604
605 @glueVBal@ copes with arguments which can be of any size.
606 But: all keys in first arg are $<$ all keys in second.
607
608 \begin{code}
609 glueVBal :: (Ord key OUTPUTABLE_key)
610          => FiniteMap key elt -> FiniteMap key elt
611          -> FiniteMap key elt
612
613 glueVBal EmptyFM fm2 = fm2
614 glueVBal fm1 EmptyFM = fm1
615 glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
616          fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
617   | sIZE_RATIO * size_l < size_r
618   = mkBalBranch key_r elt_r (glueVBal fm_l fm_rl) fm_rr
619
620   | sIZE_RATIO * size_r < size_l
621   = mkBalBranch key_l elt_l fm_ll (glueVBal fm_lr fm_r)
622
623   | otherwise           -- We now need the same two cases as in glueBal above.
624   = glueBal fm_l fm_r
625   where
626     (mid_key_l,mid_elt_l) = findMax fm_l
627     (mid_key_r,mid_elt_r) = findMin fm_r
628     size_l = sizeFM fm_l
629     size_r = sizeFM fm_r
630 \end{code}
631
632 %************************************************************************
633 %*                                                                      *
634 \subsection{Local utilities}
635 %*                                                                      *
636 %************************************************************************
637
638 \begin{code}
639 splitLT, splitGT :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
640
641 -- splitLT fm split_key  =  fm restricted to keys <  split_key
642 -- splitGT fm split_key  =  fm restricted to keys >  split_key
643
644 splitLT EmptyFM split_key = emptyFM
645 splitLT (Branch key elt _ fm_l fm_r) split_key
646 #ifdef __GLASGOW_HASKELL__
647   = case _tagCmp split_key key of
648         _LT -> splitLT fm_l split_key
649         _GT -> mkVBalBranch key elt fm_l (splitLT fm_r split_key)
650         _EQ -> fm_l
651 #else
652   | split_key < key = splitLT fm_l split_key
653   | split_key > key = mkVBalBranch key elt fm_l (splitLT fm_r split_key)
654   | otherwise       = fm_l
655 #endif
656
657 splitGT EmptyFM split_key = emptyFM
658 splitGT (Branch key elt _ fm_l fm_r) split_key
659 #ifdef __GLASGOW_HASKELL__
660   = case _tagCmp split_key key of
661         _GT -> splitGT fm_r split_key
662         _LT -> mkVBalBranch key elt (splitGT fm_l split_key) fm_r
663         _EQ -> fm_r
664 #else
665   | split_key > key = splitGT fm_r split_key
666   | split_key < key = mkVBalBranch key elt (splitGT fm_l split_key) fm_r
667   | otherwise       = fm_r
668 #endif
669
670 findMin :: FiniteMap key elt -> (key,elt)
671 findMin (Branch key elt _ EmptyFM _) = (key,elt)
672 findMin (Branch key elt _ fm_l    _) = findMin fm_l
673
674 deleteMin :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
675 deleteMin (Branch key elt _ EmptyFM fm_r) = fm_r
676 deleteMin (Branch key elt _ fm_l    fm_r) = mkBalBranch key elt (deleteMin fm_l) fm_r
677
678 findMax :: FiniteMap key elt -> (key,elt)
679 findMax (Branch key elt _ _ EmptyFM) = (key,elt)
680 findMax (Branch key elt _ _    fm_r) = findMax fm_r
681
682 deleteMax :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
683 deleteMax (Branch key elt _ fm_l EmptyFM) = fm_l
684 deleteMax (Branch key elt _ fm_l    fm_r) = mkBalBranch key elt fm_l (deleteMax fm_r)
685 \end{code}
686
687 %************************************************************************
688 %*                                                                      *
689 \subsection{Output-ery}
690 %*                                                                      *
691 %************************************************************************
692
693 \begin{code}
694 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
695
696 instance (Outputable key) => Outputable (FiniteMap key elt) where
697     ppr sty fm = pprX sty fm
698
699 pprX sty EmptyFM = ppChar '!'
700 pprX sty (Branch key elt sz fm_l fm_r)
701  = ppBesides [ppLparen, pprX sty fm_l, ppSP,
702               ppr sty key, ppSP, ppInt (IF_GHC(I# sz, sz)), ppSP,
703               pprX sty fm_r, ppRparen]
704 #endif
705
706 #ifndef COMPILING_GHC
707 instance (Eq key, Eq elt) => Eq (FiniteMap key elt) where
708   fm_1 == fm_2 = (sizeFM   fm_1 == sizeFM   fm_2) &&   -- quick test
709                  (fmToList fm_1 == fmToList fm_2)
710
711 {- NO: not clear what The Right Thing to do is:
712 instance (Ord key, Ord elt) => Ord (FiniteMap key elt) where
713   fm_1 <= fm_2 = (sizeFM   fm_1 <= sizeFM   fm_2) &&   -- quick test
714                  (fmToList fm_1 <= fmToList fm_2)
715 -}
716 #endif
717 \end{code}
718
719 %************************************************************************
720 %*                                                                      *
721 \subsection{FiniteSets---a thin veneer}
722 %*                                                                      *
723 %************************************************************************
724
725 \begin{code}
726 #ifdef COMPILING_GHC
727
728 type FiniteSet key = FiniteMap key ()
729 emptySet        :: FiniteSet key
730 mkSet           :: (Ord key OUTPUTABLE_key) => [key] -> FiniteSet key
731 isEmptySet      :: FiniteSet key -> Bool
732 elementOf       :: (Ord key OUTPUTABLE_key) => key -> FiniteSet key -> Bool
733 minusSet        :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
734 setToList       :: FiniteSet key -> [key]
735 union           :: (Ord key OUTPUTABLE_key) => FiniteSet key -> FiniteSet key -> FiniteSet key
736
737 emptySet = emptyFM
738 mkSet xs = listToFM [ (x, ()) | x <- xs]
739 isEmptySet = isEmptyFM
740 elementOf = elemFM
741 minusSet  = minusFM
742 setToList = keysFM
743 union = plusFM
744
745 #endif
746 \end{code}
747
748 %************************************************************************
749 %*                                                                      *
750 \subsection{Efficiency pragmas for GHC}
751 %*                                                                      *
752 %************************************************************************
753
754 When the FiniteMap module is used in GHC, we specialise it for
755 \tr{Uniques}, for dastardly efficiency reasons.
756
757 \begin{code}
758 #if defined(COMPILING_GHC) && __GLASGOW_HASKELL__ && !defined(REALLY_HASKELL_1_3)
759
760 {-# SPECIALIZE addListToFM
761                 :: FiniteMap (FAST_STRING, FAST_STRING) elt -> [((FAST_STRING, FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
762                  , FiniteMap RdrName elt -> [(RdrName,elt)] -> FiniteMap RdrName elt
763     IF_NCG(COMMA   FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
764     #-}
765 {-# SPECIALIZE addListToFM_C
766                 :: (elt -> elt -> elt) -> FiniteMap TyCon elt -> [(TyCon,elt)] -> FiniteMap TyCon elt
767                  , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
768     IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
769     #-}
770 {-# SPECIALIZE addToFM
771                 :: FiniteMap CLabel elt -> CLabel -> elt  -> FiniteMap CLabel elt
772                  , FiniteMap FAST_STRING elt -> FAST_STRING -> elt  -> FiniteMap FAST_STRING elt
773                  , FiniteMap (FAST_STRING, FAST_STRING) elt -> (FAST_STRING, FAST_STRING) -> elt  -> FiniteMap (FAST_STRING, FAST_STRING) elt
774                  , FiniteMap RdrName elt -> RdrName -> elt  -> FiniteMap RdrName elt
775                  , FiniteMap OrigName elt -> OrigName -> elt  -> FiniteMap OrigName elt
776     IF_NCG(COMMA   FiniteMap Reg elt -> Reg -> elt  -> FiniteMap Reg elt)
777     #-}
778 {-# SPECIALIZE addToFM_C
779                 :: (elt -> elt -> elt) -> FiniteMap (RdrName, RdrName) elt -> (RdrName, RdrName) -> elt -> FiniteMap (RdrName, RdrName) elt
780                  , (elt -> elt -> elt) -> FiniteMap (OrigName, OrigName) elt -> (OrigName, OrigName) -> elt -> FiniteMap (OrigName, OrigName) elt
781                  , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt
782     IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
783     #-}
784 {-# SPECIALIZE bagToFM
785                 :: Bag (FAST_STRING,elt) -> FiniteMap FAST_STRING elt
786     #-}
787 {-# SPECIALIZE delListFromFM
788                 :: FiniteMap RdrName elt -> [RdrName]   -> FiniteMap RdrName elt
789                  , FiniteMap OrigName elt -> [OrigName]   -> FiniteMap OrigName elt
790                  , FiniteMap FAST_STRING elt -> [FAST_STRING]   -> FiniteMap FAST_STRING elt
791     IF_NCG(COMMA   FiniteMap Reg elt -> [Reg]   -> FiniteMap Reg elt)
792     #-}
793 {-# SPECIALIZE listToFM
794                 :: [([Char],elt)] -> FiniteMap [Char] elt
795                  , [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
796                  , [((FAST_STRING,FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
797                  , [(OrigName,elt)] -> FiniteMap OrigName elt
798     IF_NCG(COMMA   [(Reg COMMA elt)] -> FiniteMap Reg elt)
799     #-}
800 {-# SPECIALIZE lookupFM
801                 :: FiniteMap CLabel elt -> CLabel -> Maybe elt
802                  , FiniteMap [Char] elt -> [Char] -> Maybe elt
803                  , FiniteMap FAST_STRING elt -> FAST_STRING -> Maybe elt
804                  , FiniteMap (FAST_STRING,FAST_STRING) elt -> (FAST_STRING,FAST_STRING) -> Maybe elt
805                  , FiniteMap OrigName elt -> OrigName -> Maybe elt
806                  , FiniteMap (OrigName,OrigName) elt -> (OrigName,OrigName) -> Maybe elt
807                  , FiniteMap RdrName elt -> RdrName -> Maybe elt
808                  , FiniteMap (RdrName,RdrName) elt -> (RdrName,RdrName) -> Maybe elt
809     IF_NCG(COMMA   FiniteMap Reg elt -> Reg -> Maybe elt)
810     #-}
811 {-# SPECIALIZE lookupWithDefaultFM
812                 :: FiniteMap FAST_STRING elt -> elt -> FAST_STRING -> elt
813     IF_NCG(COMMA   FiniteMap Reg elt -> elt -> Reg -> elt)
814     #-}
815 {-# SPECIALIZE plusFM
816                 :: FiniteMap RdrName elt -> FiniteMap RdrName elt -> FiniteMap RdrName elt
817                  , FiniteMap OrigName elt -> FiniteMap OrigName elt -> FiniteMap OrigName elt
818                  , FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
819     IF_NCG(COMMA   FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
820     #-}
821 {-# SPECIALIZE plusFM_C
822                 :: (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
823     IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
824     #-}
825
826 #endif {- compiling for GHC -}
827 \end{code}