Faster filterF, filterNotByte
[packages/old-time.git] / Data / IntSet.hs
1 {-# OPTIONS -cpp -fglasgow-exts #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Data.IntSet
5 -- Copyright : (c) Daan Leijen 2002
6 -- License : BSD-style
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
10 --
11 -- An efficient implementation of integer sets.
12 --
13 -- This module is intended to be imported @qualified@, to avoid name
14 -- clashes with "Prelude" functions. eg.
15 --
16 -- > import Data.IntSet as Set
17 --
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").
23 --
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>
27 --
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.
31 --
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'
35 -- (32 or 64).
36 -----------------------------------------------------------------------------
37
38 module Data.IntSet (
39 -- * Set type
40 IntSet -- instance Eq,Show
41
42 -- * Operators
43 , (\\)
44
45 -- * Query
46 , null
47 , size
48 , member
49 , notMember
50 , isSubsetOf
51 , isProperSubsetOf
52
53 -- * Construction
54 , empty
55 , singleton
56 , insert
57 , delete
58
59 -- * Combine
60 , union, unions
61 , difference
62 , intersection
63
64 -- * Filter
65 , filter
66 , partition
67 , split
68 , splitMember
69
70 -- * Map
71 , map
72
73 -- * Fold
74 , fold
75
76 -- * Conversion
77 -- ** List
78 , elems
79 , toList
80 , fromList
81
82 -- ** Ordered list
83 , toAscList
84 , fromAscList
85 , fromDistinctAscList
86
87 -- * Debugging
88 , showTree
89 , showTreeWith
90 ) where
91
92
93 import Prelude hiding (lookup,filter,foldr,foldl,null,map)
94 import Data.Bits
95 import Data.Int
96
97 import qualified Data.List as List
98 import Data.Monoid (Monoid(..))
99 import Data.Typeable
100
101 {-
102 -- just for testing
103 import QuickCheck
104 import List (nub,sort)
105 import qualified List
106 -}
107
108 #if __GLASGOW_HASKELL__
109 import Text.Read
110 import Data.Generics.Basics
111 import Data.Generics.Instances
112 #endif
113
114 #if __GLASGOW_HASKELL__ >= 503
115 import GHC.Word
116 import GHC.Exts ( Word(..), Int(..), shiftRL# )
117 #elif __GLASGOW_HASKELL__
118 import Word
119 import GlaExts ( Word(..), Int(..), shiftRL# )
120 #else
121 import Data.Word
122 #endif
123
124 infixl 9 \\{-This comment teaches CPP correct behaviour -}
125
126 -- A "Nat" is a natural machine word (an unsigned Int)
127 type Nat = Word
128
129 natFromInt :: Int -> Nat
130 natFromInt i = fromIntegral i
131
132 intFromNat :: Nat -> Int
133 intFromNat w = fromIntegral w
134
135 shiftRL :: Nat -> Int -> Nat
136 #if __GLASGOW_HASKELL__
137 {--------------------------------------------------------------------
138 GHC: use unboxing to get @shiftRL@ inlined.
139 --------------------------------------------------------------------}
140 shiftRL (W# x) (I# i)
141 = W# (shiftRL# x i)
142 #else
143 shiftRL x i = shiftR x i
144 #endif
145
146 {--------------------------------------------------------------------
147 Operators
148 --------------------------------------------------------------------}
149 -- | /O(n+m)/. See 'difference'.
150 (\\) :: IntSet -> IntSet -> IntSet
151 m1 \\ m2 = difference m1 m2
152
153 {--------------------------------------------------------------------
154 Types
155 --------------------------------------------------------------------}
156 -- | A set of integers.
157 data IntSet = Nil
158 | Tip {-# UNPACK #-} !Int
159 | Bin {-# UNPACK #-} !Prefix {-# UNPACK #-} !Mask !IntSet !IntSet
160
161 type Prefix = Int
162 type Mask = Int
163
164 instance Monoid IntSet where
165 mempty = empty
166 mappend = union
167 mconcat = unions
168
169 #if __GLASGOW_HASKELL__
170
171 {--------------------------------------------------------------------
172 A Data instance
173 --------------------------------------------------------------------}
174
175 -- This instance preserves data abstraction at the cost of inefficiency.
176 -- We omit reflection services for the sake of data abstraction.
177
178 instance Data IntSet where
179 gfoldl f z is = z fromList `f` (toList is)
180 toConstr _ = error "toConstr"
181 gunfold _ _ = error "gunfold"
182 dataTypeOf _ = mkNorepType "Data.IntSet.IntSet"
183
184 #endif
185
186 {--------------------------------------------------------------------
187 Query
188 --------------------------------------------------------------------}
189 -- | /O(1)/. Is the set empty?
190 null :: IntSet -> Bool
191 null Nil = True
192 null other = False
193
194 -- | /O(n)/. Cardinality of the set.
195 size :: IntSet -> Int
196 size t
197 = case t of
198 Bin p m l r -> size l + size r
199 Tip y -> 1
200 Nil -> 0
201
202 -- | /O(min(n,W))/. Is the value a member of the set?
203 member :: Int -> IntSet -> Bool
204 member x t
205 = case t of
206 Bin p m l r
207 | nomatch x p m -> False
208 | zero x m -> member x l
209 | otherwise -> member x r
210 Tip y -> (x==y)
211 Nil -> False
212
213 -- | /O(log n)/. Is the element not in the set?
214 notMember :: Int -> IntSet -> Bool
215 notMember k = not . member k
216
217 -- 'lookup' is used by 'intersection' for left-biasing
218 lookup :: Int -> IntSet -> Maybe Int
219 lookup k t
220 = let nk = natFromInt k in seq nk (lookupN nk t)
221
222 lookupN :: Nat -> IntSet -> Maybe Int
223 lookupN k t
224 = case t of
225 Bin p m l r
226 | zeroN k (natFromInt m) -> lookupN k l
227 | otherwise -> lookupN k r
228 Tip kx
229 | (k == natFromInt kx) -> Just kx
230 | otherwise -> Nothing
231 Nil -> Nothing
232
233 {--------------------------------------------------------------------
234 Construction
235 --------------------------------------------------------------------}
236 -- | /O(1)/. The empty set.
237 empty :: IntSet
238 empty
239 = Nil
240
241 -- | /O(1)/. A set of one element.
242 singleton :: Int -> IntSet
243 singleton x
244 = Tip x
245
246 {--------------------------------------------------------------------
247 Insert
248 --------------------------------------------------------------------}
249 -- | /O(min(n,W))/. Add a value to the set. When the value is already
250 -- an element of the set, it is replaced by the new one, ie. 'insert'
251 -- is left-biased.
252 insert :: Int -> IntSet -> IntSet
253 insert x t
254 = case t of
255 Bin p m l r
256 | nomatch x p m -> join x (Tip x) p t
257 | zero x m -> Bin p m (insert x l) r
258 | otherwise -> Bin p m l (insert x r)
259 Tip y
260 | x==y -> Tip x
261 | otherwise -> join x (Tip x) y t
262 Nil -> Tip x
263
264 -- right-biased insertion, used by 'union'
265 insertR :: Int -> IntSet -> IntSet
266 insertR x t
267 = case t of
268 Bin p m l r
269 | nomatch x p m -> join x (Tip x) p t
270 | zero x m -> Bin p m (insert x l) r
271 | otherwise -> Bin p m l (insert x r)
272 Tip y
273 | x==y -> t
274 | otherwise -> join x (Tip x) y t
275 Nil -> Tip x
276
277 -- | /O(min(n,W))/. Delete a value in the set. Returns the
278 -- original set when the value was not present.
279 delete :: Int -> IntSet -> IntSet
280 delete x t
281 = case t of
282 Bin p m l r
283 | nomatch x p m -> t
284 | zero x m -> bin p m (delete x l) r
285 | otherwise -> bin p m l (delete x r)
286 Tip y
287 | x==y -> Nil
288 | otherwise -> t
289 Nil -> Nil
290
291
292 {--------------------------------------------------------------------
293 Union
294 --------------------------------------------------------------------}
295 -- | The union of a list of sets.
296 unions :: [IntSet] -> IntSet
297 unions xs
298 = foldlStrict union empty xs
299
300
301 -- | /O(n+m)/. The union of two sets.
302 union :: IntSet -> IntSet -> IntSet
303 union t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
304 | shorter m1 m2 = union1
305 | shorter m2 m1 = union2
306 | p1 == p2 = Bin p1 m1 (union l1 l2) (union r1 r2)
307 | otherwise = join p1 t1 p2 t2
308 where
309 union1 | nomatch p2 p1 m1 = join p1 t1 p2 t2
310 | zero p2 m1 = Bin p1 m1 (union l1 t2) r1
311 | otherwise = Bin p1 m1 l1 (union r1 t2)
312
313 union2 | nomatch p1 p2 m2 = join p1 t1 p2 t2
314 | zero p1 m2 = Bin p2 m2 (union t1 l2) r2
315 | otherwise = Bin p2 m2 l2 (union t1 r2)
316
317 union (Tip x) t = insert x t
318 union t (Tip x) = insertR x t -- right bias
319 union Nil t = t
320 union t Nil = t
321
322
323 {--------------------------------------------------------------------
324 Difference
325 --------------------------------------------------------------------}
326 -- | /O(n+m)/. Difference between two sets.
327 difference :: IntSet -> IntSet -> IntSet
328 difference t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
329 | shorter m1 m2 = difference1
330 | shorter m2 m1 = difference2
331 | p1 == p2 = bin p1 m1 (difference l1 l2) (difference r1 r2)
332 | otherwise = t1
333 where
334 difference1 | nomatch p2 p1 m1 = t1
335 | zero p2 m1 = bin p1 m1 (difference l1 t2) r1
336 | otherwise = bin p1 m1 l1 (difference r1 t2)
337
338 difference2 | nomatch p1 p2 m2 = t1
339 | zero p1 m2 = difference t1 l2
340 | otherwise = difference t1 r2
341
342 difference t1@(Tip x) t2
343 | member x t2 = Nil
344 | otherwise = t1
345
346 difference Nil t = Nil
347 difference t (Tip x) = delete x t
348 difference t Nil = t
349
350
351
352 {--------------------------------------------------------------------
353 Intersection
354 --------------------------------------------------------------------}
355 -- | /O(n+m)/. The intersection of two sets.
356 intersection :: IntSet -> IntSet -> IntSet
357 intersection t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
358 | shorter m1 m2 = intersection1
359 | shorter m2 m1 = intersection2
360 | p1 == p2 = bin p1 m1 (intersection l1 l2) (intersection r1 r2)
361 | otherwise = Nil
362 where
363 intersection1 | nomatch p2 p1 m1 = Nil
364 | zero p2 m1 = intersection l1 t2
365 | otherwise = intersection r1 t2
366
367 intersection2 | nomatch p1 p2 m2 = Nil
368 | zero p1 m2 = intersection t1 l2
369 | otherwise = intersection t1 r2
370
371 intersection t1@(Tip x) t2
372 | member x t2 = t1
373 | otherwise = Nil
374 intersection t (Tip x)
375 = case lookup x t of
376 Just y -> Tip y
377 Nothing -> Nil
378 intersection Nil t = Nil
379 intersection t Nil = Nil
380
381
382
383 {--------------------------------------------------------------------
384 Subset
385 --------------------------------------------------------------------}
386 -- | /O(n+m)/. Is this a proper subset? (ie. a subset but not equal).
387 isProperSubsetOf :: IntSet -> IntSet -> Bool
388 isProperSubsetOf t1 t2
389 = case subsetCmp t1 t2 of
390 LT -> True
391 ge -> False
392
393 subsetCmp t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
394 | shorter m1 m2 = GT
395 | shorter m2 m1 = subsetCmpLt
396 | p1 == p2 = subsetCmpEq
397 | otherwise = GT -- disjoint
398 where
399 subsetCmpLt | nomatch p1 p2 m2 = GT
400 | zero p1 m2 = subsetCmp t1 l2
401 | otherwise = subsetCmp t1 r2
402 subsetCmpEq = case (subsetCmp l1 l2, subsetCmp r1 r2) of
403 (GT,_ ) -> GT
404 (_ ,GT) -> GT
405 (EQ,EQ) -> EQ
406 other -> LT
407
408 subsetCmp (Bin p m l r) t = GT
409 subsetCmp (Tip x) (Tip y)
410 | x==y = EQ
411 | otherwise = GT -- disjoint
412 subsetCmp (Tip x) t
413 | member x t = LT
414 | otherwise = GT -- disjoint
415 subsetCmp Nil Nil = EQ
416 subsetCmp Nil t = LT
417
418 -- | /O(n+m)/. Is this a subset?
419 -- @(s1 `isSubsetOf` s2)@ tells whether @s1@ is a subset of @s2@.
420
421 isSubsetOf :: IntSet -> IntSet -> Bool
422 isSubsetOf t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2)
423 | shorter m1 m2 = False
424 | shorter m2 m1 = match p1 p2 m2 && (if zero p1 m2 then isSubsetOf t1 l2
425 else isSubsetOf t1 r2)
426 | otherwise = (p1==p2) && isSubsetOf l1 l2 && isSubsetOf r1 r2
427 isSubsetOf (Bin p m l r) t = False
428 isSubsetOf (Tip x) t = member x t
429 isSubsetOf Nil t = True
430
431
432 {--------------------------------------------------------------------
433 Filter
434 --------------------------------------------------------------------}
435 -- | /O(n)/. Filter all elements that satisfy some predicate.
436 filter :: (Int -> Bool) -> IntSet -> IntSet
437 filter pred t
438 = case t of
439 Bin p m l r
440 -> bin p m (filter pred l) (filter pred r)
441 Tip x
442 | pred x -> t
443 | otherwise -> Nil
444 Nil -> Nil
445
446 -- | /O(n)/. partition the set according to some predicate.
447 partition :: (Int -> Bool) -> IntSet -> (IntSet,IntSet)
448 partition pred t
449 = case t of
450 Bin p m l r
451 -> let (l1,l2) = partition pred l
452 (r1,r2) = partition pred r
453 in (bin p m l1 r1, bin p m l2 r2)
454 Tip x
455 | pred x -> (t,Nil)
456 | otherwise -> (Nil,t)
457 Nil -> (Nil,Nil)
458
459
460 -- | /O(log n)/. The expression (@'split' x set@) is a pair @(set1,set2)@
461 -- where all elements in @set1@ are lower than @x@ and all elements in
462 -- @set2@ larger than @x@.
463 --
464 -- > split 3 (fromList [1..5]) == (fromList [1,2], fromList [3,4])
465 split :: Int -> IntSet -> (IntSet,IntSet)
466 split x t
467 = case t of
468 Bin p m l r
469 | m < 0 -> if x >= 0 then let (lt,gt) = split' x l in (union r lt, gt)
470 else let (lt,gt) = split' x r in (lt, union gt l)
471 -- handle negative numbers.
472 | otherwise -> split' x t
473 Tip y
474 | x>y -> (t,Nil)
475 | x<y -> (Nil,t)
476 | otherwise -> (Nil,Nil)
477 Nil -> (Nil, Nil)
478
479 split' :: Int -> IntSet -> (IntSet,IntSet)
480 split' x t
481 = case t of
482 Bin p m l r
483 | match x p m -> if zero x m then let (lt,gt) = split' x l in (lt,union gt r)
484 else let (lt,gt) = split' x r in (union l lt,gt)
485 | otherwise -> if x < p then (Nil, t)
486 else (t, Nil)
487 Tip y
488 | x>y -> (t,Nil)
489 | x<y -> (Nil,t)
490 | otherwise -> (Nil,Nil)
491 Nil -> (Nil,Nil)
492
493 -- | /O(log n)/. Performs a 'split' but also returns whether the pivot
494 -- element was found in the original set.
495 splitMember :: Int -> IntSet -> (IntSet,Bool,IntSet)
496 splitMember x t
497 = case t of
498 Bin p m l r
499 | m < 0 -> if x >= 0 then let (lt,found,gt) = splitMember' x l in (union r lt, found, gt)
500 else let (lt,found,gt) = splitMember' x r in (lt, found, union gt l)
501 -- handle negative numbers.
502 | otherwise -> splitMember' x t
503 Tip y
504 | x>y -> (t,False,Nil)
505 | x<y -> (Nil,False,t)
506 | otherwise -> (Nil,True,Nil)
507 Nil -> (Nil,False,Nil)
508
509 splitMember' :: Int -> IntSet -> (IntSet,Bool,IntSet)
510 splitMember' x t
511 = case t of
512 Bin p m l r
513 | match x p m -> if zero x m then let (lt,found,gt) = splitMember x l in (lt,found,union gt r)
514 else let (lt,found,gt) = splitMember x r in (union l lt,found,gt)
515 | otherwise -> if x < p then (Nil, False, t)
516 else (t, False, Nil)
517 Tip y
518 | x>y -> (t,False,Nil)
519 | x<y -> (Nil,False,t)
520 | otherwise -> (Nil,True,Nil)
521 Nil -> (Nil,False,Nil)
522
523 {----------------------------------------------------------------------
524 Map
525 ----------------------------------------------------------------------}
526
527 -- | /O(n*min(n,W))/.
528 -- @'map' f s@ is the set obtained by applying @f@ to each element of @s@.
529 --
530 -- It's worth noting that the size of the result may be smaller if,
531 -- for some @(x,y)@, @x \/= y && f x == f y@
532
533 map :: (Int->Int) -> IntSet -> IntSet
534 map f = fromList . List.map f . toList
535
536 {--------------------------------------------------------------------
537 Fold
538 --------------------------------------------------------------------}
539 -- | /O(n)/. Fold over the elements of a set in an unspecified order.
540 --
541 -- > sum set == fold (+) 0 set
542 -- > elems set == fold (:) [] set
543 fold :: (Int -> b -> b) -> b -> IntSet -> b
544 fold f z t
545 = case t of
546 Bin 0 m l r | m < 0 -> foldr f (foldr f z l) r
547 -- put negative numbers before.
548 Bin p m l r -> foldr f z t
549 Tip x -> f x z
550 Nil -> z
551
552 foldr :: (Int -> b -> b) -> b -> IntSet -> b
553 foldr f z t
554 = case t of
555 Bin p m l r -> foldr f (foldr f z r) l
556 Tip x -> f x z
557 Nil -> z
558
559 {--------------------------------------------------------------------
560 List variations
561 --------------------------------------------------------------------}
562 -- | /O(n)/. The elements of a set. (For sets, this is equivalent to toList)
563 elems :: IntSet -> [Int]
564 elems s
565 = toList s
566
567 {--------------------------------------------------------------------
568 Lists
569 --------------------------------------------------------------------}
570 -- | /O(n)/. Convert the set to a list of elements.
571 toList :: IntSet -> [Int]
572 toList t
573 = fold (:) [] t
574
575 -- | /O(n)/. Convert the set to an ascending list of elements.
576 toAscList :: IntSet -> [Int]
577 toAscList t = toList t
578
579 -- | /O(n*min(n,W))/. Create a set from a list of integers.
580 fromList :: [Int] -> IntSet
581 fromList xs
582 = foldlStrict ins empty xs
583 where
584 ins t x = insert x t
585
586 -- | /O(n*min(n,W))/. Build a set from an ascending list of elements.
587 fromAscList :: [Int] -> IntSet
588 fromAscList xs
589 = fromList xs
590
591 -- | /O(n*min(n,W))/. Build a set from an ascending list of distinct elements.
592 fromDistinctAscList :: [Int] -> IntSet
593 fromDistinctAscList xs
594 = fromList xs
595
596
597 {--------------------------------------------------------------------
598 Eq
599 --------------------------------------------------------------------}
600 instance Eq IntSet where
601 t1 == t2 = equal t1 t2
602 t1 /= t2 = nequal t1 t2
603
604 equal :: IntSet -> IntSet -> Bool
605 equal (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
606 = (m1 == m2) && (p1 == p2) && (equal l1 l2) && (equal r1 r2)
607 equal (Tip x) (Tip y)
608 = (x==y)
609 equal Nil Nil = True
610 equal t1 t2 = False
611
612 nequal :: IntSet -> IntSet -> Bool
613 nequal (Bin p1 m1 l1 r1) (Bin p2 m2 l2 r2)
614 = (m1 /= m2) || (p1 /= p2) || (nequal l1 l2) || (nequal r1 r2)
615 nequal (Tip x) (Tip y)
616 = (x/=y)
617 nequal Nil Nil = False
618 nequal t1 t2 = True
619
620 {--------------------------------------------------------------------
621 Ord
622 --------------------------------------------------------------------}
623
624 instance Ord IntSet where
625 compare s1 s2 = compare (toAscList s1) (toAscList s2)
626 -- tentative implementation. See if more efficient exists.
627
628 {--------------------------------------------------------------------
629 Show
630 --------------------------------------------------------------------}
631 instance Show IntSet where
632 showsPrec p xs = showParen (p > 10) $
633 showString "fromList " . shows (toList xs)
634
635 showSet :: [Int] -> ShowS
636 showSet []
637 = showString "{}"
638 showSet (x:xs)
639 = showChar '{' . shows x . showTail xs
640 where
641 showTail [] = showChar '}'
642 showTail (x:xs) = showChar ',' . shows x . showTail xs
643
644 {--------------------------------------------------------------------
645 Read
646 --------------------------------------------------------------------}
647 instance Read IntSet where
648 #ifdef __GLASGOW_HASKELL__
649 readPrec = parens $ prec 10 $ do
650 Ident "fromList" <- lexP
651 xs <- readPrec
652 return (fromList xs)
653
654 readListPrec = readListPrecDefault
655 #else
656 readsPrec p = readParen (p > 10) $ \ r -> do
657 ("fromList",s) <- lex r
658 (xs,t) <- reads s
659 return (fromList xs,t)
660 #endif
661
662 {--------------------------------------------------------------------
663 Typeable
664 --------------------------------------------------------------------}
665
666 #include "Typeable.h"
667 INSTANCE_TYPEABLE0(IntSet,intSetTc,"IntSet")
668
669 {--------------------------------------------------------------------
670 Debugging
671 --------------------------------------------------------------------}
672 -- | /O(n)/. Show the tree that implements the set. The tree is shown
673 -- in a compressed, hanging format.
674 showTree :: IntSet -> String
675 showTree s
676 = showTreeWith True False s
677
678
679 {- | /O(n)/. The expression (@'showTreeWith' hang wide map@) shows
680 the tree that implements the set. If @hang@ is
681 'True', a /hanging/ tree is shown otherwise a rotated tree is shown. If
682 @wide@ is 'True', an extra wide version is shown.
683 -}
684 showTreeWith :: Bool -> Bool -> IntSet -> String
685 showTreeWith hang wide t
686 | hang = (showsTreeHang wide [] t) ""
687 | otherwise = (showsTree wide [] [] t) ""
688
689 showsTree :: Bool -> [String] -> [String] -> IntSet -> ShowS
690 showsTree wide lbars rbars t
691 = case t of
692 Bin p m l r
693 -> showsTree wide (withBar rbars) (withEmpty rbars) r .
694 showWide wide rbars .
695 showsBars lbars . showString (showBin p m) . showString "\n" .
696 showWide wide lbars .
697 showsTree wide (withEmpty lbars) (withBar lbars) l
698 Tip x
699 -> showsBars lbars . showString " " . shows x . showString "\n"
700 Nil -> showsBars lbars . showString "|\n"
701
702 showsTreeHang :: Bool -> [String] -> IntSet -> ShowS
703 showsTreeHang wide bars t
704 = case t of
705 Bin p m l r
706 -> showsBars bars . showString (showBin p m) . showString "\n" .
707 showWide wide bars .
708 showsTreeHang wide (withBar bars) l .
709 showWide wide bars .
710 showsTreeHang wide (withEmpty bars) r
711 Tip x
712 -> showsBars bars . showString " " . shows x . showString "\n"
713 Nil -> showsBars bars . showString "|\n"
714
715 showBin p m
716 = "*" -- ++ show (p,m)
717
718 showWide wide bars
719 | wide = showString (concat (reverse bars)) . showString "|\n"
720 | otherwise = id
721
722 showsBars :: [String] -> ShowS
723 showsBars bars
724 = case bars of
725 [] -> id
726 _ -> showString (concat (reverse (tail bars))) . showString node
727
728 node = "+--"
729 withBar bars = "| ":bars
730 withEmpty bars = " ":bars
731
732
733 {--------------------------------------------------------------------
734 Helpers
735 --------------------------------------------------------------------}
736 {--------------------------------------------------------------------
737 Join
738 --------------------------------------------------------------------}
739 join :: Prefix -> IntSet -> Prefix -> IntSet -> IntSet
740 join p1 t1 p2 t2
741 | zero p1 m = Bin p m t1 t2
742 | otherwise = Bin p m t2 t1
743 where
744 m = branchMask p1 p2
745 p = mask p1 m
746
747 {--------------------------------------------------------------------
748 @bin@ assures that we never have empty trees within a tree.
749 --------------------------------------------------------------------}
750 bin :: Prefix -> Mask -> IntSet -> IntSet -> IntSet
751 bin p m l Nil = l
752 bin p m Nil r = r
753 bin p m l r = Bin p m l r
754
755
756 {--------------------------------------------------------------------
757 Endian independent bit twiddling
758 --------------------------------------------------------------------}
759 zero :: Int -> Mask -> Bool
760 zero i m
761 = (natFromInt i) .&. (natFromInt m) == 0
762
763 nomatch,match :: Int -> Prefix -> Mask -> Bool
764 nomatch i p m
765 = (mask i m) /= p
766
767 match i p m
768 = (mask i m) == p
769
770 mask :: Int -> Mask -> Prefix
771 mask i m
772 = maskW (natFromInt i) (natFromInt m)
773
774 zeroN :: Nat -> Nat -> Bool
775 zeroN i m = (i .&. m) == 0
776
777 {--------------------------------------------------------------------
778 Big endian operations
779 --------------------------------------------------------------------}
780 maskW :: Nat -> Nat -> Prefix
781 maskW i m
782 = intFromNat (i .&. (complement (m-1) `xor` m))
783
784 shorter :: Mask -> Mask -> Bool
785 shorter m1 m2
786 = (natFromInt m1) > (natFromInt m2)
787
788 branchMask :: Prefix -> Prefix -> Mask
789 branchMask p1 p2
790 = intFromNat (highestBitMask (natFromInt p1 `xor` natFromInt p2))
791
792 {----------------------------------------------------------------------
793 Finding the highest bit (mask) in a word [x] can be done efficiently in
794 three ways:
795 * convert to a floating point value and the mantissa tells us the
796 [log2(x)] that corresponds with the highest bit position. The mantissa
797 is retrieved either via the standard C function [frexp] or by some bit
798 twiddling on IEEE compatible numbers (float). Note that one needs to
799 use at least [double] precision for an accurate mantissa of 32 bit
800 numbers.
801 * use bit twiddling, a logarithmic sequence of bitwise or's and shifts (bit).
802 * use processor specific assembler instruction (asm).
803
804 The most portable way would be [bit], but is it efficient enough?
805 I have measured the cycle counts of the different methods on an AMD
806 Athlon-XP 1800 (~ Pentium III 1.8Ghz) using the RDTSC instruction:
807
808 highestBitMask: method cycles
809 --------------
810 frexp 200
811 float 33
812 bit 11
813 asm 12
814
815 highestBit: method cycles
816 --------------
817 frexp 195
818 float 33
819 bit 11
820 asm 11
821
822 Wow, the bit twiddling is on today's RISC like machines even faster
823 than a single CISC instruction (BSR)!
824 ----------------------------------------------------------------------}
825
826 {----------------------------------------------------------------------
827 [highestBitMask] returns a word where only the highest bit is set.
828 It is found by first setting all bits in lower positions than the
829 highest bit and than taking an exclusive or with the original value.
830 Allthough the function may look expensive, GHC compiles this into
831 excellent C code that subsequently compiled into highly efficient
832 machine code. The algorithm is derived from Jorg Arndt's FXT library.
833 ----------------------------------------------------------------------}
834 highestBitMask :: Nat -> Nat
835 highestBitMask x
836 = case (x .|. shiftRL x 1) of
837 x -> case (x .|. shiftRL x 2) of
838 x -> case (x .|. shiftRL x 4) of
839 x -> case (x .|. shiftRL x 8) of
840 x -> case (x .|. shiftRL x 16) of
841 x -> case (x .|. shiftRL x 32) of -- for 64 bit platforms
842 x -> (x `xor` (shiftRL x 1))
843
844
845 {--------------------------------------------------------------------
846 Utilities
847 --------------------------------------------------------------------}
848 foldlStrict f z xs
849 = case xs of
850 [] -> z
851 (x:xx) -> let z' = f z x in seq z' (foldlStrict f z' xx)
852
853
854 {-
855 {--------------------------------------------------------------------
856 Testing
857 --------------------------------------------------------------------}
858 testTree :: [Int] -> IntSet
859 testTree xs = fromList xs
860 test1 = testTree [1..20]
861 test2 = testTree [30,29..10]
862 test3 = testTree [1,4,6,89,2323,53,43,234,5,79,12,9,24,9,8,423,8,42,4,8,9,3]
863
864 {--------------------------------------------------------------------
865 QuickCheck
866 --------------------------------------------------------------------}
867 qcheck prop
868 = check config prop
869 where
870 config = Config
871 { configMaxTest = 500
872 , configMaxFail = 5000
873 , configSize = \n -> (div n 2 + 3)
874 , configEvery = \n args -> let s = show n in s ++ [ '\b' | _ <- s ]
875 }
876
877
878 {--------------------------------------------------------------------
879 Arbitrary, reasonably balanced trees
880 --------------------------------------------------------------------}
881 instance Arbitrary IntSet where
882 arbitrary = do{ xs <- arbitrary
883 ; return (fromList xs)
884 }
885
886
887 {--------------------------------------------------------------------
888 Single, Insert, Delete
889 --------------------------------------------------------------------}
890 prop_Single :: Int -> Bool
891 prop_Single x
892 = (insert x empty == singleton x)
893
894 prop_InsertDelete :: Int -> IntSet -> Property
895 prop_InsertDelete k t
896 = not (member k t) ==> delete k (insert k t) == t
897
898
899 {--------------------------------------------------------------------
900 Union
901 --------------------------------------------------------------------}
902 prop_UnionInsert :: Int -> IntSet -> Bool
903 prop_UnionInsert x t
904 = union t (singleton x) == insert x t
905
906 prop_UnionAssoc :: IntSet -> IntSet -> IntSet -> Bool
907 prop_UnionAssoc t1 t2 t3
908 = union t1 (union t2 t3) == union (union t1 t2) t3
909
910 prop_UnionComm :: IntSet -> IntSet -> Bool
911 prop_UnionComm t1 t2
912 = (union t1 t2 == union t2 t1)
913
914 prop_Diff :: [Int] -> [Int] -> Bool
915 prop_Diff xs ys
916 = toAscList (difference (fromList xs) (fromList ys))
917 == List.sort ((List.\\) (nub xs) (nub ys))
918
919 prop_Int :: [Int] -> [Int] -> Bool
920 prop_Int xs ys
921 = toAscList (intersection (fromList xs) (fromList ys))
922 == List.sort (nub ((List.intersect) (xs) (ys)))
923
924 {--------------------------------------------------------------------
925 Lists
926 --------------------------------------------------------------------}
927 prop_Ordered
928 = forAll (choose (5,100)) $ \n ->
929 let xs = [0..n::Int]
930 in fromAscList xs == fromList xs
931
932 prop_List :: [Int] -> Bool
933 prop_List xs
934 = (sort (nub xs) == toAscList (fromList xs))
935 -}