1 -----------------------------------------------------------------------------
3 -- Module : Data.FiniteMap
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- A finite map implementation, derived from the paper:
12 -- /Efficient sets: a balancing act/, S. Adams,
13 -- Journal of functional programming 3(4) Oct 1993, pp553-562
15 -----------------------------------------------------------------------------
17 -- ToDo: clean up, remove the COMPILING_GHC stuff.
19 -- The code is SPECIALIZEd to various highly-desirable types (e.g., Id)
20 -- near the end (only \tr{#ifdef COMPILING_GHC}).
23 #include "HsVersions.h"
24 #define IF_NOT_GHC(a) {--}
26 #define ASSERT(e) {--}
27 #define IF_NOT_GHC(a) a
29 #define _tagCmp compare
35 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)/* NB NB NB */
36 #define OUTPUTABLE_key , Outputable key
38 #define OUTPUTABLE_key {--}
41 module Data.FiniteMap (
42 -- * The @FiniteMap@ type
43 FiniteMap, -- abstract type
46 emptyFM, unitFM, listToFM,
48 -- * Lookup operations
49 lookupFM, lookupWithDefaultFM,
58 -- * Deleting elements
59 IF_NOT_GHC(delFromFM COMMA)
66 -- * Extracting information
67 fmToList, keysFM, eltsFM,
73 IF_NOT_GHC(intersectFM COMMA)
74 IF_NOT_GHC(intersectFM_C COMMA)
75 IF_NOT_GHC(mapFM COMMA filterFM COMMA)
84 import Data.Maybe ( isJust )
85 #ifdef __GLASGOW_HASKELL__
94 import Bag ( foldBag )
96 # if ! OMIT_NATIVE_CODEGEN
99 # define IF_NCG(a) {--}
103 -- SIGH: but we use unboxed "sizes"...
104 #if __GLASGOW_HASKELL__
105 #define IF_GHC(a,b) a
107 #define IF_GHC(a,b) b
111 -- ---------------------------------------------------------------------------
112 -- The signature of the module
114 -- | An empty 'FiniteMap'.
115 emptyFM :: FiniteMap key elt
117 -- | A 'FiniteMap' containing a single mapping
118 unitFM :: key -> elt -> FiniteMap key elt
120 -- | Makes a 'FiniteMap' from a list of @(key,value)@ pairs. In the
121 -- case of duplicates, the last is taken
122 listToFM :: (Ord key OUTPUTABLE_key) => [(key,elt)] -> FiniteMap key elt
125 bagToFM :: (Ord key OUTPUTABLE_key) => Bag (key,elt) -> FiniteMap key elt
126 -- In the case of duplicates, who knows which is taken
129 -- ADDING AND DELETING
131 -- | Adds an element to a 'FiniteMap'. Any previous mapping with the same
132 -- key is overwritten.
133 addToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> elt -> FiniteMap key elt
135 -- | Adds a list of elements to a 'FiniteMap', in the order given in
136 -- the list. Overwrites previous mappings.
137 addListToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [(key,elt)] -> FiniteMap key elt
139 -- Combines with previous binding
140 -- In the combining function, the first argument is the "old" element,
141 -- while the second is the "new" one.
143 -- | Adds an element to a 'FiniteMap'. If there is already an element
144 -- with the same key, then the specified combination function is used
145 -- to calculate the new value.
146 addToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
147 -> FiniteMap key elt -> key -> elt
150 -- | A list version of 'addToFM_C'. The elements are added in the
151 -- order given in the list.
152 addListToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
153 -> FiniteMap key elt -> [(key,elt)]
156 -- | Deletes an element from a 'FiniteMap'. If there is no element with
157 -- the specified key, then the original 'FiniteMap' is returned.
158 delFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
160 -- | List version of 'delFromFM'.
161 delListFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [key] -> FiniteMap key elt
163 -- | Combine two 'FiniteMaps'. Mappings in the second argument shadow
164 -- those in the first.
165 plusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
168 -- | Combine two 'FiniteMaps'. The specified combination function is
169 -- used to calculate the new value when there are two elements with
171 plusFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
172 -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
174 -- | @(minusFM a1 a2)@ deletes from @a1@ any mappings which are bound in @a2@
175 minusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
177 -- | @(intersectFM a1 a2)@ returns a new 'FiniteMap' containing
178 -- mappings from @a1@ for which @a2@ also has a mapping with the same
180 intersectFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
182 -- | Returns the interesction of two mappings, using the specified
183 -- combination function to combine values.
184 intersectFM_C :: (Ord key OUTPUTABLE_key) => (elt1 -> elt2 -> elt3)
185 -> FiniteMap key elt1 -> FiniteMap key elt2 -> FiniteMap key elt3
187 -- MAPPING, FOLDING, FILTERING
188 foldFM :: (key -> elt -> a -> a) -> a -> FiniteMap key elt -> a
189 mapFM :: (key -> elt1 -> elt2) -> FiniteMap key elt1 -> FiniteMap key elt2
190 filterFM :: (Ord key OUTPUTABLE_key) => (key -> elt -> Bool)
191 -> FiniteMap key elt -> FiniteMap key elt
194 sizeFM :: FiniteMap key elt -> Int
195 isEmptyFM :: FiniteMap key elt -> Bool
197 -- | Returns 'True' if the specified @key@ has a mapping in this
198 -- 'FiniteMap', or 'False' otherwise.
199 elemFM :: (Ord key OUTPUTABLE_key) => key -> FiniteMap key elt -> Bool
201 -- | Looks up a key in a 'FiniteMap', returning @'Just' v@ if the key
202 -- was found with value @v@, or 'Nothing' otherwise.
203 lookupFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> Maybe elt
205 -- | Looks up a key in a 'FiniteMap', returning @elt@ if the specified
206 -- @key@ was not found.
208 :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> elt -> key -> elt
209 -- lookupWithDefaultFM supplies a "default" elt
210 -- to return for an unmapped key
213 fmToList :: FiniteMap key elt -> [(key,elt)]
214 keysFM :: FiniteMap key elt -> [key]
215 eltsFM :: FiniteMap key elt -> [elt]
217 -- ---------------------------------------------------------------------------
218 -- The @FiniteMap@ data type, and building of same
220 -- Invariants about @FiniteMap@:
222 -- * all keys in a FiniteMap are distinct
224 -- * all keys in left subtree are $<$ key in Branch and
225 -- all keys in right subtree are $>$ key in Branch
227 -- * size field of a Branch gives number of Branch nodes in the tree
229 -- * size of left subtree is differs from size of right subtree by a
230 -- factor of at most \tr{sIZE_RATIO}
232 -- | A mapping from @key@s to @elt@s.
233 data FiniteMap key elt
235 | Branch key elt -- Key and elt stored here
236 IF_GHC(Int#,Int{-STRICT-}) -- Size >= 1
237 (FiniteMap key elt) -- Children
244 = Branch bottom bottom IF_GHC(0#,0) bottom bottom
246 bottom = panic "emptyFM"
249 -- #define EmptyFM (Branch _ _ IF_GHC(0#,0) _ _)
251 unitFM key elt = Branch key elt IF_GHC(1#,1) emptyFM emptyFM
253 listToFM = addListToFM emptyFM
256 bagToFM = foldBag plusFM (\ (k,v) -> unitFM k v) emptyFM
260 -- ---------------------------------------------------------------------------
261 -- Adding to and deleting from @FiniteMaps@
263 addToFM fm key elt = addToFM_C (\ old new -> new) fm key elt
265 addToFM_C combiner EmptyFM key elt = unitFM key elt
266 addToFM_C combiner (Branch key elt size fm_l fm_r) new_key new_elt
267 #ifdef __GLASGOW_HASKELL__
268 = case _tagCmp new_key key of
269 _LT -> mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
270 _GT -> mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
271 _EQ -> Branch new_key (combiner elt new_elt) size fm_l fm_r
273 | new_key < key = mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
274 | new_key > key = mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
275 | otherwise = Branch new_key (combiner elt new_elt) size fm_l fm_r
278 addListToFM fm key_elt_pairs = addListToFM_C (\ old new -> new) fm key_elt_pairs
280 addListToFM_C combiner fm key_elt_pairs
281 = foldl add fm key_elt_pairs -- foldl adds from the left
283 add fmap (key,elt) = addToFM_C combiner fmap key elt
286 delFromFM EmptyFM del_key = emptyFM
287 delFromFM (Branch key elt size fm_l fm_r) del_key
288 #if __GLASGOW_HASKELL__
289 = case _tagCmp del_key key of
290 _GT -> mkBalBranch key elt fm_l (delFromFM fm_r del_key)
291 _LT -> mkBalBranch key elt (delFromFM fm_l del_key) fm_r
292 _EQ -> glueBal fm_l fm_r
295 = mkBalBranch key elt fm_l (delFromFM fm_r del_key)
298 = mkBalBranch key elt (delFromFM fm_l del_key) fm_r
304 delListFromFM fm keys = foldl delFromFM fm keys
306 -- ---------------------------------------------------------------------------
307 -- Combining @FiniteMaps@
309 plusFM_C combiner EmptyFM fm2 = fm2
310 plusFM_C combiner fm1 EmptyFM = fm1
311 plusFM_C combiner fm1 (Branch split_key elt2 _ left right)
312 = mkVBalBranch split_key new_elt
313 (plusFM_C combiner lts left)
314 (plusFM_C combiner gts right)
316 lts = splitLT fm1 split_key
317 gts = splitGT fm1 split_key
318 new_elt = case lookupFM fm1 split_key of
320 Just elt1 -> combiner elt1 elt2
322 -- It's worth doing plusFM specially, because we don't need
323 -- to do the lookup in fm1.
325 plusFM EmptyFM fm2 = fm2
326 plusFM fm1 EmptyFM = fm1
327 plusFM fm1 (Branch split_key elt1 _ left right)
328 = mkVBalBranch split_key elt1 (plusFM lts left) (plusFM gts right)
330 lts = splitLT fm1 split_key
331 gts = splitGT fm1 split_key
333 minusFM EmptyFM fm2 = emptyFM
334 minusFM fm1 EmptyFM = fm1
335 minusFM fm1 (Branch split_key elt _ left right)
336 = glueVBal (minusFM lts left) (minusFM gts right)
337 -- The two can be way different, so we need glueVBal
339 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
340 gts = splitGT fm1 split_key -- are not in either.
342 intersectFM fm1 fm2 = intersectFM_C (\ left right -> right) fm1 fm2
344 intersectFM_C combiner fm1 EmptyFM = emptyFM
345 intersectFM_C combiner EmptyFM fm2 = emptyFM
346 intersectFM_C combiner fm1 (Branch split_key elt2 _ left right)
348 | isJust maybe_elt1 -- split_elt *is* in intersection
349 = mkVBalBranch split_key (combiner elt1 elt2) (intersectFM_C combiner lts left)
350 (intersectFM_C combiner gts right)
352 | otherwise -- split_elt is *not* in intersection
353 = glueVBal (intersectFM_C combiner lts left) (intersectFM_C combiner gts right)
356 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
357 gts = splitGT fm1 split_key -- are not in either.
359 maybe_elt1 = lookupFM fm1 split_key
360 Just elt1 = maybe_elt1
363 -- ---------------------------------------------------------------------------
364 -- Mapping, folding, and filtering with @FiniteMaps@
366 foldFM k z EmptyFM = z
367 foldFM k z (Branch key elt _ fm_l fm_r)
368 = foldFM k (k key elt (foldFM k z fm_r)) fm_l
370 mapFM f EmptyFM = emptyFM
371 mapFM f (Branch key elt size fm_l fm_r)
372 = Branch key (f key elt) size (mapFM f fm_l) (mapFM f fm_r)
374 filterFM p EmptyFM = emptyFM
375 filterFM p (Branch key elt _ fm_l fm_r)
376 | p key elt -- Keep the item
377 = mkVBalBranch key elt (filterFM p fm_l) (filterFM p fm_r)
379 | otherwise -- Drop the item
380 = glueVBal (filterFM p fm_l) (filterFM p fm_r)
383 -- ---------------------------------------------------------------------------
384 -- Interrogating @FiniteMaps@
386 --{-# INLINE sizeFM #-}
388 sizeFM (Branch _ _ size _ _) = IF_GHC(I# size, size)
390 isEmptyFM fm = sizeFM fm == 0
392 lookupFM EmptyFM key = Nothing
393 lookupFM (Branch key elt _ fm_l fm_r) key_to_find
394 #if __GLASGOW_HASKELL__
395 = case _tagCmp key_to_find key of
396 _LT -> lookupFM fm_l key_to_find
397 _GT -> lookupFM fm_r key_to_find
400 | key_to_find < key = lookupFM fm_l key_to_find
401 | key_to_find > key = lookupFM fm_r key_to_find
402 | otherwise = Just elt
406 = case (lookupFM fm key) of { Nothing -> False; Just elt -> True }
408 lookupWithDefaultFM fm deflt key
409 = case (lookupFM fm key) of { Nothing -> deflt; Just elt -> elt }
412 -- ---------------------------------------------------------------------------
413 -- Listifying @FiniteMaps@
415 fmToList fm = foldFM (\ key elt rest -> (key,elt) : rest) [] fm
416 keysFM fm = foldFM (\ key elt rest -> key : rest) [] fm
417 eltsFM fm = foldFM (\ key elt rest -> elt : rest) [] fm
420 -- ---------------------------------------------------------------------------
421 -- The implementation of balancing
423 -- Basic construction of a @FiniteMap@:
425 -- @mkBranch@ simply gets the size component right. This is the ONLY
426 -- (non-trivial) place the Branch object is built, so the ASSERTion
427 -- recursively checks consistency. (The trivial use of Branch is in
433 mkBranch :: (Ord key OUTPUTABLE_key) -- Used for the assertion checking only
436 -> FiniteMap key elt -> FiniteMap key elt
439 mkBranch which key elt fm_l fm_r
440 = --ASSERT( left_ok && right_ok && balance_ok )
441 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
442 if not ( left_ok && right_ok && balance_ok ) then
443 pprPanic ("mkBranch:"++show which) (ppAboves [ppr PprDebug [left_ok, right_ok, balance_ok],
450 result = Branch key elt (unbox (1 + left_size + right_size)) fm_l fm_r
452 -- if sizeFM result <= 8 then
455 -- pprTrace ("mkBranch:"++(show which)) (ppr PprDebug result) (
459 left_ok = case fm_l of
461 Branch left_key _ _ _ _ -> let
462 biggest_left_key = fst (findMax fm_l)
464 biggest_left_key < key
465 right_ok = case fm_r of
467 Branch right_key _ _ _ _ -> let
468 smallest_right_key = fst (findMin fm_r)
470 key < smallest_right_key
471 balance_ok = True -- sigh
474 = -- Both subtrees have one or no elements...
475 (left_size + right_size <= 1)
476 -- NO || left_size == 0 -- ???
477 -- NO || right_size == 0 -- ???
478 -- ... or the number of elements in a subtree does not exceed
479 -- sIZE_RATIO times the number of elements in the other subtree
480 || (left_size * sIZE_RATIO >= right_size &&
481 right_size * sIZE_RATIO >= left_size)
484 left_size = sizeFM fm_l
485 right_size = sizeFM fm_r
487 #if __GLASGOW_HASKELL__
489 unbox (I# size) = size
496 -- ---------------------------------------------------------------------------
497 -- {\em Balanced} construction of a @FiniteMap@
499 -- @mkBalBranch@ rebalances, assuming that the subtrees aren't too far
502 mkBalBranch :: (Ord key OUTPUTABLE_key)
504 -> FiniteMap key elt -> FiniteMap key elt
507 mkBalBranch key elt fm_L fm_R
509 | size_l + size_r < 2
510 = mkBranch 1{-which-} key elt fm_L fm_R
512 | size_r > sIZE_RATIO * size_l -- Right tree too big
514 Branch _ _ _ fm_rl fm_rr
515 | sizeFM fm_rl < 2 * sizeFM fm_rr -> single_L fm_L fm_R
516 | otherwise -> double_L fm_L fm_R
517 -- Other case impossible
519 | size_l > sIZE_RATIO * size_r -- Left tree too big
521 Branch _ _ _ fm_ll fm_lr
522 | sizeFM fm_lr < 2 * sizeFM fm_ll -> single_R fm_L fm_R
523 | otherwise -> double_R fm_L fm_R
524 -- Other case impossible
526 | otherwise -- No imbalance
527 = mkBranch 2{-which-} key elt fm_L fm_R
533 single_L fm_l (Branch key_r elt_r _ fm_rl fm_rr)
534 = mkBranch 3{-which-} key_r elt_r (mkBranch 4{-which-} key elt fm_l fm_rl) fm_rr
536 double_L fm_l (Branch key_r elt_r _ (Branch key_rl elt_rl _ fm_rll fm_rlr) fm_rr)
537 = mkBranch 5{-which-} key_rl elt_rl (mkBranch 6{-which-} key elt fm_l fm_rll)
538 (mkBranch 7{-which-} key_r elt_r fm_rlr fm_rr)
540 single_R (Branch key_l elt_l _ fm_ll fm_lr) fm_r
541 = mkBranch 8{-which-} key_l elt_l fm_ll (mkBranch 9{-which-} key elt fm_lr fm_r)
543 double_R (Branch key_l elt_l _ fm_ll (Branch key_lr elt_lr _ fm_lrl fm_lrr)) fm_r
544 = mkBranch 10{-which-} key_lr elt_lr (mkBranch 11{-which-} key_l elt_l fm_ll fm_lrl)
545 (mkBranch 12{-which-} key elt fm_lrr fm_r)
548 mkVBalBranch :: (Ord key OUTPUTABLE_key)
550 -> FiniteMap key elt -> FiniteMap key elt
553 -- Assert: in any call to (mkVBalBranch_C comb key elt l r),
554 -- (a) all keys in l are < all keys in r
555 -- (b) all keys in l are < key
556 -- (c) all keys in r are > key
558 mkVBalBranch key elt EmptyFM fm_r = addToFM fm_r key elt
559 mkVBalBranch key elt fm_l EmptyFM = addToFM fm_l key elt
561 mkVBalBranch key elt fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
562 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
563 | sIZE_RATIO * size_l < size_r
564 = mkBalBranch key_r elt_r (mkVBalBranch key elt fm_l fm_rl) fm_rr
566 | sIZE_RATIO * size_r < size_l
567 = mkBalBranch key_l elt_l fm_ll (mkVBalBranch key elt fm_lr fm_r)
570 = mkBranch 13{-which-} key elt fm_l fm_r
576 -- ---------------------------------------------------------------------------
577 -- Gluing two trees together
579 -- @glueBal@ assumes its two arguments aren't too far out of whack, just
580 -- like @mkBalBranch@. But: all keys in first arg are $<$ all keys in
583 glueBal :: (Ord key OUTPUTABLE_key)
584 => FiniteMap key elt -> FiniteMap key elt
587 glueBal EmptyFM fm2 = fm2
588 glueBal fm1 EmptyFM = fm1
590 -- The case analysis here (absent in Adams' program) is really to deal
591 -- with the case where fm2 is a singleton. Then deleting the minimum means
592 -- we pass an empty tree to mkBalBranch, which breaks its invariant.
593 | sizeFM fm2 > sizeFM fm1
594 = mkBalBranch mid_key2 mid_elt2 fm1 (deleteMin fm2)
597 = mkBalBranch mid_key1 mid_elt1 (deleteMax fm1) fm2
599 (mid_key1, mid_elt1) = findMax fm1
600 (mid_key2, mid_elt2) = findMin fm2
602 -- @glueVBal@ copes with arguments which can be of any size.
603 -- But: all keys in first arg are $<$ all keys in second.
605 glueVBal :: (Ord key OUTPUTABLE_key)
606 => FiniteMap key elt -> FiniteMap key elt
609 glueVBal EmptyFM fm2 = fm2
610 glueVBal fm1 EmptyFM = fm1
611 glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
612 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
613 | sIZE_RATIO * size_l < size_r
614 = mkBalBranch key_r elt_r (glueVBal fm_l fm_rl) fm_rr
616 | sIZE_RATIO * size_r < size_l
617 = mkBalBranch key_l elt_l fm_ll (glueVBal fm_lr fm_r)
619 | otherwise -- We now need the same two cases as in glueBal above.
626 -- ---------------------------------------------------------------------------
629 splitLT, splitGT :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
631 -- splitLT fm split_key = fm restricted to keys < split_key
632 -- splitGT fm split_key = fm restricted to keys > split_key
634 splitLT EmptyFM split_key = emptyFM
635 splitLT (Branch key elt _ fm_l fm_r) split_key
636 #if __GLASGOW_HASKELL__
637 = case _tagCmp split_key key of
638 _LT -> splitLT fm_l split_key
639 _GT -> mkVBalBranch key elt fm_l (splitLT fm_r split_key)
642 | split_key < key = splitLT fm_l split_key
643 | split_key > key = mkVBalBranch key elt fm_l (splitLT fm_r split_key)
647 splitGT EmptyFM split_key = emptyFM
648 splitGT (Branch key elt _ fm_l fm_r) split_key
649 #if __GLASGOW_HASKELL__
650 = case _tagCmp split_key key of
651 _GT -> splitGT fm_r split_key
652 _LT -> mkVBalBranch key elt (splitGT fm_l split_key) fm_r
655 | split_key > key = splitGT fm_r split_key
656 | split_key < key = mkVBalBranch key elt (splitGT fm_l split_key) fm_r
660 findMin :: FiniteMap key elt -> (key,elt)
661 findMin (Branch key elt _ EmptyFM _) = (key,elt)
662 findMin (Branch key elt _ fm_l _) = findMin fm_l
664 deleteMin :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
665 deleteMin (Branch key elt _ EmptyFM fm_r) = fm_r
666 deleteMin (Branch key elt _ fm_l fm_r) = mkBalBranch key elt (deleteMin fm_l) fm_r
668 findMax :: FiniteMap key elt -> (key,elt)
669 findMax (Branch key elt _ _ EmptyFM) = (key,elt)
670 findMax (Branch key elt _ _ fm_r) = findMax fm_r
672 deleteMax :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
673 deleteMax (Branch key elt _ fm_l EmptyFM) = fm_l
674 deleteMax (Branch key elt _ fm_l fm_r) = mkBalBranch key elt fm_l (deleteMax fm_r)
677 -- ---------------------------------------------------------------------------
680 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
682 instance (Outputable key) => Outputable (FiniteMap key elt) where
683 ppr sty fm = pprX sty fm
685 pprX sty EmptyFM = ppChar '!'
686 pprX sty (Branch key elt sz fm_l fm_r)
687 = ppBesides [ppLparen, pprX sty fm_l, ppSP,
688 ppr sty key, ppSP, ppInt (IF_GHC(I# sz, sz)), ppSP,
689 pprX sty fm_r, ppRparen]
692 #ifndef COMPILING_GHC
693 instance (Eq key, Eq elt) => Eq (FiniteMap key elt) where
694 fm_1 == fm_2 = (sizeFM fm_1 == sizeFM fm_2) && -- quick test
695 (fmToList fm_1 == fmToList fm_2)
697 {- NO: not clear what The Right Thing to do is:
698 instance (Ord key, Ord elt) => Ord (FiniteMap key elt) where
699 fm_1 <= fm_2 = (sizeFM fm_1 <= sizeFM fm_2) && -- quick test
700 (fmToList fm_1 <= fmToList fm_2)
704 -- ---------------------------------------------------------------------------
705 -- Efficiency pragmas for GHC
707 -- When the FiniteMap module is used in GHC, we specialise it for
708 -- \tr{Uniques}, for dastardly efficiency reasons.
710 #if defined(COMPILING_GHC) && __GLASGOW_HASKELL__ && !defined(REALLY_HASKELL_1_3)
712 {-# SPECIALIZE addListToFM
713 :: FiniteMap (FAST_STRING, 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 COMMA elt)] -> FiniteMap Reg elt)
717 {-# SPECIALIZE addListToFM_C
718 :: (elt -> elt -> elt) -> FiniteMap TyCon elt -> [(TyCon,elt)] -> FiniteMap TyCon elt
719 , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
720 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
722 {-# SPECIALIZE addToFM
723 :: FiniteMap CLabel elt -> CLabel -> elt -> FiniteMap CLabel elt
724 , FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt
725 , FiniteMap (FAST_STRING, FAST_STRING) elt -> (FAST_STRING, FAST_STRING) -> elt -> FiniteMap (FAST_STRING, FAST_STRING) elt
726 , FiniteMap RdrName elt -> RdrName -> elt -> FiniteMap RdrName elt
727 , FiniteMap OrigName elt -> OrigName -> elt -> FiniteMap OrigName elt
728 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
730 {-# SPECIALIZE addToFM_C
731 :: (elt -> elt -> elt) -> FiniteMap (RdrName, RdrName) elt -> (RdrName, RdrName) -> elt -> FiniteMap (RdrName, RdrName) elt
732 , (elt -> elt -> elt) -> FiniteMap (OrigName, OrigName) elt -> (OrigName, OrigName) -> elt -> FiniteMap (OrigName, OrigName) elt
733 , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt
734 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
736 {-# SPECIALIZE bagToFM
737 :: Bag (FAST_STRING,elt) -> FiniteMap FAST_STRING elt
739 {-# SPECIALIZE delListFromFM
740 :: FiniteMap RdrName elt -> [RdrName] -> FiniteMap RdrName elt
741 , FiniteMap OrigName elt -> [OrigName] -> FiniteMap OrigName elt
742 , FiniteMap FAST_STRING elt -> [FAST_STRING] -> FiniteMap FAST_STRING elt
743 IF_NCG(COMMA FiniteMap Reg elt -> [Reg] -> FiniteMap Reg elt)
745 {-# SPECIALIZE listToFM
746 :: [([Char],elt)] -> FiniteMap [Char] elt
747 , [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
748 , [((FAST_STRING,FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
749 , [(OrigName,elt)] -> FiniteMap OrigName elt
750 IF_NCG(COMMA [(Reg COMMA elt)] -> FiniteMap Reg elt)
752 {-# SPECIALIZE lookupFM
753 :: FiniteMap CLabel elt -> CLabel -> Maybe elt
754 , FiniteMap [Char] elt -> [Char] -> Maybe elt
755 , FiniteMap FAST_STRING elt -> FAST_STRING -> Maybe elt
756 , FiniteMap (FAST_STRING,FAST_STRING) elt -> (FAST_STRING,FAST_STRING) -> Maybe elt
757 , FiniteMap OrigName elt -> OrigName -> Maybe elt
758 , FiniteMap (OrigName,OrigName) elt -> (OrigName,OrigName) -> Maybe elt
759 , FiniteMap RdrName elt -> RdrName -> Maybe elt
760 , FiniteMap (RdrName,RdrName) elt -> (RdrName,RdrName) -> Maybe elt
761 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> Maybe elt)
763 {-# SPECIALIZE lookupWithDefaultFM
764 :: FiniteMap FAST_STRING elt -> elt -> FAST_STRING -> elt
765 IF_NCG(COMMA FiniteMap Reg elt -> elt -> Reg -> elt)
767 {-# SPECIALIZE plusFM
768 :: FiniteMap RdrName elt -> FiniteMap RdrName elt -> FiniteMap RdrName elt
769 , FiniteMap OrigName elt -> FiniteMap OrigName elt -> FiniteMap OrigName elt
770 , FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
771 IF_NCG(COMMA FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
773 {-# SPECIALIZE plusFM_C
774 :: (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
775 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
778 #endif {- compiling for GHC -}