f1db267b57a6d6b1717ffe1316675f25cb740562
[packages/containers.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 -- /Efficient sets: a balancing act/, S. Adams,
13 -- Journal of functional programming 3(4) Oct 1993, pp553-562
14 --
15 -----------------------------------------------------------------------------
16
17 -- ToDo: clean up, remove the COMPILING_GHC stuff.
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 -- * The @FiniteMap@ type
43 FiniteMap, -- abstract type
44
45 -- * Construction
46 emptyFM, unitFM, listToFM,
47
48 -- * Lookup operations
49 lookupFM, lookupWithDefaultFM,
50 elemFM,
51
52 -- * Adding elements
53 addToFM,
54 addToFM_C,
55 addListToFM,
56 addListToFM_C,
57
58 -- * Deleting elements
59 IF_NOT_GHC(delFromFM COMMA)
60 delListFromFM,
61
62 -- * Combination
63 plusFM,
64 plusFM_C,
65
66 -- * Extracting information
67 fmToList, keysFM, eltsFM,
68 sizeFM, isEmptyFM,
69
70 -- * Other operations
71 minusFM,
72 foldFM,
73 IF_NOT_GHC(intersectFM COMMA)
74 IF_NOT_GHC(intersectFM_C COMMA)
75 IF_NOT_GHC(mapFM COMMA filterFM COMMA)
76
77 foldFM_GE, fmToList_GE, keysFM_GE, eltsFM_GE,
78 foldFM_LE, fmToList_LE, keysFM_LE, eltsFM_LE,
79
80 minFM, maxFM,
81
82 #ifdef COMPILING_GHC
83 , bagToFM
84 #endif
85 ) where
86
87 import Data.Maybe ( isJust )
88 #ifdef __GLASGOW_HASKELL__
89 import GHC.Base
90 #endif
91
92 #ifdef COMPILING_GHC
93 IMP_Ubiq(){-uitous-}
94 # ifdef DEBUG
95 import Pretty
96 # endif
97 import Bag ( foldBag )
98
99 # if ! OMIT_NATIVE_CODEGEN
100 # define IF_NCG(a) a
101 # else
102 # define IF_NCG(a) {--}
103 # endif
104 #endif
105
106 -- SIGH: but we use unboxed "sizes"...
107 #if __GLASGOW_HASKELL__
108 #define IF_GHC(a,b) a
109 #else /* not GHC */
110 #define IF_GHC(a,b) b
111 #endif /* not GHC */
112
113
114 -- ---------------------------------------------------------------------------
115 -- The signature of the module
116
117 -- | An empty 'FiniteMap'.
118 emptyFM :: FiniteMap key elt
119
120 -- | A 'FiniteMap' containing a single mapping
121 unitFM :: key -> elt -> FiniteMap key elt
122
123 -- | Makes a 'FiniteMap' from a list of @(key,value)@ pairs. In the
124 -- case of duplicates, the last is taken
125 listToFM :: (Ord key OUTPUTABLE_key) => [(key,elt)] -> FiniteMap key elt
126
127 #ifdef COMPILING_GHC
128 bagToFM :: (Ord key OUTPUTABLE_key) => Bag (key,elt) -> FiniteMap key elt
129 -- In the case of duplicates, who knows which is taken
130 #endif
131
132 -- ADDING AND DELETING
133
134 -- | Adds an element to a 'FiniteMap'. Any previous mapping with the same
135 -- key is overwritten.
136 addToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> elt -> FiniteMap key elt
137
138 -- | Adds a list of elements to a 'FiniteMap', in the order given in
139 -- the list. Overwrites previous mappings.
140 addListToFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [(key,elt)] -> FiniteMap key elt
141
142 -- Combines with previous binding
143 -- In the combining function, the first argument is the "old" element,
144 -- while the second is the "new" one.
145
146 -- | Adds an element to a 'FiniteMap'. If there is already an element
147 -- with the same key, then the specified combination function is used
148 -- to calculate the new value. The already present element is passed as
149 -- the first argument and the new element to add as second.
150 addToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
151 -> FiniteMap key elt -> key -> elt
152 -> FiniteMap key elt
153
154 -- | A list version of 'addToFM_C'. The elements are added in the
155 -- order given in the list.
156 addListToFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
157 -> FiniteMap key elt -> [(key,elt)]
158 -> FiniteMap key elt
159
160 -- | Deletes an element from a 'FiniteMap'. If there is no element with
161 -- the specified key, then the original 'FiniteMap' is returned.
162 delFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
163
164 -- | List version of 'delFromFM'.
165 delListFromFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> [key] -> FiniteMap key elt
166
167 -- | Combine two 'FiniteMap's. Mappings in the second argument shadow
168 -- those in the first.
169 plusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
170 -> FiniteMap key elt
171
172 -- | Combine two 'FiniteMap's. The specified combination function is
173 -- used to calculate the new value when there are two elements with
174 -- the same key.
175 plusFM_C :: (Ord key OUTPUTABLE_key) => (elt -> elt -> elt)
176 -> FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
177
178 -- | @(minusFM a1 a2)@ deletes from @a1@ any mappings which are bound in @a2@
179 minusFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
180
181 -- | @(intersectFM a1 a2)@ returns a new 'FiniteMap' containing
182 -- mappings from @a1@ for which @a2@ also has a mapping with the same
183 -- key.
184 intersectFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt -> FiniteMap key elt
185
186 -- | Returns the interesction of two mappings, using the specified
187 -- combination function to combine values.
188 intersectFM_C :: (Ord key OUTPUTABLE_key) => (elt1 -> elt2 -> elt3)
189 -> FiniteMap key elt1 -> FiniteMap key elt2 -> FiniteMap key elt3
190
191 -- MAPPING, FOLDING, FILTERING
192 foldFM :: (key -> elt -> a -> a) -> a -> FiniteMap key elt -> a
193 mapFM :: (key -> elt1 -> elt2) -> FiniteMap key elt1 -> FiniteMap key elt2
194 filterFM :: (Ord key OUTPUTABLE_key) => (key -> elt -> Bool)
195 -> FiniteMap key elt -> FiniteMap key elt
196
197 -- INTERROGATING
198 sizeFM :: FiniteMap key elt -> Int
199 isEmptyFM :: FiniteMap key elt -> Bool
200
201 -- | Returns 'True' if the specified @key@ has a mapping in this
202 -- 'FiniteMap', or 'False' otherwise.
203 elemFM :: (Ord key OUTPUTABLE_key) => key -> FiniteMap key elt -> Bool
204
205 -- | Looks up a key in a 'FiniteMap', returning @'Just' v@ if the key
206 -- was found with value @v@, or 'Nothing' otherwise.
207 lookupFM :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> Maybe elt
208
209 -- | Looks up a key in a 'FiniteMap', returning @elt@ if the specified
210 -- @key@ was not found.
211 lookupWithDefaultFM
212 :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> elt -> key -> elt
213 -- lookupWithDefaultFM supplies a "default" elt
214 -- to return for an unmapped key
215
216 -- LISTIFYING
217
218 -- | Convert a 'FiniteMap' to a @[(key, elt)]@ sorted by 'Ord' key
219 --
220 fmToList :: FiniteMap key elt -> [(key,elt)]
221
222 -- | Extract the keys from a 'FiniteMap', in the order of the keys, so
223 --
224 -- > keysFM == map fst . fmToList
225 --
226 keysFM :: FiniteMap key elt -> [key]
227
228 -- | Extract the elements from a 'FiniteMap', in the order of the keys, so
229 --
230 -- > eltsFM == map snd . fmToList
231 --
232 eltsFM :: FiniteMap key elt -> [elt]
233
234 -- ---------------------------------------------------------------------------
235 -- The @FiniteMap@ data type, and building of same
236
237 -- Invariants about @FiniteMap@:
238 --
239 -- * all keys in a FiniteMap are distinct
240 --
241 -- * all keys in left subtree are $<$ key in Branch and
242 -- all keys in right subtree are $>$ key in Branch
243 --
244 -- * size field of a Branch gives number of Branch nodes in the tree
245 --
246 -- * size of left subtree is differs from size of right subtree by a
247 -- factor of at most \tr{sIZE_RATIO}
248
249 -- | A mapping from @key@s to @elt@s.
250 data FiniteMap key elt
251 = EmptyFM
252 | Branch key elt -- Key and elt stored here
253 IF_GHC(Int#,Int{-STRICT-}) -- Size >= 1
254 (FiniteMap key elt) -- Children
255 (FiniteMap key elt)
256
257
258 emptyFM = EmptyFM
259 {-
260 emptyFM
261 = Branch bottom bottom IF_GHC(0#,0) bottom bottom
262 where
263 bottom = panic "emptyFM"
264 -}
265
266 -- #define EmptyFM (Branch _ _ IF_GHC(0#,0) _ _)
267
268 unitFM key elt = Branch key elt IF_GHC(1#,1) emptyFM emptyFM
269
270 listToFM = addListToFM emptyFM
271
272 #ifdef COMPILING_GHC
273 bagToFM = foldBag plusFM (\ (k,v) -> unitFM k v) emptyFM
274 #endif
275
276
277 -- ---------------------------------------------------------------------------
278 -- Adding to and deleting from @FiniteMaps@
279
280 addToFM fm key elt = addToFM_C (\ old new -> new) fm key elt
281
282 addToFM_C combiner EmptyFM key elt = unitFM key elt
283 addToFM_C combiner (Branch key elt size fm_l fm_r) new_key new_elt
284 #ifdef __GLASGOW_HASKELL__
285 = case _tagCmp new_key key of
286 _LT -> mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
287 _GT -> mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
288 _EQ -> Branch new_key (combiner elt new_elt) size fm_l fm_r
289 #else
290 | new_key < key = mkBalBranch key elt (addToFM_C combiner fm_l new_key new_elt) fm_r
291 | new_key > key = mkBalBranch key elt fm_l (addToFM_C combiner fm_r new_key new_elt)
292 | otherwise = Branch new_key (combiner elt new_elt) size fm_l fm_r
293 #endif
294
295 addListToFM fm key_elt_pairs = addListToFM_C (\ old new -> new) fm key_elt_pairs
296
297 addListToFM_C combiner fm key_elt_pairs
298 = foldl add fm key_elt_pairs -- foldl adds from the left
299 where
300 add fmap (key,elt) = addToFM_C combiner fmap key elt
301
302
303 delFromFM EmptyFM del_key = emptyFM
304 delFromFM (Branch key elt size fm_l fm_r) del_key
305 #if __GLASGOW_HASKELL__
306 = case _tagCmp del_key key of
307 _GT -> mkBalBranch key elt fm_l (delFromFM fm_r del_key)
308 _LT -> mkBalBranch key elt (delFromFM fm_l del_key) fm_r
309 _EQ -> glueBal fm_l fm_r
310 #else
311 | del_key > key
312 = mkBalBranch key elt fm_l (delFromFM fm_r del_key)
313
314 | del_key < key
315 = mkBalBranch key elt (delFromFM fm_l del_key) fm_r
316
317 | key == del_key
318 = glueBal fm_l fm_r
319 #endif
320
321 delListFromFM fm keys = foldl delFromFM fm keys
322
323 -- ---------------------------------------------------------------------------
324 -- Combining @FiniteMaps@
325
326 plusFM_C combiner EmptyFM fm2 = fm2
327 plusFM_C combiner fm1 EmptyFM = fm1
328 plusFM_C combiner fm1 (Branch split_key elt2 _ left right)
329 = mkVBalBranch split_key new_elt
330 (plusFM_C combiner lts left)
331 (plusFM_C combiner gts right)
332 where
333 lts = splitLT fm1 split_key
334 gts = splitGT fm1 split_key
335 new_elt = case lookupFM fm1 split_key of
336 Nothing -> elt2
337 Just elt1 -> combiner elt1 elt2
338
339 -- It's worth doing plusFM specially, because we don't need
340 -- to do the lookup in fm1.
341
342 plusFM EmptyFM fm2 = fm2
343 plusFM fm1 EmptyFM = fm1
344 plusFM fm1 (Branch split_key elt1 _ left right)
345 = mkVBalBranch split_key elt1 (plusFM lts left) (plusFM gts right)
346 where
347 lts = splitLT fm1 split_key
348 gts = splitGT fm1 split_key
349
350 minusFM EmptyFM fm2 = emptyFM
351 minusFM fm1 EmptyFM = fm1
352 minusFM fm1 (Branch split_key elt _ left right)
353 = glueVBal (minusFM lts left) (minusFM gts right)
354 -- The two can be way different, so we need glueVBal
355 where
356 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
357 gts = splitGT fm1 split_key -- are not in either.
358
359 intersectFM fm1 fm2 = intersectFM_C (\ left right -> right) fm1 fm2
360
361 intersectFM_C combiner fm1 EmptyFM = emptyFM
362 intersectFM_C combiner EmptyFM fm2 = emptyFM
363 intersectFM_C combiner fm1 (Branch split_key elt2 _ left right)
364
365 | isJust maybe_elt1 -- split_elt *is* in intersection
366 = mkVBalBranch split_key (combiner elt1 elt2) (intersectFM_C combiner lts left)
367 (intersectFM_C combiner gts right)
368
369 | otherwise -- split_elt is *not* in intersection
370 = glueVBal (intersectFM_C combiner lts left) (intersectFM_C combiner gts right)
371
372 where
373 lts = splitLT fm1 split_key -- NB gt and lt, so the equal ones
374 gts = splitGT fm1 split_key -- are not in either.
375
376 maybe_elt1 = lookupFM fm1 split_key
377 Just elt1 = maybe_elt1
378
379
380 -- ---------------------------------------------------------------------------
381 -- Mapping, folding, and filtering with @FiniteMaps@
382
383 foldFM k z EmptyFM = z
384 foldFM k z (Branch key elt _ fm_l fm_r)
385 = foldFM k (k key elt (foldFM k z fm_r)) fm_l
386
387 mapFM f EmptyFM = emptyFM
388 mapFM f (Branch key elt size fm_l fm_r)
389 = Branch key (f key elt) size (mapFM f fm_l) (mapFM f fm_r)
390
391 filterFM p EmptyFM = emptyFM
392 filterFM p (Branch key elt _ fm_l fm_r)
393 | p key elt -- Keep the item
394 = mkVBalBranch key elt (filterFM p fm_l) (filterFM p fm_r)
395
396 | otherwise -- Drop the item
397 = glueVBal (filterFM p fm_l) (filterFM p fm_r)
398
399
400 -- ---------------------------------------------------------------------------
401 -- Interrogating @FiniteMaps@
402
403 --{-# INLINE sizeFM #-}
404 sizeFM EmptyFM = 0
405 sizeFM (Branch _ _ size _ _) = IF_GHC(I# size, size)
406
407 isEmptyFM fm = sizeFM fm == 0
408
409 lookupFM EmptyFM key = Nothing
410 lookupFM (Branch key elt _ fm_l fm_r) key_to_find
411 #if __GLASGOW_HASKELL__
412 = case _tagCmp key_to_find key of
413 _LT -> lookupFM fm_l key_to_find
414 _GT -> lookupFM fm_r key_to_find
415 _EQ -> Just elt
416 #else
417 | key_to_find < key = lookupFM fm_l key_to_find
418 | key_to_find > key = lookupFM fm_r key_to_find
419 | otherwise = Just elt
420 #endif
421
422 key `elemFM` fm
423 = case (lookupFM fm key) of { Nothing -> False; Just elt -> True }
424
425 lookupWithDefaultFM fm deflt key
426 = case (lookupFM fm key) of { Nothing -> deflt; Just elt -> elt }
427
428
429 -- ---------------------------------------------------------------------------
430 -- Listifying @FiniteMaps@
431
432 fmToList fm = foldFM (\ key elt rest -> (key,elt) : rest) [] fm
433 keysFM fm = foldFM (\ key elt rest -> key : rest) [] fm
434 eltsFM fm = foldFM (\ key elt rest -> elt : rest) [] fm
435
436
437 -- ---------------------------------------------------------------------------
438 -- Bulk operations on all keys >= or <= a certain threshold
439
440 -- | Fold through all elements greater than or equal to the supplied key,
441 -- in increasing order.
442 foldFM_GE :: Ord key => (key -> elt -> a -> a) -> a -> key ->
443 FiniteMap key elt -> a
444
445 foldFM_GE k z fr EmptyFM = z
446 foldFM_GE k z fr (Branch key elt _ fm_l fm_r)
447 | key >= fr = foldFM_GE k (k key elt (foldFM_GE k z fr fm_r)) fr fm_l
448 | otherwise = foldFM_GE k z fr fm_r
449
450 -- | List elements greater than or equal to the supplied key, in increasing
451 -- order
452 fmToList_GE :: Ord key => FiniteMap key elt -> key -> [(key,elt)]
453 fmToList_GE fm fr = foldFM_GE (\ key elt rest -> (key,elt) : rest) [] fr fm
454
455 -- | List keys greater than or equal to the supplied key, in increasing order
456 keysFM_GE :: Ord key => FiniteMap key elt -> key -> [key]
457 keysFM_GE fm fr = foldFM_GE (\ key elt rest -> key : rest) [] fr fm
458
459 -- | List elements corresponding to keys greater than or equal to the supplied
460 -- key, in increasing order of key.
461 eltsFM_GE :: Ord key => FiniteMap key elt -> key -> [elt]
462 eltsFM_GE fm fr = foldFM_GE (\ key elt rest -> elt : rest) [] fr fm
463
464 -- | Fold through all elements less than or equal to the supplied key,
465 -- in decreasing order.
466 foldFM_LE :: Ord key => (key -> elt -> a -> a) -> a -> key ->
467 FiniteMap key elt -> a
468 foldFM_LE k z fr EmptyFM = z
469 foldFM_LE k z fr (Branch key elt _ fm_l fm_r)
470 | key <= fr = foldFM_LE k (k key elt (foldFM_LE k z fr fm_l)) fr fm_r
471 | otherwise = foldFM_LE k z fr fm_l
472
473 -- | List elements greater than or equal to the supplied key, in decreasing
474 -- order
475 fmToList_LE :: Ord key => FiniteMap key elt -> key -> [(key,elt)]
476 fmToList_LE fm fr = foldFM_LE (\ key elt rest -> (key,elt) : rest) [] fr fm
477
478 -- | List keys greater than or equal to the supplied key, in decreasing order
479 keysFM_LE :: Ord key => FiniteMap key elt -> key -> [key]
480 keysFM_LE fm fr = foldFM_LE (\ key elt rest -> key : rest) [] fr fm
481
482 -- | List elements corresponding to keys greater than or equal to the supplied
483 -- key, in decreasing order of key.
484 eltsFM_LE :: Ord key => FiniteMap key elt -> key -> [elt]
485 eltsFM_LE fm fr = foldFM_LE (\ key elt rest -> elt : rest) [] fr fm
486
487 -- ---------------------------------------------------------------------------
488 -- Getting minimum and maximum key out.
489 -- ---------------------------------------------------------------------------
490
491 -- | Extract minimum key, or Nothing if the map is empty.
492 minFM :: Ord key => FiniteMap key elt -> Maybe key
493 minFM EmptyFM = Nothing
494 minFM (Branch key _ _ fm_l _) =
495 case minFM fm_l of
496 Nothing -> Just key
497 Just key1 -> Just key1
498
499 -- | Extract maximum key, or Nothing if the map is empty.
500 maxFM :: Ord key => FiniteMap key elt -> Maybe key
501 maxFM EmptyFM = Nothing
502 maxFM (Branch key _ _ _ fm_r) =
503 case maxFM fm_r of
504 Nothing -> Just key
505 Just key1 -> Just key1
506
507
508 -- ---------------------------------------------------------------------------
509 -- The implementation of balancing
510
511 -- Basic construction of a @FiniteMap@:
512
513 -- @mkBranch@ simply gets the size component right. This is the ONLY
514 -- (non-trivial) place the Branch object is built, so the ASSERTion
515 -- recursively checks consistency. (The trivial use of Branch is in
516 -- @unitFM@.)
517
518 sIZE_RATIO :: Int
519 sIZE_RATIO = 5
520
521 mkBranch :: (Ord key OUTPUTABLE_key) -- Used for the assertion checking only
522 => Int
523 -> key -> elt
524 -> FiniteMap key elt -> FiniteMap key elt
525 -> FiniteMap key elt
526
527 mkBranch which key elt fm_l fm_r
528 = --ASSERT( left_ok && right_ok && balance_ok )
529 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
530 if not ( left_ok && right_ok && balance_ok ) then
531 pprPanic ("mkBranch:"++show which) (ppAboves [ppr PprDebug [left_ok, right_ok, balance_ok],
532 ppr PprDebug key,
533 ppr PprDebug fm_l,
534 ppr PprDebug fm_r])
535 else
536 #endif
537 let
538 result = Branch key elt (unbox (1 + left_size + right_size)) fm_l fm_r
539 in
540 -- if sizeFM result <= 8 then
541 result
542 -- else
543 -- pprTrace ("mkBranch:"++(show which)) (ppr PprDebug result) (
544 -- result
545 -- )
546 where
547 left_ok = case fm_l of
548 EmptyFM -> True
549 Branch left_key _ _ _ _ -> let
550 biggest_left_key = fst (findMax fm_l)
551 in
552 biggest_left_key < key
553 right_ok = case fm_r of
554 EmptyFM -> True
555 Branch right_key _ _ _ _ -> let
556 smallest_right_key = fst (findMin fm_r)
557 in
558 key < smallest_right_key
559 balance_ok = True -- sigh
560 {- LATER:
561 balance_ok
562 = -- Both subtrees have one or no elements...
563 (left_size + right_size <= 1)
564 -- NO || left_size == 0 -- ???
565 -- NO || right_size == 0 -- ???
566 -- ... or the number of elements in a subtree does not exceed
567 -- sIZE_RATIO times the number of elements in the other subtree
568 || (left_size * sIZE_RATIO >= right_size &&
569 right_size * sIZE_RATIO >= left_size)
570 -}
571
572 left_size = sizeFM fm_l
573 right_size = sizeFM fm_r
574
575 #if __GLASGOW_HASKELL__
576 unbox :: Int -> Int#
577 unbox (I# size) = size
578 #else
579 unbox :: Int -> Int
580 unbox x = x
581 #endif
582
583
584 -- ---------------------------------------------------------------------------
585 -- {\em Balanced} construction of a @FiniteMap@
586
587 -- @mkBalBranch@ rebalances, assuming that the subtrees aren't too far
588 -- out of whack.
589
590 mkBalBranch :: (Ord key OUTPUTABLE_key)
591 => key -> elt
592 -> FiniteMap key elt -> FiniteMap key elt
593 -> FiniteMap key elt
594
595 mkBalBranch key elt fm_L fm_R
596
597 | size_l + size_r < 2
598 = mkBranch 1{-which-} key elt fm_L fm_R
599
600 | size_r > sIZE_RATIO * size_l -- Right tree too big
601 = case fm_R of
602 Branch _ _ _ fm_rl fm_rr
603 | sizeFM fm_rl < 2 * sizeFM fm_rr -> single_L fm_L fm_R
604 | otherwise -> double_L fm_L fm_R
605 -- Other case impossible
606
607 | size_l > sIZE_RATIO * size_r -- Left tree too big
608 = case fm_L of
609 Branch _ _ _ fm_ll fm_lr
610 | sizeFM fm_lr < 2 * sizeFM fm_ll -> single_R fm_L fm_R
611 | otherwise -> double_R fm_L fm_R
612 -- Other case impossible
613
614 | otherwise -- No imbalance
615 = mkBranch 2{-which-} key elt fm_L fm_R
616
617 where
618 size_l = sizeFM fm_L
619 size_r = sizeFM fm_R
620
621 single_L fm_l (Branch key_r elt_r _ fm_rl fm_rr)
622 = mkBranch 3{-which-} key_r elt_r (mkBranch 4{-which-} key elt fm_l fm_rl) fm_rr
623
624 double_L fm_l (Branch key_r elt_r _ (Branch key_rl elt_rl _ fm_rll fm_rlr) fm_rr)
625 = mkBranch 5{-which-} key_rl elt_rl (mkBranch 6{-which-} key elt fm_l fm_rll)
626 (mkBranch 7{-which-} key_r elt_r fm_rlr fm_rr)
627
628 single_R (Branch key_l elt_l _ fm_ll fm_lr) fm_r
629 = mkBranch 8{-which-} key_l elt_l fm_ll (mkBranch 9{-which-} key elt fm_lr fm_r)
630
631 double_R (Branch key_l elt_l _ fm_ll (Branch key_lr elt_lr _ fm_lrl fm_lrr)) fm_r
632 = mkBranch 10{-which-} key_lr elt_lr (mkBranch 11{-which-} key_l elt_l fm_ll fm_lrl)
633 (mkBranch 12{-which-} key elt fm_lrr fm_r)
634
635
636 mkVBalBranch :: (Ord key OUTPUTABLE_key)
637 => key -> elt
638 -> FiniteMap key elt -> FiniteMap key elt
639 -> FiniteMap key elt
640
641 -- Assert: in any call to (mkVBalBranch_C comb key elt l r),
642 -- (a) all keys in l are < all keys in r
643 -- (b) all keys in l are < key
644 -- (c) all keys in r are > key
645
646 mkVBalBranch key elt EmptyFM fm_r = addToFM fm_r key elt
647 mkVBalBranch key elt fm_l EmptyFM = addToFM fm_l key elt
648
649 mkVBalBranch key elt fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
650 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
651 | sIZE_RATIO * size_l < size_r
652 = mkBalBranch key_r elt_r (mkVBalBranch key elt fm_l fm_rl) fm_rr
653
654 | sIZE_RATIO * size_r < size_l
655 = mkBalBranch key_l elt_l fm_ll (mkVBalBranch key elt fm_lr fm_r)
656
657 | otherwise
658 = mkBranch 13{-which-} key elt fm_l fm_r
659
660 where
661 size_l = sizeFM fm_l
662 size_r = sizeFM fm_r
663
664 -- ---------------------------------------------------------------------------
665 -- Gluing two trees together
666
667 -- @glueBal@ assumes its two arguments aren't too far out of whack, just
668 -- like @mkBalBranch@. But: all keys in first arg are $<$ all keys in
669 -- second.
670
671 glueBal :: (Ord key OUTPUTABLE_key)
672 => FiniteMap key elt -> FiniteMap key elt
673 -> FiniteMap key elt
674
675 glueBal EmptyFM fm2 = fm2
676 glueBal fm1 EmptyFM = fm1
677 glueBal fm1 fm2
678 -- The case analysis here (absent in Adams' program) is really to deal
679 -- with the case where fm2 is a singleton. Then deleting the minimum means
680 -- we pass an empty tree to mkBalBranch, which breaks its invariant.
681 | sizeFM fm2 > sizeFM fm1
682 = mkBalBranch mid_key2 mid_elt2 fm1 (deleteMin fm2)
683
684 | otherwise
685 = mkBalBranch mid_key1 mid_elt1 (deleteMax fm1) fm2
686 where
687 (mid_key1, mid_elt1) = findMax fm1
688 (mid_key2, mid_elt2) = findMin fm2
689
690 -- @glueVBal@ copes with arguments which can be of any size.
691 -- But: all keys in first arg are $<$ all keys in second.
692
693 glueVBal :: (Ord key OUTPUTABLE_key)
694 => FiniteMap key elt -> FiniteMap key elt
695 -> FiniteMap key elt
696
697 glueVBal EmptyFM fm2 = fm2
698 glueVBal fm1 EmptyFM = fm1
699 glueVBal fm_l@(Branch key_l elt_l _ fm_ll fm_lr)
700 fm_r@(Branch key_r elt_r _ fm_rl fm_rr)
701 | sIZE_RATIO * size_l < size_r
702 = mkBalBranch key_r elt_r (glueVBal fm_l fm_rl) fm_rr
703
704 | sIZE_RATIO * size_r < size_l
705 = mkBalBranch key_l elt_l fm_ll (glueVBal fm_lr fm_r)
706
707 | otherwise -- We now need the same two cases as in glueBal above.
708 = glueBal fm_l fm_r
709 where
710 size_l = sizeFM fm_l
711 size_r = sizeFM fm_r
712
713
714 -- ---------------------------------------------------------------------------
715 -- Local utilities
716
717 splitLT, splitGT :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> key -> FiniteMap key elt
718
719 -- splitLT fm split_key = fm restricted to keys < split_key
720 -- splitGT fm split_key = fm restricted to keys > split_key
721
722 splitLT EmptyFM split_key = emptyFM
723 splitLT (Branch key elt _ fm_l fm_r) split_key
724 #if __GLASGOW_HASKELL__
725 = case _tagCmp split_key key of
726 _LT -> splitLT fm_l split_key
727 _GT -> mkVBalBranch key elt fm_l (splitLT fm_r split_key)
728 _EQ -> fm_l
729 #else
730 | split_key < key = splitLT fm_l split_key
731 | split_key > key = mkVBalBranch key elt fm_l (splitLT fm_r split_key)
732 | otherwise = fm_l
733 #endif
734
735 splitGT EmptyFM split_key = emptyFM
736 splitGT (Branch key elt _ fm_l fm_r) split_key
737 #if __GLASGOW_HASKELL__
738 = case _tagCmp split_key key of
739 _GT -> splitGT fm_r split_key
740 _LT -> mkVBalBranch key elt (splitGT fm_l split_key) fm_r
741 _EQ -> fm_r
742 #else
743 | split_key > key = splitGT fm_r split_key
744 | split_key < key = mkVBalBranch key elt (splitGT fm_l split_key) fm_r
745 | otherwise = fm_r
746 #endif
747
748 findMin :: FiniteMap key elt -> (key,elt)
749 findMin (Branch key elt _ EmptyFM _) = (key,elt)
750 findMin (Branch key elt _ fm_l _) = findMin fm_l
751
752 deleteMin :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
753 deleteMin (Branch key elt _ EmptyFM fm_r) = fm_r
754 deleteMin (Branch key elt _ fm_l fm_r) = mkBalBranch key elt (deleteMin fm_l) fm_r
755
756 findMax :: FiniteMap key elt -> (key,elt)
757 findMax (Branch key elt _ _ EmptyFM) = (key,elt)
758 findMax (Branch key elt _ _ fm_r) = findMax fm_r
759
760 deleteMax :: (Ord key OUTPUTABLE_key) => FiniteMap key elt -> FiniteMap key elt
761 deleteMax (Branch key elt _ fm_l EmptyFM) = fm_l
762 deleteMax (Branch key elt _ fm_l fm_r) = mkBalBranch key elt fm_l (deleteMax fm_r)
763
764
765 -- ---------------------------------------------------------------------------
766 -- Output-ery
767
768 #if defined(COMPILING_GHC) && defined(DEBUG_FINITEMAPS)
769
770 instance (Outputable key) => Outputable (FiniteMap key elt) where
771 ppr sty fm = pprX sty fm
772
773 pprX sty EmptyFM = ppChar '!'
774 pprX sty (Branch key elt sz fm_l fm_r)
775 = ppBesides [ppLparen, pprX sty fm_l, ppSP,
776 ppr sty key, ppSP, ppInt (IF_GHC(I# sz, sz)), ppSP,
777 pprX sty fm_r, ppRparen]
778 #endif
779
780 #ifndef COMPILING_GHC
781 instance (Eq key, Eq elt) => Eq (FiniteMap key elt) where
782 fm_1 == fm_2 = (sizeFM fm_1 == sizeFM fm_2) && -- quick test
783 (fmToList fm_1 == fmToList fm_2)
784
785 {- NO: not clear what The Right Thing to do is:
786 instance (Ord key, Ord elt) => Ord (FiniteMap key elt) where
787 fm_1 <= fm_2 = (sizeFM fm_1 <= sizeFM fm_2) && -- quick test
788 (fmToList fm_1 <= fmToList fm_2)
789 -}
790 #endif
791
792 -- ---------------------------------------------------------------------------
793 -- Efficiency pragmas for GHC
794
795 -- When the FiniteMap module is used in GHC, we specialise it for
796 -- \tr{Uniques}, for dastardly efficiency reasons.
797
798 #if defined(COMPILING_GHC) && __GLASGOW_HASKELL__ && !defined(REALLY_HASKELL_1_3)
799
800 {-# SPECIALIZE addListToFM
801 :: FiniteMap (FAST_STRING, FAST_STRING) elt -> [((FAST_STRING, FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
802 , FiniteMap RdrName elt -> [(RdrName,elt)] -> FiniteMap RdrName elt
803 IF_NCG(COMMA FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
804 #-}
805 {-# SPECIALIZE addListToFM_C
806 :: (elt -> elt -> elt) -> FiniteMap TyCon elt -> [(TyCon,elt)] -> FiniteMap TyCon elt
807 , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
808 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> [(Reg COMMA elt)] -> FiniteMap Reg elt)
809 #-}
810 {-# SPECIALIZE addToFM
811 :: FiniteMap CLabel elt -> CLabel -> elt -> FiniteMap CLabel elt
812 , FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt
813 , FiniteMap (FAST_STRING, FAST_STRING) elt -> (FAST_STRING, FAST_STRING) -> elt -> FiniteMap (FAST_STRING, FAST_STRING) elt
814 , FiniteMap RdrName elt -> RdrName -> elt -> FiniteMap RdrName elt
815 , FiniteMap OrigName elt -> OrigName -> elt -> FiniteMap OrigName elt
816 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
817 #-}
818 {-# SPECIALIZE addToFM_C
819 :: (elt -> elt -> elt) -> FiniteMap (RdrName, RdrName) elt -> (RdrName, RdrName) -> elt -> FiniteMap (RdrName, RdrName) elt
820 , (elt -> elt -> elt) -> FiniteMap (OrigName, OrigName) elt -> (OrigName, OrigName) -> elt -> FiniteMap (OrigName, OrigName) elt
821 , (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FAST_STRING -> elt -> FiniteMap FAST_STRING elt
822 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> Reg -> elt -> FiniteMap Reg elt)
823 #-}
824 {-# SPECIALIZE bagToFM
825 :: Bag (FAST_STRING,elt) -> FiniteMap FAST_STRING elt
826 #-}
827 {-# SPECIALIZE delListFromFM
828 :: FiniteMap RdrName elt -> [RdrName] -> FiniteMap RdrName elt
829 , FiniteMap OrigName elt -> [OrigName] -> FiniteMap OrigName elt
830 , FiniteMap FAST_STRING elt -> [FAST_STRING] -> FiniteMap FAST_STRING elt
831 IF_NCG(COMMA FiniteMap Reg elt -> [Reg] -> FiniteMap Reg elt)
832 #-}
833 {-# SPECIALIZE listToFM
834 :: [([Char],elt)] -> FiniteMap [Char] elt
835 , [(FAST_STRING,elt)] -> FiniteMap FAST_STRING elt
836 , [((FAST_STRING,FAST_STRING),elt)] -> FiniteMap (FAST_STRING, FAST_STRING) elt
837 , [(OrigName,elt)] -> FiniteMap OrigName elt
838 IF_NCG(COMMA [(Reg COMMA elt)] -> FiniteMap Reg elt)
839 #-}
840 {-# SPECIALIZE lookupFM
841 :: FiniteMap CLabel elt -> CLabel -> Maybe elt
842 , FiniteMap [Char] elt -> [Char] -> Maybe elt
843 , FiniteMap FAST_STRING elt -> FAST_STRING -> Maybe elt
844 , FiniteMap (FAST_STRING,FAST_STRING) elt -> (FAST_STRING,FAST_STRING) -> Maybe elt
845 , FiniteMap OrigName elt -> OrigName -> Maybe elt
846 , FiniteMap (OrigName,OrigName) elt -> (OrigName,OrigName) -> Maybe elt
847 , FiniteMap RdrName elt -> RdrName -> Maybe elt
848 , FiniteMap (RdrName,RdrName) elt -> (RdrName,RdrName) -> Maybe elt
849 IF_NCG(COMMA FiniteMap Reg elt -> Reg -> Maybe elt)
850 #-}
851 {-# SPECIALIZE lookupWithDefaultFM
852 :: FiniteMap FAST_STRING elt -> elt -> FAST_STRING -> elt
853 IF_NCG(COMMA FiniteMap Reg elt -> elt -> Reg -> elt)
854 #-}
855 {-# SPECIALIZE plusFM
856 :: FiniteMap RdrName elt -> FiniteMap RdrName elt -> FiniteMap RdrName elt
857 , FiniteMap OrigName elt -> FiniteMap OrigName elt -> FiniteMap OrigName elt
858 , FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
859 IF_NCG(COMMA FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
860 #-}
861 {-# SPECIALIZE plusFM_C
862 :: (elt -> elt -> elt) -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt -> FiniteMap FAST_STRING elt
863 IF_NCG(COMMA (elt -> elt -> elt) -> FiniteMap Reg elt -> FiniteMap Reg elt -> FiniteMap Reg elt)
864 #-}
865
866 #endif /* compiling for GHC */