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