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