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