lots of portability changes (#1405)
[ghc-hetmet.git] / compiler / utils / FiniteMap.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1994-1998
4 %
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.
19
20 \begin{code}
21 {-# OPTIONS -w #-}
22 -- The above warning supression flag is a temporary kludge.
23 -- While working on this module you are encouraged to remove it and fix
24 -- any warnings in the module. See
25 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
26 -- for details
27
28 module FiniteMap (
29         FiniteMap,              -- abstract type
30
31         emptyFM, unitFM, listToFM,
32
33         addToFM,
34         addToFM_C,
35         addListToFM,
36         addListToFM_C,
37         delFromFM,
38         delListFromFM,
39
40         plusFM,
41         plusFM_C,
42         minusFM,
43         foldFM,
44
45         intersectFM,
46         intersectFM_C,
47         mapFM, filterFM, 
48
49         sizeFM, isEmptyFM, elemFM, lookupFM, lookupWithDefaultFM,
50
51         fmToList, keysFM, eltsFM
52
53         , bagToFM
54
55     ) where
56
57 #include "HsVersions.h"
58
59 #if defined(DEBUG_FINITEMAPS)/* NB NB NB */
60 #define OUTPUTABLE_key , Outputable key
61 #else
62 #define OUTPUTABLE_key {--}
63 #endif
64
65 import Maybes
66 import Bag        ( Bag, foldrBag )
67 import Util
68 import Outputable
69
70 #if 0
71 import GHC.Exts
72 -- was this import only needed for I#, or does it have something
73 -- to do with the (not-presently-used) IF_NCG also?
74 #endif
75
76 import Data.List
77
78 #if 0
79 #if ! OMIT_NATIVE_CODEGEN
80 #  define IF_NCG(a) a
81 #else
82 #  define IF_NCG(a) {--}
83 #endif
84 #endif
85 \end{code}
86
87
88 %************************************************************************
89 %*                                                                      *
90 \subsection{The signature of the module}
91 %*                                                                      *
92 %************************************************************************
93
94 \begin{code}
95 --      BUILDING
96 emptyFM         :: FiniteMap key elt
97 unitFM          :: key -> elt -> FiniteMap key elt
98 listToFM        :: (Ord key OUTPUTABLE_key) => [(key,elt)] -> FiniteMap key elt
99                         -- In the case of duplicates, the last is taken
100 bagToFM         :: (Ord key OUTPUTABLE_key) => Bag (key,elt) -> FiniteMap key elt
101                         -- In the case of duplicates, who knows which is taken
102
103 --      ADDING AND DELETING
104                    -- Throws away any previous binding
105                    -- In the list case, the items are added starting with the
106                    -- first one in the list
107 addToFM         :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> elt  -> FiniteMap key elt
108 addListToFM     :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [(key,elt)] -> FiniteMap key elt
109
110                    -- Combines with previous binding
111                    -- The combining fn goes (old -> new -> new)
112 addToFM_C       :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
113                            -> FiniteMap key elt -> key -> elt
114                            -> FiniteMap key elt
115 addListToFM_C   :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
116                            -> FiniteMap key elt -> [(key,elt)]
117                            -> FiniteMap key elt
118
119                    -- Deletion doesn't complain if you try to delete something
120                    -- which isn't there
121 delFromFM       :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key   -> FiniteMap key elt
122 delListFromFM   :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [key] -> FiniteMap key elt
123
124 --      COMBINING
125                    -- Bindings in right argument shadow those in the left
126 plusFM          :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
127                            -> FiniteMap key elt
128
129                    -- Combines bindings for the same thing with the given function
130 plusFM_C        :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
131                            -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
132
133 minusFM         :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
134                    -- (minusFM a1 a2) deletes from a1 any bindings which are bound in a2
135
136 intersectFM     :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
137 intersectFM_C   :: (Ord key OUTPUTABLE_key) => (elt1 -> elt2 -> elt3)
138                            -> FiniteMap key elt1 -> FiniteMap key elt2 -> FiniteMap key elt3
139
140 --      MAPPING, FOLDING, FILTERING
141 foldFM          :: (key -> elt -> a -> a) -> a -> FiniteMap key elt -> a
142 mapFM           :: (key -> elt1 -> elt2) -> FiniteMap key elt1 -> FiniteMap key elt2
143 filterFM        :: (Ord key OUTPUTABLE_key) => (key -> elt -> Bool)
144                            -> FiniteMap key elt -> FiniteMap key elt
145
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     {-# UNPACK #-} !Int   -- 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 0 bottom bottom
198   where
199     bottom = panic "emptyFM"
200 -}
201
202 --  #define EmptyFM (Branch _ _ 0 _ _)
203
204 unitFM key elt = Branch key elt 1 emptyFM emptyFM
205
206 listToFM = addListToFM emptyFM
207
208 bagToFM = foldrBag (\(k,v) fm -> addToFM fm k v) emptyFM
209 \end{code}
210
211 %************************************************************************
212 %*                                                                      *
213 \subsection{Adding to and deleting from @FiniteMaps@}
214 %*                                                                      *
215 %************************************************************************
216
217 \begin{code}
218 addToFM fm key elt = addToFM_C (\ old new -> new) fm key elt
219
220 addToFM_C combiner EmptyFM key elt = unitFM key elt
221 addToFM_C combiner (Branch key elt size fm_l fm_r) new_key new_elt
222   = case compare new_key key of
223         LT -> mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
224         GT -> mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
225         EQ -> Branch new_key (combiner elt new_elt) size fm_l fm_r
226
227 addListToFM fm key_elt_pairs = addListToFM_C (\ old new -> new) fm key_elt_pairs
228
229 addListToFM_C combiner fm key_elt_pairs
230   = foldl' add fm key_elt_pairs -- foldl adds from the left
231   where
232     add fmap (key,elt) = addToFM_C combiner fmap key elt
233 \end{code}
234
235 \begin{code}
236 delFromFM EmptyFM del_key = emptyFM
237 delFromFM (Branch key elt size fm_l fm_r) del_key
238   = case compare del_key key of
239         GT -> mkBalBranch key elt fm_l (delFromFM fm_r del_key)
240         LT -> mkBalBranch key elt (delFromFM fm_l del_key) fm_r
241         EQ -> glueBal fm_l fm_r
242
243 delListFromFM fm keys = foldl' delFromFM fm keys
244 \end{code}
245
246 %************************************************************************
247 %*                                                                      *
248 \subsection{Combining @FiniteMaps@}
249 %*                                                                      *
250 %************************************************************************
251
252 \begin{code}
253 plusFM_C combiner EmptyFM fm2 = fm2
254 plusFM_C combiner fm1 EmptyFM = fm1
255 plusFM_C combiner fm1 (Branch split_key elt2 _ left right)
256   = mkVBalBranch split_key new_elt
257                  (plusFM_C combiner lts left)
258                  (plusFM_C combiner gts right)
259   where
260     lts     = splitLT fm1 split_key
261     gts     = splitGT fm1 split_key
262     new_elt = case lookupFM fm1 split_key of
263                 Nothing   -> elt2
264                 Just elt1 -> combiner elt1 elt2
265
266 -- It's worth doing plusFM specially, because we don't need
267 -- to do the lookup in fm1.
268 -- FM2 over-rides FM1.
269
270 plusFM EmptyFM fm2 = fm2
271 plusFM fm1 EmptyFM = fm1
272 plusFM fm1 (Branch split_key elt1 _ left right)
273   = mkVBalBranch split_key elt1 (plusFM lts left) (plusFM gts right)
274   where
275     lts     = splitLT fm1 split_key
276     gts     = splitGT fm1 split_key
277
278 minusFM EmptyFM fm2 = emptyFM
279 minusFM fm1 EmptyFM = fm1
280 minusFM fm1 (Branch split_key elt _ left right)
281   = glueVBal (minusFM lts left) (minusFM gts right)
282         -- The two can be way different, so we need glueVBal
283   where
284     lts = splitLT fm1 split_key         -- NB gt and lt, so the equal ones
285     gts = splitGT fm1 split_key         -- are not in either.
286
287 intersectFM fm1 fm2 = intersectFM_C (\ left right -> right) fm1 fm2
288
289 intersectFM_C combiner fm1 EmptyFM = emptyFM
290 intersectFM_C combiner EmptyFM fm2 = emptyFM
291 intersectFM_C combiner fm1 (Branch split_key elt2 _ left right)
292
293   | maybeToBool maybe_elt1      -- split_elt *is* in intersection
294   = mkVBalBranch split_key (combiner elt1 elt2) (intersectFM_C combiner lts left)
295                                                 (intersectFM_C combiner gts right)
296
297   | otherwise                   -- split_elt is *not* in intersection
298   = glueVBal (intersectFM_C combiner lts left) (intersectFM_C combiner gts right)
299
300   where
301     lts = splitLT fm1 split_key         -- NB gt and lt, so the equal ones
302     gts = splitGT fm1 split_key         -- are not in either.
303
304     maybe_elt1 = lookupFM fm1 split_key
305     Just elt1  = maybe_elt1
306 \end{code}
307
308 %************************************************************************
309 %*                                                                      *
310 \subsection{Mapping, folding, and filtering with @FiniteMaps@}
311 %*                                                                      *
312 %************************************************************************
313
314 \begin{code}
315 foldFM k z EmptyFM = z
316 foldFM k z (Branch key elt _ fm_l fm_r)
317   = foldFM k (k key elt (foldFM k z fm_r)) fm_l
318
319 mapFM f EmptyFM = emptyFM
320 mapFM f (Branch key elt size fm_l fm_r)
321   = Branch key (f key elt) size (mapFM f fm_l) (mapFM f fm_r)
322
323 filterFM p EmptyFM = emptyFM
324 filterFM p (Branch key elt _ fm_l fm_r)
325   | p key elt           -- Keep the item
326   = mkVBalBranch key elt (filterFM p fm_l) (filterFM p fm_r)
327
328   | otherwise           -- Drop the item
329   = glueVBal (filterFM p fm_l) (filterFM p fm_r)
330 \end{code}
331
332 %************************************************************************
333 %*                                                                      *
334 \subsection{Interrogating @FiniteMaps@}
335 %*                                                                      *
336 %************************************************************************
337
338 \begin{code}
339 --{-# INLINE sizeFM #-}
340 sizeFM EmptyFM               = 0
341 sizeFM (Branch _ _ size _ _) = size
342
343 isEmptyFM fm = sizeFM fm == 0
344
345 lookupFM EmptyFM key = Nothing
346 lookupFM (Branch key elt _ fm_l fm_r) key_to_find
347   = case compare key_to_find key of
348         LT -> lookupFM fm_l key_to_find
349         GT -> lookupFM fm_r key_to_find
350         EQ -> Just elt
351
352 key `elemFM` fm
353   = case (lookupFM fm key) of { Nothing -> False; Just elt -> True }
354
355 lookupWithDefaultFM fm deflt key
356   = case (lookupFM fm key) of { Nothing -> deflt; Just elt -> elt }
357 \end{code}
358
359 %************************************************************************
360 %*                                                                      *
361 \subsection{Listifying @FiniteMaps@}
362 %*                                                                      *
363 %************************************************************************
364
365 \begin{code}
366 fmToList fm = foldFM (\ key elt rest -> (key,elt) : rest) [] fm
367 keysFM fm   = foldFM (\ key elt rest -> key : rest)       [] fm
368 eltsFM fm   = foldFM (\ key elt rest -> elt : rest)       [] fm
369 \end{code}
370
371
372 %************************************************************************
373 %*                                                                      *
374 \subsection{The implementation of balancing}
375 %*                                                                      *
376 %************************************************************************
377
378 %************************************************************************
379 %*                                                                      *
380 \subsubsection{Basic construction of a @FiniteMap@}
381 %*                                                                      *
382 %************************************************************************
383
384 @mkBranch@ simply gets the size component right.  This is the ONLY
385 (non-trivial) place the Branch object is built, so the ASSERTion
386 recursively checks consistency.  (The trivial use of Branch is in
387 @unitFM@.)
388
389 \begin{code}
390 sIZE_RATIO :: Int
391 sIZE_RATIO = 5
392
393 mkBranch :: (Ord key OUTPUTABLE_key)            -- Used for the assertion checking only
394          => Int
395          -> key -> elt
396          -> FiniteMap key elt -> FiniteMap key elt
397          -> FiniteMap key elt
398
399 mkBranch which key elt fm_l fm_r
400   = --ASSERT( left_ok && right_ok && balance_ok )
401 #if defined(DEBUG_FINITEMAPS)
402     if not ( left_ok && right_ok && balance_ok ) then
403         pprPanic ("mkBranch:"++show which) (vcat [ppr [left_ok, right_ok, balance_ok],
404                                        ppr key,
405                                        ppr fm_l,
406                                        ppr fm_r])
407     else
408 #endif
409     let
410         result = Branch key elt (1 + left_size + right_size) fm_l fm_r
411     in
412 --    if sizeFM result <= 8 then
413         result
414 --    else
415 --      pprTrace ("mkBranch:"++(show which)) (ppr result) (
416 --      result
417 --      )
418   where
419     left_ok  = case fm_l of
420                 EmptyFM                  -> True
421                 Branch left_key _ _ _ _  -> let
422                                                 biggest_left_key = fst (findMax fm_l)
423                                             in
424                                             biggest_left_key < key
425     right_ok = case fm_r of
426                 EmptyFM                  -> True
427                 Branch right_key _ _ _ _ -> let
428                                                 smallest_right_key = fst (findMin fm_r)
429                                             in
430                                             key < smallest_right_key
431     balance_ok = True -- sigh
432 {- LATER:
433     balance_ok
434       = -- Both subtrees have one or no elements...
435         (left_size + right_size <= 1)
436 -- NO         || left_size == 0  -- ???
437 -- NO         || right_size == 0 -- ???
438         -- ... or the number of elements in a subtree does not exceed
439         -- sIZE_RATIO times the number of elements in the other subtree
440       || (left_size  * sIZE_RATIO >= right_size &&
441           right_size * sIZE_RATIO >= left_size)
442 -}
443
444     left_size  = sizeFM fm_l
445     right_size = sizeFM fm_r
446 \end{code}
447
448 %************************************************************************
449 %*                                                                      *
450 \subsubsection{{\em Balanced} construction of a @FiniteMap@}
451 %*                                                                      *
452 %************************************************************************
453
454 @mkBalBranch@ rebalances, assuming that the subtrees aren't too far
455 out of whack.
456
457 \begin{code}
458 mkBalBranch :: (Ord key OUTPUTABLE_key)
459             => key -> elt
460             -> FiniteMap key elt -> FiniteMap key elt
461             -> FiniteMap key elt
462
463 mkBalBranch key elt fm_L fm_R
464
465   | size_l + size_r < 2
466   = mkBranch 1{-which-} key elt fm_L fm_R
467
468   | size_r > sIZE_RATIO * size_l        -- Right tree too big
469   = case fm_R of
470         Branch _ _ _ fm_rl fm_rr
471                 | sizeFM fm_rl < 2 * sizeFM fm_rr -> single_L fm_L fm_R
472                 | otherwise                       -> double_L fm_L fm_R
473         -- Other case impossible
474
475   | size_l > sIZE_RATIO * size_r        -- Left tree too big
476   = case fm_L of
477         Branch _ _ _ fm_ll fm_lr
478                 | sizeFM fm_lr < 2 * sizeFM fm_ll -> single_R fm_L fm_R
479                 | otherwise                       -> double_R fm_L fm_R
480         -- Other case impossible
481
482   | otherwise                           -- No imbalance
483   = mkBranch 2{-which-} key elt fm_L fm_R
484
485   where
486     size_l   = sizeFM fm_L
487     size_r   = sizeFM fm_R
488
489     single_L fm_l (Branch key_r elt_r _ fm_rl fm_rr)
490         = mkBranch 3{-which-} key_r elt_r (mkBranch 4{-which-} key elt fm_l fm_rl) fm_rr
491
492     double_L fm_l (Branch key_r elt_r _ (Branch key_rl elt_rl _ fm_rll fm_rlr) fm_rr)
493         = mkBranch 5{-which-} key_rl elt_rl (mkBranch 6{-which-} key   elt   fm_l   fm_rll)
494                                  (mkBranch 7{-which-} key_r elt_r fm_rlr fm_rr)
495
496     single_R (Branch key_l elt_l _ fm_ll fm_lr) fm_r
497         = mkBranch 8{-which-} key_l elt_l fm_ll (mkBranch 9{-which-} key elt fm_lr fm_r)
498
499     double_R (Branch key_l elt_l _ fm_ll (Branch key_lr elt_lr _ fm_lrl fm_lrr)) fm_r
500         = mkBranch 10{-which-} key_lr elt_lr (mkBranch 11{-which-} key_l elt_l fm_ll  fm_lrl)
501                                  (mkBranch 12{-which-} key   elt   fm_lrr fm_r)
502 \end{code}
503
504
505 \begin{code}
506 mkVBalBranch :: (Ord key OUTPUTABLE_key)
507              => key -> elt
508              -> FiniteMap key elt -> FiniteMap key elt
509              -> FiniteMap key elt
510
511 -- Assert: in any call to (mkVBalBranch_C comb key elt l r),
512 --         (a) all keys in l are < all keys in r
513 --         (b) all keys in l are < key
514 --         (c) all keys in r are > key
515
516 mkVBalBranch key elt EmptyFM fm_r = addToFM fm_r key elt
517 mkVBalBranch key elt fm_l EmptyFM = addToFM fm_l key elt
518
519 mkVBalBranch key elt fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
520                      fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
521   | sIZE_RATIO * size_l < size_r
522   = mkBalBranch key_r elt_r (mkVBalBranch key elt fm_l fm_rl) fm_rr
523
524   | sIZE_RATIO * size_r < size_l
525   = mkBalBranch key_l elt_l fm_ll (mkVBalBranch key elt fm_lr fm_r)
526
527   | otherwise
528   = mkBranch 13{-which-} key elt fm_l fm_r
529
530   where
531     size_l = sizeFM fm_l
532     size_r = sizeFM fm_r
533 \end{code}
534
535 %************************************************************************
536 %*                                                                      *
537 \subsubsection{Gluing two trees together}
538 %*                                                                      *
539 %************************************************************************
540
541 @glueBal@ assumes its two arguments aren't too far out of whack, just
542 like @mkBalBranch@.  But: all keys in first arg are $<$ all keys in
543 second.
544
545 \begin{code}
546 glueBal :: (Ord key OUTPUTABLE_key)
547         => FiniteMap key elt -> FiniteMap key elt
548         -> FiniteMap key elt
549
550 glueBal EmptyFM fm2 = fm2
551 glueBal fm1 EmptyFM = fm1
552 glueBal fm1 fm2
553         -- The case analysis here (absent in Adams' program) is really to deal
554         -- with the case where fm2 is a singleton. Then deleting the minimum means
555         -- we pass an empty tree to mkBalBranch, which breaks its invariant.
556   | sizeFM fm2 > sizeFM fm1
557   = mkBalBranch mid_key2 mid_elt2 fm1 (deleteMin fm2)
558
559   | otherwise
560   = mkBalBranch mid_key1 mid_elt1 (deleteMax fm1) fm2
561   where
562     (mid_key1, mid_elt1) = findMax fm1
563     (mid_key2, mid_elt2) = findMin fm2
564 \end{code}
565
566 @glueVBal@ copes with arguments which can be of any size.
567 But: all keys in first arg are $<$ all keys in second.
568
569 \begin{code}
570 glueVBal :: (Ord key OUTPUTABLE_key)
571          => FiniteMap key elt -> FiniteMap key elt
572          -> FiniteMap key elt
573
574 glueVBal EmptyFM fm2 = fm2
575 glueVBal fm1 EmptyFM = fm1
576 glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
577          fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
578   | sIZE_RATIO * size_l < size_r
579   = mkBalBranch key_r elt_r (glueVBal fm_l fm_rl) fm_rr
580
581   | sIZE_RATIO * size_r < size_l
582   = mkBalBranch key_l elt_l fm_ll (glueVBal fm_lr fm_r)
583
584   | otherwise           -- We now need the same two cases as in glueBal above.
585   = glueBal fm_l fm_r
586   where
587     size_l = sizeFM fm_l
588     size_r = sizeFM fm_r
589 \end{code}
590
591 %************************************************************************
592 %*                                                                      *
593 \subsection{Local utilities}
594 %*                                                                      *
595 %************************************************************************
596
597 \begin{code}
598 splitLT, splitGT :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
599
600 -- splitLT fm split_key  =  fm restricted to keys <  split_key
601 -- splitGT fm split_key  =  fm restricted to keys >  split_key
602
603 splitLT EmptyFM split_key = emptyFM
604 splitLT (Branch key elt _ fm_l fm_r) split_key
605   = case compare split_key key of
606         LT -> splitLT fm_l split_key
607         GT -> mkVBalBranch key elt fm_l (splitLT fm_r split_key)
608         EQ -> fm_l
609
610 splitGT EmptyFM split_key = emptyFM
611 splitGT (Branch key elt _ fm_l fm_r) split_key
612   = case compare split_key key of
613         GT -> splitGT fm_r split_key
614         LT -> mkVBalBranch key elt (splitGT fm_l split_key) fm_r
615         EQ -> fm_r
616
617 findMin :: FiniteMap key elt -> (key,elt)
618 findMin (Branch key elt _ EmptyFM _) = (key,elt)
619 findMin (Branch key elt _ fm_l    _) = findMin fm_l
620
621 deleteMin :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
622 deleteMin (Branch key elt _ EmptyFM fm_r) = fm_r
623 deleteMin (Branch key elt _ fm_l    fm_r) = mkBalBranch key elt (deleteMin fm_l) fm_r
624
625 findMax :: FiniteMap key elt -> (key,elt)
626 findMax (Branch key elt _ _ EmptyFM) = (key,elt)
627 findMax (Branch key elt _ _    fm_r) = findMax fm_r
628
629 deleteMax :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
630 deleteMax (Branch key elt _ fm_l EmptyFM) = fm_l
631 deleteMax (Branch key elt _ fm_l    fm_r) = mkBalBranch key elt fm_l (deleteMax fm_r)
632 \end{code}
633
634 %************************************************************************
635 %*                                                                      *
636 \subsection{Output-ery}
637 %*                                                                      *
638 %************************************************************************
639
640 \begin{code}
641 #if defined(DEBUG_FINITEMAPS)
642
643 instance (Outputable key) => Outputable (FiniteMap key elt) where
644     ppr fm = pprX fm
645
646 pprX EmptyFM = char '!'
647 pprX (Branch key elt sz fm_l fm_r)
648  = parens (hcat [pprX fm_l, space,
649                       ppr key, space, int sz, space,
650                       pprX fm_r])
651 #else
652 -- and when not debugging the package itself...
653 instance (Outputable key, Outputable elt) => Outputable (FiniteMap key elt) where
654     ppr fm = ppr (fmToList fm)
655 #endif
656
657 #if 0
658 instance (Eq key, Eq elt) => Eq (FiniteMap key elt) where
659   fm_1 == fm_2 = (sizeFM   fm_1 == sizeFM   fm_2) &&   -- quick test
660                  (fmToList fm_1 == fmToList fm_2)
661
662 {- NO: not clear what The Right Thing to do is:
663 instance (Ord key, Ord elt) => Ord (FiniteMap key elt) where
664   fm_1 <= fm_2 = (sizeFM   fm_1 <= sizeFM   fm_2) &&   -- quick test
665                  (fmToList fm_1 <= fmToList fm_2)
666 -}
667 #endif
668 \end{code}
669
670 %************************************************************************
671 %*                                                                      *
672 \subsection{Efficiency pragmas for GHC}
673 %*                                                                      *
674 %************************************************************************
675
676 When the FiniteMap module is used in GHC, we specialise it for
677 \tr{Uniques}, for dastardly efficiency reasons.
678
679 \begin{code}
680 #if 0
681
682 #ifdef __GLASGOW_HASKELL__
683
684 {-# SPECIALIZE addListToFM
685                 :: FiniteMap (FastString, FAST_STRING) elt -> [((FAST_STRING, FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
686                  , FiniteMap RdrName elt -> [(RdrName,elt)] -> FiniteMap RdrName elt
687     IF_NCG(COMMA   FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
688     #-}
689 {-# SPECIALIZE addListToFM_C
690                 :: (elt -> elt -> elt) -> FiniteMap TyCon elt -> [(TyCon,elt)] -> FiniteMap TyCon elt
691                  , (elt -> elt -> elt) -> FiniteMap FastString elt -> [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
692     IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
693     #-}
694 {-# SPECIALIZE addToFM
695                 :: FiniteMap CLabel elt -> CLabel -> elt  -> FiniteMap CLabel elt
696                  , FiniteMap FastString elt -> FAST_STRING -> elt  -> FiniteMap FAST_STRING elt
697                  , FiniteMap (FastString, FAST_STRING) elt -> (FAST_STRING, FAST_STRING) -> elt  -> FiniteMap (FAST_STRING, FAST_STRING) elt
698                  , FiniteMap RdrName elt -> RdrName -> elt  -> FiniteMap RdrName elt
699     IF_NCG(COMMA   FiniteMap Reg elt -> Reg -> elt  -> FiniteMap Reg elt)
700     #-}
701 {-# SPECIALIZE addToFM_C
702                 :: (elt -> elt -> elt) -> FiniteMap (RdrName, RdrName) elt -> (RdrName, RdrName) -> elt -> FiniteMap (RdrName, RdrName) elt
703                  , (elt -> elt -> elt) -> FiniteMap FastString elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt
704     IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
705     #-}
706 {-# SPECIALIZE bagToFM
707                 :: Bag (FastString,elt) -> FiniteMap FAST_STRING elt
708     #-}
709 {-# SPECIALIZE delListFromFM
710                 :: FiniteMap RdrName elt -> [RdrName]   -> FiniteMap RdrName elt
711                  , FiniteMap FastString elt -> [FAST_STRING]   -> FiniteMap FAST_STRING elt
712     IF_NCG(COMMA   FiniteMap Reg elt -> [Reg]   -> FiniteMap Reg elt)
713     #-}
714 {-# SPECIALIZE listToFM
715                 :: [([Char],elt)] -> FiniteMap [Char] elt
716                  , [(FastString,elt)] -> FiniteMap FAST_STRING elt
717                  , [((FastString,FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
718     IF_NCG(COMMA   [(Reg COMMA elt)] -> FiniteMap Reg elt)
719     #-}
720 {-# SPECIALIZE lookupFM
721                 :: FiniteMap CLabel elt -> CLabel -> Maybe elt
722                  , FiniteMap [Char] elt -> [Char] -> Maybe elt
723                  , FiniteMap FastString elt -> FAST_STRING -> Maybe elt
724                  , FiniteMap (FastString,FAST_STRING) elt -> (FAST_STRING,FAST_STRING) -> Maybe elt
725                  , FiniteMap RdrName elt -> RdrName -> Maybe elt
726                  , FiniteMap (RdrName,RdrName) elt -> (RdrName,RdrName) -> Maybe elt
727     IF_NCG(COMMA   FiniteMap Reg elt -> Reg -> Maybe elt)
728     #-}
729 {-# SPECIALIZE lookupWithDefaultFM
730                 :: FiniteMap FastString elt -> elt -> FAST_STRING -> elt
731     IF_NCG(COMMA   FiniteMap Reg elt -> elt -> Reg -> elt)
732     #-}
733 {-# SPECIALIZE plusFM
734                 :: FiniteMap RdrName elt -> FiniteMap RdrName elt -> FiniteMap RdrName elt
735                  , FiniteMap FastString elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
736     IF_NCG(COMMA   FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
737     #-}
738 {-# SPECIALIZE plusFM_C
739                 :: (elt -> elt -> elt) -> FiniteMap FastString elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
740     IF_NCG(COMMA   (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
741     #-}
742
743 #endif /* compiling with ghc and have specialiser */
744
745 #endif /* 0 */
746 \end{code}