Refactor internal modules (#324)
[packages/containers.git] / Data / Set / Internal.hs
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE BangPatterns #-}
3 #if __GLASGOW_HASKELL__
4 {-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
5 #endif
6 #if !defined(TESTING) && __GLASGOW_HASKELL__ >= 703
7 {-# LANGUAGE Trustworthy #-}
8 #endif
9 #if __GLASGOW_HASKELL__ >= 708
10 {-# LANGUAGE RoleAnnotations #-}
11 {-# LANGUAGE TypeFamilies #-}
12 #endif
13
14 #include "containers.h"
15
16 -----------------------------------------------------------------------------
17 -- |
18 -- Module : Data.Set.Internal
19 -- Copyright : (c) Daan Leijen 2002
20 -- License : BSD-style
21 -- Maintainer : libraries@haskell.org
22 -- Stability : provisional
23 -- Portability : portable
24 --
25 -- = WARNING
26 --
27 -- This module is considered __internal__.
28 --
29 -- The Package Versioning Policy __does not apply__.
30 --
31 -- This contents of this module may change __in any way whatsoever__
32 -- and __without any warning__ between minor versions of this package.
33 --
34 -- Authors importing this module are expected to track development
35 -- closely.
36 --
37 -- = Description
38 --
39 -- An efficient implementation of sets.
40 --
41 -- These modules are intended to be imported qualified, to avoid name
42 -- clashes with Prelude functions, e.g.
43 --
44 -- > import Data.Set (Set)
45 -- > import qualified Data.Set as Set
46 --
47 -- The implementation of 'Set' is based on /size balanced/ binary trees (or
48 -- trees of /bounded balance/) as described by:
49 --
50 -- * Stephen Adams, \"/Efficient sets: a balancing act/\",
51 -- Journal of Functional Programming 3(4):553-562, October 1993,
52 -- <http://www.swiss.ai.mit.edu/~adams/BB/>.
53 -- * J. Nievergelt and E.M. Reingold,
54 -- \"/Binary search trees of bounded balance/\",
55 -- SIAM journal of computing 2(1), March 1973.
56 --
57 -- Bounds for 'union', 'intersection', and 'difference' are as given
58 -- by
59 --
60 -- * Guy Blelloch, Daniel Ferizovic, and Yihan Sun,
61 -- \"/Just Join for Parallel Ordered Sets/\",
62 -- <https://arxiv.org/abs/1602.02120v3>.
63 --
64 -- Note that the implementation is /left-biased/ -- the elements of a
65 -- first argument are always preferred to the second, for example in
66 -- 'union' or 'insert'. Of course, left-biasing can only be observed
67 -- when equality is an equivalence relation instead of structural
68 -- equality.
69 --
70 -- /Warning/: The size of the set must not exceed @maxBound::Int@. Violation of
71 -- this condition is not detected and if the size limit is exceeded, the
72 -- behavior of the set is completely undefined.
73 -----------------------------------------------------------------------------
74
75 -- [Note: Using INLINABLE]
76 -- ~~~~~~~~~~~~~~~~~~~~~~~
77 -- It is crucial to the performance that the functions specialize on the Ord
78 -- type when possible. GHC 7.0 and higher does this by itself when it sees th
79 -- unfolding of a function -- that is why all public functions are marked
80 -- INLINABLE (that exposes the unfolding).
81
82
83 -- [Note: Using INLINE]
84 -- ~~~~~~~~~~~~~~~~~~~~
85 -- For other compilers and GHC pre 7.0, we mark some of the functions INLINE.
86 -- We mark the functions that just navigate down the tree (lookup, insert,
87 -- delete and similar). That navigation code gets inlined and thus specialized
88 -- when possible. There is a price to pay -- code growth. The code INLINED is
89 -- therefore only the tree navigation, all the real work (rebalancing) is not
90 -- INLINED by using a NOINLINE.
91 --
92 -- All methods marked INLINE have to be nonrecursive -- a 'go' function doing
93 -- the real work is provided.
94
95
96 -- [Note: Type of local 'go' function]
97 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98 -- If the local 'go' function uses an Ord class, it sometimes heap-allocates
99 -- the Ord dictionary when the 'go' function does not have explicit type.
100 -- In that case we give 'go' explicit type. But this slightly decrease
101 -- performance, as the resulting 'go' function can float out to top level.
102
103
104 -- [Note: Local 'go' functions and capturing]
105 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106 -- As opposed to IntSet, when 'go' function captures an argument, increased
107 -- heap-allocation can occur: sometimes in a polymorphic function, the 'go'
108 -- floats out of its enclosing function and then it heap-allocates the
109 -- dictionary and the argument. Maybe it floats out too late and strictness
110 -- analyzer cannot see that these could be passed on stack.
111
112 -- [Note: Order of constructors]
113 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114 -- The order of constructors of Set matters when considering performance.
115 -- Currently in GHC 7.0, when type has 2 constructors, a forward conditional
116 -- jump is made when successfully matching second constructor. Successful match
117 -- of first constructor results in the forward jump not taken.
118 -- On GHC 7.0, reordering constructors from Tip | Bin to Bin | Tip
119 -- improves the benchmark by up to 10% on x86.
120
121 module Data.Set.Internal (
122 -- * Set type
123 Set(..) -- instance Eq,Ord,Show,Read,Data,Typeable
124
125 -- * Operators
126 , (\\)
127
128 -- * Query
129 , null
130 , size
131 , member
132 , notMember
133 , lookupLT
134 , lookupGT
135 , lookupLE
136 , lookupGE
137 , isSubsetOf
138 , isProperSubsetOf
139
140 -- * Construction
141 , empty
142 , singleton
143 , insert
144 , delete
145
146 -- * Combine
147 , union
148 , unions
149 , difference
150 , intersection
151
152 -- * Filter
153 , filter
154 , takeWhileAntitone
155 , dropWhileAntitone
156 , spanAntitone
157 , partition
158 , split
159 , splitMember
160 , splitRoot
161
162 -- * Indexed
163 , lookupIndex
164 , findIndex
165 , elemAt
166 , deleteAt
167 , take
168 , drop
169 , splitAt
170
171 -- * Map
172 , map
173 , mapMonotonic
174
175 -- * Folds
176 , foldr
177 , foldl
178 -- ** Strict folds
179 , foldr'
180 , foldl'
181 -- ** Legacy folds
182 , fold
183
184 -- * Min\/Max
185 , findMin
186 , findMax
187 , deleteMin
188 , deleteMax
189 , deleteFindMin
190 , deleteFindMax
191 , maxView
192 , minView
193
194 -- * Conversion
195
196 -- ** List
197 , elems
198 , toList
199 , fromList
200
201 -- ** Ordered list
202 , toAscList
203 , toDescList
204 , fromAscList
205 , fromDistinctAscList
206 , fromDescList
207 , fromDistinctDescList
208
209 -- * Debugging
210 , showTree
211 , showTreeWith
212 , valid
213
214 -- Internals (for testing)
215 , bin
216 , balanced
217 , link
218 , merge
219 ) where
220
221 import Prelude hiding (filter,foldl,foldr,null,map,take,drop,splitAt)
222 import qualified Data.List as List
223 import Data.Bits (shiftL, shiftR)
224 #if !MIN_VERSION_base(4,8,0)
225 import Data.Monoid (Monoid(..))
226 #endif
227 #if MIN_VERSION_base(4,9,0)
228 import Data.Semigroup (Semigroup((<>), stimes), stimesIdempotentMonoid)
229 #endif
230 import qualified Data.Foldable as Foldable
231 import Data.Typeable
232 import Control.DeepSeq (NFData(rnf))
233
234 import Data.Utils.StrictFold
235 import Data.Utils.StrictPair
236 import Data.Utils.PtrEquality
237
238 #if __GLASGOW_HASKELL__
239 import GHC.Exts ( build )
240 #if __GLASGOW_HASKELL__ >= 708
241 import qualified GHC.Exts as GHCExts
242 #endif
243 import Text.Read
244 import Data.Data
245 #endif
246
247
248 {--------------------------------------------------------------------
249 Operators
250 --------------------------------------------------------------------}
251 infixl 9 \\ --
252
253 -- | /O(m*log(n\/m+1)), m <= n/. See 'difference'.
254 (\\) :: Ord a => Set a -> Set a -> Set a
255 m1 \\ m2 = difference m1 m2
256 #if __GLASGOW_HASKELL__
257 {-# INLINABLE (\\) #-}
258 #endif
259
260 {--------------------------------------------------------------------
261 Sets are size balanced trees
262 --------------------------------------------------------------------}
263 -- | A set of values @a@.
264
265 -- See Note: Order of constructors
266 data Set a = Bin {-# UNPACK #-} !Size !a !(Set a) !(Set a)
267 | Tip
268
269 type Size = Int
270
271 #if __GLASGOW_HASKELL__ >= 708
272 type role Set nominal
273 #endif
274
275 instance Ord a => Monoid (Set a) where
276 mempty = empty
277 mconcat = unions
278 #if !(MIN_VERSION_base(4,9,0))
279 mappend = union
280 #else
281 mappend = (<>)
282
283 instance Ord a => Semigroup (Set a) where
284 (<>) = union
285 stimes = stimesIdempotentMonoid
286 #endif
287
288
289 instance Foldable.Foldable Set where
290 fold = go
291 where go Tip = mempty
292 go (Bin 1 k _ _) = k
293 go (Bin _ k l r) = go l `mappend` (k `mappend` go r)
294 {-# INLINABLE fold #-}
295 foldr = foldr
296 {-# INLINE foldr #-}
297 foldl = foldl
298 {-# INLINE foldl #-}
299 foldMap f t = go t
300 where go Tip = mempty
301 go (Bin 1 k _ _) = f k
302 go (Bin _ k l r) = go l `mappend` (f k `mappend` go r)
303 {-# INLINE foldMap #-}
304
305 #if MIN_VERSION_base(4,6,0)
306 foldl' = foldl'
307 {-# INLINE foldl' #-}
308 foldr' = foldr'
309 {-# INLINE foldr' #-}
310 #endif
311 #if MIN_VERSION_base(4,8,0)
312 length = size
313 {-# INLINE length #-}
314 null = null
315 {-# INLINE null #-}
316 toList = toList
317 {-# INLINE toList #-}
318 elem = go
319 where go !_ Tip = False
320 go x (Bin _ y l r) = x == y || go x l || go x r
321 {-# INLINABLE elem #-}
322 minimum = findMin
323 {-# INLINE minimum #-}
324 maximum = findMax
325 {-# INLINE maximum #-}
326 sum = foldl' (+) 0
327 {-# INLINABLE sum #-}
328 product = foldl' (*) 1
329 {-# INLINABLE product #-}
330 #endif
331
332
333 #if __GLASGOW_HASKELL__
334
335 {--------------------------------------------------------------------
336 A Data instance
337 --------------------------------------------------------------------}
338
339 -- This instance preserves data abstraction at the cost of inefficiency.
340 -- We provide limited reflection services for the sake of data abstraction.
341
342 instance (Data a, Ord a) => Data (Set a) where
343 gfoldl f z set = z fromList `f` (toList set)
344 toConstr _ = fromListConstr
345 gunfold k z c = case constrIndex c of
346 1 -> k (z fromList)
347 _ -> error "gunfold"
348 dataTypeOf _ = setDataType
349 dataCast1 f = gcast1 f
350
351 fromListConstr :: Constr
352 fromListConstr = mkConstr setDataType "fromList" [] Prefix
353
354 setDataType :: DataType
355 setDataType = mkDataType "Data.Set.Internal.Set" [fromListConstr]
356
357 #endif
358
359 {--------------------------------------------------------------------
360 Query
361 --------------------------------------------------------------------}
362 -- | /O(1)/. Is this the empty set?
363 null :: Set a -> Bool
364 null Tip = True
365 null (Bin {}) = False
366 {-# INLINE null #-}
367
368 -- | /O(1)/. The number of elements in the set.
369 size :: Set a -> Int
370 size Tip = 0
371 size (Bin sz _ _ _) = sz
372 {-# INLINE size #-}
373
374 -- | /O(log n)/. Is the element in the set?
375 member :: Ord a => a -> Set a -> Bool
376 member = go
377 where
378 go !_ Tip = False
379 go x (Bin _ y l r) = case compare x y of
380 LT -> go x l
381 GT -> go x r
382 EQ -> True
383 #if __GLASGOW_HASKELL__
384 {-# INLINABLE member #-}
385 #else
386 {-# INLINE member #-}
387 #endif
388
389 -- | /O(log n)/. Is the element not in the set?
390 notMember :: Ord a => a -> Set a -> Bool
391 notMember a t = not $ member a t
392 #if __GLASGOW_HASKELL__
393 {-# INLINABLE notMember #-}
394 #else
395 {-# INLINE notMember #-}
396 #endif
397
398 -- | /O(log n)/. Find largest element smaller than the given one.
399 --
400 -- > lookupLT 3 (fromList [3, 5]) == Nothing
401 -- > lookupLT 5 (fromList [3, 5]) == Just 3
402 lookupLT :: Ord a => a -> Set a -> Maybe a
403 lookupLT = goNothing
404 where
405 goNothing !_ Tip = Nothing
406 goNothing x (Bin _ y l r) | x <= y = goNothing x l
407 | otherwise = goJust x y r
408
409 goJust !_ best Tip = Just best
410 goJust x best (Bin _ y l r) | x <= y = goJust x best l
411 | otherwise = goJust x y r
412 #if __GLASGOW_HASKELL__
413 {-# INLINABLE lookupLT #-}
414 #else
415 {-# INLINE lookupLT #-}
416 #endif
417
418 -- | /O(log n)/. Find smallest element greater than the given one.
419 --
420 -- > lookupGT 4 (fromList [3, 5]) == Just 5
421 -- > lookupGT 5 (fromList [3, 5]) == Nothing
422 lookupGT :: Ord a => a -> Set a -> Maybe a
423 lookupGT = goNothing
424 where
425 goNothing !_ Tip = Nothing
426 goNothing x (Bin _ y l r) | x < y = goJust x y l
427 | otherwise = goNothing x r
428
429 goJust !_ best Tip = Just best
430 goJust x best (Bin _ y l r) | x < y = goJust x y l
431 | otherwise = goJust x best r
432 #if __GLASGOW_HASKELL__
433 {-# INLINABLE lookupGT #-}
434 #else
435 {-# INLINE lookupGT #-}
436 #endif
437
438 -- | /O(log n)/. Find largest element smaller or equal to the given one.
439 --
440 -- > lookupLE 2 (fromList [3, 5]) == Nothing
441 -- > lookupLE 4 (fromList [3, 5]) == Just 3
442 -- > lookupLE 5 (fromList [3, 5]) == Just 5
443 lookupLE :: Ord a => a -> Set a -> Maybe a
444 lookupLE = goNothing
445 where
446 goNothing !_ Tip = Nothing
447 goNothing x (Bin _ y l r) = case compare x y of LT -> goNothing x l
448 EQ -> Just y
449 GT -> goJust x y r
450
451 goJust !_ best Tip = Just best
452 goJust x best (Bin _ y l r) = case compare x y of LT -> goJust x best l
453 EQ -> Just y
454 GT -> goJust x y r
455 #if __GLASGOW_HASKELL__
456 {-# INLINABLE lookupLE #-}
457 #else
458 {-# INLINE lookupLE #-}
459 #endif
460
461 -- | /O(log n)/. Find smallest element greater or equal to the given one.
462 --
463 -- > lookupGE 3 (fromList [3, 5]) == Just 3
464 -- > lookupGE 4 (fromList [3, 5]) == Just 5
465 -- > lookupGE 6 (fromList [3, 5]) == Nothing
466 lookupGE :: Ord a => a -> Set a -> Maybe a
467 lookupGE = goNothing
468 where
469 goNothing !_ Tip = Nothing
470 goNothing x (Bin _ y l r) = case compare x y of LT -> goJust x y l
471 EQ -> Just y
472 GT -> goNothing x r
473
474 goJust !_ best Tip = Just best
475 goJust x best (Bin _ y l r) = case compare x y of LT -> goJust x y l
476 EQ -> Just y
477 GT -> goJust x best r
478 #if __GLASGOW_HASKELL__
479 {-# INLINABLE lookupGE #-}
480 #else
481 {-# INLINE lookupGE #-}
482 #endif
483
484 {--------------------------------------------------------------------
485 Construction
486 --------------------------------------------------------------------}
487 -- | /O(1)/. The empty set.
488 empty :: Set a
489 empty = Tip
490 {-# INLINE empty #-}
491
492 -- | /O(1)/. Create a singleton set.
493 singleton :: a -> Set a
494 singleton x = Bin 1 x Tip Tip
495 {-# INLINE singleton #-}
496
497 {--------------------------------------------------------------------
498 Insertion, Deletion
499 --------------------------------------------------------------------}
500 -- | /O(log n)/. Insert an element in a set.
501 -- If the set already contains an element equal to the given value,
502 -- it is replaced with the new value.
503
504 -- See Note: Type of local 'go' function
505 insert :: Ord a => a -> Set a -> Set a
506 insert = go
507 where
508 go :: Ord a => a -> Set a -> Set a
509 go !x Tip = singleton x
510 go !x t@(Bin sz y l r) = case compare x y of
511 LT | l' `ptrEq` l -> t
512 | otherwise -> balanceL y l' r
513 where !l' = go x l
514 GT | r' `ptrEq` r -> t
515 | otherwise -> balanceR y l r'
516 where !r' = go x r
517 EQ | x `ptrEq` y -> t
518 | otherwise -> Bin sz x l r
519 #if __GLASGOW_HASKELL__
520 {-# INLINABLE insert #-}
521 #else
522 {-# INLINE insert #-}
523 #endif
524
525 -- Insert an element to the set only if it is not in the set.
526 -- Used by `union`.
527
528 -- See Note: Type of local 'go' function
529 insertR :: Ord a => a -> Set a -> Set a
530 insertR = go
531 where
532 go :: Ord a => a -> Set a -> Set a
533 go !x Tip = singleton x
534 go !x t@(Bin _ y l r) = case compare x y of
535 LT | l' `ptrEq` l -> t
536 | otherwise -> balanceL y l' r
537 where !l' = go x l
538 GT | r' `ptrEq` r -> t
539 | otherwise -> balanceR y l r'
540 where !r' = go x r
541 EQ -> t
542 #if __GLASGOW_HASKELL__
543 {-# INLINABLE insertR #-}
544 #else
545 {-# INLINE insertR #-}
546 #endif
547
548 -- | /O(log n)/. Delete an element from a set.
549
550 -- See Note: Type of local 'go' function
551 delete :: Ord a => a -> Set a -> Set a
552 delete = go
553 where
554 go :: Ord a => a -> Set a -> Set a
555 go !_ Tip = Tip
556 go x t@(Bin _ y l r) = case compare x y of
557 LT | l' `ptrEq` l -> t
558 | otherwise -> balanceR y l' r
559 where !l' = go x l
560 GT | r' `ptrEq` r -> t
561 | otherwise -> balanceL y l r'
562 where !r' = go x r
563 EQ -> glue l r
564 #if __GLASGOW_HASKELL__
565 {-# INLINABLE delete #-}
566 #else
567 {-# INLINE delete #-}
568 #endif
569
570 {--------------------------------------------------------------------
571 Subset
572 --------------------------------------------------------------------}
573 -- | /O(n+m)/. Is this a proper subset? (ie. a subset but not equal).
574 isProperSubsetOf :: Ord a => Set a -> Set a -> Bool
575 isProperSubsetOf s1 s2
576 = (size s1 < size s2) && (isSubsetOf s1 s2)
577 #if __GLASGOW_HASKELL__
578 {-# INLINABLE isProperSubsetOf #-}
579 #endif
580
581
582 -- | /O(n+m)/. Is this a subset?
583 -- @(s1 `isSubsetOf` s2)@ tells whether @s1@ is a subset of @s2@.
584 isSubsetOf :: Ord a => Set a -> Set a -> Bool
585 isSubsetOf t1 t2
586 = (size t1 <= size t2) && (isSubsetOfX t1 t2)
587 #if __GLASGOW_HASKELL__
588 {-# INLINABLE isSubsetOf #-}
589 #endif
590
591 isSubsetOfX :: Ord a => Set a -> Set a -> Bool
592 isSubsetOfX Tip _ = True
593 isSubsetOfX _ Tip = False
594 isSubsetOfX (Bin _ x l r) t
595 = found && isSubsetOfX l lt && isSubsetOfX r gt
596 where
597 (lt,found,gt) = splitMember x t
598 #if __GLASGOW_HASKELL__
599 {-# INLINABLE isSubsetOfX #-}
600 #endif
601
602
603 {--------------------------------------------------------------------
604 Minimal, Maximal
605 --------------------------------------------------------------------}
606 -- | /O(log n)/. The minimal element of a set.
607 findMin :: Set a -> a
608 findMin (Bin _ x Tip _) = x
609 findMin (Bin _ _ l _) = findMin l
610 findMin Tip = error "Set.findMin: empty set has no minimal element"
611
612 -- | /O(log n)/. The maximal element of a set.
613 findMax :: Set a -> a
614 findMax (Bin _ x _ Tip) = x
615 findMax (Bin _ _ _ r) = findMax r
616 findMax Tip = error "Set.findMax: empty set has no maximal element"
617
618 -- | /O(log n)/. Delete the minimal element. Returns an empty set if the set is empty.
619 deleteMin :: Set a -> Set a
620 deleteMin (Bin _ _ Tip r) = r
621 deleteMin (Bin _ x l r) = balanceR x (deleteMin l) r
622 deleteMin Tip = Tip
623
624 -- | /O(log n)/. Delete the maximal element. Returns an empty set if the set is empty.
625 deleteMax :: Set a -> Set a
626 deleteMax (Bin _ _ l Tip) = l
627 deleteMax (Bin _ x l r) = balanceL x l (deleteMax r)
628 deleteMax Tip = Tip
629
630 {--------------------------------------------------------------------
631 Union.
632 --------------------------------------------------------------------}
633 -- | The union of a list of sets: (@'unions' == 'foldl' 'union' 'empty'@).
634 unions :: Ord a => [Set a] -> Set a
635 unions = foldlStrict union empty
636 #if __GLASGOW_HASKELL__
637 {-# INLINABLE unions #-}
638 #endif
639
640 -- | /O(m*log(n/m + 1)), m <= n/. The union of two sets, preferring the first set when
641 -- equal elements are encountered.
642 union :: Ord a => Set a -> Set a -> Set a
643 union t1 Tip = t1
644 union t1 (Bin _ x Tip Tip) = insertR x t1
645 union (Bin _ x Tip Tip) t2 = insert x t2
646 union Tip t2 = t2
647 union t1@(Bin _ x l1 r1) t2 = case splitS x t2 of
648 (l2 :*: r2)
649 | l1l2 `ptrEq` l1 && r1r2 `ptrEq` r1 -> t1
650 | otherwise -> link x l1l2 r1r2
651 where !l1l2 = union l1 l2
652 !r1r2 = union r1 r2
653 #if __GLASGOW_HASKELL__
654 {-# INLINABLE union #-}
655 #endif
656
657 {--------------------------------------------------------------------
658 Difference
659 --------------------------------------------------------------------}
660 -- | /O(m*log(n/m + 1)), m <= n/. Difference of two sets.
661 difference :: Ord a => Set a -> Set a -> Set a
662 difference Tip _ = Tip
663 difference t1 Tip = t1
664 difference t1 (Bin _ x l2 r2) = case split x t1 of
665 (l1, r1)
666 | size l1l2 + size r1r2 == size t1 -> t1
667 | otherwise -> merge l1l2 r1r2
668 where !l1l2 = difference l1 l2
669 !r1r2 = difference r1 r2
670 #if __GLASGOW_HASKELL__
671 {-# INLINABLE difference #-}
672 #endif
673
674 {--------------------------------------------------------------------
675 Intersection
676 --------------------------------------------------------------------}
677 -- | /O(m*log(n/m + 1)), m <= n/. The intersection of two sets.
678 -- Elements of the result come from the first set, so for example
679 --
680 -- > import qualified Data.Set as S
681 -- > data AB = A | B deriving Show
682 -- > instance Ord AB where compare _ _ = EQ
683 -- > instance Eq AB where _ == _ = True
684 -- > main = print (S.singleton A `S.intersection` S.singleton B,
685 -- > S.singleton B `S.intersection` S.singleton A)
686 --
687 -- prints @(fromList [A],fromList [B])@.
688 intersection :: Ord a => Set a -> Set a -> Set a
689 intersection Tip _ = Tip
690 intersection _ Tip = Tip
691 intersection t1@(Bin _ x l1 r1) t2
692 | b = if l1l2 `ptrEq` l1 && r1r2 `ptrEq` r1
693 then t1
694 else link x l1l2 r1r2
695 | otherwise = merge l1l2 r1r2
696 where
697 !(l2, b, r2) = splitMember x t2
698 !l1l2 = intersection l1 l2
699 !r1r2 = intersection r1 r2
700 #if __GLASGOW_HASKELL__
701 {-# INLINABLE intersection #-}
702 #endif
703
704 {--------------------------------------------------------------------
705 Filter and partition
706 --------------------------------------------------------------------}
707 -- | /O(n)/. Filter all elements that satisfy the predicate.
708 filter :: (a -> Bool) -> Set a -> Set a
709 filter _ Tip = Tip
710 filter p t@(Bin _ x l r)
711 | p x = if l `ptrEq` l' && r `ptrEq` r'
712 then t
713 else link x l' r'
714 | otherwise = merge l' r'
715 where
716 !l' = filter p l
717 !r' = filter p r
718
719 -- | /O(n)/. Partition the set into two sets, one with all elements that satisfy
720 -- the predicate and one with all elements that don't satisfy the predicate.
721 -- See also 'split'.
722 partition :: (a -> Bool) -> Set a -> (Set a,Set a)
723 partition p0 t0 = toPair $ go p0 t0
724 where
725 go _ Tip = (Tip :*: Tip)
726 go p t@(Bin _ x l r) = case (go p l, go p r) of
727 ((l1 :*: l2), (r1 :*: r2))
728 | p x -> (if l1 `ptrEq` l && r1 `ptrEq` r
729 then t
730 else link x l1 r1) :*: merge l2 r2
731 | otherwise -> merge l1 r1 :*:
732 (if l2 `ptrEq` l && r2 `ptrEq` r
733 then t
734 else link x l2 r2)
735
736 {----------------------------------------------------------------------
737 Map
738 ----------------------------------------------------------------------}
739
740 -- | /O(n*log n)/.
741 -- @'map' f s@ is the set obtained by applying @f@ to each element of @s@.
742 --
743 -- It's worth noting that the size of the result may be smaller if,
744 -- for some @(x,y)@, @x \/= y && f x == f y@
745
746 map :: Ord b => (a->b) -> Set a -> Set b
747 map f = fromList . List.map f . toList
748 #if __GLASGOW_HASKELL__
749 {-# INLINABLE map #-}
750 #endif
751
752 -- | /O(n)/. The
753 --
754 -- @'mapMonotonic' f s == 'map' f s@, but works only when @f@ is strictly increasing.
755 -- /The precondition is not checked./
756 -- Semi-formally, we have:
757 --
758 -- > and [x < y ==> f x < f y | x <- ls, y <- ls]
759 -- > ==> mapMonotonic f s == map f s
760 -- > where ls = toList s
761
762 mapMonotonic :: (a->b) -> Set a -> Set b
763 mapMonotonic _ Tip = Tip
764 mapMonotonic f (Bin sz x l r) = Bin sz (f x) (mapMonotonic f l) (mapMonotonic f r)
765
766 {--------------------------------------------------------------------
767 Fold
768 --------------------------------------------------------------------}
769 -- | /O(n)/. Fold the elements in the set using the given right-associative
770 -- binary operator. This function is an equivalent of 'foldr' and is present
771 -- for compatibility only.
772 --
773 -- /Please note that fold will be deprecated in the future and removed./
774 fold :: (a -> b -> b) -> b -> Set a -> b
775 fold = foldr
776 {-# INLINE fold #-}
777
778 -- | /O(n)/. Fold the elements in the set using the given right-associative
779 -- binary operator, such that @'foldr' f z == 'Prelude.foldr' f z . 'toAscList'@.
780 --
781 -- For example,
782 --
783 -- > toAscList set = foldr (:) [] set
784 foldr :: (a -> b -> b) -> b -> Set a -> b
785 foldr f z = go z
786 where
787 go z' Tip = z'
788 go z' (Bin _ x l r) = go (f x (go z' r)) l
789 {-# INLINE foldr #-}
790
791 -- | /O(n)/. A strict version of 'foldr'. Each application of the operator is
792 -- evaluated before using the result in the next application. This
793 -- function is strict in the starting value.
794 foldr' :: (a -> b -> b) -> b -> Set a -> b
795 foldr' f z = go z
796 where
797 go !z' Tip = z'
798 go z' (Bin _ x l r) = go (f x (go z' r)) l
799 {-# INLINE foldr' #-}
800
801 -- | /O(n)/. Fold the elements in the set using the given left-associative
802 -- binary operator, such that @'foldl' f z == 'Prelude.foldl' f z . 'toAscList'@.
803 --
804 -- For example,
805 --
806 -- > toDescList set = foldl (flip (:)) [] set
807 foldl :: (a -> b -> a) -> a -> Set b -> a
808 foldl f z = go z
809 where
810 go z' Tip = z'
811 go z' (Bin _ x l r) = go (f (go z' l) x) r
812 {-# INLINE foldl #-}
813
814 -- | /O(n)/. A strict version of 'foldl'. Each application of the operator is
815 -- evaluated before using the result in the next application. This
816 -- function is strict in the starting value.
817 foldl' :: (a -> b -> a) -> a -> Set b -> a
818 foldl' f z = go z
819 where
820 go !z' Tip = z'
821 go z' (Bin _ x l r) = go (f (go z' l) x) r
822 {-# INLINE foldl' #-}
823
824 {--------------------------------------------------------------------
825 List variations
826 --------------------------------------------------------------------}
827 -- | /O(n)/. An alias of 'toAscList'. The elements of a set in ascending order.
828 -- Subject to list fusion.
829 elems :: Set a -> [a]
830 elems = toAscList
831
832 {--------------------------------------------------------------------
833 Lists
834 --------------------------------------------------------------------}
835 #if __GLASGOW_HASKELL__ >= 708
836 instance (Ord a) => GHCExts.IsList (Set a) where
837 type Item (Set a) = a
838 fromList = fromList
839 toList = toList
840 #endif
841
842 -- | /O(n)/. Convert the set to a list of elements. Subject to list fusion.
843 toList :: Set a -> [a]
844 toList = toAscList
845
846 -- | /O(n)/. Convert the set to an ascending list of elements. Subject to list fusion.
847 toAscList :: Set a -> [a]
848 toAscList = foldr (:) []
849
850 -- | /O(n)/. Convert the set to a descending list of elements. Subject to list
851 -- fusion.
852 toDescList :: Set a -> [a]
853 toDescList = foldl (flip (:)) []
854
855 -- List fusion for the list generating functions.
856 #if __GLASGOW_HASKELL__
857 -- The foldrFB and foldlFB are foldr and foldl equivalents, used for list fusion.
858 -- They are important to convert unfused to{Asc,Desc}List back, see mapFB in prelude.
859 foldrFB :: (a -> b -> b) -> b -> Set a -> b
860 foldrFB = foldr
861 {-# INLINE[0] foldrFB #-}
862 foldlFB :: (a -> b -> a) -> a -> Set b -> a
863 foldlFB = foldl
864 {-# INLINE[0] foldlFB #-}
865
866 -- Inline elems and toList, so that we need to fuse only toAscList.
867 {-# INLINE elems #-}
868 {-# INLINE toList #-}
869
870 -- The fusion is enabled up to phase 2 included. If it does not succeed,
871 -- convert in phase 1 the expanded to{Asc,Desc}List calls back to
872 -- to{Asc,Desc}List. In phase 0, we inline fold{lr}FB (which were used in
873 -- a list fusion, otherwise it would go away in phase 1), and let compiler do
874 -- whatever it wants with to{Asc,Desc}List -- it was forbidden to inline it
875 -- before phase 0, otherwise the fusion rules would not fire at all.
876 {-# NOINLINE[0] toAscList #-}
877 {-# NOINLINE[0] toDescList #-}
878 {-# RULES "Set.toAscList" [~1] forall s . toAscList s = build (\c n -> foldrFB c n s) #-}
879 {-# RULES "Set.toAscListBack" [1] foldrFB (:) [] = toAscList #-}
880 {-# RULES "Set.toDescList" [~1] forall s . toDescList s = build (\c n -> foldlFB (\xs x -> c x xs) n s) #-}
881 {-# RULES "Set.toDescListBack" [1] foldlFB (\xs x -> x : xs) [] = toDescList #-}
882 #endif
883
884 -- | /O(n*log n)/. Create a set from a list of elements.
885 --
886 -- If the elements are ordered, a linear-time implementation is used,
887 -- with the performance equal to 'fromDistinctAscList'.
888
889 -- For some reason, when 'singleton' is used in fromList or in
890 -- create, it is not inlined, so we inline it manually.
891 fromList :: Ord a => [a] -> Set a
892 fromList [] = Tip
893 fromList [x] = Bin 1 x Tip Tip
894 fromList (x0 : xs0) | not_ordered x0 xs0 = fromList' (Bin 1 x0 Tip Tip) xs0
895 | otherwise = go (1::Int) (Bin 1 x0 Tip Tip) xs0
896 where
897 not_ordered _ [] = False
898 not_ordered x (y : _) = x >= y
899 {-# INLINE not_ordered #-}
900
901 fromList' t0 xs = foldlStrict ins t0 xs
902 where ins t x = insert x t
903
904 go !_ t [] = t
905 go _ t [x] = insertMax x t
906 go s l xs@(x : xss) | not_ordered x xss = fromList' l xs
907 | otherwise = case create s xss of
908 (r, ys, []) -> go (s `shiftL` 1) (link x l r) ys
909 (r, _, ys) -> fromList' (link x l r) ys
910
911 -- The create is returning a triple (tree, xs, ys). Both xs and ys
912 -- represent not yet processed elements and only one of them can be nonempty.
913 -- If ys is nonempty, the keys in ys are not ordered with respect to tree
914 -- and must be inserted using fromList'. Otherwise the keys have been
915 -- ordered so far.
916 create !_ [] = (Tip, [], [])
917 create s xs@(x : xss)
918 | s == 1 = if not_ordered x xss then (Bin 1 x Tip Tip, [], xss)
919 else (Bin 1 x Tip Tip, xss, [])
920 | otherwise = case create (s `shiftR` 1) xs of
921 res@(_, [], _) -> res
922 (l, [y], zs) -> (insertMax y l, [], zs)
923 (l, ys@(y:yss), _) | not_ordered y yss -> (l, [], ys)
924 | otherwise -> case create (s `shiftR` 1) yss of
925 (r, zs, ws) -> (link y l r, zs, ws)
926 #if __GLASGOW_HASKELL__
927 {-# INLINABLE fromList #-}
928 #endif
929
930 {--------------------------------------------------------------------
931 Building trees from ascending/descending lists can be done in linear time.
932
933 Note that if [xs] is ascending that:
934 fromAscList xs == fromList xs
935 --------------------------------------------------------------------}
936 -- | /O(n)/. Build a set from an ascending list in linear time.
937 -- /The precondition (input list is ascending) is not checked./
938 fromAscList :: Eq a => [a] -> Set a
939 fromAscList xs = fromDistinctAscList (combineEq xs)
940 #if __GLASGOW_HASKELL__
941 {-# INLINABLE fromAscList #-}
942 #endif
943
944 -- | /O(n)/. Build a set from a descending list in linear time.
945 -- /The precondition (input list is descending) is not checked./
946 fromDescList :: Eq a => [a] -> Set a
947 fromDescList xs = fromDistinctDescList (combineEq xs)
948 #if __GLASGOW_HASKELL__
949 {-# INLINABLE fromDescList #-}
950 #endif
951
952 -- [combineEq xs] combines equal elements with [const] in an ordered list [xs]
953 --
954 -- TODO: combineEq allocates an intermediate list. It *should* be better to
955 -- make fromAscListBy and fromDescListBy the fundamental operations, and to
956 -- implement the rest using those.
957 combineEq :: Eq a => [a] -> [a]
958 combineEq [] = []
959 combineEq (x : xs) = combineEq' x xs
960 where
961 combineEq' z [] = [z]
962 combineEq' z (y:ys)
963 | z == y = combineEq' z ys
964 | otherwise = z : combineEq' y ys
965
966 -- | /O(n)/. Build a set from an ascending list of distinct elements in linear time.
967 -- /The precondition (input list is strictly ascending) is not checked./
968
969 -- For some reason, when 'singleton' is used in fromDistinctAscList or in
970 -- create, it is not inlined, so we inline it manually.
971 fromDistinctAscList :: [a] -> Set a
972 fromDistinctAscList [] = Tip
973 fromDistinctAscList (x0 : xs0) = go (1::Int) (Bin 1 x0 Tip Tip) xs0
974 where
975 go !_ t [] = t
976 go s l (x : xs) = case create s xs of
977 (r :*: ys) -> go (s `shiftL` 1) (link x l r) ys
978
979 create !_ [] = (Tip :*: [])
980 create s xs@(x : xs')
981 | s == 1 = (Bin 1 x Tip Tip :*: xs')
982 | otherwise = case create (s `shiftR` 1) xs of
983 res@(_ :*: []) -> res
984 (l :*: (y:ys)) -> case create (s `shiftR` 1) ys of
985 (r :*: zs) -> (link y l r :*: zs)
986
987 -- | /O(n)/. Build a set from a descending list of distinct elements in linear time.
988 -- /The precondition (input list is strictly descending) is not checked./
989
990 -- For some reason, when 'singleton' is used in fromDistinctDescList or in
991 -- create, it is not inlined, so we inline it manually.
992 fromDistinctDescList :: [a] -> Set a
993 fromDistinctDescList [] = Tip
994 fromDistinctDescList (x0 : xs0) = go (1::Int) (Bin 1 x0 Tip Tip) xs0
995 where
996 go !_ t [] = t
997 go s r (x : xs) = case create s xs of
998 (l :*: ys) -> go (s `shiftL` 1) (link x l r) ys
999
1000 create !_ [] = (Tip :*: [])
1001 create s xs@(x : xs')
1002 | s == 1 = (Bin 1 x Tip Tip :*: xs')
1003 | otherwise = case create (s `shiftR` 1) xs of
1004 res@(_ :*: []) -> res
1005 (r :*: (y:ys)) -> case create (s `shiftR` 1) ys of
1006 (l :*: zs) -> (link y l r :*: zs)
1007
1008 {--------------------------------------------------------------------
1009 Eq converts the set to a list. In a lazy setting, this
1010 actually seems one of the faster methods to compare two trees
1011 and it is certainly the simplest :-)
1012 --------------------------------------------------------------------}
1013 instance Eq a => Eq (Set a) where
1014 t1 == t2 = (size t1 == size t2) && (toAscList t1 == toAscList t2)
1015
1016 {--------------------------------------------------------------------
1017 Ord
1018 --------------------------------------------------------------------}
1019
1020 instance Ord a => Ord (Set a) where
1021 compare s1 s2 = compare (toAscList s1) (toAscList s2)
1022
1023 {--------------------------------------------------------------------
1024 Show
1025 --------------------------------------------------------------------}
1026 instance Show a => Show (Set a) where
1027 showsPrec p xs = showParen (p > 10) $
1028 showString "fromList " . shows (toList xs)
1029
1030 {--------------------------------------------------------------------
1031 Read
1032 --------------------------------------------------------------------}
1033 instance (Read a, Ord a) => Read (Set a) where
1034 #ifdef __GLASGOW_HASKELL__
1035 readPrec = parens $ prec 10 $ do
1036 Ident "fromList" <- lexP
1037 xs <- readPrec
1038 return (fromList xs)
1039
1040 readListPrec = readListPrecDefault
1041 #else
1042 readsPrec p = readParen (p > 10) $ \ r -> do
1043 ("fromList",s) <- lex r
1044 (xs,t) <- reads s
1045 return (fromList xs,t)
1046 #endif
1047
1048 {--------------------------------------------------------------------
1049 Typeable/Data
1050 --------------------------------------------------------------------}
1051
1052 INSTANCE_TYPEABLE1(Set)
1053
1054 {--------------------------------------------------------------------
1055 NFData
1056 --------------------------------------------------------------------}
1057
1058 instance NFData a => NFData (Set a) where
1059 rnf Tip = ()
1060 rnf (Bin _ y l r) = rnf y `seq` rnf l `seq` rnf r
1061
1062 {--------------------------------------------------------------------
1063 Split
1064 --------------------------------------------------------------------}
1065 -- | /O(log n)/. The expression (@'split' x set@) is a pair @(set1,set2)@
1066 -- where @set1@ comprises the elements of @set@ less than @x@ and @set2@
1067 -- comprises the elements of @set@ greater than @x@.
1068 split :: Ord a => a -> Set a -> (Set a,Set a)
1069 split x t = toPair $ splitS x t
1070 {-# INLINABLE split #-}
1071
1072 splitS :: Ord a => a -> Set a -> StrictPair (Set a) (Set a)
1073 splitS _ Tip = (Tip :*: Tip)
1074 splitS x (Bin _ y l r)
1075 = case compare x y of
1076 LT -> let (lt :*: gt) = splitS x l in (lt :*: link y gt r)
1077 GT -> let (lt :*: gt) = splitS x r in (link y l lt :*: gt)
1078 EQ -> (l :*: r)
1079 {-# INLINABLE splitS #-}
1080
1081 -- | /O(log n)/. Performs a 'split' but also returns whether the pivot
1082 -- element was found in the original set.
1083 splitMember :: Ord a => a -> Set a -> (Set a,Bool,Set a)
1084 splitMember _ Tip = (Tip, False, Tip)
1085 splitMember x (Bin _ y l r)
1086 = case compare x y of
1087 LT -> let (lt, found, gt) = splitMember x l
1088 !gt' = link y gt r
1089 in (lt, found, gt')
1090 GT -> let (lt, found, gt) = splitMember x r
1091 !lt' = link y l lt
1092 in (lt', found, gt)
1093 EQ -> (l, True, r)
1094 #if __GLASGOW_HASKELL__
1095 {-# INLINABLE splitMember #-}
1096 #endif
1097
1098 {--------------------------------------------------------------------
1099 Indexing
1100 --------------------------------------------------------------------}
1101
1102 -- | /O(log n)/. Return the /index/ of an element, which is its zero-based
1103 -- index in the sorted sequence of elements. The index is a number from /0/ up
1104 -- to, but not including, the 'size' of the set. Calls 'error' when the element
1105 -- is not a 'member' of the set.
1106 --
1107 -- > findIndex 2 (fromList [5,3]) Error: element is not in the set
1108 -- > findIndex 3 (fromList [5,3]) == 0
1109 -- > findIndex 5 (fromList [5,3]) == 1
1110 -- > findIndex 6 (fromList [5,3]) Error: element is not in the set
1111
1112 -- See Note: Type of local 'go' function
1113 findIndex :: Ord a => a -> Set a -> Int
1114 findIndex = go 0
1115 where
1116 go :: Ord a => Int -> a -> Set a -> Int
1117 go !_ !_ Tip = error "Set.findIndex: element is not in the set"
1118 go idx x (Bin _ kx l r) = case compare x kx of
1119 LT -> go idx x l
1120 GT -> go (idx + size l + 1) x r
1121 EQ -> idx + size l
1122 #if __GLASGOW_HASKELL__
1123 {-# INLINABLE findIndex #-}
1124 #endif
1125
1126 -- | /O(log n)/. Lookup the /index/ of an element, which is its zero-based index in
1127 -- the sorted sequence of elements. The index is a number from /0/ up to, but not
1128 -- including, the 'size' of the set.
1129 --
1130 -- > isJust (lookupIndex 2 (fromList [5,3])) == False
1131 -- > fromJust (lookupIndex 3 (fromList [5,3])) == 0
1132 -- > fromJust (lookupIndex 5 (fromList [5,3])) == 1
1133 -- > isJust (lookupIndex 6 (fromList [5,3])) == False
1134
1135 -- See Note: Type of local 'go' function
1136 lookupIndex :: Ord a => a -> Set a -> Maybe Int
1137 lookupIndex = go 0
1138 where
1139 go :: Ord a => Int -> a -> Set a -> Maybe Int
1140 go !_ !_ Tip = Nothing
1141 go idx x (Bin _ kx l r) = case compare x kx of
1142 LT -> go idx x l
1143 GT -> go (idx + size l + 1) x r
1144 EQ -> Just $! idx + size l
1145 #if __GLASGOW_HASKELL__
1146 {-# INLINABLE lookupIndex #-}
1147 #endif
1148
1149 -- | /O(log n)/. Retrieve an element by its /index/, i.e. by its zero-based
1150 -- index in the sorted sequence of elements. If the /index/ is out of range (less
1151 -- than zero, greater or equal to 'size' of the set), 'error' is called.
1152 --
1153 -- > elemAt 0 (fromList [5,3]) == 3
1154 -- > elemAt 1 (fromList [5,3]) == 5
1155 -- > elemAt 2 (fromList [5,3]) Error: index out of range
1156
1157 elemAt :: Int -> Set a -> a
1158 elemAt !_ Tip = error "Set.elemAt: index out of range"
1159 elemAt i (Bin _ x l r)
1160 = case compare i sizeL of
1161 LT -> elemAt i l
1162 GT -> elemAt (i-sizeL-1) r
1163 EQ -> x
1164 where
1165 sizeL = size l
1166
1167 -- | /O(log n)/. Delete the element at /index/, i.e. by its zero-based index in
1168 -- the sorted sequence of elements. If the /index/ is out of range (less than zero,
1169 -- greater or equal to 'size' of the set), 'error' is called.
1170 --
1171 -- > deleteAt 0 (fromList [5,3]) == singleton 5
1172 -- > deleteAt 1 (fromList [5,3]) == singleton 3
1173 -- > deleteAt 2 (fromList [5,3]) Error: index out of range
1174 -- > deleteAt (-1) (fromList [5,3]) Error: index out of range
1175
1176 deleteAt :: Int -> Set a -> Set a
1177 deleteAt !i t =
1178 case t of
1179 Tip -> error "Set.deleteAt: index out of range"
1180 Bin _ x l r -> case compare i sizeL of
1181 LT -> balanceR x (deleteAt i l) r
1182 GT -> balanceL x l (deleteAt (i-sizeL-1) r)
1183 EQ -> glue l r
1184 where
1185 sizeL = size l
1186
1187 -- | Take a given number of elements in order, beginning
1188 -- with the smallest ones.
1189 --
1190 -- @
1191 -- take n = 'fromDistinctAscList' . 'Prelude.take' n . 'toAscList'
1192 -- @
1193 take :: Int -> Set a -> Set a
1194 take i m | i >= size m = m
1195 take i0 m0 = go i0 m0
1196 where
1197 go i !_ | i <= 0 = Tip
1198 go !_ Tip = Tip
1199 go i (Bin _ x l r) =
1200 case compare i sizeL of
1201 LT -> go i l
1202 GT -> link x l (go (i - sizeL - 1) r)
1203 EQ -> l
1204 where sizeL = size l
1205
1206 -- | Drop a given number of elements in order, beginning
1207 -- with the smallest ones.
1208 --
1209 -- @
1210 -- drop n = 'fromDistinctAscList' . 'Prelude.drop' n . 'toAscList'
1211 -- @
1212 drop :: Int -> Set a -> Set a
1213 drop i m | i >= size m = Tip
1214 drop i0 m0 = go i0 m0
1215 where
1216 go i m | i <= 0 = m
1217 go !_ Tip = Tip
1218 go i (Bin _ x l r) =
1219 case compare i sizeL of
1220 LT -> link x (go i l) r
1221 GT -> go (i - sizeL - 1) r
1222 EQ -> insertMin x r
1223 where sizeL = size l
1224
1225 -- | /O(log n)/. Split a set at a particular index.
1226 --
1227 -- @
1228 -- splitAt !n !xs = ('take' n xs, 'drop' n xs)
1229 -- @
1230 splitAt :: Int -> Set a -> (Set a, Set a)
1231 splitAt i0 m0
1232 | i0 >= size m0 = (m0, Tip)
1233 | otherwise = toPair $ go i0 m0
1234 where
1235 go i m | i <= 0 = Tip :*: m
1236 go !_ Tip = Tip :*: Tip
1237 go i (Bin _ x l r)
1238 = case compare i sizeL of
1239 LT -> case go i l of
1240 ll :*: lr -> ll :*: link x lr r
1241 GT -> case go (i - sizeL - 1) r of
1242 rl :*: rr -> link x l rl :*: rr
1243 EQ -> l :*: insertMin x r
1244 where sizeL = size l
1245
1246 -- | /O(log n)/. Take while a predicate on the elements holds.
1247 -- The user is responsible for ensuring that for all elements @j@ and @k@ in the set,
1248 -- @j \< k ==\> p j \>= p k@. See note at 'spanAntitone'.
1249 --
1250 -- @
1251 -- takeWhileAntitone p = 'fromDistinctAscList' . 'Data.List.takeWhile' p . 'toList'
1252 -- takeWhileAntitone p = 'filter' p
1253 -- @
1254
1255 takeWhileAntitone :: (a -> Bool) -> Set a -> Set a
1256 takeWhileAntitone _ Tip = Tip
1257 takeWhileAntitone p (Bin _ x l r)
1258 | p x = link x l (takeWhileAntitone p r)
1259 | otherwise = takeWhileAntitone p l
1260
1261 -- | /O(log n)/. Drop while a predicate on the elements holds.
1262 -- The user is responsible for ensuring that for all elements @j@ and @k@ in the set,
1263 -- @j \< k ==\> p j \>= p k@. See note at 'spanAntitone'.
1264 --
1265 -- @
1266 -- dropWhileAntitone p = 'fromDistinctAscList' . 'Data.List.dropWhile' p . 'toList'
1267 -- dropWhileAntitone p = 'filter' (not . p)
1268 -- @
1269
1270 dropWhileAntitone :: (a -> Bool) -> Set a -> Set a
1271 dropWhileAntitone _ Tip = Tip
1272 dropWhileAntitone p (Bin _ x l r)
1273 | p x = dropWhileAntitone p r
1274 | otherwise = link x (dropWhileAntitone p l) r
1275
1276 -- | /O(log n)/. Divide a set at the point where a predicate on the elements stops holding.
1277 -- The user is responsible for ensuring that for all elements @j@ and @k@ in the set,
1278 -- @j \< k ==\> p j \>= p k@.
1279 --
1280 -- @
1281 -- spanAntitone p xs = ('takeWhileAntitone' p xs, 'dropWhileAntitone' p xs)
1282 -- spanAntitone p xs = partition p xs
1283 -- @
1284 --
1285 -- Note: if @p@ is not actually antitone, then @spanAntitone@ will split the set
1286 -- at some /unspecified/ point where the predicate switches from holding to not
1287 -- holding (where the predicate is seen to hold before the first element and to fail
1288 -- after the last element).
1289
1290 spanAntitone :: (a -> Bool) -> Set a -> (Set a, Set a)
1291 spanAntitone p0 m = toPair (go p0 m)
1292 where
1293 go _ Tip = Tip :*: Tip
1294 go p (Bin _ x l r)
1295 | p x = let u :*: v = go p r in link x l u :*: v
1296 | otherwise = let u :*: v = go p l in u :*: link x v r
1297
1298
1299 {--------------------------------------------------------------------
1300 Utility functions that maintain the balance properties of the tree.
1301 All constructors assume that all values in [l] < [x] and all values
1302 in [r] > [x], and that [l] and [r] are valid trees.
1303
1304 In order of sophistication:
1305 [Bin sz x l r] The type constructor.
1306 [bin x l r] Maintains the correct size, assumes that both [l]
1307 and [r] are balanced with respect to each other.
1308 [balance x l r] Restores the balance and size.
1309 Assumes that the original tree was balanced and
1310 that [l] or [r] has changed by at most one element.
1311 [link x l r] Restores balance and size.
1312
1313 Furthermore, we can construct a new tree from two trees. Both operations
1314 assume that all values in [l] < all values in [r] and that [l] and [r]
1315 are valid:
1316 [glue l r] Glues [l] and [r] together. Assumes that [l] and
1317 [r] are already balanced with respect to each other.
1318 [merge l r] Merges two trees and restores balance.
1319 --------------------------------------------------------------------}
1320
1321 {--------------------------------------------------------------------
1322 Link
1323 --------------------------------------------------------------------}
1324 link :: a -> Set a -> Set a -> Set a
1325 link x Tip r = insertMin x r
1326 link x l Tip = insertMax x l
1327 link x l@(Bin sizeL y ly ry) r@(Bin sizeR z lz rz)
1328 | delta*sizeL < sizeR = balanceL z (link x l lz) rz
1329 | delta*sizeR < sizeL = balanceR y ly (link x ry r)
1330 | otherwise = bin x l r
1331
1332
1333 -- insertMin and insertMax don't perform potentially expensive comparisons.
1334 insertMax,insertMin :: a -> Set a -> Set a
1335 insertMax x t
1336 = case t of
1337 Tip -> singleton x
1338 Bin _ y l r
1339 -> balanceR y l (insertMax x r)
1340
1341 insertMin x t
1342 = case t of
1343 Tip -> singleton x
1344 Bin _ y l r
1345 -> balanceL y (insertMin x l) r
1346
1347 {--------------------------------------------------------------------
1348 [merge l r]: merges two trees.
1349 --------------------------------------------------------------------}
1350 merge :: Set a -> Set a -> Set a
1351 merge Tip r = r
1352 merge l Tip = l
1353 merge l@(Bin sizeL x lx rx) r@(Bin sizeR y ly ry)
1354 | delta*sizeL < sizeR = balanceL y (merge l ly) ry
1355 | delta*sizeR < sizeL = balanceR x lx (merge rx r)
1356 | otherwise = glue l r
1357
1358 {--------------------------------------------------------------------
1359 [glue l r]: glues two trees together.
1360 Assumes that [l] and [r] are already balanced with respect to each other.
1361 --------------------------------------------------------------------}
1362 glue :: Set a -> Set a -> Set a
1363 glue Tip r = r
1364 glue l Tip = l
1365 glue l r
1366 | size l > size r = let (m,l') = deleteFindMax l in balanceR m l' r
1367 | otherwise = let (m,r') = deleteFindMin r in balanceL m l r'
1368
1369 -- | /O(log n)/. Delete and find the minimal element.
1370 --
1371 -- > deleteFindMin set = (findMin set, deleteMin set)
1372
1373 deleteFindMin :: Set a -> (a,Set a)
1374 deleteFindMin t
1375 = case t of
1376 Bin _ x Tip r -> (x,r)
1377 Bin _ x l r -> let (xm,l') = deleteFindMin l in (xm,balanceR x l' r)
1378 Tip -> (error "Set.deleteFindMin: can not return the minimal element of an empty set", Tip)
1379
1380 -- | /O(log n)/. Delete and find the maximal element.
1381 --
1382 -- > deleteFindMax set = (findMax set, deleteMax set)
1383 deleteFindMax :: Set a -> (a,Set a)
1384 deleteFindMax t
1385 = case t of
1386 Bin _ x l Tip -> (x,l)
1387 Bin _ x l r -> let (xm,r') = deleteFindMax r in (xm,balanceL x l r')
1388 Tip -> (error "Set.deleteFindMax: can not return the maximal element of an empty set", Tip)
1389
1390 -- | /O(log n)/. Retrieves the minimal key of the set, and the set
1391 -- stripped of that element, or 'Nothing' if passed an empty set.
1392 minView :: Set a -> Maybe (a, Set a)
1393 minView Tip = Nothing
1394 minView x = Just (deleteFindMin x)
1395
1396 -- | /O(log n)/. Retrieves the maximal key of the set, and the set
1397 -- stripped of that element, or 'Nothing' if passed an empty set.
1398 maxView :: Set a -> Maybe (a, Set a)
1399 maxView Tip = Nothing
1400 maxView x = Just (deleteFindMax x)
1401
1402 {--------------------------------------------------------------------
1403 [balance x l r] balances two trees with value x.
1404 The sizes of the trees should balance after decreasing the
1405 size of one of them. (a rotation).
1406
1407 [delta] is the maximal relative difference between the sizes of
1408 two trees, it corresponds with the [w] in Adams' paper.
1409 [ratio] is the ratio between an outer and inner sibling of the
1410 heavier subtree in an unbalanced setting. It determines
1411 whether a double or single rotation should be performed
1412 to restore balance. It is correspondes with the inverse
1413 of $\alpha$ in Adam's article.
1414
1415 Note that according to the Adam's paper:
1416 - [delta] should be larger than 4.646 with a [ratio] of 2.
1417 - [delta] should be larger than 3.745 with a [ratio] of 1.534.
1418
1419 But the Adam's paper is errorneous:
1420 - it can be proved that for delta=2 and delta>=5 there does
1421 not exist any ratio that would work
1422 - delta=4.5 and ratio=2 does not work
1423
1424 That leaves two reasonable variants, delta=3 and delta=4,
1425 both with ratio=2.
1426
1427 - A lower [delta] leads to a more 'perfectly' balanced tree.
1428 - A higher [delta] performs less rebalancing.
1429
1430 In the benchmarks, delta=3 is faster on insert operations,
1431 and delta=4 has slightly better deletes. As the insert speedup
1432 is larger, we currently use delta=3.
1433
1434 --------------------------------------------------------------------}
1435 delta,ratio :: Int
1436 delta = 3
1437 ratio = 2
1438
1439 -- The balance function is equivalent to the following:
1440 --
1441 -- balance :: a -> Set a -> Set a -> Set a
1442 -- balance x l r
1443 -- | sizeL + sizeR <= 1 = Bin sizeX x l r
1444 -- | sizeR > delta*sizeL = rotateL x l r
1445 -- | sizeL > delta*sizeR = rotateR x l r
1446 -- | otherwise = Bin sizeX x l r
1447 -- where
1448 -- sizeL = size l
1449 -- sizeR = size r
1450 -- sizeX = sizeL + sizeR + 1
1451 --
1452 -- rotateL :: a -> Set a -> Set a -> Set a
1453 -- rotateL x l r@(Bin _ _ ly ry) | size ly < ratio*size ry = singleL x l r
1454 -- | otherwise = doubleL x l r
1455 -- rotateR :: a -> Set a -> Set a -> Set a
1456 -- rotateR x l@(Bin _ _ ly ry) r | size ry < ratio*size ly = singleR x l r
1457 -- | otherwise = doubleR x l r
1458 --
1459 -- singleL, singleR :: a -> Set a -> Set a -> Set a
1460 -- singleL x1 t1 (Bin _ x2 t2 t3) = bin x2 (bin x1 t1 t2) t3
1461 -- singleR x1 (Bin _ x2 t1 t2) t3 = bin x2 t1 (bin x1 t2 t3)
1462 --
1463 -- doubleL, doubleR :: a -> Set a -> Set a -> Set a
1464 -- doubleL x1 t1 (Bin _ x2 (Bin _ x3 t2 t3) t4) = bin x3 (bin x1 t1 t2) (bin x2 t3 t4)
1465 -- doubleR x1 (Bin _ x2 t1 (Bin _ x3 t2 t3)) t4 = bin x3 (bin x2 t1 t2) (bin x1 t3 t4)
1466 --
1467 -- It is only written in such a way that every node is pattern-matched only once.
1468 --
1469 -- Only balanceL and balanceR are needed at the moment, so balance is not here anymore.
1470 -- In case it is needed, it can be found in Data.Map.
1471
1472 -- Functions balanceL and balanceR are specialised versions of balance.
1473 -- balanceL only checks whether the left subtree is too big,
1474 -- balanceR only checks whether the right subtree is too big.
1475
1476 -- balanceL is called when left subtree might have been inserted to or when
1477 -- right subtree might have been deleted from.
1478 balanceL :: a -> Set a -> Set a -> Set a
1479 balanceL x l r = case r of
1480 Tip -> case l of
1481 Tip -> Bin 1 x Tip Tip
1482 (Bin _ _ Tip Tip) -> Bin 2 x l Tip
1483 (Bin _ lx Tip (Bin _ lrx _ _)) -> Bin 3 lrx (Bin 1 lx Tip Tip) (Bin 1 x Tip Tip)
1484 (Bin _ lx ll@(Bin _ _ _ _) Tip) -> Bin 3 lx ll (Bin 1 x Tip Tip)
1485 (Bin ls lx ll@(Bin lls _ _ _) lr@(Bin lrs lrx lrl lrr))
1486 | lrs < ratio*lls -> Bin (1+ls) lx ll (Bin (1+lrs) x lr Tip)
1487 | otherwise -> Bin (1+ls) lrx (Bin (1+lls+size lrl) lx ll lrl) (Bin (1+size lrr) x lrr Tip)
1488
1489 (Bin rs _ _ _) -> case l of
1490 Tip -> Bin (1+rs) x Tip r
1491
1492 (Bin ls lx ll lr)
1493 | ls > delta*rs -> case (ll, lr) of
1494 (Bin lls _ _ _, Bin lrs lrx lrl lrr)
1495 | lrs < ratio*lls -> Bin (1+ls+rs) lx ll (Bin (1+rs+lrs) x lr r)
1496 | otherwise -> Bin (1+ls+rs) lrx (Bin (1+lls+size lrl) lx ll lrl) (Bin (1+rs+size lrr) x lrr r)
1497 (_, _) -> error "Failure in Data.Map.balanceL"
1498 | otherwise -> Bin (1+ls+rs) x l r
1499 {-# NOINLINE balanceL #-}
1500
1501 -- balanceR is called when right subtree might have been inserted to or when
1502 -- left subtree might have been deleted from.
1503 balanceR :: a -> Set a -> Set a -> Set a
1504 balanceR x l r = case l of
1505 Tip -> case r of
1506 Tip -> Bin 1 x Tip Tip
1507 (Bin _ _ Tip Tip) -> Bin 2 x Tip r
1508 (Bin _ rx Tip rr@(Bin _ _ _ _)) -> Bin 3 rx (Bin 1 x Tip Tip) rr
1509 (Bin _ rx (Bin _ rlx _ _) Tip) -> Bin 3 rlx (Bin 1 x Tip Tip) (Bin 1 rx Tip Tip)
1510 (Bin rs rx rl@(Bin rls rlx rll rlr) rr@(Bin rrs _ _ _))
1511 | rls < ratio*rrs -> Bin (1+rs) rx (Bin (1+rls) x Tip rl) rr
1512 | otherwise -> Bin (1+rs) rlx (Bin (1+size rll) x Tip rll) (Bin (1+rrs+size rlr) rx rlr rr)
1513
1514 (Bin ls _ _ _) -> case r of
1515 Tip -> Bin (1+ls) x l Tip
1516
1517 (Bin rs rx rl rr)
1518 | rs > delta*ls -> case (rl, rr) of
1519 (Bin rls rlx rll rlr, Bin rrs _ _ _)
1520 | rls < ratio*rrs -> Bin (1+ls+rs) rx (Bin (1+ls+rls) x l rl) rr
1521 | otherwise -> Bin (1+ls+rs) rlx (Bin (1+ls+size rll) x l rll) (Bin (1+rrs+size rlr) rx rlr rr)
1522 (_, _) -> error "Failure in Data.Map.balanceR"
1523 | otherwise -> Bin (1+ls+rs) x l r
1524 {-# NOINLINE balanceR #-}
1525
1526 {--------------------------------------------------------------------
1527 The bin constructor maintains the size of the tree
1528 --------------------------------------------------------------------}
1529 bin :: a -> Set a -> Set a -> Set a
1530 bin x l r
1531 = Bin (size l + size r + 1) x l r
1532 {-# INLINE bin #-}
1533
1534
1535 {--------------------------------------------------------------------
1536 Utilities
1537 --------------------------------------------------------------------}
1538
1539 -- | /O(1)/. Decompose a set into pieces based on the structure of the underlying
1540 -- tree. This function is useful for consuming a set in parallel.
1541 --
1542 -- No guarantee is made as to the sizes of the pieces; an internal, but
1543 -- deterministic process determines this. However, it is guaranteed that the pieces
1544 -- returned will be in ascending order (all elements in the first subset less than all
1545 -- elements in the second, and so on).
1546 --
1547 -- Examples:
1548 --
1549 -- > splitRoot (fromList [1..6]) ==
1550 -- > [fromList [1,2,3],fromList [4],fromList [5,6]]
1551 --
1552 -- > splitRoot empty == []
1553 --
1554 -- Note that the current implementation does not return more than three subsets,
1555 -- but you should not depend on this behaviour because it can change in the
1556 -- future without notice.
1557 splitRoot :: Set a -> [Set a]
1558 splitRoot orig =
1559 case orig of
1560 Tip -> []
1561 Bin _ v l r -> [l, singleton v, r]
1562 {-# INLINE splitRoot #-}
1563
1564
1565 {--------------------------------------------------------------------
1566 Debugging
1567 --------------------------------------------------------------------}
1568 -- | /O(n)/. Show the tree that implements the set. The tree is shown
1569 -- in a compressed, hanging format.
1570 showTree :: Show a => Set a -> String
1571 showTree s
1572 = showTreeWith True False s
1573
1574
1575 {- | /O(n)/. The expression (@showTreeWith hang wide map@) shows
1576 the tree that implements the set. If @hang@ is
1577 @True@, a /hanging/ tree is shown otherwise a rotated tree is shown. If
1578 @wide@ is 'True', an extra wide version is shown.
1579
1580 > Set> putStrLn $ showTreeWith True False $ fromDistinctAscList [1..5]
1581 > 4
1582 > +--2
1583 > | +--1
1584 > | +--3
1585 > +--5
1586 >
1587 > Set> putStrLn $ showTreeWith True True $ fromDistinctAscList [1..5]
1588 > 4
1589 > |
1590 > +--2
1591 > | |
1592 > | +--1
1593 > | |
1594 > | +--3
1595 > |
1596 > +--5
1597 >
1598 > Set> putStrLn $ showTreeWith False True $ fromDistinctAscList [1..5]
1599 > +--5
1600 > |
1601 > 4
1602 > |
1603 > | +--3
1604 > | |
1605 > +--2
1606 > |
1607 > +--1
1608
1609 -}
1610 showTreeWith :: Show a => Bool -> Bool -> Set a -> String
1611 showTreeWith hang wide t
1612 | hang = (showsTreeHang wide [] t) ""
1613 | otherwise = (showsTree wide [] [] t) ""
1614
1615 showsTree :: Show a => Bool -> [String] -> [String] -> Set a -> ShowS
1616 showsTree wide lbars rbars t
1617 = case t of
1618 Tip -> showsBars lbars . showString "|\n"
1619 Bin _ x Tip Tip
1620 -> showsBars lbars . shows x . showString "\n"
1621 Bin _ x l r
1622 -> showsTree wide (withBar rbars) (withEmpty rbars) r .
1623 showWide wide rbars .
1624 showsBars lbars . shows x . showString "\n" .
1625 showWide wide lbars .
1626 showsTree wide (withEmpty lbars) (withBar lbars) l
1627
1628 showsTreeHang :: Show a => Bool -> [String] -> Set a -> ShowS
1629 showsTreeHang wide bars t
1630 = case t of
1631 Tip -> showsBars bars . showString "|\n"
1632 Bin _ x Tip Tip
1633 -> showsBars bars . shows x . showString "\n"
1634 Bin _ x l r
1635 -> showsBars bars . shows x . showString "\n" .
1636 showWide wide bars .
1637 showsTreeHang wide (withBar bars) l .
1638 showWide wide bars .
1639 showsTreeHang wide (withEmpty bars) r
1640
1641 showWide :: Bool -> [String] -> String -> String
1642 showWide wide bars
1643 | wide = showString (concat (reverse bars)) . showString "|\n"
1644 | otherwise = id
1645
1646 showsBars :: [String] -> ShowS
1647 showsBars bars
1648 = case bars of
1649 [] -> id
1650 _ -> showString (concat (reverse (tail bars))) . showString node
1651
1652 node :: String
1653 node = "+--"
1654
1655 withBar, withEmpty :: [String] -> [String]
1656 withBar bars = "| ":bars
1657 withEmpty bars = " ":bars
1658
1659 {--------------------------------------------------------------------
1660 Assertions
1661 --------------------------------------------------------------------}
1662 -- | /O(n)/. Test if the internal set structure is valid.
1663 valid :: Ord a => Set a -> Bool
1664 valid t
1665 = balanced t && ordered t && validsize t
1666
1667 ordered :: Ord a => Set a -> Bool
1668 ordered t
1669 = bounded (const True) (const True) t
1670 where
1671 bounded lo hi t'
1672 = case t' of
1673 Tip -> True
1674 Bin _ x l r -> (lo x) && (hi x) && bounded lo (<x) l && bounded (>x) hi r
1675
1676 balanced :: Set a -> Bool
1677 balanced t
1678 = case t of
1679 Tip -> True
1680 Bin _ _ l r -> (size l + size r <= 1 || (size l <= delta*size r && size r <= delta*size l)) &&
1681 balanced l && balanced r
1682
1683 validsize :: Set a -> Bool
1684 validsize t
1685 = (realsize t == Just (size t))
1686 where
1687 realsize t'
1688 = case t' of
1689 Tip -> Just 0
1690 Bin sz _ l r -> case (realsize l,realsize r) of
1691 (Just n,Just m) | n+m+1 == sz -> Just sz
1692 _ -> Nothing