2 % (c) The AQUA Project, Glasgow University, 1994-1998
4 \section[UniqFM]{Specialised finite maps, for things with @Uniques@}
6 Based on @FiniteMaps@ (as you would expect).
8 Basically, the things need to be in class @Uniquable@, and we use the
9 @getUnique@ method to grab their @Uniques@.
11 (A similar thing to @UniqSet@, as opposed to @Set@.)
15 UniqFM, -- abstract type
23 addListToUFM,addListToUFM_C,
25 addListToUFM_Directly,
41 lookupUFM, lookupUFM_Directly,
42 lookupWithDefaultUFM, lookupWithDefaultUFM_Directly,
47 #include "HsVersions.h"
49 import {-# SOURCE #-} Name ( Name )
51 import Unique ( Uniquable(..), Unique, u2i, mkUniqueGrimily )
53 import GlaExts -- Lots of Int# operations
58 %************************************************************************
60 \subsection{The @UniqFM@ type, and signatures for the functions}
62 %************************************************************************
64 We use @FiniteMaps@, with a (@getUnique@-able) @Unique@ as ``key''.
67 emptyUFM :: UniqFM elt
68 isNullUFM :: UniqFM elt -> Bool
69 unitUFM :: Uniquable key => key -> elt -> UniqFM elt
70 unitDirectlyUFM -- got the Unique already
71 :: Unique -> elt -> UniqFM elt
72 listToUFM :: Uniquable key => [(key,elt)] -> UniqFM elt
74 :: [(Unique, elt)] -> UniqFM elt
76 addToUFM :: Uniquable key => UniqFM elt -> key -> elt -> UniqFM elt
77 addListToUFM :: Uniquable key => UniqFM elt -> [(key,elt)] -> UniqFM elt
79 :: UniqFM elt -> Unique -> elt -> UniqFM elt
81 addToUFM_C :: Uniquable key => (elt -> elt -> elt) -- old -> new -> result
84 -> UniqFM elt -- result
86 addListToUFM_C :: Uniquable key => (elt -> elt -> elt)
87 -> UniqFM elt -> [(key,elt)]
90 delFromUFM :: Uniquable key => UniqFM elt -> key -> UniqFM elt
91 delListFromUFM :: Uniquable key => UniqFM elt -> [key] -> UniqFM elt
92 delFromUFM_Directly :: UniqFM elt -> Unique -> UniqFM elt
94 plusUFM :: UniqFM elt -> UniqFM elt -> UniqFM elt
96 plusUFM_C :: (elt -> elt -> elt)
97 -> UniqFM elt -> UniqFM elt -> UniqFM elt
99 minusUFM :: UniqFM elt -> UniqFM elt -> UniqFM elt
101 intersectUFM :: UniqFM elt -> UniqFM elt -> UniqFM elt
102 intersectUFM_C :: (elt -> elt -> elt)
103 -> UniqFM elt -> UniqFM elt -> UniqFM elt
104 foldUFM :: (elt -> a -> a) -> a -> UniqFM elt -> a
105 mapUFM :: (elt1 -> elt2) -> UniqFM elt1 -> UniqFM elt2
106 filterUFM :: (elt -> Bool) -> UniqFM elt -> UniqFM elt
108 sizeUFM :: UniqFM elt -> Int
109 hashUFM :: UniqFM elt -> Int
110 elemUFM :: Uniquable key => key -> UniqFM elt -> Bool
112 lookupUFM :: Uniquable key => UniqFM elt -> key -> Maybe elt
113 lookupUFM_Directly -- when you've got the Unique already
114 :: UniqFM elt -> Unique -> Maybe elt
116 :: Uniquable key => UniqFM elt -> elt -> key -> elt
117 lookupWithDefaultUFM_Directly
118 :: UniqFM elt -> elt -> Unique -> elt
120 keysUFM :: UniqFM elt -> [Int] -- Get the keys
121 eltsUFM :: UniqFM elt -> [elt]
122 ufmToList :: UniqFM elt -> [(Unique, elt)]
125 %************************************************************************
127 \subsection{The @IdFinMap@ and @TyVarFinMap@ specialisations for Ids/TyVars}
129 %************************************************************************
132 -- Turn off for now, these need to be updated (SDM 4/98)
135 #ifdef __GLASGOW_HASKELL__
136 -- I don't think HBC was too happy about this (WDP 94/10)
139 addListToUFM :: UniqFM elt -> [(Name, elt)] -> UniqFM elt
142 addListToUFM_C :: (elt -> elt -> elt) -> UniqFM elt -> [(Name, elt)] -> UniqFM elt
145 addToUFM :: UniqFM elt -> Unique -> elt -> UniqFM elt
148 listToUFM :: [(Unique, elt)] -> UniqFM elt
151 lookupUFM :: UniqFM elt -> Name -> Maybe elt
152 , UniqFM elt -> Unique -> Maybe elt
155 #endif {- __GLASGOW_HASKELL__ -}
159 %************************************************************************
161 \subsection{Andy Gill's underlying @UniqFM@ machinery}
163 %************************************************************************
165 ``Uniq Finite maps'' are the heart and soul of the compiler's
166 lookup-tables/environments. Important stuff! It works well with
167 Dense and Sparse ranges.
168 Both @Uq@ Finite maps and @Hash@ Finite Maps
169 are built ontop of Int Finite Maps.
171 This code is explained in the paper:
173 A Gill, S Peyton Jones, B O'Sullivan, W Partain and Aqua Friends
174 "A Cheap balancing act that grows on a tree"
175 Glasgow FP Workshop, Sep 1994, pp??-??
178 %************************************************************************
180 \subsubsection{The @UniqFM@ type, and signatures for the functions}
182 %************************************************************************
184 @UniqFM a@ is a mapping from Unique to a.
186 First, the DataType itself; which is either a Node, a Leaf, or an Empty.
191 | LeafUFM FastInt ele
192 | NodeUFM FastInt -- the switching
198 -- for debugging only :-)
199 instance Outputable (UniqFM a) where
200 ppr(NodeUFM a b t1 t2) =
201 sep [text "NodeUFM " <+> int IBOX(a) <+> int IBOX(b),
202 nest 1 (parens (ppr t1)),
203 nest 1 (parens (ppr t2))]
204 ppr (LeafUFM x a) = text "LeafUFM " <+> int IBOX(x)
205 ppr (EmptyUFM) = empty
207 -- and when not debugging the package itself...
208 instance Outputable a => Outputable (UniqFM a) where
209 ppr ufm = ppr (ufmToList ufm)
212 %************************************************************************
214 \subsubsection{The @UniqFM@ functions}
216 %************************************************************************
218 First the ways of building a UniqFM.
222 unitUFM key elt = mkLeafUFM (u2i (getUnique key)) elt
223 unitDirectlyUFM key elt = mkLeafUFM (u2i key) elt
225 listToUFM key_elt_pairs
226 = addListToUFM_C use_snd EmptyUFM key_elt_pairs
228 listToUFM_Directly uniq_elt_pairs
229 = addListToUFM_directly_C use_snd EmptyUFM uniq_elt_pairs
232 Now ways of adding things to UniqFMs.
234 There is an alternative version of @addListToUFM_C@, that uses @plusUFM@,
235 but the semantics of this operation demands a linear insertion;
236 perhaps the version without the combinator function
237 could be optimised using it.
240 addToUFM fm key elt = addToUFM_C use_snd fm key elt
242 addToUFM_Directly fm u elt = insert_ele use_snd fm (u2i u) elt
244 addToUFM_C combiner fm key elt
245 = insert_ele combiner fm (u2i (getUnique key)) elt
247 addListToUFM fm key_elt_pairs = addListToUFM_C use_snd fm key_elt_pairs
248 addListToUFM_Directly fm uniq_elt_pairs = addListToUFM_directly_C use_snd fm uniq_elt_pairs
250 addListToUFM_C combiner fm key_elt_pairs
251 = foldl (\ fm (k, e) -> insert_ele combiner fm (u2i (getUnique k)) e)
254 addListToUFM_directly_C combiner fm uniq_elt_pairs
255 = foldl (\ fm (k, e) -> insert_ele combiner fm (u2i k) e)
259 Now ways of removing things from UniqFM.
262 delListFromUFM fm lst = foldl delFromUFM fm lst
264 delFromUFM fm key = delete fm (u2i (getUnique key))
265 delFromUFM_Directly fm u = delete fm (u2i u)
267 delete EmptyUFM _ = EmptyUFM
268 delete fm key = del_ele fm
270 del_ele :: UniqFM a -> UniqFM a
272 del_ele lf@(LeafUFM j _)
273 | j ==# key = EmptyUFM
274 | otherwise = lf -- no delete!
276 del_ele nd@(NodeUFM j p t1 t2)
278 = mkSLNodeUFM (NodeUFMData j p) (del_ele t1) t2
280 = mkLSNodeUFM (NodeUFMData j p) t1 (del_ele t2)
282 del_ele _ = panic "Found EmptyUFM FM when rec-deleting"
285 Now ways of adding two UniqFM's together.
288 plusUFM tr1 tr2 = plusUFM_C use_snd tr1 tr2
290 plusUFM_C f EmptyUFM tr = tr
291 plusUFM_C f tr EmptyUFM = tr
292 plusUFM_C f fm1 fm2 = mix_trees fm1 fm2
294 mix_trees (LeafUFM i a) t2 = insert_ele (flip f) t2 i a
295 mix_trees t1 (LeafUFM i a) = insert_ele f t1 i a
297 mix_trees left_t@(NodeUFM j p t1 t2) right_t@(NodeUFM j' p' t1' t2')
299 (ask_about_common_ancestor
303 -- Given a disjoint j,j' (p >^ p' && p' >^ p):
307 -- t1 t2 t1' t2' j j'
312 mix_branches (NewRoot nd False)
313 = mkLLNodeUFM nd left_t right_t
314 mix_branches (NewRoot nd True)
315 = mkLLNodeUFM nd right_t left_t
321 -- t1 t2 t1' t2' t1 + t1' t2 + t2'
323 mix_branches (SameRoot)
324 = mkSSNodeUFM (NodeUFMData j p)
327 -- Now the 4 different other ways; all like this:
329 -- Given j >^ j' (and, say, j > j')
333 -- t1 t2 t1' t2' t1 t2 + j'
336 mix_branches (LeftRoot Leftt) -- | trace "LL" True
339 (mix_trees t1 right_t)
342 mix_branches (LeftRoot Rightt) -- | trace "LR" True
346 (mix_trees t2 right_t)
348 mix_branches (RightRoot Leftt) -- | trace "RL" True
351 (mix_trees left_t t1')
354 mix_branches (RightRoot Rightt) -- | trace "RR" True
358 (mix_trees left_t t2')
360 mix_trees _ _ = panic "EmptyUFM found when inserting into plusInt"
363 And ways of subtracting them. First the base cases,
364 then the full D&C approach.
367 minusUFM EmptyUFM _ = EmptyUFM
368 minusUFM t1 EmptyUFM = t1
369 minusUFM fm1 fm2 = minus_trees fm1 fm2
372 -- Notice the asymetry of subtraction
374 minus_trees lf@(LeafUFM i a) t2 =
379 minus_trees t1 (LeafUFM i _) = delete t1 i
381 minus_trees left_t@(NodeUFM j p t1 t2) right_t@(NodeUFM j' p' t1' t2')
383 (ask_about_common_ancestor
387 -- Given a disjoint j,j' (p >^ p' && p' >^ p):
391 -- t1 t2 t1' t2' t1 t2
396 minus_branches (NewRoot nd _) = left_t
402 -- t1 t2 t1' t2' t1 + t1' t2 + t2'
404 minus_branches (SameRoot)
405 = mkSSNodeUFM (NodeUFMData j p)
408 -- Now the 4 different other ways; all like this:
409 -- again, with asymatry
412 -- The left is above the right
414 minus_branches (LeftRoot Leftt)
417 (minus_trees t1 right_t)
419 minus_branches (LeftRoot Rightt)
423 (minus_trees t2 right_t)
426 -- The right is above the left
428 minus_branches (RightRoot Leftt)
429 = minus_trees left_t t1'
430 minus_branches (RightRoot Rightt)
431 = minus_trees left_t t2'
433 minus_trees _ _ = panic "EmptyUFM found when insering into plusInt"
436 And taking the intersection of two UniqFM's.
439 intersectUFM t1 t2 = intersectUFM_C use_snd t1 t2
441 intersectUFM_C f EmptyUFM _ = EmptyUFM
442 intersectUFM_C f _ EmptyUFM = EmptyUFM
443 intersectUFM_C f fm1 fm2 = intersect_trees fm1 fm2
445 intersect_trees (LeafUFM i a) t2 =
448 Just b -> mkLeafUFM i (f a b)
450 intersect_trees t1 (LeafUFM i a) =
453 Just b -> mkLeafUFM i (f b a)
455 intersect_trees left_t@(NodeUFM j p t1 t2) right_t@(NodeUFM j' p' t1' t2')
457 (ask_about_common_ancestor
461 -- Given a disjoint j,j' (p >^ p' && p' >^ p):
464 -- / \ + / \ ==> EmptyUFM
469 intersect_branches (NewRoot nd _) = EmptyUFM
475 -- t1 t2 t1' t2' t1 x t1' t2 x t2'
477 intersect_branches (SameRoot)
478 = mkSSNodeUFM (NodeUFMData j p)
479 (intersect_trees t1 t1')
480 (intersect_trees t2 t2')
481 -- Now the 4 different other ways; all like this:
483 -- Given j >^ j' (and, say, j > j')
487 -- t1 t2 t1' t2' t1' t2'
489 -- This does cut down the search space quite a bit.
491 intersect_branches (LeftRoot Leftt)
492 = intersect_trees t1 right_t
493 intersect_branches (LeftRoot Rightt)
494 = intersect_trees t2 right_t
495 intersect_branches (RightRoot Leftt)
496 = intersect_trees left_t t1'
497 intersect_branches (RightRoot Rightt)
498 = intersect_trees left_t t2'
500 intersect_trees x y = panic ("EmptyUFM found when intersecting trees")
503 Now the usual set of `collection' operators, like map, fold, etc.
506 foldUFM f a (NodeUFM _ _ t1 t2) = foldUFM f (foldUFM f a t2) t1
507 foldUFM f a (LeafUFM _ obj) = f obj a
508 foldUFM f a EmptyUFM = a
512 mapUFM fn EmptyUFM = EmptyUFM
513 mapUFM fn fm = map_tree fn fm
515 filterUFM fn EmptyUFM = EmptyUFM
516 filterUFM fn fm = filter_tree fn fm
519 Note, this takes a long time, O(n), but
520 because we dont want to do this very often, we put up with this.
521 O'rable, but how often do we look at the size of
526 sizeUFM (NodeUFM _ _ t1 t2) = sizeUFM t1 + sizeUFM t2
527 sizeUFM (LeafUFM _ _) = 1
529 isNullUFM EmptyUFM = True
532 -- hashing is used in VarSet.uniqAway, and should be fast
533 -- We use a cheap and cheerful method for now
535 hashUFM (NodeUFM n _ _ _) = iBox n
536 hashUFM (LeafUFM n _) = iBox n
539 looking up in a hurry is the {\em whole point} of this binary tree lark.
540 Lookup up a binary tree is easy (and fast).
543 elemUFM key fm = case lookUp fm (u2i (getUnique key)) of
547 lookupUFM fm key = lookUp fm (u2i (getUnique key))
548 lookupUFM_Directly fm key = lookUp fm (u2i key)
550 lookupWithDefaultUFM fm deflt key
551 = case lookUp fm (u2i (getUnique key)) of
555 lookupWithDefaultUFM_Directly fm deflt key
556 = case lookUp fm (u2i key) of
560 lookUp EmptyUFM _ = Nothing
561 lookUp fm i = lookup_tree fm
563 lookup_tree :: UniqFM a -> Maybe a
565 lookup_tree (LeafUFM j b)
567 | otherwise = Nothing
568 lookup_tree (NodeUFM j p t1 t2)
569 | j ># i = lookup_tree t1
570 | otherwise = lookup_tree t2
572 lookup_tree EmptyUFM = panic "lookup Failed"
575 folds are *wonderful* things.
578 eltsUFM fm = foldUFM (:) [] fm
580 ufmToList fm = fold_tree (\ iu elt rest -> (mkUniqueGrimily iu, elt) : rest) [] fm
582 keysUFM fm = fold_tree (\ iu elt rest -> iBox iu : rest) [] fm
584 fold_tree f a (NodeUFM _ _ t1 t2) = fold_tree f (fold_tree f a t2) t1
585 fold_tree f a (LeafUFM iu obj) = f iu obj a
586 fold_tree f a EmptyUFM = a
589 %************************************************************************
591 \subsubsection{The @UniqFM@ type, and its functions}
593 %************************************************************************
595 You should always use these to build the tree.
596 There are 4 versions of mkNodeUFM, depending on
597 the strictness of the two sub-tree arguments.
598 The strictness is used *both* to prune out
599 empty trees, *and* to improve performance,
600 stoping needless thunks lying around.
601 The rule of thumb (from experence with these trees)
602 is make thunks strict, but data structures lazy.
603 If in doubt, use mkSSNodeUFM, which has the `strongest'
604 functionality, but may do a few needless evaluations.
607 mkLeafUFM :: FastInt -> a -> UniqFM a
608 mkLeafUFM i a = LeafUFM i a
610 -- The *ONLY* ways of building a NodeUFM.
612 mkSSNodeUFM (NodeUFMData j p) EmptyUFM t2 = t2
613 mkSSNodeUFM (NodeUFMData j p) t1 EmptyUFM = t1
614 mkSSNodeUFM (NodeUFMData j p) t1 t2
615 = ASSERT(correctNodeUFM (iBox j) (iBox p) t1 t2)
618 mkSLNodeUFM (NodeUFMData j p) EmptyUFM t2 = t2
619 mkSLNodeUFM (NodeUFMData j p) t1 t2
620 = ASSERT(correctNodeUFM (iBox j) (iBox p) t1 t2)
623 mkLSNodeUFM (NodeUFMData j p) t1 EmptyUFM = t1
624 mkLSNodeUFM (NodeUFMData j p) t1 t2
625 = ASSERT(correctNodeUFM (iBox j) (iBox p) t1 t2)
628 mkLLNodeUFM (NodeUFMData j p) t1 t2
629 = ASSERT(correctNodeUFM (iBox j) (iBox p) t1 t2)
639 correctNodeUFM j p t1 t2
640 = correct (j-p) (j-1) p t1 && correct j ((j-1)+p) p t2
642 correct low high _ (LeafUFM i _)
643 = low <= iBox i && iBox i <= high
644 correct low high above_p (NodeUFM j p _ _)
645 = low <= iBox j && iBox j <= high && above_p > iBox p
646 correct _ _ _ EmptyUFM = panic "EmptyUFM stored inside a tree"
649 Note: doing SAT on this by hand seems to make it worse. Todo: Investigate,
650 and if necessary do $\lambda$ lifting on our functions that are bound.
660 insert_ele f EmptyUFM i new = mkLeafUFM i new
662 insert_ele f (LeafUFM j old) i new
664 mkLLNodeUFM (getCommonNodeUFMData
669 | j ==# i = mkLeafUFM j (f old new)
671 mkLLNodeUFM (getCommonNodeUFMData
677 insert_ele f n@(NodeUFM j p t1 t2) i a
679 = if (i >=# (j -# p))
680 then mkSLNodeUFM (NodeUFMData j p) (insert_ele f t1 i a) t2
681 else mkLLNodeUFM (getCommonNodeUFMData
687 = if (i <=# ((j -# _ILIT(1)) +# p))
688 then mkLSNodeUFM (NodeUFMData j p) t1 (insert_ele f t2 i a)
689 else mkLLNodeUFM (getCommonNodeUFMData
699 map_tree f (NodeUFM j p t1 t2)
700 = mkSSNodeUFM (NodeUFMData j p) (map_tree f t1) (map_tree f t2)
701 map_tree f (LeafUFM i obj)
702 = mkLeafUFM i (f obj)
704 map_tree f _ = panic "map_tree failed"
708 filter_tree f nd@(NodeUFM j p t1 t2)
709 = mkSSNodeUFM (NodeUFMData j p) (filter_tree f t1) (filter_tree f t2)
711 filter_tree f lf@(LeafUFM i obj)
713 | otherwise = EmptyUFM
714 filter_tree f _ = panic "filter_tree failed"
717 %************************************************************************
719 \subsubsection{The @UniqFM@ type, and signatures for the functions}
721 %************************************************************************
725 This is the information that is held inside a NodeUFM, packaged up for
730 = NodeUFMData FastInt
734 This is the information used when computing new NodeUFMs.
737 data Side = Leftt | Rightt -- NB: avoid 1.3 names "Left" and "Right"
739 = LeftRoot Side -- which side is the right down ?
740 | RightRoot Side -- which side is the left down ?
741 | SameRoot -- they are the same !
742 | NewRoot NodeUFMData -- here's the new, common, root
743 Bool -- do you need to swap left and right ?
746 This specifies the relationship between NodeUFMData and CalcNodeUFMData.
749 indexToRoot :: FastInt -> NodeUFMData
753 l = (_ILIT(1) :: FastInt)
755 NodeUFMData (((i `shiftR_` l) `shiftL_` l) +# _ILIT(1)) l
757 getCommonNodeUFMData :: NodeUFMData -> NodeUFMData -> NodeUFMData
759 getCommonNodeUFMData (NodeUFMData i p) (NodeUFMData i2 p2)
760 | p ==# p2 = getCommonNodeUFMData_ p j j2
761 | p <# p2 = getCommonNodeUFMData_ p2 (j `quotFastInt` (p2 `quotFastInt` p)) j2
762 | otherwise = getCommonNodeUFMData_ p j (j2 `quotFastInt` (p `quotFastInt` p2))
764 l = (_ILIT(1) :: FastInt)
765 j = i `quotFastInt` (p `shiftL_` l)
766 j2 = i2 `quotFastInt` (p2 `shiftL_` l)
768 getCommonNodeUFMData_ :: FastInt -> FastInt -> FastInt -> NodeUFMData
770 getCommonNodeUFMData_ p j j_
772 = NodeUFMData (((j `shiftL_` l) +# l) *# p) p
774 = getCommonNodeUFMData_ (p `shiftL_` l) (j `shiftR_` l) (j_ `shiftR_` l)
776 ask_about_common_ancestor :: NodeUFMData -> NodeUFMData -> CommonRoot
778 ask_about_common_ancestor x@(NodeUFMData j p) y@(NodeUFMData j2 p2)
779 | j ==# j2 = SameRoot
781 = case getCommonNodeUFMData x y of
782 nd@(NodeUFMData j3 p3)
783 | j3 ==# j -> LeftRoot (decideSide (j ># j2))
784 | j3 ==# j2 -> RightRoot (decideSide (j <# j2))
785 | otherwise -> NewRoot nd (j ># j2)
787 decideSide :: Bool -> Side
788 decideSide True = Leftt
789 decideSide False = Rightt
792 This might be better in Util.lhs ?
795 Now the bit twiddling functions.
797 shiftL_ :: FastInt -> FastInt -> FastInt
798 shiftR_ :: FastInt -> FastInt -> FastInt
800 #if __GLASGOW_HASKELL__
801 {-# INLINE shiftL_ #-}
802 {-# INLINE shiftR_ #-}
803 shiftL_ n p = word2Int#((int2Word# n) `shiftL#` p)
804 shiftR_ n p = word2Int#((int2Word# n) `shiftr` p)
806 shiftr x y = shiftRL# x y
809 shiftL_ n p = n * (2 ^ p)
810 shiftR_ n p = n `quot` (2 ^ p)
816 use_snd :: a -> b -> b