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