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