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
107 #ifdef __GLASGOW_HASKELL__
108 {--------------------------------------------------------------------
109 GHC: use unboxing to get @shiftRL@ inlined.
110 --------------------------------------------------------------------}
111 #if __GLASGOW_HASKELL__ >= 503
113 import GHC.Exts ( Word(..), Int(..), shiftRL# )
116 import GlaExts ( Word(..), Int(..), shiftRL# )
119 infixl 9 \\{-This comment teaches CPP correct behaviour -}
123 natFromInt :: Int -> Nat
124 natFromInt i = fromIntegral i
126 intFromNat :: Nat -> Int
127 intFromNat w = fromIntegral w
129 shiftRL :: Nat -> Int -> Nat
130 shiftRL (W# x) (I# i)
134 {--------------------------------------------------------------------
136 * raises errors on boundary values when using 'fromIntegral'
137 but not with the deprecated 'fromInt/toInt'.
138 * Older Hugs doesn't define 'Word'.
139 * Newer Hugs defines 'Word' in the Prelude but no operations.
140 --------------------------------------------------------------------}
142 infixl 9 \\ -- comment to fool cpp
144 type Nat = Word32 -- illegal on 64-bit platforms!
146 natFromInt :: Int -> Nat
147 natFromInt i = fromInt i
149 intFromNat :: Nat -> Int
150 intFromNat w = toInt w
152 shiftRL :: Nat -> Int -> Nat
153 shiftRL x i = shiftR x i
156 {--------------------------------------------------------------------
158 * A "Nat" is a natural machine word (an unsigned Int)
159 --------------------------------------------------------------------}
161 infixl 9 \\ -- comment to fool cpp
165 natFromInt :: Int -> Nat
166 natFromInt i = fromIntegral i
168 intFromNat :: Nat -> Int
169 intFromNat w = fromIntegral w
171 shiftRL :: Nat -> Int -> Nat
172 shiftRL w i = shiftR w i
176 {--------------------------------------------------------------------
178 --------------------------------------------------------------------}
179 -- | /O(n+m)/. See 'difference'.
180 (\\) :: IntSet -> IntSet -> IntSet
181 m1 \\ m2 = difference m1 m2
183 {--------------------------------------------------------------------
185 --------------------------------------------------------------------}
186 -- | A set of integers.
188 | Tip {-# UNPACK #-} !Int
189 | Bin {-# UNPACK #-} !Prefix {-# UNPACK #-} !Mask !IntSet !IntSet
194 {--------------------------------------------------------------------
196 --------------------------------------------------------------------}
197 -- | /O(1)/. Is the set empty?
198 null :: IntSet -> Bool
202 -- | /O(n)/. Cardinality of the set.
203 size :: IntSet -> Int
206 Bin p m l r -> size l + size r
210 -- | /O(min(n,W))/. Is the value a member of the set?
211 member :: Int -> IntSet -> Bool
215 | nomatch x p m -> False
216 | zero x m -> member x l
217 | otherwise -> member x r
221 -- 'lookup' is used by 'intersection' for left-biasing
222 lookup :: Int -> IntSet -> Maybe Int
224 = let nk = natFromInt k in seq nk (lookupN nk t)
226 lookupN :: Nat -> IntSet -> Maybe Int
230 | zeroN k (natFromInt m) -> lookupN k l
231 | otherwise -> lookupN k r
233 | (k == natFromInt kx) -> Just kx
234 | otherwise -> Nothing
237 {--------------------------------------------------------------------
239 --------------------------------------------------------------------}
240 -- | /O(1)/. The empty set.
245 -- | /O(1)/. A set of one element.
246 singleton :: Int -> IntSet
250 {--------------------------------------------------------------------
252 --------------------------------------------------------------------}
253 -- | /O(min(n,W))/. Add a value to the set. When the value is already
254 -- an element of the set, it is replaced by the new one, ie. 'insert'
256 insert :: Int -> IntSet -> IntSet
260 | nomatch x p m -> join x (Tip x) p t
261 | zero x m -> Bin p m (insert x l) r
262 | otherwise -> Bin p m l (insert x r)
265 | otherwise -> join x (Tip x) y t
268 -- right-biased insertion, used by 'union'
269 insertR :: Int -> IntSet -> IntSet
273 | nomatch x p m -> join x (Tip x) p t
274 | zero x m -> Bin p m (insert x l) r
275 | otherwise -> Bin p m l (insert x r)
278 | otherwise -> join x (Tip x) y t
281 -- | /O(min(n,W))/. Delete a value in the set. Returns the
282 -- original set when the value was not present.
283 delete :: Int -> IntSet -> IntSet
288 | zero x m -> bin p m (delete x l) r
289 | otherwise -> bin p m l (delete x r)
296 {--------------------------------------------------------------------
298 --------------------------------------------------------------------}
299 -- | The union of a list of sets.
300 unions :: [IntSet] -> IntSet
302 = foldlStrict union empty xs
305 -- | /O(n+m)/. The union of two sets.
306 union :: IntSet -> IntSet -> IntSet
307 union t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
308 | shorter m1 m2 = union1
309 | shorter m2 m1 = union2
310 | p1 == p2 = Bin p1 m1 (union l1 l2) (union r1 r2)
311 | otherwise = join p1 t1 p2 t2
313 union1 | nomatch p2 p1 m1 = join p1 t1 p2 t2
314 | zero p2 m1 = Bin p1 m1 (union l1 t2) r1
315 | otherwise = Bin p1 m1 l1 (union r1 t2)
317 union2 | nomatch p1 p2 m2 = join p1 t1 p2 t2
318 | zero p1 m2 = Bin p2 m2 (union t1 l2) r2
319 | otherwise = Bin p2 m2 l2 (union t1 r2)
321 union (Tip x) t = insert x t
322 union t (Tip x) = insertR x t -- right bias
327 {--------------------------------------------------------------------
329 --------------------------------------------------------------------}
330 -- | /O(n+m)/. Difference between two sets.
331 difference :: IntSet -> IntSet -> IntSet
332 difference t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
333 | shorter m1 m2 = difference1
334 | shorter m2 m1 = difference2
335 | p1 == p2 = bin p1 m1 (difference l1 l2) (difference r1 r2)
338 difference1 | nomatch p2 p1 m1 = t1
339 | zero p2 m1 = bin p1 m1 (difference l1 t2) r1
340 | otherwise = bin p1 m1 l1 (difference r1 t2)
342 difference2 | nomatch p1 p2 m2 = t1
343 | zero p1 m2 = difference t1 l2
344 | otherwise = difference t1 r2
346 difference t1@(Tip x) t2
350 difference Nil t = Nil
351 difference t (Tip x) = delete x t
356 {--------------------------------------------------------------------
358 --------------------------------------------------------------------}
359 -- | /O(n+m)/. The intersection of two sets.
360 intersection :: IntSet -> IntSet -> IntSet
361 intersection t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
362 | shorter m1 m2 = intersection1
363 | shorter m2 m1 = intersection2
364 | p1 == p2 = bin p1 m1 (intersection l1 l2) (intersection r1 r2)
367 intersection1 | nomatch p2 p1 m1 = Nil
368 | zero p2 m1 = intersection l1 t2
369 | otherwise = intersection r1 t2
371 intersection2 | nomatch p1 p2 m2 = Nil
372 | zero p1 m2 = intersection t1 l2
373 | otherwise = intersection t1 r2
375 intersection t1@(Tip x) t2
378 intersection t (Tip x)
382 intersection Nil t = Nil
383 intersection t Nil = Nil
387 {--------------------------------------------------------------------
389 --------------------------------------------------------------------}
390 -- | /O(n+m)/. Is this a proper subset? (ie. a subset but not equal).
391 isProperSubsetOf :: IntSet -> IntSet -> Bool
392 isProperSubsetOf t1 t2
393 = case subsetCmp t1 t2 of
397 subsetCmp t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
399 | shorter m2 m1 = subsetCmpLt
400 | p1 == p2 = subsetCmpEq
401 | otherwise = GT -- disjoint
403 subsetCmpLt | nomatch p1 p2 m2 = GT
404 | zero p1 m2 = subsetCmp t1 l2
405 | otherwise = subsetCmp t1 r2
406 subsetCmpEq = case (subsetCmp l1 l2, subsetCmp r1 r2) of
412 subsetCmp (Bin p m l r) t = GT
413 subsetCmp (Tip x) (Tip y)
415 | otherwise = GT -- disjoint
418 | otherwise = GT -- disjoint
419 subsetCmp Nil Nil = EQ
422 -- | /O(n+m)/. Is this a subset?
423 -- @(s1 `isSubsetOf` s2)@ tells whether s1 is a subset of s2.
425 isSubsetOf :: IntSet -> IntSet -> Bool
426 isSubsetOf t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
427 | shorter m1 m2 = False
428 | shorter m2 m1 = match p1 p2 m2 && (if zero p1 m2 then isSubsetOf t1 l2
429 else isSubsetOf t1 r2)
430 | otherwise = (p1==p2) && isSubsetOf l1 l2 && isSubsetOf r1 r2
431 isSubsetOf (Bin p m l r) t = False
432 isSubsetOf (Tip x) t = member x t
433 isSubsetOf Nil t = True
436 {--------------------------------------------------------------------
438 --------------------------------------------------------------------}
439 -- | /O(n)/. Filter all elements that satisfy some predicate.
440 filter :: (Int -> Bool) -> IntSet -> IntSet
444 -> bin p m (filter pred l) (filter pred r)
450 -- | /O(n)/. partition the set according to some predicate.
451 partition :: (Int -> Bool) -> IntSet -> (IntSet,IntSet)
455 -> let (l1,l2) = partition pred l
456 (r1,r2) = partition pred r
457 in (bin p m l1 r1, bin p m l2 r2)
460 | otherwise -> (Nil,t)
464 -- | /O(log n)/. The expression (@split x set@) is a pair @(set1,set2)@
465 -- where all elements in @set1@ are lower than @x@ and all elements in
466 -- @set2@ larger than @x@.
468 -- > split 3 (fromList [1..5]) == (fromList [1,2], fromList [3,4])
469 split :: Int -> IntSet -> (IntSet,IntSet)
473 | zero x m -> let (lt,gt) = split x l in (lt,union gt r)
474 | otherwise -> let (lt,gt) = split x r in (union l lt,gt)
478 | otherwise -> (Nil,Nil)
481 -- | /O(log n)/. Performs a 'split' but also returns whether the pivot
482 -- element was found in the original set.
483 splitMember :: Int -> IntSet -> (Bool,IntSet,IntSet)
487 | zero x m -> let (found,lt,gt) = splitMember x l in (found,lt,union gt r)
488 | otherwise -> let (found,lt,gt) = splitMember x r in (found,union l lt,gt)
490 | x>y -> (False,t,Nil)
491 | x<y -> (False,Nil,t)
492 | otherwise -> (True,Nil,Nil)
493 Nil -> (False,Nil,Nil)
495 {----------------------------------------------------------------------
497 ----------------------------------------------------------------------}
499 -- | /O(n*min(n,W))/.
500 -- @map f s@ is the set obtained by applying @f@ to each element of @s@.
502 -- It's worth noting that the size of the result may be smaller if,
503 -- for some @(x,y)@, @x \/= y && f x == f y@
505 map :: (Int->Int) -> IntSet -> IntSet
506 map f = fromList . List.map f . toList
508 {--------------------------------------------------------------------
510 --------------------------------------------------------------------}
511 -- | /O(n)/. Fold over the elements of a set in an unspecified order.
513 -- > sum set == fold (+) 0 set
514 -- > elems set == fold (:) [] set
515 fold :: (Int -> b -> b) -> b -> IntSet -> b
519 foldr :: (Int -> b -> b) -> b -> IntSet -> b
522 Bin p m l r -> foldr f (foldr f z r) l
526 {--------------------------------------------------------------------
528 --------------------------------------------------------------------}
529 -- | /O(n)/. The elements of a set. (For sets, this is equivalent to toList)
530 elems :: IntSet -> [Int]
534 {--------------------------------------------------------------------
536 --------------------------------------------------------------------}
537 -- | /O(n)/. Convert the set to a list of elements.
538 toList :: IntSet -> [Int]
542 -- | /O(n)/. Convert the set to an ascending list of elements.
543 toAscList :: IntSet -> [Int]
545 = -- NOTE: the following algorithm only works for big-endian trees
546 let (pos,neg) = span (>=0) (foldr (:) [] t) in neg ++ pos
548 -- | /O(n*min(n,W))/. Create a set from a list of integers.
549 fromList :: [Int] -> IntSet
551 = foldlStrict ins empty xs
555 -- | /O(n*min(n,W))/. Build a set from an ascending list of elements.
556 fromAscList :: [Int] -> IntSet
560 -- | /O(n*min(n,W))/. Build a set from an ascending list of distinct elements.
561 fromDistinctAscList :: [Int] -> IntSet
562 fromDistinctAscList xs
566 {--------------------------------------------------------------------
568 --------------------------------------------------------------------}
569 instance Eq IntSet where
570 t1 == t2 = equal t1 t2
571 t1 /= t2 = nequal t1 t2
573 equal :: IntSet -> IntSet -> Bool
574 equal (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
575 = (m1 == m2) && (p1 == p2) && (equal l1 l2) && (equal r1 r2)
576 equal (Tip x) (Tip y)
581 nequal :: IntSet -> IntSet -> Bool
582 nequal (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
583 = (m1 /= m2) || (p1 /= p2) || (nequal l1 l2) || (nequal r1 r2)
584 nequal (Tip x) (Tip y)
586 nequal Nil Nil = False
589 {--------------------------------------------------------------------
591 --------------------------------------------------------------------}
593 instance Ord IntSet where
594 compare s1 s2 = compare (toAscList s1) (toAscList s2)
595 -- tentative implementation. See if more efficient exists.
597 {--------------------------------------------------------------------
599 --------------------------------------------------------------------}
601 instance Monoid IntSet where
606 {--------------------------------------------------------------------
608 --------------------------------------------------------------------}
609 instance Show IntSet where
610 showsPrec d s = showSet (toList s)
612 showSet :: [Int] -> ShowS
616 = showChar '{' . shows x . showTail xs
618 showTail [] = showChar '}'
619 showTail (x:xs) = showChar ',' . shows x . showTail xs
621 {--------------------------------------------------------------------
623 --------------------------------------------------------------------}
624 -- | /O(n)/. Show the tree that implements the set. The tree is shown
625 -- in a compressed, hanging format.
626 showTree :: IntSet -> String
628 = showTreeWith True False s
631 {- | /O(n)/. The expression (@showTreeWith hang wide map@) shows
632 the tree that implements the set. If @hang@ is
633 @True@, a /hanging/ tree is shown otherwise a rotated tree is shown. If
634 @wide@ is true, an extra wide version is shown.
636 showTreeWith :: Bool -> Bool -> IntSet -> String
637 showTreeWith hang wide t
638 | hang = (showsTreeHang wide [] t) ""
639 | otherwise = (showsTree wide [] [] t) ""
641 showsTree :: Bool -> [String] -> [String] -> IntSet -> ShowS
642 showsTree wide lbars rbars t
645 -> showsTree wide (withBar rbars) (withEmpty rbars) r .
646 showWide wide rbars .
647 showsBars lbars . showString (showBin p m) . showString "\n" .
648 showWide wide lbars .
649 showsTree wide (withEmpty lbars) (withBar lbars) l
651 -> showsBars lbars . showString " " . shows x . showString "\n"
652 Nil -> showsBars lbars . showString "|\n"
654 showsTreeHang :: Bool -> [String] -> IntSet -> ShowS
655 showsTreeHang wide bars t
658 -> showsBars bars . showString (showBin p m) . showString "\n" .
660 showsTreeHang wide (withBar bars) l .
662 showsTreeHang wide (withEmpty bars) r
664 -> showsBars bars . showString " " . shows x . showString "\n"
665 Nil -> showsBars bars . showString "|\n"
668 = "*" -- ++ show (p,m)
671 | wide = showString (concat (reverse bars)) . showString "|\n"
674 showsBars :: [String] -> ShowS
678 _ -> showString (concat (reverse (tail bars))) . showString node
681 withBar bars = "| ":bars
682 withEmpty bars = " ":bars
685 {--------------------------------------------------------------------
687 --------------------------------------------------------------------}
688 {--------------------------------------------------------------------
690 --------------------------------------------------------------------}
691 join :: Prefix -> IntSet -> Prefix -> IntSet -> IntSet
693 | zero p1 m = Bin p m t1 t2
694 | otherwise = Bin p m t2 t1
699 {--------------------------------------------------------------------
700 @bin@ assures that we never have empty trees within a tree.
701 --------------------------------------------------------------------}
702 bin :: Prefix -> Mask -> IntSet -> IntSet -> IntSet
705 bin p m l r = Bin p m l r
708 {--------------------------------------------------------------------
709 Endian independent bit twiddling
710 --------------------------------------------------------------------}
711 zero :: Int -> Mask -> Bool
713 = (natFromInt i) .&. (natFromInt m) == 0
715 nomatch,match :: Int -> Prefix -> Mask -> Bool
722 mask :: Int -> Mask -> Prefix
724 = maskW (natFromInt i) (natFromInt m)
726 zeroN :: Nat -> Nat -> Bool
727 zeroN i m = (i .&. m) == 0
729 {--------------------------------------------------------------------
730 Big endian operations
731 --------------------------------------------------------------------}
732 maskW :: Nat -> Nat -> Prefix
734 = intFromNat (i .&. (complement (m-1) `xor` m))
736 shorter :: Mask -> Mask -> Bool
738 = (natFromInt m1) > (natFromInt m2)
740 branchMask :: Prefix -> Prefix -> Mask
742 = intFromNat (highestBitMask (natFromInt p1 `xor` natFromInt p2))
744 {----------------------------------------------------------------------
745 Finding the highest bit (mask) in a word [x] can be done efficiently in
747 * convert to a floating point value and the mantissa tells us the
748 [log2(x)] that corresponds with the highest bit position. The mantissa
749 is retrieved either via the standard C function [frexp] or by some bit
750 twiddling on IEEE compatible numbers (float). Note that one needs to
751 use at least [double] precision for an accurate mantissa of 32 bit
753 * use bit twiddling, a logarithmic sequence of bitwise or's and shifts (bit).
754 * use processor specific assembler instruction (asm).
756 The most portable way would be [bit], but is it efficient enough?
757 I have measured the cycle counts of the different methods on an AMD
758 Athlon-XP 1800 (~ Pentium III 1.8Ghz) using the RDTSC instruction:
760 highestBitMask: method cycles
767 highestBit: method cycles
774 Wow, the bit twiddling is on today's RISC like machines even faster
775 than a single CISC instruction (BSR)!
776 ----------------------------------------------------------------------}
778 {----------------------------------------------------------------------
779 [highestBitMask] returns a word where only the highest bit is set.
780 It is found by first setting all bits in lower positions than the
781 highest bit and than taking an exclusive or with the original value.
782 Allthough the function may look expensive, GHC compiles this into
783 excellent C code that subsequently compiled into highly efficient
784 machine code. The algorithm is derived from Jorg Arndt's FXT library.
785 ----------------------------------------------------------------------}
786 highestBitMask :: Nat -> Nat
788 = case (x .|. shiftRL x 1) of
789 x -> case (x .|. shiftRL x 2) of
790 x -> case (x .|. shiftRL x 4) of
791 x -> case (x .|. shiftRL x 8) of
792 x -> case (x .|. shiftRL x 16) of
793 x -> case (x .|. shiftRL x 32) of -- for 64 bit platforms
794 x -> (x `xor` (shiftRL x 1))
797 {--------------------------------------------------------------------
799 --------------------------------------------------------------------}
803 (x:xx) -> let z' = f z x in seq z' (foldlStrict f z' xx)
807 {--------------------------------------------------------------------
809 --------------------------------------------------------------------}
810 testTree :: [Int] -> IntSet
811 testTree xs = fromList xs
812 test1 = testTree [1..20]
813 test2 = testTree [30,29..10]
814 test3 = testTree [1,4,6,89,2323,53,43,234,5,79,12,9,24,9,8,423,8,42,4,8,9,3]
816 {--------------------------------------------------------------------
818 --------------------------------------------------------------------}
823 { configMaxTest = 500
824 , configMaxFail = 5000
825 , configSize = \n -> (div n 2 + 3)
826 , configEvery = \n args -> let s = show n in s ++ [ '\b' | _ <- s ]
830 {--------------------------------------------------------------------
831 Arbitrary, reasonably balanced trees
832 --------------------------------------------------------------------}
833 instance Arbitrary IntSet where
834 arbitrary = do{ xs <- arbitrary
835 ; return (fromList xs)
839 {--------------------------------------------------------------------
840 Single, Insert, Delete
841 --------------------------------------------------------------------}
842 prop_Single :: Int -> Bool
844 = (insert x empty == singleton x)
846 prop_InsertDelete :: Int -> IntSet -> Property
847 prop_InsertDelete k t
848 = not (member k t) ==> delete k (insert k t) == t
851 {--------------------------------------------------------------------
853 --------------------------------------------------------------------}
854 prop_UnionInsert :: Int -> IntSet -> Bool
856 = union t (singleton x) == insert x t
858 prop_UnionAssoc :: IntSet -> IntSet -> IntSet -> Bool
859 prop_UnionAssoc t1 t2 t3
860 = union t1 (union t2 t3) == union (union t1 t2) t3
862 prop_UnionComm :: IntSet -> IntSet -> Bool
864 = (union t1 t2 == union t2 t1)
866 prop_Diff :: [Int] -> [Int] -> Bool
868 = toAscList (difference (fromList xs) (fromList ys))
869 == List.sort ((List.\\) (nub xs) (nub ys))
871 prop_Int :: [Int] -> [Int] -> Bool
873 = toAscList (intersection (fromList xs) (fromList ys))
874 == List.sort (nub ((List.intersect) (xs) (ys)))
876 {--------------------------------------------------------------------
878 --------------------------------------------------------------------}
880 = forAll (choose (5,100)) $ \n ->
882 in fromAscList xs == fromList xs
884 prop_List :: [Int] -> Bool
886 = (sort (nub xs) == toAscList (fromList xs))