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