1 {-# OPTIONS -cpp -fglasgow-exts #-}
2 -----------------------------------------------------------------------------
4 -- Module : Data.IntSet
5 -- Copyright : (c) Daan Leijen 2002
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- An efficient implementation of integer sets.
13 -- This module is intended to be imported @qualified@, to avoid name
14 -- clashes with "Prelude" functions. eg.
16 -- > import Data.IntSet as Set
18 -- The implementation is based on /big-endian patricia trees/. This data
19 -- structure performs especially well on binary operations like 'union'
20 -- and 'intersection'. However, my benchmarks show that it is also
21 -- (much) faster on insertions and deletions when compared to a generic
22 -- size-balanced set implementation (see "Data.Set").
24 -- * Chris Okasaki and Andy Gill, \"/Fast Mergeable Integer Maps/\",
25 -- Workshop on ML, September 1998, pages 77-86,
26 -- <http://www.cse.ogi.edu/~andy/pub/finite.htm>
28 -- * D.R. Morrison, \"/PATRICIA -- Practical Algorithm To Retrieve
29 -- Information Coded In Alphanumeric/\", Journal of the ACM, 15(4),
30 -- October 1968, pages 514-534.
32 -- Many operations have a worst-case complexity of /O(min(n,W))/.
33 -- This means that the operation can become linear in the number of
34 -- elements with a maximum of /W/ -- the number of bits in an 'Int'
36 -----------------------------------------------------------------------------
40 IntSet -- instance Eq,Show
92 import Prelude hiding (lookup,filter,foldr,foldl,null,map)
96 import qualified Data.List as List
102 import List (nub,sort)
103 import qualified List
106 #if __GLASGOW_HASKELL__
108 import Data.Generics.Basics
109 import Data.Generics.Instances
112 #if __GLASGOW_HASKELL__ >= 503
114 import GHC.Exts ( Word(..), Int(..), shiftRL# )
115 #elif __GLASGOW_HASKELL__
117 import GlaExts ( Word(..), Int(..), shiftRL# )
122 infixl 9 \\{-This comment teaches CPP correct behaviour -}
124 -- A "Nat" is a natural machine word (an unsigned Int)
127 natFromInt :: Int -> Nat
128 natFromInt i = fromIntegral i
130 intFromNat :: Nat -> Int
131 intFromNat w = fromIntegral w
133 shiftRL :: Nat -> Int -> Nat
134 #if __GLASGOW_HASKELL__
135 {--------------------------------------------------------------------
136 GHC: use unboxing to get @shiftRL@ inlined.
137 --------------------------------------------------------------------}
138 shiftRL (W# x) (I# i)
141 shiftRL x i = shiftR x i
144 {--------------------------------------------------------------------
146 --------------------------------------------------------------------}
147 -- | /O(n+m)/. See 'difference'.
148 (\\) :: IntSet -> IntSet -> IntSet
149 m1 \\ m2 = difference m1 m2
151 {--------------------------------------------------------------------
153 --------------------------------------------------------------------}
154 -- | A set of integers.
156 | Tip {-# UNPACK #-} !Int
157 | Bin {-# UNPACK #-} !Prefix {-# UNPACK #-} !Mask !IntSet !IntSet
162 #if __GLASGOW_HASKELL__
164 {--------------------------------------------------------------------
166 --------------------------------------------------------------------}
168 -- This instance preserves data abstraction at the cost of inefficiency.
169 -- We omit reflection services for the sake of data abstraction.
171 instance Data IntSet where
172 gfoldl f z is = z fromList `f` (toList is)
173 toConstr _ = error "toConstr"
174 gunfold _ _ = error "gunfold"
175 dataTypeOf _ = mkNorepType "Data.IntSet.IntSet"
179 {--------------------------------------------------------------------
181 --------------------------------------------------------------------}
182 -- | /O(1)/. Is the set empty?
183 null :: IntSet -> Bool
187 -- | /O(n)/. Cardinality of the set.
188 size :: IntSet -> Int
191 Bin p m l r -> size l + size r
195 -- | /O(min(n,W))/. Is the value a member of the set?
196 member :: Int -> IntSet -> Bool
200 | nomatch x p m -> False
201 | zero x m -> member x l
202 | otherwise -> member x r
206 -- 'lookup' is used by 'intersection' for left-biasing
207 lookup :: Int -> IntSet -> Maybe Int
209 = let nk = natFromInt k in seq nk (lookupN nk t)
211 lookupN :: Nat -> IntSet -> Maybe Int
215 | zeroN k (natFromInt m) -> lookupN k l
216 | otherwise -> lookupN k r
218 | (k == natFromInt kx) -> Just kx
219 | otherwise -> Nothing
222 {--------------------------------------------------------------------
224 --------------------------------------------------------------------}
225 -- | /O(1)/. The empty set.
230 -- | /O(1)/. A set of one element.
231 singleton :: Int -> IntSet
235 {--------------------------------------------------------------------
237 --------------------------------------------------------------------}
238 -- | /O(min(n,W))/. Add a value to the set. When the value is already
239 -- an element of the set, it is replaced by the new one, ie. 'insert'
241 insert :: Int -> IntSet -> IntSet
245 | nomatch x p m -> join x (Tip x) p t
246 | zero x m -> Bin p m (insert x l) r
247 | otherwise -> Bin p m l (insert x r)
250 | otherwise -> join x (Tip x) y t
253 -- right-biased insertion, used by 'union'
254 insertR :: Int -> IntSet -> IntSet
258 | nomatch x p m -> join x (Tip x) p t
259 | zero x m -> Bin p m (insert x l) r
260 | otherwise -> Bin p m l (insert x r)
263 | otherwise -> join x (Tip x) y t
266 -- | /O(min(n,W))/. Delete a value in the set. Returns the
267 -- original set when the value was not present.
268 delete :: Int -> IntSet -> IntSet
273 | zero x m -> bin p m (delete x l) r
274 | otherwise -> bin p m l (delete x r)
281 {--------------------------------------------------------------------
283 --------------------------------------------------------------------}
284 -- | The union of a list of sets.
285 unions :: [IntSet] -> IntSet
287 = foldlStrict union empty xs
290 -- | /O(n+m)/. The union of two sets.
291 union :: IntSet -> IntSet -> IntSet
292 union t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
293 | shorter m1 m2 = union1
294 | shorter m2 m1 = union2
295 | p1 == p2 = Bin p1 m1 (union l1 l2) (union r1 r2)
296 | otherwise = join p1 t1 p2 t2
298 union1 | nomatch p2 p1 m1 = join p1 t1 p2 t2
299 | zero p2 m1 = Bin p1 m1 (union l1 t2) r1
300 | otherwise = Bin p1 m1 l1 (union r1 t2)
302 union2 | nomatch p1 p2 m2 = join p1 t1 p2 t2
303 | zero p1 m2 = Bin p2 m2 (union t1 l2) r2
304 | otherwise = Bin p2 m2 l2 (union t1 r2)
306 union (Tip x) t = insert x t
307 union t (Tip x) = insertR x t -- right bias
312 {--------------------------------------------------------------------
314 --------------------------------------------------------------------}
315 -- | /O(n+m)/. Difference between two sets.
316 difference :: IntSet -> IntSet -> IntSet
317 difference t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
318 | shorter m1 m2 = difference1
319 | shorter m2 m1 = difference2
320 | p1 == p2 = bin p1 m1 (difference l1 l2) (difference r1 r2)
323 difference1 | nomatch p2 p1 m1 = t1
324 | zero p2 m1 = bin p1 m1 (difference l1 t2) r1
325 | otherwise = bin p1 m1 l1 (difference r1 t2)
327 difference2 | nomatch p1 p2 m2 = t1
328 | zero p1 m2 = difference t1 l2
329 | otherwise = difference t1 r2
331 difference t1@(Tip x) t2
335 difference Nil t = Nil
336 difference t (Tip x) = delete x t
341 {--------------------------------------------------------------------
343 --------------------------------------------------------------------}
344 -- | /O(n+m)/. The intersection of two sets.
345 intersection :: IntSet -> IntSet -> IntSet
346 intersection t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
347 | shorter m1 m2 = intersection1
348 | shorter m2 m1 = intersection2
349 | p1 == p2 = bin p1 m1 (intersection l1 l2) (intersection r1 r2)
352 intersection1 | nomatch p2 p1 m1 = Nil
353 | zero p2 m1 = intersection l1 t2
354 | otherwise = intersection r1 t2
356 intersection2 | nomatch p1 p2 m2 = Nil
357 | zero p1 m2 = intersection t1 l2
358 | otherwise = intersection t1 r2
360 intersection t1@(Tip x) t2
363 intersection t (Tip x)
367 intersection Nil t = Nil
368 intersection t Nil = Nil
372 {--------------------------------------------------------------------
374 --------------------------------------------------------------------}
375 -- | /O(n+m)/. Is this a proper subset? (ie. a subset but not equal).
376 isProperSubsetOf :: IntSet -> IntSet -> Bool
377 isProperSubsetOf t1 t2
378 = case subsetCmp t1 t2 of
382 subsetCmp t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
384 | shorter m2 m1 = subsetCmpLt
385 | p1 == p2 = subsetCmpEq
386 | otherwise = GT -- disjoint
388 subsetCmpLt | nomatch p1 p2 m2 = GT
389 | zero p1 m2 = subsetCmp t1 l2
390 | otherwise = subsetCmp t1 r2
391 subsetCmpEq = case (subsetCmp l1 l2, subsetCmp r1 r2) of
397 subsetCmp (Bin p m l r) t = GT
398 subsetCmp (Tip x) (Tip y)
400 | otherwise = GT -- disjoint
403 | otherwise = GT -- disjoint
404 subsetCmp Nil Nil = EQ
407 -- | /O(n+m)/. Is this a subset?
408 -- @(s1 `isSubsetOf` s2)@ tells whether @s1@ is a subset of @s2@.
410 isSubsetOf :: IntSet -> IntSet -> Bool
411 isSubsetOf t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
412 | shorter m1 m2 = False
413 | shorter m2 m1 = match p1 p2 m2 && (if zero p1 m2 then isSubsetOf t1 l2
414 else isSubsetOf t1 r2)
415 | otherwise = (p1==p2) && isSubsetOf l1 l2 && isSubsetOf r1 r2
416 isSubsetOf (Bin p m l r) t = False
417 isSubsetOf (Tip x) t = member x t
418 isSubsetOf Nil t = True
421 {--------------------------------------------------------------------
423 --------------------------------------------------------------------}
424 -- | /O(n)/. Filter all elements that satisfy some predicate.
425 filter :: (Int -> Bool) -> IntSet -> IntSet
429 -> bin p m (filter pred l) (filter pred r)
435 -- | /O(n)/. partition the set according to some predicate.
436 partition :: (Int -> Bool) -> IntSet -> (IntSet,IntSet)
440 -> let (l1,l2) = partition pred l
441 (r1,r2) = partition pred r
442 in (bin p m l1 r1, bin p m l2 r2)
445 | otherwise -> (Nil,t)
449 -- | /O(log n)/. The expression (@'split' x set@) is a pair @(set1,set2)@
450 -- where all elements in @set1@ are lower than @x@ and all elements in
451 -- @set2@ larger than @x@.
453 -- > split 3 (fromList [1..5]) == (fromList [1,2], fromList [3,4])
454 split :: Int -> IntSet -> (IntSet,IntSet)
458 | zero x m -> let (lt,gt) = split x l in (lt,union gt r)
459 | otherwise -> let (lt,gt) = split x r in (union l lt,gt)
463 | otherwise -> (Nil,Nil)
466 -- | /O(log n)/. Performs a 'split' but also returns whether the pivot
467 -- element was found in the original set.
468 splitMember :: Int -> IntSet -> (IntSet,Bool,IntSet)
472 | zero x m -> let (lt,found,gt) = splitMember x l in (lt,found,union gt r)
473 | otherwise -> let (lt,found,gt) = splitMember x r in (union l lt,found,gt)
475 | x>y -> (t,False,Nil)
476 | x<y -> (Nil,False,t)
477 | otherwise -> (Nil,True,Nil)
478 Nil -> (Nil,False,Nil)
480 {----------------------------------------------------------------------
482 ----------------------------------------------------------------------}
484 -- | /O(n*min(n,W))/.
485 -- @'map' f s@ is the set obtained by applying @f@ to each element of @s@.
487 -- It's worth noting that the size of the result may be smaller if,
488 -- for some @(x,y)@, @x \/= y && f x == f y@
490 map :: (Int->Int) -> IntSet -> IntSet
491 map f = fromList . List.map f . toList
493 {--------------------------------------------------------------------
495 --------------------------------------------------------------------}
496 -- | /O(n)/. Fold over the elements of a set in an unspecified order.
498 -- > sum set == fold (+) 0 set
499 -- > elems set == fold (:) [] set
500 fold :: (Int -> b -> b) -> b -> IntSet -> b
504 foldr :: (Int -> b -> b) -> b -> IntSet -> b
507 Bin p m l r -> foldr f (foldr f z r) l
511 {--------------------------------------------------------------------
513 --------------------------------------------------------------------}
514 -- | /O(n)/. The elements of a set. (For sets, this is equivalent to toList)
515 elems :: IntSet -> [Int]
519 {--------------------------------------------------------------------
521 --------------------------------------------------------------------}
522 -- | /O(n)/. Convert the set to a list of elements.
523 toList :: IntSet -> [Int]
527 -- | /O(n)/. Convert the set to an ascending list of elements.
528 toAscList :: IntSet -> [Int]
530 = -- NOTE: the following algorithm only works for big-endian trees
531 let (pos,neg) = span (>=0) (foldr (:) [] t) in neg ++ pos
533 -- | /O(n*min(n,W))/. Create a set from a list of integers.
534 fromList :: [Int] -> IntSet
536 = foldlStrict ins empty xs
540 -- | /O(n*min(n,W))/. Build a set from an ascending list of elements.
541 fromAscList :: [Int] -> IntSet
545 -- | /O(n*min(n,W))/. Build a set from an ascending list of distinct elements.
546 fromDistinctAscList :: [Int] -> IntSet
547 fromDistinctAscList xs
551 {--------------------------------------------------------------------
553 --------------------------------------------------------------------}
554 instance Eq IntSet where
555 t1 == t2 = equal t1 t2
556 t1 /= t2 = nequal t1 t2
558 equal :: IntSet -> IntSet -> Bool
559 equal (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
560 = (m1 == m2) && (p1 == p2) && (equal l1 l2) && (equal r1 r2)
561 equal (Tip x) (Tip y)
566 nequal :: IntSet -> IntSet -> Bool
567 nequal (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
568 = (m1 /= m2) || (p1 /= p2) || (nequal l1 l2) || (nequal r1 r2)
569 nequal (Tip x) (Tip y)
571 nequal Nil Nil = False
574 {--------------------------------------------------------------------
576 --------------------------------------------------------------------}
578 instance Ord IntSet where
579 compare s1 s2 = compare (toAscList s1) (toAscList s2)
580 -- tentative implementation. See if more efficient exists.
582 {--------------------------------------------------------------------
584 --------------------------------------------------------------------}
585 instance Show IntSet where
586 showsPrec p xs = showParen (p > 10) $
587 showString "fromList " . shows (toList xs)
589 showSet :: [Int] -> ShowS
593 = showChar '{' . shows x . showTail xs
595 showTail [] = showChar '}'
596 showTail (x:xs) = showChar ',' . shows x . showTail xs
598 {--------------------------------------------------------------------
600 --------------------------------------------------------------------}
601 instance Read IntSet where
602 #ifdef __GLASGOW_HASKELL__
603 readPrec = parens $ prec 10 $ do
604 Ident "fromList" <- lexP
608 readListPrec = readListPrecDefault
610 readsPrec p = readParen (p > 10) $ \ r -> do
611 ("fromList",s) <- lex r
613 return (fromList xs,t)
616 {--------------------------------------------------------------------
618 --------------------------------------------------------------------}
620 #include "Typeable.h"
621 INSTANCE_TYPEABLE0(IntSet,intSetTc,"IntSet")
623 {--------------------------------------------------------------------
625 --------------------------------------------------------------------}
626 -- | /O(n)/. Show the tree that implements the set. The tree is shown
627 -- in a compressed, hanging format.
628 showTree :: IntSet -> String
630 = showTreeWith True False s
633 {- | /O(n)/. The expression (@'showTreeWith' hang wide map@) shows
634 the tree that implements the set. If @hang@ is
635 'True', a /hanging/ tree is shown otherwise a rotated tree is shown. If
636 @wide@ is 'True', an extra wide version is shown.
638 showTreeWith :: Bool -> Bool -> IntSet -> String
639 showTreeWith hang wide t
640 | hang = (showsTreeHang wide [] t) ""
641 | otherwise = (showsTree wide [] [] t) ""
643 showsTree :: Bool -> [String] -> [String] -> IntSet -> ShowS
644 showsTree wide lbars rbars t
647 -> showsTree wide (withBar rbars) (withEmpty rbars) r .
648 showWide wide rbars .
649 showsBars lbars . showString (showBin p m) . showString "\n" .
650 showWide wide lbars .
651 showsTree wide (withEmpty lbars) (withBar lbars) l
653 -> showsBars lbars . showString " " . shows x . showString "\n"
654 Nil -> showsBars lbars . showString "|\n"
656 showsTreeHang :: Bool -> [String] -> IntSet -> ShowS
657 showsTreeHang wide bars t
660 -> showsBars bars . showString (showBin p m) . showString "\n" .
662 showsTreeHang wide (withBar bars) l .
664 showsTreeHang wide (withEmpty bars) r
666 -> showsBars bars . showString " " . shows x . showString "\n"
667 Nil -> showsBars bars . showString "|\n"
670 = "*" -- ++ show (p,m)
673 | wide = showString (concat (reverse bars)) . showString "|\n"
676 showsBars :: [String] -> ShowS
680 _ -> showString (concat (reverse (tail bars))) . showString node
683 withBar bars = "| ":bars
684 withEmpty bars = " ":bars
687 {--------------------------------------------------------------------
689 --------------------------------------------------------------------}
690 {--------------------------------------------------------------------
692 --------------------------------------------------------------------}
693 join :: Prefix -> IntSet -> Prefix -> IntSet -> IntSet
695 | zero p1 m = Bin p m t1 t2
696 | otherwise = Bin p m t2 t1
701 {--------------------------------------------------------------------
702 @bin@ assures that we never have empty trees within a tree.
703 --------------------------------------------------------------------}
704 bin :: Prefix -> Mask -> IntSet -> IntSet -> IntSet
707 bin p m l r = Bin p m l r
710 {--------------------------------------------------------------------
711 Endian independent bit twiddling
712 --------------------------------------------------------------------}
713 zero :: Int -> Mask -> Bool
715 = (natFromInt i) .&. (natFromInt m) == 0
717 nomatch,match :: Int -> Prefix -> Mask -> Bool
724 mask :: Int -> Mask -> Prefix
726 = maskW (natFromInt i) (natFromInt m)
728 zeroN :: Nat -> Nat -> Bool
729 zeroN i m = (i .&. m) == 0
731 {--------------------------------------------------------------------
732 Big endian operations
733 --------------------------------------------------------------------}
734 maskW :: Nat -> Nat -> Prefix
736 = intFromNat (i .&. (complement (m-1) `xor` m))
738 shorter :: Mask -> Mask -> Bool
740 = (natFromInt m1) > (natFromInt m2)
742 branchMask :: Prefix -> Prefix -> Mask
744 = intFromNat (highestBitMask (natFromInt p1 `xor` natFromInt p2))
746 {----------------------------------------------------------------------
747 Finding the highest bit (mask) in a word [x] can be done efficiently in
749 * convert to a floating point value and the mantissa tells us the
750 [log2(x)] that corresponds with the highest bit position. The mantissa
751 is retrieved either via the standard C function [frexp] or by some bit
752 twiddling on IEEE compatible numbers (float). Note that one needs to
753 use at least [double] precision for an accurate mantissa of 32 bit
755 * use bit twiddling, a logarithmic sequence of bitwise or's and shifts (bit).
756 * use processor specific assembler instruction (asm).
758 The most portable way would be [bit], but is it efficient enough?
759 I have measured the cycle counts of the different methods on an AMD
760 Athlon-XP 1800 (~ Pentium III 1.8Ghz) using the RDTSC instruction:
762 highestBitMask: method cycles
769 highestBit: method cycles
776 Wow, the bit twiddling is on today's RISC like machines even faster
777 than a single CISC instruction (BSR)!
778 ----------------------------------------------------------------------}
780 {----------------------------------------------------------------------
781 [highestBitMask] returns a word where only the highest bit is set.
782 It is found by first setting all bits in lower positions than the
783 highest bit and than taking an exclusive or with the original value.
784 Allthough the function may look expensive, GHC compiles this into
785 excellent C code that subsequently compiled into highly efficient
786 machine code. The algorithm is derived from Jorg Arndt's FXT library.
787 ----------------------------------------------------------------------}
788 highestBitMask :: Nat -> Nat
790 = case (x .|. shiftRL x 1) of
791 x -> case (x .|. shiftRL x 2) of
792 x -> case (x .|. shiftRL x 4) of
793 x -> case (x .|. shiftRL x 8) of
794 x -> case (x .|. shiftRL x 16) of
795 x -> case (x .|. shiftRL x 32) of -- for 64 bit platforms
796 x -> (x `xor` (shiftRL x 1))
799 {--------------------------------------------------------------------
801 --------------------------------------------------------------------}
805 (x:xx) -> let z' = f z x in seq z' (foldlStrict f z' xx)
809 {--------------------------------------------------------------------
811 --------------------------------------------------------------------}
812 testTree :: [Int] -> IntSet
813 testTree xs = fromList xs
814 test1 = testTree [1..20]
815 test2 = testTree [30,29..10]
816 test3 = testTree [1,4,6,89,2323,53,43,234,5,79,12,9,24,9,8,423,8,42,4,8,9,3]
818 {--------------------------------------------------------------------
820 --------------------------------------------------------------------}
825 { configMaxTest = 500
826 , configMaxFail = 5000
827 , configSize = \n -> (div n 2 + 3)
828 , configEvery = \n args -> let s = show n in s ++ [ '\b' | _ <- s ]
832 {--------------------------------------------------------------------
833 Arbitrary, reasonably balanced trees
834 --------------------------------------------------------------------}
835 instance Arbitrary IntSet where
836 arbitrary = do{ xs <- arbitrary
837 ; return (fromList xs)
841 {--------------------------------------------------------------------
842 Single, Insert, Delete
843 --------------------------------------------------------------------}
844 prop_Single :: Int -> Bool
846 = (insert x empty == singleton x)
848 prop_InsertDelete :: Int -> IntSet -> Property
849 prop_InsertDelete k t
850 = not (member k t) ==> delete k (insert k t) == t
853 {--------------------------------------------------------------------
855 --------------------------------------------------------------------}
856 prop_UnionInsert :: Int -> IntSet -> Bool
858 = union t (singleton x) == insert x t
860 prop_UnionAssoc :: IntSet -> IntSet -> IntSet -> Bool
861 prop_UnionAssoc t1 t2 t3
862 = union t1 (union t2 t3) == union (union t1 t2) t3
864 prop_UnionComm :: IntSet -> IntSet -> Bool
866 = (union t1 t2 == union t2 t1)
868 prop_Diff :: [Int] -> [Int] -> Bool
870 = toAscList (difference (fromList xs) (fromList ys))
871 == List.sort ((List.\\) (nub xs) (nub ys))
873 prop_Int :: [Int] -> [Int] -> Bool
875 = toAscList (intersection (fromList xs) (fromList ys))
876 == List.sort (nub ((List.intersect) (xs) (ys)))
878 {--------------------------------------------------------------------
880 --------------------------------------------------------------------}
882 = forAll (choose (5,100)) $ \n ->
884 in fromAscList xs == fromList xs
886 prop_List :: [Int] -> Bool
888 = (sort (nub xs) == toAscList (fromList xs))