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