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