1 {-# OPTIONS -cpp -fglasgow-exts #-}
2 -----------------------------------------------------------------------------
3 -- Module : Data.IntMap
4 -- Copyright : (c) Daan Leijen 2002
6 -- Maintainer : libraries@haskell.org
7 -- Stability : provisional
8 -- Portability : portable
10 -- An efficient implementation of maps from integer keys to values.
12 -- This module is intended to be imported @qualified@, to avoid name
13 -- clashes with "Prelude" functions. eg.
15 -- > import Data.IntMap as Map
17 -- The implementation is based on /big-endian patricia trees/. This data
18 -- structure performs especially well on binary operations like 'union'
19 -- and 'intersection'. However, my benchmarks show that it is also
20 -- (much) faster on insertions and deletions when compared to a generic
21 -- size-balanced map implementation (see "Data.Map" and "Data.FiniteMap").
23 -- * Chris Okasaki and Andy Gill, \"/Fast Mergeable Integer Maps/\",
24 -- Workshop on ML, September 1998, pages 77-86,
25 -- <http://www.cse.ogi.edu/~andy/pub/finite.htm>
27 -- * D.R. Morrison, \"/PATRICIA -- Practical Algorithm To Retrieve
28 -- Information Coded In Alphanumeric/\", Journal of the ACM, 15(4),
29 -- October 1968, pages 514-534.
31 -- Many operations have a worst-case complexity of /O(min(n,W))/.
32 -- This means that the operation can become linear in the number of
33 -- elements with a maximum of /W/ -- the number of bits in an 'Int'
35 -----------------------------------------------------------------------------
39 IntMap, Key -- instance Eq,Show
57 , insertWith, insertWithKey, insertLookupWithKey
114 , fromDistinctAscList
126 , isSubmapOf, isSubmapOfBy
127 , isProperSubmapOf, isProperSubmapOfBy
135 import Prelude hiding (lookup,map,filter,foldr,foldl,null)
138 import qualified Data.IntSet as IntSet
139 import Data.Monoid (Monoid(..))
141 import Data.Foldable (Foldable(foldMap))
145 import qualified Prelude
146 import Debug.QuickCheck
147 import List (nub,sort)
148 import qualified List
151 #if __GLASGOW_HASKELL__
153 import Data.Generics.Basics
154 import Data.Generics.Instances
157 #if __GLASGOW_HASKELL__ >= 503
159 import GHC.Exts ( Word(..), Int(..), shiftRL# )
160 #elif __GLASGOW_HASKELL__
162 import GlaExts ( Word(..), Int(..), shiftRL# )
167 infixl 9 \\{-This comment teaches CPP correct behaviour -}
169 -- A "Nat" is a natural machine word (an unsigned Int)
172 natFromInt :: Key -> Nat
173 natFromInt i = fromIntegral i
175 intFromNat :: Nat -> Key
176 intFromNat w = fromIntegral w
178 shiftRL :: Nat -> Key -> Nat
179 #if __GLASGOW_HASKELL__
180 {--------------------------------------------------------------------
181 GHC: use unboxing to get @shiftRL@ inlined.
182 --------------------------------------------------------------------}
183 shiftRL (W# x) (I# i)
186 shiftRL x i = shiftR x i
189 {--------------------------------------------------------------------
191 --------------------------------------------------------------------}
193 -- | /O(min(n,W))/. Find the value at a key.
194 -- Calls 'error' when the element can not be found.
196 (!) :: IntMap a -> Key -> a
199 -- | /O(n+m)/. See 'difference'.
200 (\\) :: IntMap a -> IntMap b -> IntMap a
201 m1 \\ m2 = difference m1 m2
203 {--------------------------------------------------------------------
205 --------------------------------------------------------------------}
206 -- | A map of integers to values @a@.
208 | Tip {-# UNPACK #-} !Key a
209 | Bin {-# UNPACK #-} !Prefix {-# UNPACK #-} !Mask !(IntMap a) !(IntMap a)
215 instance Ord a => Monoid (IntMap a) where
220 instance Foldable IntMap where
221 foldMap f Nil = mempty
222 foldMap f (Tip _k v) = f v
223 foldMap f (Bin _ _ l r) = foldMap f l `mappend` foldMap f r
225 #if __GLASGOW_HASKELL__
227 {--------------------------------------------------------------------
229 --------------------------------------------------------------------}
231 -- This instance preserves data abstraction at the cost of inefficiency.
232 -- We omit reflection services for the sake of data abstraction.
234 instance Data a => Data (IntMap a) where
235 gfoldl f z im = z fromList `f` (toList im)
236 toConstr _ = error "toConstr"
237 gunfold _ _ = error "gunfold"
238 dataTypeOf _ = mkNorepType "Data.IntMap.IntMap"
242 {--------------------------------------------------------------------
244 --------------------------------------------------------------------}
245 -- | /O(1)/. Is the map empty?
246 null :: IntMap a -> Bool
250 -- | /O(n)/. Number of elements in the map.
251 size :: IntMap a -> Int
254 Bin p m l r -> size l + size r
258 -- | /O(min(n,W))/. Is the key a member of the map?
259 member :: Key -> IntMap a -> Bool
265 -- | /O(min(n,W))/. Lookup the value at a key in the map.
266 lookup :: Key -> IntMap a -> Maybe a
268 = let nk = natFromInt k in seq nk (lookupN nk t)
270 lookupN :: Nat -> IntMap a -> Maybe a
274 | zeroN k (natFromInt m) -> lookupN k l
275 | otherwise -> lookupN k r
277 | (k == natFromInt kx) -> Just x
278 | otherwise -> Nothing
281 find' :: Key -> IntMap a -> a
284 Nothing -> error ("IntMap.find: key " ++ show k ++ " is not an element of the map")
288 -- | /O(min(n,W))/. The expression @('findWithDefault' def k map)@
289 -- returns the value at key @k@ or returns @def@ when the key is not an
290 -- element of the map.
291 findWithDefault :: a -> Key -> IntMap a -> a
292 findWithDefault def k m
297 {--------------------------------------------------------------------
299 --------------------------------------------------------------------}
300 -- | /O(1)/. The empty map.
305 -- | /O(1)/. A map of one element.
306 singleton :: Key -> a -> IntMap a
310 {--------------------------------------------------------------------
312 --------------------------------------------------------------------}
313 -- | /O(min(n,W))/. Insert a new key\/value pair in the map.
314 -- If the key is already present in the map, the associated value is
315 -- replaced with the supplied value, i.e. 'insert' is equivalent to
316 -- @'insertWith' 'const'@.
317 insert :: Key -> a -> IntMap a -> IntMap a
321 | nomatch k p m -> join k (Tip k x) p t
322 | zero k m -> Bin p m (insert k x l) r
323 | otherwise -> Bin p m l (insert k x r)
326 | otherwise -> join k (Tip k x) ky t
329 -- right-biased insertion, used by 'union'
330 -- | /O(min(n,W))/. Insert with a combining function.
331 -- @'insertWith' f key value mp@
332 -- will insert the pair (key, value) into @mp@ if key does
333 -- not exist in the map. If the key does exist, the function will
334 -- insert @f new_value old_value@.
335 insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
337 = insertWithKey (\k x y -> f x y) k x t
339 -- | /O(min(n,W))/. Insert with a combining function.
340 -- @'insertWithKey' f key value mp@
341 -- will insert the pair (key, value) into @mp@ if key does
342 -- not exist in the map. If the key does exist, the function will
343 -- insert @f key new_value old_value@.
344 insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
345 insertWithKey f k x t
348 | nomatch k p m -> join k (Tip k x) p t
349 | zero k m -> Bin p m (insertWithKey f k x l) r
350 | otherwise -> Bin p m l (insertWithKey f k x r)
352 | k==ky -> Tip k (f k x y)
353 | otherwise -> join k (Tip k x) ky t
357 -- | /O(min(n,W))/. The expression (@'insertLookupWithKey' f k x map@)
358 -- is a pair where the first element is equal to (@'lookup' k map@)
359 -- and the second element equal to (@'insertWithKey' f k x map@).
360 insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)
361 insertLookupWithKey f k x t
364 | nomatch k p m -> (Nothing,join k (Tip k x) p t)
365 | zero k m -> let (found,l') = insertLookupWithKey f k x l in (found,Bin p m l' r)
366 | otherwise -> let (found,r') = insertLookupWithKey f k x r in (found,Bin p m l r')
368 | k==ky -> (Just y,Tip k (f k x y))
369 | otherwise -> (Nothing,join k (Tip k x) ky t)
370 Nil -> (Nothing,Tip k x)
373 {--------------------------------------------------------------------
375 [delete] is the inlined version of [deleteWith (\k x -> Nothing)]
376 --------------------------------------------------------------------}
377 -- | /O(min(n,W))/. Delete a key and its value from the map. When the key is not
378 -- a member of the map, the original map is returned.
379 delete :: Key -> IntMap a -> IntMap a
384 | zero k m -> bin p m (delete k l) r
385 | otherwise -> bin p m l (delete k r)
391 -- | /O(min(n,W))/. Adjust a value at a specific key. When the key is not
392 -- a member of the map, the original map is returned.
393 adjust :: (a -> a) -> Key -> IntMap a -> IntMap a
395 = adjustWithKey (\k x -> f x) k m
397 -- | /O(min(n,W))/. Adjust a value at a specific key. When the key is not
398 -- a member of the map, the original map is returned.
399 adjustWithKey :: (Key -> a -> a) -> Key -> IntMap a -> IntMap a
401 = updateWithKey (\k x -> Just (f k x)) k m
403 -- | /O(min(n,W))/. The expression (@'update' f k map@) updates the value @x@
404 -- at @k@ (if it is in the map). If (@f x@) is 'Nothing', the element is
405 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@.
406 update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap a
408 = updateWithKey (\k x -> f x) k m
410 -- | /O(min(n,W))/. The expression (@'update' f k map@) updates the value @x@
411 -- at @k@ (if it is in the map). If (@f k x@) is 'Nothing', the element is
412 -- deleted. If it is (@'Just' y@), the key @k@ is bound to the new value @y@.
413 updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
418 | zero k m -> bin p m (updateWithKey f k l) r
419 | otherwise -> bin p m l (updateWithKey f k r)
421 | k==ky -> case (f k y) of
427 -- | /O(min(n,W))/. Lookup and update.
428 updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a,IntMap a)
429 updateLookupWithKey f k t
432 | nomatch k p m -> (Nothing,t)
433 | zero k m -> let (found,l') = updateLookupWithKey f k l in (found,bin p m l' r)
434 | otherwise -> let (found,r') = updateLookupWithKey f k r in (found,bin p m l r')
436 | k==ky -> case (f k y) of
437 Just y' -> (Just y,Tip ky y')
438 Nothing -> (Just y,Nil)
439 | otherwise -> (Nothing,t)
443 {--------------------------------------------------------------------
445 --------------------------------------------------------------------}
446 -- | The union of a list of maps.
447 unions :: [IntMap a] -> IntMap a
449 = foldlStrict union empty xs
451 -- | The union of a list of maps, with a combining operation
452 unionsWith :: (a->a->a) -> [IntMap a] -> IntMap a
454 = foldlStrict (unionWith f) empty ts
456 -- | /O(n+m)/. The (left-biased) union of two maps.
457 -- It prefers the first map when duplicate keys are encountered,
458 -- i.e. (@'union' == 'unionWith' 'const'@).
459 union :: IntMap a -> IntMap a -> IntMap a
460 union t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
461 | shorter m1 m2 = union1
462 | shorter m2 m1 = union2
463 | p1 == p2 = Bin p1 m1 (union l1 l2) (union r1 r2)
464 | otherwise = join p1 t1 p2 t2
466 union1 | nomatch p2 p1 m1 = join p1 t1 p2 t2
467 | zero p2 m1 = Bin p1 m1 (union l1 t2) r1
468 | otherwise = Bin p1 m1 l1 (union r1 t2)
470 union2 | nomatch p1 p2 m2 = join p1 t1 p2 t2
471 | zero p1 m2 = Bin p2 m2 (union t1 l2) r2
472 | otherwise = Bin p2 m2 l2 (union t1 r2)
474 union (Tip k x) t = insert k x t
475 union t (Tip k x) = insertWith (\x y -> y) k x t -- right bias
479 -- | /O(n+m)/. The union with a combining function.
480 unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
482 = unionWithKey (\k x y -> f x y) m1 m2
484 -- | /O(n+m)/. The union with a combining function.
485 unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
486 unionWithKey f t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
487 | shorter m1 m2 = union1
488 | shorter m2 m1 = union2
489 | p1 == p2 = Bin p1 m1 (unionWithKey f l1 l2) (unionWithKey f r1 r2)
490 | otherwise = join p1 t1 p2 t2
492 union1 | nomatch p2 p1 m1 = join p1 t1 p2 t2
493 | zero p2 m1 = Bin p1 m1 (unionWithKey f l1 t2) r1
494 | otherwise = Bin p1 m1 l1 (unionWithKey f r1 t2)
496 union2 | nomatch p1 p2 m2 = join p1 t1 p2 t2
497 | zero p1 m2 = Bin p2 m2 (unionWithKey f t1 l2) r2
498 | otherwise = Bin p2 m2 l2 (unionWithKey f t1 r2)
500 unionWithKey f (Tip k x) t = insertWithKey f k x t
501 unionWithKey f t (Tip k x) = insertWithKey (\k x y -> f k y x) k x t -- right bias
502 unionWithKey f Nil t = t
503 unionWithKey f t Nil = t
505 {--------------------------------------------------------------------
507 --------------------------------------------------------------------}
508 -- | /O(n+m)/. Difference between two maps (based on keys).
509 difference :: IntMap a -> IntMap b -> IntMap a
510 difference t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
511 | shorter m1 m2 = difference1
512 | shorter m2 m1 = difference2
513 | p1 == p2 = bin p1 m1 (difference l1 l2) (difference r1 r2)
516 difference1 | nomatch p2 p1 m1 = t1
517 | zero p2 m1 = bin p1 m1 (difference l1 t2) r1
518 | otherwise = bin p1 m1 l1 (difference r1 t2)
520 difference2 | nomatch p1 p2 m2 = t1
521 | zero p1 m2 = difference t1 l2
522 | otherwise = difference t1 r2
524 difference t1@(Tip k x) t2
528 difference Nil t = Nil
529 difference t (Tip k x) = delete k t
532 -- | /O(n+m)/. Difference with a combining function.
533 differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
534 differenceWith f m1 m2
535 = differenceWithKey (\k x y -> f x y) m1 m2
537 -- | /O(n+m)/. Difference with a combining function. When two equal keys are
538 -- encountered, the combining function is applied to the key and both values.
539 -- If it returns 'Nothing', the element is discarded (proper set difference).
540 -- If it returns (@'Just' y@), the element is updated with a new value @y@.
541 differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
542 differenceWithKey f t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
543 | shorter m1 m2 = difference1
544 | shorter m2 m1 = difference2
545 | p1 == p2 = bin p1 m1 (differenceWithKey f l1 l2) (differenceWithKey f r1 r2)
548 difference1 | nomatch p2 p1 m1 = t1
549 | zero p2 m1 = bin p1 m1 (differenceWithKey f l1 t2) r1
550 | otherwise = bin p1 m1 l1 (differenceWithKey f r1 t2)
552 difference2 | nomatch p1 p2 m2 = t1
553 | zero p1 m2 = differenceWithKey f t1 l2
554 | otherwise = differenceWithKey f t1 r2
556 differenceWithKey f t1@(Tip k x) t2
557 = case lookup k t2 of
558 Just y -> case f k x y of
563 differenceWithKey f Nil t = Nil
564 differenceWithKey f t (Tip k y) = updateWithKey (\k x -> f k x y) k t
565 differenceWithKey f t Nil = t
568 {--------------------------------------------------------------------
570 --------------------------------------------------------------------}
571 -- | /O(n+m)/. The (left-biased) intersection of two maps (based on keys).
572 intersection :: IntMap a -> IntMap b -> IntMap a
573 intersection t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
574 | shorter m1 m2 = intersection1
575 | shorter m2 m1 = intersection2
576 | p1 == p2 = bin p1 m1 (intersection l1 l2) (intersection r1 r2)
579 intersection1 | nomatch p2 p1 m1 = Nil
580 | zero p2 m1 = intersection l1 t2
581 | otherwise = intersection r1 t2
583 intersection2 | nomatch p1 p2 m2 = Nil
584 | zero p1 m2 = intersection t1 l2
585 | otherwise = intersection t1 r2
587 intersection t1@(Tip k x) t2
590 intersection t (Tip k x)
594 intersection Nil t = Nil
595 intersection t Nil = Nil
597 -- | /O(n+m)/. The intersection with a combining function.
598 intersectionWith :: (a -> b -> a) -> IntMap a -> IntMap b -> IntMap a
599 intersectionWith f m1 m2
600 = intersectionWithKey (\k x y -> f x y) m1 m2
602 -- | /O(n+m)/. The intersection with a combining function.
603 intersectionWithKey :: (Key -> a -> b -> a) -> IntMap a -> IntMap b -> IntMap a
604 intersectionWithKey f t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
605 | shorter m1 m2 = intersection1
606 | shorter m2 m1 = intersection2
607 | p1 == p2 = bin p1 m1 (intersectionWithKey f l1 l2) (intersectionWithKey f r1 r2)
610 intersection1 | nomatch p2 p1 m1 = Nil
611 | zero p2 m1 = intersectionWithKey f l1 t2
612 | otherwise = intersectionWithKey f r1 t2
614 intersection2 | nomatch p1 p2 m2 = Nil
615 | zero p1 m2 = intersectionWithKey f t1 l2
616 | otherwise = intersectionWithKey f t1 r2
618 intersectionWithKey f t1@(Tip k x) t2
619 = case lookup k t2 of
620 Just y -> Tip k (f k x y)
622 intersectionWithKey f t1 (Tip k y)
623 = case lookup k t1 of
624 Just x -> Tip k (f k x y)
626 intersectionWithKey f Nil t = Nil
627 intersectionWithKey f t Nil = Nil
630 {--------------------------------------------------------------------
632 --------------------------------------------------------------------}
633 -- | /O(n+m)/. Is this a proper submap? (ie. a submap but not equal).
634 -- Defined as (@'isProperSubmapOf' = 'isProperSubmapOfBy' (==)@).
635 isProperSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool
636 isProperSubmapOf m1 m2
637 = isProperSubmapOfBy (==) m1 m2
639 {- | /O(n+m)/. Is this a proper submap? (ie. a submap but not equal).
640 The expression (@'isProperSubmapOfBy' f m1 m2@) returns 'True' when
641 @m1@ and @m2@ are not equal,
642 all keys in @m1@ are in @m2@, and when @f@ returns 'True' when
643 applied to their respective values. For example, the following
644 expressions are all 'True':
646 > isProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
647 > isProperSubmapOfBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
649 But the following are all 'False':
651 > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
652 > isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
653 > isProperSubmapOfBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
655 isProperSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
656 isProperSubmapOfBy pred t1 t2
657 = case submapCmp pred t1 t2 of
661 submapCmp pred t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
663 | shorter m2 m1 = submapCmpLt
664 | p1 == p2 = submapCmpEq
665 | otherwise = GT -- disjoint
667 submapCmpLt | nomatch p1 p2 m2 = GT
668 | zero p1 m2 = submapCmp pred t1 l2
669 | otherwise = submapCmp pred t1 r2
670 submapCmpEq = case (submapCmp pred l1 l2, submapCmp pred r1 r2) of
676 submapCmp pred (Bin p m l r) t = GT
677 submapCmp pred (Tip kx x) (Tip ky y)
678 | (kx == ky) && pred x y = EQ
679 | otherwise = GT -- disjoint
680 submapCmp pred (Tip k x) t
682 Just y | pred x y -> LT
683 other -> GT -- disjoint
684 submapCmp pred Nil Nil = EQ
685 submapCmp pred Nil t = LT
687 -- | /O(n+m)/. Is this a submap?
688 -- Defined as (@'isSubmapOf' = 'isSubmapOfBy' (==)@).
689 isSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool
691 = isSubmapOfBy (==) m1 m2
694 The expression (@'isSubmapOfBy' f m1 m2@) returns 'True' if
695 all keys in @m1@ are in @m2@, and when @f@ returns 'True' when
696 applied to their respective values. For example, the following
697 expressions are all 'True':
699 > isSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
700 > isSubmapOfBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
701 > isSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
703 But the following are all 'False':
705 > isSubmapOfBy (==) (fromList [(1,2)]) (fromList [(1,1),(2,2)])
706 > isSubmapOfBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
707 > isSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
710 isSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
711 isSubmapOfBy pred t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
712 | shorter m1 m2 = False
713 | shorter m2 m1 = match p1 p2 m2 && (if zero p1 m2 then isSubmapOfBy pred t1 l2
714 else isSubmapOfBy pred t1 r2)
715 | otherwise = (p1==p2) && isSubmapOfBy pred l1 l2 && isSubmapOfBy pred r1 r2
716 isSubmapOfBy pred (Bin p m l r) t = False
717 isSubmapOfBy pred (Tip k x) t = case lookup k t of
720 isSubmapOfBy pred Nil t = True
722 {--------------------------------------------------------------------
724 --------------------------------------------------------------------}
725 -- | /O(n)/. Map a function over all values in the map.
726 map :: (a -> b) -> IntMap a -> IntMap b
728 = mapWithKey (\k x -> f x) m
730 -- | /O(n)/. Map a function over all values in the map.
731 mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap b
734 Bin p m l r -> Bin p m (mapWithKey f l) (mapWithKey f r)
735 Tip k x -> Tip k (f k x)
738 -- | /O(n)/. The function @'mapAccum'@ threads an accumulating
739 -- argument through the map in ascending order of keys.
740 mapAccum :: (a -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
742 = mapAccumWithKey (\a k x -> f a x) a m
744 -- | /O(n)/. The function @'mapAccumWithKey'@ threads an accumulating
745 -- argument through the map in ascending order of keys.
746 mapAccumWithKey :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
747 mapAccumWithKey f a t
750 -- | /O(n)/. The function @'mapAccumL'@ threads an accumulating
751 -- argument through the map in ascending order of keys.
752 mapAccumL :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
755 Bin p m l r -> let (a1,l') = mapAccumL f a l
756 (a2,r') = mapAccumL f a1 r
757 in (a2,Bin p m l' r')
758 Tip k x -> let (a',x') = f a k x in (a',Tip k x')
762 -- | /O(n)/. The function @'mapAccumR'@ threads an accumulating
763 -- argument throught the map in descending order of keys.
764 mapAccumR :: (a -> Key -> b -> (a,c)) -> a -> IntMap b -> (a,IntMap c)
767 Bin p m l r -> let (a1,r') = mapAccumR f a r
768 (a2,l') = mapAccumR f a1 l
769 in (a2,Bin p m l' r')
770 Tip k x -> let (a',x') = f a k x in (a',Tip k x')
773 {--------------------------------------------------------------------
775 --------------------------------------------------------------------}
776 -- | /O(n)/. Filter all values that satisfy some predicate.
777 filter :: (a -> Bool) -> IntMap a -> IntMap a
779 = filterWithKey (\k x -> p x) m
781 -- | /O(n)/. Filter all keys\/values that satisfy some predicate.
782 filterWithKey :: (Key -> a -> Bool) -> IntMap a -> IntMap a
786 -> bin p m (filterWithKey pred l) (filterWithKey pred r)
792 -- | /O(n)/. partition the map according to some predicate. The first
793 -- map contains all elements that satisfy the predicate, the second all
794 -- elements that fail the predicate. See also 'split'.
795 partition :: (a -> Bool) -> IntMap a -> (IntMap a,IntMap a)
797 = partitionWithKey (\k x -> p x) m
799 -- | /O(n)/. partition the map according to some predicate. The first
800 -- map contains all elements that satisfy the predicate, the second all
801 -- elements that fail the predicate. See also 'split'.
802 partitionWithKey :: (Key -> a -> Bool) -> IntMap a -> (IntMap a,IntMap a)
803 partitionWithKey pred t
806 -> let (l1,l2) = partitionWithKey pred l
807 (r1,r2) = partitionWithKey pred r
808 in (bin p m l1 r1, bin p m l2 r2)
810 | pred k x -> (t,Nil)
811 | otherwise -> (Nil,t)
815 -- | /O(log n)/. The expression (@'split' k map@) is a pair @(map1,map2)@
816 -- where all keys in @map1@ are lower than @k@ and all keys in
817 -- @map2@ larger than @k@. Any key equal to @k@ is found in neither @map1@ nor @map2@.
818 split :: Key -> IntMap a -> (IntMap a,IntMap a)
822 | m < 0 -> (if k >= 0 -- handle negative numbers.
823 then let (lt,gt) = split' k l in (union r lt, gt)
824 else let (lt,gt) = split' k r in (lt, union gt l))
825 | otherwise -> split' k t
829 | otherwise -> (Nil,Nil)
832 split' :: Key -> IntMap a -> (IntMap a,IntMap a)
836 | nomatch k p m -> if k>p then (t,Nil) else (Nil,t)
837 | zero k m -> let (lt,gt) = split k l in (lt,union gt r)
838 | otherwise -> let (lt,gt) = split k r in (union l lt,gt)
842 | otherwise -> (Nil,Nil)
845 -- | /O(log n)/. Performs a 'split' but also returns whether the pivot
846 -- key was found in the original map.
847 splitLookup :: Key -> IntMap a -> (IntMap a,Maybe a,IntMap a)
851 | m < 0 -> (if k >= 0 -- handle negative numbers.
852 then let (lt,found,gt) = splitLookup' k l in (union r lt,found, gt)
853 else let (lt,found,gt) = splitLookup' k r in (lt,found, union gt l))
854 | otherwise -> splitLookup' k t
856 | k>ky -> (t,Nothing,Nil)
857 | k<ky -> (Nil,Nothing,t)
858 | otherwise -> (Nil,Just y,Nil)
859 Nil -> (Nil,Nothing,Nil)
861 splitLookup' :: Key -> IntMap a -> (IntMap a,Maybe a,IntMap a)
865 | nomatch k p m -> if k>p then (t,Nothing,Nil) else (Nil,Nothing,t)
866 | zero k m -> let (lt,found,gt) = splitLookup k l in (lt,found,union gt r)
867 | otherwise -> let (lt,found,gt) = splitLookup k r in (union l lt,found,gt)
869 | k>ky -> (t,Nothing,Nil)
870 | k<ky -> (Nil,Nothing,t)
871 | otherwise -> (Nil,Just y,Nil)
872 Nil -> (Nil,Nothing,Nil)
874 {--------------------------------------------------------------------
876 --------------------------------------------------------------------}
877 -- | /O(n)/. Fold the values in the map, such that
878 -- @'fold' f z == 'Prelude.foldr' f z . 'elems'@.
881 -- > elems map = fold (:) [] map
883 fold :: (a -> b -> b) -> b -> IntMap a -> b
885 = foldWithKey (\k x y -> f x y) z t
887 -- | /O(n)/. Fold the keys and values in the map, such that
888 -- @'foldWithKey' f z == 'Prelude.foldr' ('uncurry' f) z . 'toAscList'@.
891 -- > keys map = foldWithKey (\k x ks -> k:ks) [] map
893 foldWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> b
897 foldr :: (Key -> a -> b -> b) -> b -> IntMap a -> b
900 Bin 0 m l r | m < 0 -> foldr' f (foldr' f z l) r -- put negative numbers before.
901 Bin _ _ _ _ -> foldr' f z t
905 foldr' :: (Key -> a -> b -> b) -> b -> IntMap a -> b
908 Bin p m l r -> foldr' f (foldr' f z r) l
914 {--------------------------------------------------------------------
916 --------------------------------------------------------------------}
918 -- Return all elements of the map in the ascending order of their keys.
919 elems :: IntMap a -> [a]
921 = foldWithKey (\k x xs -> x:xs) [] m
923 -- | /O(n)/. Return all keys of the map in ascending order.
924 keys :: IntMap a -> [Key]
926 = foldWithKey (\k x ks -> k:ks) [] m
928 -- | /O(n*min(n,W))/. The set of all keys of the map.
929 keysSet :: IntMap a -> IntSet.IntSet
930 keysSet m = IntSet.fromDistinctAscList (keys m)
933 -- | /O(n)/. Return all key\/value pairs in the map in ascending key order.
934 assocs :: IntMap a -> [(Key,a)]
939 {--------------------------------------------------------------------
941 --------------------------------------------------------------------}
942 -- | /O(n)/. Convert the map to a list of key\/value pairs.
943 toList :: IntMap a -> [(Key,a)]
945 = foldWithKey (\k x xs -> (k,x):xs) [] t
947 -- | /O(n)/. Convert the map to a list of key\/value pairs where the
948 -- keys are in ascending order.
949 toAscList :: IntMap a -> [(Key,a)]
951 = -- NOTE: the following algorithm only works for big-endian trees
952 let (pos,neg) = span (\(k,x) -> k >=0) (foldr (\k x xs -> (k,x):xs) [] t) in neg ++ pos
954 -- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs.
955 fromList :: [(Key,a)] -> IntMap a
957 = foldlStrict ins empty xs
959 ins t (k,x) = insert k x t
961 -- | /O(n*min(n,W))/. Create a map from a list of key\/value pairs with a combining function. See also 'fromAscListWith'.
962 fromListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a
964 = fromListWithKey (\k x y -> f x y) xs
966 -- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs with a combining function. See also fromAscListWithKey'.
967 fromListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
969 = foldlStrict ins empty xs
971 ins t (k,x) = insertWithKey f k x t
973 -- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs where
974 -- the keys are in ascending order.
975 fromAscList :: [(Key,a)] -> IntMap a
979 -- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs where
980 -- the keys are in ascending order, with a combining function on equal keys.
981 fromAscListWith :: (a -> a -> a) -> [(Key,a)] -> IntMap a
985 -- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs where
986 -- the keys are in ascending order, with a combining function on equal keys.
987 fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key,a)] -> IntMap a
988 fromAscListWithKey f xs
989 = fromListWithKey f xs
991 -- | /O(n*min(n,W))/. Build a map from a list of key\/value pairs where
992 -- the keys are in ascending order and all distinct.
993 fromDistinctAscList :: [(Key,a)] -> IntMap a
994 fromDistinctAscList xs
998 {--------------------------------------------------------------------
1000 --------------------------------------------------------------------}
1001 instance Eq a => Eq (IntMap a) where
1002 t1 == t2 = equal t1 t2
1003 t1 /= t2 = nequal t1 t2
1005 equal :: Eq a => IntMap a -> IntMap a -> Bool
1006 equal (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
1007 = (m1 == m2) && (p1 == p2) && (equal l1 l2) && (equal r1 r2)
1008 equal (Tip kx x) (Tip ky y)
1009 = (kx == ky) && (x==y)
1010 equal Nil Nil = True
1013 nequal :: Eq a => IntMap a -> IntMap a -> Bool
1014 nequal (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
1015 = (m1 /= m2) || (p1 /= p2) || (nequal l1 l2) || (nequal r1 r2)
1016 nequal (Tip kx x) (Tip ky y)
1017 = (kx /= ky) || (x/=y)
1018 nequal Nil Nil = False
1021 {--------------------------------------------------------------------
1023 --------------------------------------------------------------------}
1025 instance Ord a => Ord (IntMap a) where
1026 compare m1 m2 = compare (toList m1) (toList m2)
1028 {--------------------------------------------------------------------
1030 --------------------------------------------------------------------}
1032 instance Functor IntMap where
1035 {--------------------------------------------------------------------
1037 --------------------------------------------------------------------}
1039 instance Show a => Show (IntMap a) where
1040 showsPrec d m = showParen (d > 10) $
1041 showString "fromList " . shows (toList m)
1043 showMap :: (Show a) => [(Key,a)] -> ShowS
1047 = showChar '{' . showElem x . showTail xs
1049 showTail [] = showChar '}'
1050 showTail (x:xs) = showChar ',' . showElem x . showTail xs
1052 showElem (k,x) = shows k . showString ":=" . shows x
1054 {--------------------------------------------------------------------
1056 --------------------------------------------------------------------}
1057 instance (Read e) => Read (IntMap e) where
1058 #ifdef __GLASGOW_HASKELL__
1059 readPrec = parens $ prec 10 $ do
1060 Ident "fromList" <- lexP
1062 return (fromList xs)
1064 readListPrec = readListPrecDefault
1066 readsPrec p = readParen (p > 10) $ \ r -> do
1067 ("fromList",s) <- lex r
1069 return (fromList xs,t)
1072 {--------------------------------------------------------------------
1074 --------------------------------------------------------------------}
1076 #include "Typeable.h"
1077 INSTANCE_TYPEABLE1(IntMap,intMapTc,"IntMap")
1079 {--------------------------------------------------------------------
1081 --------------------------------------------------------------------}
1082 -- | /O(n)/. Show the tree that implements the map. The tree is shown
1083 -- in a compressed, hanging format.
1084 showTree :: Show a => IntMap a -> String
1086 = showTreeWith True False s
1089 {- | /O(n)/. The expression (@'showTreeWith' hang wide map@) shows
1090 the tree that implements the map. If @hang@ is
1091 'True', a /hanging/ tree is shown otherwise a rotated tree is shown. If
1092 @wide@ is 'True', an extra wide version is shown.
1094 showTreeWith :: Show a => Bool -> Bool -> IntMap a -> String
1095 showTreeWith hang wide t
1096 | hang = (showsTreeHang wide [] t) ""
1097 | otherwise = (showsTree wide [] [] t) ""
1099 showsTree :: Show a => Bool -> [String] -> [String] -> IntMap a -> ShowS
1100 showsTree wide lbars rbars t
1103 -> showsTree wide (withBar rbars) (withEmpty rbars) r .
1104 showWide wide rbars .
1105 showsBars lbars . showString (showBin p m) . showString "\n" .
1106 showWide wide lbars .
1107 showsTree wide (withEmpty lbars) (withBar lbars) l
1109 -> showsBars lbars . showString " " . shows k . showString ":=" . shows x . showString "\n"
1110 Nil -> showsBars lbars . showString "|\n"
1112 showsTreeHang :: Show a => Bool -> [String] -> IntMap a -> ShowS
1113 showsTreeHang wide bars t
1116 -> showsBars bars . showString (showBin p m) . showString "\n" .
1117 showWide wide bars .
1118 showsTreeHang wide (withBar bars) l .
1119 showWide wide bars .
1120 showsTreeHang wide (withEmpty bars) r
1122 -> showsBars bars . showString " " . shows k . showString ":=" . shows x . showString "\n"
1123 Nil -> showsBars bars . showString "|\n"
1126 = "*" -- ++ show (p,m)
1129 | wide = showString (concat (reverse bars)) . showString "|\n"
1132 showsBars :: [String] -> ShowS
1136 _ -> showString (concat (reverse (tail bars))) . showString node
1139 withBar bars = "| ":bars
1140 withEmpty bars = " ":bars
1143 {--------------------------------------------------------------------
1145 --------------------------------------------------------------------}
1146 {--------------------------------------------------------------------
1148 --------------------------------------------------------------------}
1149 join :: Prefix -> IntMap a -> Prefix -> IntMap a -> IntMap a
1151 | zero p1 m = Bin p m t1 t2
1152 | otherwise = Bin p m t2 t1
1154 m = branchMask p1 p2
1157 {--------------------------------------------------------------------
1158 @bin@ assures that we never have empty trees within a tree.
1159 --------------------------------------------------------------------}
1160 bin :: Prefix -> Mask -> IntMap a -> IntMap a -> IntMap a
1163 bin p m l r = Bin p m l r
1166 {--------------------------------------------------------------------
1167 Endian independent bit twiddling
1168 --------------------------------------------------------------------}
1169 zero :: Key -> Mask -> Bool
1171 = (natFromInt i) .&. (natFromInt m) == 0
1173 nomatch,match :: Key -> Prefix -> Mask -> Bool
1180 mask :: Key -> Mask -> Prefix
1182 = maskW (natFromInt i) (natFromInt m)
1185 zeroN :: Nat -> Nat -> Bool
1186 zeroN i m = (i .&. m) == 0
1188 {--------------------------------------------------------------------
1189 Big endian operations
1190 --------------------------------------------------------------------}
1191 maskW :: Nat -> Nat -> Prefix
1193 = intFromNat (i .&. (complement (m-1) `xor` m))
1195 shorter :: Mask -> Mask -> Bool
1197 = (natFromInt m1) > (natFromInt m2)
1199 branchMask :: Prefix -> Prefix -> Mask
1201 = intFromNat (highestBitMask (natFromInt p1 `xor` natFromInt p2))
1203 {----------------------------------------------------------------------
1204 Finding the highest bit (mask) in a word [x] can be done efficiently in
1206 * convert to a floating point value and the mantissa tells us the
1207 [log2(x)] that corresponds with the highest bit position. The mantissa
1208 is retrieved either via the standard C function [frexp] or by some bit
1209 twiddling on IEEE compatible numbers (float). Note that one needs to
1210 use at least [double] precision for an accurate mantissa of 32 bit
1212 * use bit twiddling, a logarithmic sequence of bitwise or's and shifts (bit).
1213 * use processor specific assembler instruction (asm).
1215 The most portable way would be [bit], but is it efficient enough?
1216 I have measured the cycle counts of the different methods on an AMD
1217 Athlon-XP 1800 (~ Pentium III 1.8Ghz) using the RDTSC instruction:
1219 highestBitMask: method cycles
1226 highestBit: method cycles
1233 Wow, the bit twiddling is on today's RISC like machines even faster
1234 than a single CISC instruction (BSR)!
1235 ----------------------------------------------------------------------}
1237 {----------------------------------------------------------------------
1238 [highestBitMask] returns a word where only the highest bit is set.
1239 It is found by first setting all bits in lower positions than the
1240 highest bit and than taking an exclusive or with the original value.
1241 Allthough the function may look expensive, GHC compiles this into
1242 excellent C code that subsequently compiled into highly efficient
1243 machine code. The algorithm is derived from Jorg Arndt's FXT library.
1244 ----------------------------------------------------------------------}
1245 highestBitMask :: Nat -> Nat
1247 = case (x .|. shiftRL x 1) of
1248 x -> case (x .|. shiftRL x 2) of
1249 x -> case (x .|. shiftRL x 4) of
1250 x -> case (x .|. shiftRL x 8) of
1251 x -> case (x .|. shiftRL x 16) of
1252 x -> case (x .|. shiftRL x 32) of -- for 64 bit platforms
1253 x -> (x `xor` (shiftRL x 1))
1256 {--------------------------------------------------------------------
1258 --------------------------------------------------------------------}
1262 (x:xx) -> let z' = f z x in seq z' (foldlStrict f z' xx)
1265 {--------------------------------------------------------------------
1267 --------------------------------------------------------------------}
1268 testTree :: [Int] -> IntMap Int
1269 testTree xs = fromList [(x,x*x*30696 `mod` 65521) | x <- xs]
1270 test1 = testTree [1..20]
1271 test2 = testTree [30,29..10]
1272 test3 = testTree [1,4,6,89,2323,53,43,234,5,79,12,9,24,9,8,423,8,42,4,8,9,3]
1274 {--------------------------------------------------------------------
1276 --------------------------------------------------------------------}
1281 { configMaxTest = 500
1282 , configMaxFail = 5000
1283 , configSize = \n -> (div n 2 + 3)
1284 , configEvery = \n args -> let s = show n in s ++ [ '\b' | _ <- s ]
1288 {--------------------------------------------------------------------
1289 Arbitrary, reasonably balanced trees
1290 --------------------------------------------------------------------}
1291 instance Arbitrary a => Arbitrary (IntMap a) where
1292 arbitrary = do{ ks <- arbitrary
1293 ; xs <- mapM (\k -> do{ x <- arbitrary; return (k,x)}) ks
1294 ; return (fromList xs)
1298 {--------------------------------------------------------------------
1299 Single, Insert, Delete
1300 --------------------------------------------------------------------}
1301 prop_Single :: Key -> Int -> Bool
1303 = (insert k x empty == singleton k x)
1305 prop_InsertDelete :: Key -> Int -> IntMap Int -> Property
1306 prop_InsertDelete k x t
1307 = not (member k t) ==> delete k (insert k x t) == t
1309 prop_UpdateDelete :: Key -> IntMap Int -> Bool
1310 prop_UpdateDelete k t
1311 = update (const Nothing) k t == delete k t
1314 {--------------------------------------------------------------------
1316 --------------------------------------------------------------------}
1317 prop_UnionInsert :: Key -> Int -> IntMap Int -> Bool
1318 prop_UnionInsert k x t
1319 = union (singleton k x) t == insert k x t
1321 prop_UnionAssoc :: IntMap Int -> IntMap Int -> IntMap Int -> Bool
1322 prop_UnionAssoc t1 t2 t3
1323 = union t1 (union t2 t3) == union (union t1 t2) t3
1325 prop_UnionComm :: IntMap Int -> IntMap Int -> Bool
1326 prop_UnionComm t1 t2
1327 = (union t1 t2 == unionWith (\x y -> y) t2 t1)
1330 prop_Diff :: [(Key,Int)] -> [(Key,Int)] -> Bool
1332 = List.sort (keys (difference (fromListWith (+) xs) (fromListWith (+) ys)))
1333 == List.sort ((List.\\) (nub (Prelude.map fst xs)) (nub (Prelude.map fst ys)))
1335 prop_Int :: [(Key,Int)] -> [(Key,Int)] -> Bool
1337 = List.sort (keys (intersection (fromListWith (+) xs) (fromListWith (+) ys)))
1338 == List.sort (nub ((List.intersect) (Prelude.map fst xs) (Prelude.map fst ys)))
1340 {--------------------------------------------------------------------
1342 --------------------------------------------------------------------}
1344 = forAll (choose (5,100)) $ \n ->
1345 let xs = [(x,()) | x <- [0..n::Int]]
1346 in fromAscList xs == fromList xs
1348 prop_List :: [Key] -> Bool
1350 = (sort (nub xs) == [x | (x,()) <- toAscList (fromList [(x,()) | x <- xs])])