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