2 % (c) The AQUA Project, Glasgow University, 1994-1996
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 @uniqueOf@ 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,
39 lookupUFM, lookupUFM_Directly,
40 lookupWithDefaultUFM, lookupWithDefaultUFM_Directly,
46 #include "HsVersions.h"
48 import {-# SOURCE #-} Name ( Name )
50 import Unique ( Uniquable(..), Unique, u2i, mkUniqueGrimily )
52 import Outputable ( Outputable(..) )
53 import SrcLoc ( SrcLoc )
54 import GlaExts -- Lots of Int# operations
56 #if ! OMIT_NATIVE_CODEGEN
59 #define IF_NCG(a) {--}
63 %************************************************************************
65 \subsection{The @UniqFM@ type, and signatures for the functions}
67 %************************************************************************
69 We use @FiniteMaps@, with a (@uniqueOf@-able) @Unique@ as ``key''.
72 emptyUFM :: UniqFM elt
73 isNullUFM :: UniqFM elt -> Bool
74 unitUFM :: Uniquable key => key -> elt -> UniqFM elt
75 unitDirectlyUFM -- got the Unique already
76 :: Unique -> elt -> UniqFM elt
77 listToUFM :: Uniquable key => [(key,elt)] -> UniqFM elt
79 :: [(Unique, elt)] -> UniqFM elt
81 addToUFM :: Uniquable key => UniqFM elt -> key -> elt -> UniqFM elt
82 addListToUFM :: Uniquable key => UniqFM elt -> [(key,elt)] -> UniqFM elt
84 :: UniqFM elt -> Unique -> elt -> UniqFM elt
86 addToUFM_C :: Uniquable key => (elt -> elt -> elt)
87 -> UniqFM elt -> key -> elt -> UniqFM elt
88 addListToUFM_C :: Uniquable key => (elt -> elt -> elt)
89 -> UniqFM elt -> [(key,elt)]
92 delFromUFM :: Uniquable key => UniqFM elt -> key -> UniqFM elt
93 delListFromUFM :: Uniquable key => UniqFM elt -> [key] -> UniqFM elt
94 delFromUFM_Directly :: UniqFM elt -> Unique -> UniqFM elt
96 plusUFM :: UniqFM elt -> UniqFM elt -> UniqFM elt
98 plusUFM_C :: (elt -> elt -> elt)
99 -> UniqFM elt -> UniqFM elt -> UniqFM elt
101 minusUFM :: UniqFM elt -> UniqFM elt -> UniqFM elt
103 intersectUFM :: UniqFM elt -> UniqFM elt -> UniqFM elt
104 intersectUFM_C :: (elt -> elt -> elt)
105 -> UniqFM elt -> UniqFM elt -> UniqFM elt
106 foldUFM :: (elt -> a -> a) -> a -> UniqFM elt -> a
107 mapUFM :: (elt1 -> elt2) -> UniqFM elt1 -> UniqFM elt2
108 filterUFM :: (elt -> Bool) -> UniqFM elt -> UniqFM elt
110 sizeUFM :: UniqFM elt -> Int
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 #ifdef __GLASGOW_HASKELL__
133 -- I don't think HBC was too happy about this (WDP 94/10)
136 addListToUFM :: UniqFM elt -> [(Name, elt)] -> UniqFM elt
139 addListToUFM_C :: (elt -> elt -> elt) -> UniqFM elt -> [(Name, elt)] -> UniqFM elt
142 addToUFM :: UniqFM elt -> Unique -> elt -> UniqFM elt
145 listToUFM :: [(Unique, elt)] -> UniqFM elt
148 lookupUFM :: UniqFM elt -> Name -> Maybe elt
149 , UniqFM elt -> Unique -> Maybe elt
152 #endif {- __GLASGOW_HASKELL__ -}
155 %************************************************************************
157 \subsection{Andy Gill's underlying @UniqFM@ machinery}
159 %************************************************************************
161 ``Uniq Finite maps'' are the heart and soul of the compiler's
162 lookup-tables/environments. Important stuff! It works well with
163 Dense and Sparse ranges.
164 Both @Uq@ Finite maps and @Hash@ Finite Maps
165 are built ontop of Int Finite Maps.
167 This code is explained in the paper:
169 A Gill, S Peyton Jones, B O'Sullivan, W Partain and Aqua Friends
170 "A Cheap balancing act that grows on a tree"
171 Glasgow FP Workshop, Sep 1994, pp??-??
174 %************************************************************************
176 \subsubsection{The @UniqFM@ type, and signatures for the functions}
178 %************************************************************************
180 @UniqFM a@ is a mapping from Unique to a.
182 First, the DataType itself; which is either a Node, a Leaf, or an Empty.
187 | LeafUFM FAST_INT ele
188 | NodeUFM FAST_INT -- the switching
189 FAST_INT -- the delta
193 -- for debugging only :-)
195 instance Text (UniqFM a) where
196 showsPrec _ (NodeUFM a b t1 t2) =
197 showString "NodeUFM " . shows (IBOX(a))
198 . showString " " . shows (IBOX(b))
199 . showString " (" . shows t1
200 . showString ") (" . shows t2
202 showsPrec _ (LeafUFM x a) = showString "LeafUFM " . shows (IBOX(x))
203 showsPrec _ (EmptyUFM) = id
207 %************************************************************************
209 \subsubsection{The @UniqFM@ functions}
211 %************************************************************************
213 First the ways of building a UniqFM.
217 unitUFM key elt = mkLeafUFM (u2i (uniqueOf key)) elt
218 unitDirectlyUFM key elt = mkLeafUFM (u2i key) elt
220 listToUFM key_elt_pairs
221 = addListToUFM_C use_snd EmptyUFM key_elt_pairs
223 listToUFM_Directly uniq_elt_pairs
224 = addListToUFM_directly_C use_snd EmptyUFM uniq_elt_pairs
227 Now ways of adding things to UniqFMs.
229 There is an alternative version of @addListToUFM_C@, that uses @plusUFM@,
230 but the semantics of this operation demands a linear insertion;
231 perhaps the version without the combinator function
232 could be optimised using it.
235 addToUFM fm key elt = addToUFM_C use_snd fm key elt
237 addToUFM_Directly fm u elt = insert_ele use_snd fm (u2i u) elt
239 addToUFM_C combiner fm key elt
240 = insert_ele combiner fm (u2i (uniqueOf key)) elt
242 addListToUFM fm key_elt_pairs = addListToUFM_C use_snd fm key_elt_pairs
243 addListToUFM_Directly fm uniq_elt_pairs = addListToUFM_directly_C use_snd fm uniq_elt_pairs
245 addListToUFM_C combiner fm key_elt_pairs
246 = foldl (\ fm (k, e) -> insert_ele combiner fm (u2i (uniqueOf k)) e)
249 addListToUFM_directly_C combiner fm uniq_elt_pairs
250 = foldl (\ fm (k, e) -> insert_ele combiner fm (u2i k) e)
254 Now ways of removing things from UniqFM.
257 delListFromUFM fm lst = foldl delFromUFM fm lst
259 delFromUFM fm key = delete fm (u2i (uniqueOf key))
260 delFromUFM_Directly fm u = delete fm (u2i u)
262 delete EmptyUFM _ = EmptyUFM
263 delete fm key = del_ele fm
265 del_ele :: UniqFM a -> UniqFM a
267 del_ele lf@(LeafUFM j _)
268 | j _EQ_ key = EmptyUFM
269 | otherwise = lf -- no delete!
271 del_ele nd@(NodeUFM j p t1 t2)
273 = mkSLNodeUFM (NodeUFMData j p) (del_ele t1) t2
275 = mkLSNodeUFM (NodeUFMData j p) t1 (del_ele t2)
277 del_ele _ = panic "Found EmptyUFM FM when rec-deleting"
280 Now ways of adding two UniqFM's together.
283 plusUFM tr1 tr2 = plusUFM_C use_snd tr1 tr2
285 plusUFM_C f EmptyUFM tr = tr
286 plusUFM_C f tr EmptyUFM = tr
287 plusUFM_C f fm1 fm2 = mix_trees fm1 fm2
289 mix_trees (LeafUFM i a) t2 = insert_ele (flip f) t2 i a
290 mix_trees t1 (LeafUFM i a) = insert_ele f t1 i a
292 mix_trees left_t@(NodeUFM j p t1 t2) right_t@(NodeUFM j' p' t1' t2')
294 (ask_about_common_ancestor
298 -- Given a disjoint j,j' (p >^ p' && p' >^ p):
302 -- t1 t2 t1' t2' j j'
307 mix_branches (NewRoot nd False)
308 = mkLLNodeUFM nd left_t right_t
309 mix_branches (NewRoot nd True)
310 = mkLLNodeUFM nd right_t left_t
316 -- t1 t2 t1' t2' t1 + t1' t2 + t2'
318 mix_branches (SameRoot)
319 = mkSSNodeUFM (NodeUFMData j p)
322 -- Now the 4 different other ways; all like this:
324 -- Given j >^ j' (and, say, j > j')
328 -- t1 t2 t1' t2' t1 t2 + j'
331 mix_branches (LeftRoot Leftt) -- | trace "LL" True
334 (mix_trees t1 right_t)
337 mix_branches (LeftRoot Rightt) -- | trace "LR" True
341 (mix_trees t2 right_t)
343 mix_branches (RightRoot Leftt) -- | trace "RL" True
346 (mix_trees left_t t1')
349 mix_branches (RightRoot Rightt) -- | trace "RR" True
353 (mix_trees left_t t2')
355 mix_trees _ _ = panic "EmptyUFM found when inserting into plusInt"
358 And ways of subtracting them. First the base cases,
359 then the full D&C approach.
362 minusUFM EmptyUFM _ = EmptyUFM
363 minusUFM t1 EmptyUFM = t1
364 minusUFM fm1 fm2 = minus_trees fm1 fm2
367 -- Notice the asymetry of subtraction
369 minus_trees lf@(LeafUFM i a) t2 =
374 minus_trees t1 (LeafUFM i _) = delete t1 i
376 minus_trees left_t@(NodeUFM j p t1 t2) right_t@(NodeUFM j' p' t1' t2')
378 (ask_about_common_ancestor
382 -- Given a disjoint j,j' (p >^ p' && p' >^ p):
386 -- t1 t2 t1' t2' t1 t2
391 minus_branches (NewRoot nd _) = left_t
397 -- t1 t2 t1' t2' t1 + t1' t2 + t2'
399 minus_branches (SameRoot)
400 = mkSSNodeUFM (NodeUFMData j p)
403 -- Now the 4 different other ways; all like this:
404 -- again, with asymatry
407 -- The left is above the right
409 minus_branches (LeftRoot Leftt)
412 (minus_trees t1 right_t)
414 minus_branches (LeftRoot Rightt)
418 (minus_trees t2 right_t)
421 -- The right is above the left
423 minus_branches (RightRoot Leftt)
424 = minus_trees left_t t1'
425 minus_branches (RightRoot Rightt)
426 = minus_trees left_t t2'
428 minus_trees _ _ = panic "EmptyUFM found when insering into plusInt"
431 And taking the intersection of two UniqFM's.
434 intersectUFM t1 t2 = intersectUFM_C use_snd t1 t2
436 intersectUFM_C f EmptyUFM _ = EmptyUFM
437 intersectUFM_C f _ EmptyUFM = EmptyUFM
438 intersectUFM_C f fm1 fm2 = intersect_trees fm1 fm2
440 intersect_trees (LeafUFM i a) t2 =
443 Just b -> mkLeafUFM i (f a b)
445 intersect_trees t1 (LeafUFM i a) =
448 Just b -> mkLeafUFM i (f b a)
450 intersect_trees left_t@(NodeUFM j p t1 t2) right_t@(NodeUFM j' p' t1' t2')
452 (ask_about_common_ancestor
456 -- Given a disjoint j,j' (p >^ p' && p' >^ p):
459 -- / \ + / \ ==> EmptyUFM
464 intersect_branches (NewRoot nd _) = EmptyUFM
470 -- t1 t2 t1' t2' t1 x t1' t2 x t2'
472 intersect_branches (SameRoot)
473 = mkSSNodeUFM (NodeUFMData j p)
474 (intersect_trees t1 t1')
475 (intersect_trees t2 t2')
476 -- Now the 4 different other ways; all like this:
478 -- Given j >^ j' (and, say, j > j')
482 -- t1 t2 t1' t2' t1' t2'
484 -- This does cut down the search space quite a bit.
486 intersect_branches (LeftRoot Leftt)
487 = intersect_trees t1 right_t
488 intersect_branches (LeftRoot Rightt)
489 = intersect_trees t2 right_t
490 intersect_branches (RightRoot Leftt)
491 = intersect_trees left_t t1'
492 intersect_branches (RightRoot Rightt)
493 = intersect_trees left_t t2'
495 intersect_trees x y = panic ("EmptyUFM found when intersecting trees")
498 Now the usual set of `collection' operators, like map, fold, etc.
501 foldUFM f a (NodeUFM _ _ t1 t2) = foldUFM f (foldUFM f a t2) t1
502 foldUFM f a (LeafUFM _ obj) = f obj a
503 foldUFM f a EmptyUFM = a
507 mapUFM fn EmptyUFM = EmptyUFM
508 mapUFM fn fm = map_tree fn fm
510 filterUFM fn EmptyUFM = EmptyUFM
511 filterUFM fn fm = filter_tree fn fm
514 Note, this takes a long time, O(n), but
515 because we dont want to do this very often, we put up with this.
516 O'rable, but how often do we look at the size of
521 sizeUFM (NodeUFM _ _ t1 t2) = sizeUFM t1 + sizeUFM t2
522 sizeUFM (LeafUFM _ _) = 1
524 isNullUFM EmptyUFM = True
528 looking up in a hurry is the {\em whole point} of this binary tree lark.
529 Lookup up a binary tree is easy (and fast).
532 lookupUFM fm key = lookUp fm (u2i (uniqueOf key))
533 lookupUFM_Directly fm key = lookUp fm (u2i key)
535 lookupWithDefaultUFM fm deflt key
536 = case lookUp fm (u2i (uniqueOf key)) of
540 lookupWithDefaultUFM_Directly fm deflt key
541 = case lookUp fm (u2i key) of
545 lookUp EmptyUFM _ = Nothing
546 lookUp fm i = lookup_tree fm
548 lookup_tree :: UniqFM a -> Maybe a
550 lookup_tree (LeafUFM j b)
552 | otherwise = Nothing
553 lookup_tree (NodeUFM j p t1 t2)
554 | j _GT_ i = lookup_tree t1
555 | otherwise = lookup_tree t2
557 lookup_tree EmptyUFM = panic "lookup Failed"
560 folds are *wonderful* things.
563 eltsUFM fm = foldUFM (:) [] fm
565 ufmToList fm = fold_tree (\ iu elt rest -> (mkUniqueGrimily iu, elt) : rest) [] fm
567 keysUFM fm = fold_tree (\ iu elt rest -> IBOX(iu) : rest) [] fm
569 fold_tree f a (NodeUFM _ _ t1 t2) = fold_tree f (fold_tree f a t2) t1
570 fold_tree f a (LeafUFM iu obj) = f iu obj a
571 fold_tree f a EmptyUFM = a
574 %************************************************************************
576 \subsubsection{The @UniqFM@ type, and its functions}
578 %************************************************************************
580 You should always use these to build the tree.
581 There are 4 versions of mkNodeUFM, depending on
582 the strictness of the two sub-tree arguments.
583 The strictness is used *both* to prune out
584 empty trees, *and* to improve performance,
585 stoping needless thunks lying around.
586 The rule of thumb (from experence with these trees)
587 is make thunks strict, but data structures lazy.
588 If in doubt, use mkSSNodeUFM, which has the `strongest'
589 functionality, but may do a few needless evaluations.
592 mkLeafUFM :: FAST_INT -> a -> UniqFM a
593 mkLeafUFM i a = LeafUFM i a
595 -- The *ONLY* ways of building a NodeUFM.
597 mkSSNodeUFM (NodeUFMData j p) EmptyUFM t2 = t2
598 mkSSNodeUFM (NodeUFMData j p) t1 EmptyUFM = t1
599 mkSSNodeUFM (NodeUFMData j p) t1 t2
600 = ASSERT(correctNodeUFM (IBOX(j)) (IBOX(p)) t1 t2)
603 mkSLNodeUFM (NodeUFMData j p) EmptyUFM t2 = t2
604 mkSLNodeUFM (NodeUFMData j p) t1 t2
605 = ASSERT(correctNodeUFM (IBOX(j)) (IBOX(p)) t1 t2)
608 mkLSNodeUFM (NodeUFMData j p) t1 EmptyUFM = t1
609 mkLSNodeUFM (NodeUFMData j p) t1 t2
610 = ASSERT(correctNodeUFM (IBOX(j)) (IBOX(p)) t1 t2)
613 mkLLNodeUFM (NodeUFMData j p) t1 t2
614 = ASSERT(correctNodeUFM (IBOX(j)) (IBOX(p)) t1 t2)
624 correctNodeUFM j p t1 t2
625 = correct (j-p) (j-1) p t1 && correct j ((j-1)+p) p t2
627 correct low high _ (LeafUFM i _)
628 = low <= IBOX(i) && IBOX(i) <= high
629 correct low high above_p (NodeUFM j p _ _)
630 = low <= IBOX(j) && IBOX(j) <= high && above_p > IBOX(p)
631 correct _ _ _ EmptyUFM = panic "EmptyUFM stored inside a tree"
634 Note: doing SAT on this by hand seems to make it worse. Todo: Investigate,
635 and if necessary do $\lambda$ lifting on our functions that are bound.
645 insert_ele f EmptyUFM i new = mkLeafUFM i new
647 insert_ele f (LeafUFM j old) i new
649 mkLLNodeUFM (getCommonNodeUFMData
654 | j _EQ_ i = mkLeafUFM j (f old new)
656 mkLLNodeUFM (getCommonNodeUFMData
662 insert_ele f n@(NodeUFM j p t1 t2) i a
664 = if (i _GE_ (j _SUB_ p))
665 then mkSLNodeUFM (NodeUFMData j p) (insert_ele f t1 i a) t2
666 else mkLLNodeUFM (getCommonNodeUFMData
672 = if (i _LE_ ((j _SUB_ ILIT(1)) _ADD_ p))
673 then mkLSNodeUFM (NodeUFMData j p) t1 (insert_ele f t2 i a)
674 else mkLLNodeUFM (getCommonNodeUFMData
684 map_tree f (NodeUFM j p t1 t2)
685 = mkSSNodeUFM (NodeUFMData j p) (map_tree f t1) (map_tree f t2)
686 map_tree f (LeafUFM i obj)
687 = mkLeafUFM i (f obj)
689 map_tree f _ = panic "map_tree failed"
693 filter_tree f nd@(NodeUFM j p t1 t2)
694 = mkSSNodeUFM (NodeUFMData j p) (filter_tree f t1) (filter_tree f t2)
696 filter_tree f lf@(LeafUFM i obj)
698 | otherwise = EmptyUFM
699 filter_tree f _ = panic "filter_tree failed"
702 %************************************************************************
704 \subsubsection{The @UniqFM@ type, and signatures for the functions}
706 %************************************************************************
710 This is the information that is held inside a NodeUFM, packaged up for
715 = NodeUFMData FAST_INT
719 This is the information used when computing new NodeUFMs.
722 data Side = Leftt | Rightt -- NB: avoid 1.3 names "Left" and "Right"
724 = LeftRoot Side -- which side is the right down ?
725 | RightRoot Side -- which side is the left down ?
726 | SameRoot -- they are the same !
727 | NewRoot NodeUFMData -- here's the new, common, root
728 Bool -- do you need to swap left and right ?
731 This specifies the relationship between NodeUFMData and CalcNodeUFMData.
734 indexToRoot :: FAST_INT -> NodeUFMData
738 l = (ILIT(1) :: FAST_INT)
740 NodeUFMData (((i `shiftR_` l) `shiftL_` l) _ADD_ ILIT(1)) l
742 getCommonNodeUFMData :: NodeUFMData -> NodeUFMData -> NodeUFMData
744 getCommonNodeUFMData (NodeUFMData i p) (NodeUFMData i2 p2)
745 | p _EQ_ p2 = getCommonNodeUFMData_ p j j2
746 | p _LT_ p2 = getCommonNodeUFMData_ p2 (j _QUOT_ (p2 _QUOT_ p)) j2
747 | otherwise = getCommonNodeUFMData_ p j (j2 _QUOT_ (p _QUOT_ p2))
749 l = (ILIT(1) :: FAST_INT)
750 j = i _QUOT_ (p `shiftL_` l)
751 j2 = i2 _QUOT_ (p2 `shiftL_` l)
753 getCommonNodeUFMData_ :: FAST_INT -> FAST_INT -> FAST_INT -> NodeUFMData
755 getCommonNodeUFMData_ p j j_
757 = NodeUFMData (((j `shiftL_` l) _ADD_ l) _MUL_ p) p
759 = getCommonNodeUFMData_ (p `shiftL_` l) (j `shiftR_` l) (j_ `shiftR_` l)
761 ask_about_common_ancestor :: NodeUFMData -> NodeUFMData -> CommonRoot
763 ask_about_common_ancestor x@(NodeUFMData j p) y@(NodeUFMData j2 p2)
764 | j _EQ_ j2 = SameRoot
766 = case getCommonNodeUFMData x y of
767 nd@(NodeUFMData j3 p3)
768 | j3 _EQ_ j -> LeftRoot (decideSide (j _GT_ j2))
769 | j3 _EQ_ j2 -> RightRoot (decideSide (j _LT_ j2))
770 | otherwise -> NewRoot nd (j _GT_ j2)
772 decideSide :: Bool -> Side
773 decideSide True = Leftt
774 decideSide False = Rightt
777 This might be better in Util.lhs ?
780 Now the bit twiddling functions.
782 shiftL_ :: FAST_INT -> FAST_INT -> FAST_INT
783 shiftR_ :: FAST_INT -> FAST_INT -> FAST_INT
785 #if __GLASGOW_HASKELL__
786 {-# INLINE shiftL_ #-}
787 {-# INLINE shiftR_ #-}
788 shiftL_ n p = word2Int#((int2Word# n) `shiftL#` p)
789 shiftR_ n p = word2Int#((int2Word# n) `shiftr` p)
791 shiftr x y = shiftRA# x y
794 shiftL_ n p = n * (2 ^ p)
795 shiftR_ n p = n `quot` (2 ^ p)
801 use_snd :: a -> b -> b