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