Data.Set
Finite Sets
The
type represents a set of elements of type Set
ee
. Most operations
require that e
be an instance of the Ord
class. A Set
is strict in its
elements.
For a walkthrough of the most commonly used functions see the sets introduction.
Note that the implementation is generally left-biased. Functions that take
two sets as arguments and combine them, such as union
and intersection
,
prefer the entries in the first argument to those in the second. Of course,
this bias can only be observed when equality is an equivalence relation
instead of structural equality.
These modules are intended to be imported qualified, to avoid name clashes with Prelude functions, e.g.
import Data.Set (Set) import qualified Data.Set as Set
Warning
The size of the set must not exceed maxBound::Int
. Violation of
this condition is not detected and if the size limit is exceeded, its
behaviour is undefined.
Implementation
The implementation of Set
is based on size balanced binary trees (or
trees of bounded balance) as described by:
- Stephen Adams, "Efficient sets: a balancing act", Journal of Functional Programming 3(4):553-562, October 1993, http://www.swiss.ai.mit.edu/~adams/BB/.
- J. Nievergelt and E.M. Reingold, "Binary search trees of bounded balance", SIAM journal of computing 2(1), March 1973.
Bounds for union
, intersection
, and difference
are as given
by
- Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "Just Join for Parallel Ordered Sets", https://arxiv.org/abs/1602.02120v3.
Set type
A set of values a
.
Instances
Foldable Set Source # | Folds in order of increasing key. |
Defined in Data.Set.Internal Methods fold :: Monoid m => Set m -> m Source # foldMap :: Monoid m => (a -> m) -> Set a -> m Source # foldMap' :: Monoid m => (a -> m) -> Set a -> m Source # foldr :: (a -> b -> b) -> b -> Set a -> b Source # foldr' :: (a -> b -> b) -> b -> Set a -> b Source # foldl :: (b -> a -> b) -> b -> Set a -> b Source # foldl' :: (b -> a -> b) -> b -> Set a -> b Source # foldr1 :: (a -> a -> a) -> Set a -> a Source # foldl1 :: (a -> a -> a) -> Set a -> a Source # toList :: Set a -> [a] Source # null :: Set a -> Bool Source # length :: Set a -> Int Source # elem :: Eq a => a -> Set a -> Bool Source # maximum :: Ord a => Set a -> a Source # minimum :: Ord a => Set a -> a Source # | |
Eq1 Set Source # | Since: containers-0.5.9 |
Ord1 Set Source # | Since: containers-0.5.9 |
Defined in Data.Set.Internal | |
Show1 Set Source # | Since: containers-0.5.9 |
Lift a => Lift (Set a :: Type) Source # | Since: containers-0.6.6 |
(Data a, Ord a) => Data (Set a) Source # | |
Defined in Data.Set.Internal Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Set a -> c (Set a) Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Set a) Source # toConstr :: Set a -> Constr Source # dataTypeOf :: Set a -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Set a)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Set a)) Source # gmapT :: (forall b. Data b => b -> b) -> Set a -> Set a Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Set a -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Set a -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Set a -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Set a -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) Source # | |
Ord a => Monoid (Set a) Source # | |
Ord a => Semigroup (Set a) Source # | Since: containers-0.5.7 |
Ord a => IsList (Set a) Source # | Since: containers-0.5.6.2 |
(Read a, Ord a) => Read (Set a) Source # | |
Show a => Show (Set a) Source # | |
NFData a => NFData (Set a) Source # | |
Defined in Data.Set.Internal | |
Eq a => Eq (Set a) Source # | |
Ord a => Ord (Set a) Source # | |
type Item (Set a) Source # | |
Defined in Data.Set.Internal |
Construction
fromList :: Ord a => [a] -> Set a Source #
If the elements are ordered, a linear-time implementation is used,
with the performance equal to fromDistinctAscList
.
fromAscList :: Eq a => [a] -> Set a Source #
fromDescList :: Eq a => [a] -> Set a Source #
Since: containers-0.5.8
fromDistinctAscList :: [a] -> Set a Source #
fromDistinctDescList :: [a] -> Set a Source #
powerSet :: Set a -> Set (Set a) Source #
t`member`
powerSet s == t`isSubsetOf`
s
Example:
powerSet (fromList [1,2,3]) = fromList $ map fromList [[],[1],[1,2],[1,2,3],[1,3],[2],[2,3],[3]]
Since: containers-0.5.11
Insertion
insert :: Ord a => a -> Set a -> Set a Source #
Deletion
Generalized insertion/deletion
alterF :: (Ord a, Functor f) => (Bool -> f Bool) -> a -> Set a -> f (Set a) Source #
(
can delete or insert alterF
f x s)x
in s
depending on
whether an equal element is found in s
.
In short:
member
x <$>alterF
f x s = f (member
x s)
Note that unlike insert
, alterF
will not replace an element equal to
the given value.
Note: alterF
is a variant of the at
combinator from Control.Lens.At.
Since: containers-0.6.3.1
Query
lookupLT :: Ord a => a -> Set a -> Maybe a Source #
lookupLT 3 (fromList [3, 5]) == Nothing lookupLT 5 (fromList [3, 5]) == Just 3
lookupGT :: Ord a => a -> Set a -> Maybe a Source #
lookupGT 4 (fromList [3, 5]) == Just 5 lookupGT 5 (fromList [3, 5]) == Nothing
lookupLE :: Ord a => a -> Set a -> Maybe a Source #
lookupLE 2 (fromList [3, 5]) == Nothing lookupLE 4 (fromList [3, 5]) == Just 3 lookupLE 5 (fromList [3, 5]) == Just 5
lookupGE :: Ord a => a -> Set a -> Maybe a Source #
lookupGE 3 (fromList [3, 5]) == Just 3 lookupGE 4 (fromList [3, 5]) == Just 5 lookupGE 6 (fromList [3, 5]) == Nothing
isSubsetOf :: Ord a => Set a -> Set a -> Bool Source #
(s1 `isSubsetOf` s2)
indicates whether s1
is a subset of s2
.
s1 `isSubsetOf` s2 = all (`member`
s2) s1 s1 `isSubsetOf` s2 = null (s1`difference`
s2) s1 `isSubsetOf` s2 = s1`union`
s2 == s2 s1 `isSubsetOf` s2 = s1`intersection`
s2 == s1
isProperSubsetOf :: Ord a => Set a -> Set a -> Bool Source #
(s1 `isProperSubsetOf` s2)
indicates whether s1
is a
proper subset of s2
.
s1 `isProperSubsetOf` s2 = s1 `isSubsetOf`
s2 && s1 /= s2
disjoint :: Ord a => Set a -> Set a -> Bool Source #
disjoint (fromList [2,4,6]) (fromList [1,3]) == True disjoint (fromList [2,4,6,8]) (fromList [2,3,5,7]) == False disjoint (fromList [1,2]) (fromList [1,2,3,4]) == False disjoint (fromList []) (fromList []) == True
xs`disjoint`
ys = null (xs`intersection`
ys)
Since: containers-0.5.11
Combine
union :: Ord a => Set a -> Set a -> Set a Source #
difference :: Ord a => Set a -> Set a -> Set a Source #
Return elements of the first set not existing in the second set.
difference (fromList [5, 3]) (fromList [5, 7]) == singleton 3
intersection :: Ord a => Set a -> Set a -> Set a Source #
import qualified Data.Set as S data AB = A | B deriving Show instance Ord AB where compare _ _ = EQ instance Eq AB where _ == _ = True main = print (S.singleton A `S.intersection` S.singleton B, S.singleton B `S.intersection` S.singleton A)
prints (fromList [A],fromList [B])
.
cartesianProduct :: Set a -> Set b -> Set (a, b) Source #
cartesianProduct xs ys = fromList $ liftA2 (,) (toList xs) (toList ys)
Example:
cartesianProduct (fromList [1,2]) (fromList ['a','b']) = fromList [(1,'a'), (1,'b'), (2,'a'), (2,'b')]
Since: containers-0.5.11
disjointUnion :: Set a -> Set b -> Set (Either a b) Source #
disjointUnion xs ys = map Left xs `union`
map Right ys
Example:
disjointUnion (fromList [1,2]) (fromList ["hi", "bye"]) = fromList [Left 1, Left 2, Right "hi", Right "bye"]
Since: containers-0.5.11
Filter
takeWhileAntitone :: (a -> Bool) -> Set a -> Set a Source #
j
and k
in the set,
j < k ==> p j >= p k
. See note at spanAntitone
.
takeWhileAntitone p =fromDistinctAscList
.takeWhile
p .toList
takeWhileAntitone p =filter
p
Since: containers-0.5.8
dropWhileAntitone :: (a -> Bool) -> Set a -> Set a Source #
j
and k
in the set,
j < k ==> p j >= p k
. See note at spanAntitone
.
dropWhileAntitone p =fromDistinctAscList
.dropWhile
p .toList
dropWhileAntitone p =filter
(not . p)
Since: containers-0.5.8
spanAntitone :: (a -> Bool) -> Set a -> (Set a, Set a) Source #
j
and k
in the set,
j < k ==> p j >= p k
.
spanAntitone p xs = (takeWhileAntitone
p xs,dropWhileAntitone
p xs) spanAntitone p xs = partition p xs
Note: if p
is not actually antitone, then spanAntitone
will split the set
at some unspecified point where the predicate switches from holding to not
holding (where the predicate is seen to hold before the first element and to fail
after the last element).
Since: containers-0.5.8
partition :: (a -> Bool) -> Set a -> (Set a, Set a) Source #
split
.
split :: Ord a => a -> Set a -> (Set a, Set a) Source #
) is a pair split
x set(set1,set2)
where set1
comprises the elements of set
less than x
and set2
comprises the elements of set
greater than x
.
splitMember :: Ord a => a -> Set a -> (Set a, Bool, Set a) Source #
split
but also returns whether the pivot
element was found in the original set.
splitRoot :: Set a -> [Set a] Source #
No guarantee is made as to the sizes of the pieces; an internal, but deterministic process determines this. However, it is guaranteed that the pieces returned will be in ascending order (all elements in the first subset less than all elements in the second, and so on).
Examples:
splitRoot (fromList [1..6]) == [fromList [1,2,3],fromList [4],fromList [5,6]]
splitRoot empty == []
Note that the current implementation does not return more than three subsets, but you should not depend on this behaviour because it can change in the future without notice.
Since: containers-0.5.4
Indexed
lookupIndex :: Ord a => a -> Set a -> Maybe Int Source #
size
of the set.
isJust (lookupIndex 2 (fromList [5,3])) == False fromJust (lookupIndex 3 (fromList [5,3])) == 0 fromJust (lookupIndex 5 (fromList [5,3])) == 1 isJust (lookupIndex 6 (fromList [5,3])) == False
Since: containers-0.5.4
findIndex :: Ord a => a -> Set a -> Int Source #
size
of the set. Calls error
when the element
is not a member
of the set.
findIndex 2 (fromList [5,3]) Error: element is not in the set findIndex 3 (fromList [5,3]) == 0 findIndex 5 (fromList [5,3]) == 1 findIndex 6 (fromList [5,3]) Error: element is not in the set
Since: containers-0.5.4
elemAt :: Int -> Set a -> a Source #
size
of the set), error
is called.
elemAt 0 (fromList [5,3]) == 3 elemAt 1 (fromList [5,3]) == 5 elemAt 2 (fromList [5,3]) Error: index out of range
Since: containers-0.5.4
deleteAt :: Int -> Set a -> Set a Source #
size
of the set), error
is called.
deleteAt 0 (fromList [5,3]) == singleton 5 deleteAt 1 (fromList [5,3]) == singleton 3 deleteAt 2 (fromList [5,3]) Error: index out of range deleteAt (-1) (fromList [5,3]) Error: index out of range
Since: containers-0.5.4
take :: Int -> Set a -> Set a Source #
take n =fromDistinctAscList
.take
n .toAscList
Since: containers-0.5.8
drop :: Int -> Set a -> Set a Source #
drop n =fromDistinctAscList
.drop
n .toAscList
Since: containers-0.5.8
Map
map :: Ord b => (a -> b) -> Set a -> Set b Source #
is the set obtained by applying map
f sf
to each element of s
.
It's worth noting that the size of the result may be smaller if,
for some (x,y)
, x /= y && f x == f y
mapMonotonic :: (a -> b) -> Set a -> Set b Source #
, but works only when mapMonotonic
f s == map
f sf
is strictly increasing.
The precondition is not checked.
Semi-formally, we have:
and [x < y ==> f x < f y | x <- ls, y <- ls] ==> mapMonotonic f s == map f s where ls = toList s
Folds
Strict folds
foldr' :: (a -> b -> b) -> b -> Set a -> b Source #
foldr
. Each application of the operator is
evaluated before using the result in the next application. This
function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> Set b -> a Source #
foldl
. Each application of the operator is
evaluated before using the result in the next application. This
function is strict in the starting value.
Legacy folds
fold :: (a -> b -> b) -> b -> Set a -> b Source #
foldr
and is present
for compatibility only.
Please note that fold will be deprecated in the future and removed.
Min/Max
deleteMin :: Set a -> Set a Source #
deleteMax :: Set a -> Set a Source #
deleteFindMin :: Set a -> (a, Set a) Source #
deleteFindMin set = (findMin set, deleteMin set)
deleteFindMax :: Set a -> (a, Set a) Source #
deleteFindMax set = (findMax set, deleteMax set)
maxView :: Set a -> Maybe (a, Set a) Source #
Nothing
if passed an empty set.
minView :: Set a -> Maybe (a, Set a) Source #
Nothing
if passed an empty set.
Conversion
List
elems :: Set a -> [a] Source #
toAscList
. The elements of a set in ascending order.
Subject to list fusion.
toAscList :: Set a -> [a] Source #
toDescList :: Set a -> [a] Source #
Debugging
showTree :: Show a => Set a -> String Source #
showTreeWith :: Show a => Bool -> Bool -> Set a -> String Source #
showTreeWith hang wide map
) shows
the tree that implements the set. If hang
is
True
, a hanging tree is shown otherwise a rotated tree is shown. If
wide
is True
, an extra wide version is shown.
Set> putStrLn $ showTreeWith True False $ fromDistinctAscList [1..5] 4 +--2 | +--1 | +--3 +--5 Set> putStrLn $ showTreeWith True True $ fromDistinctAscList [1..5] 4 | +--2 | | | +--1 | | | +--3 | +--5 Set> putStrLn $ showTreeWith False True $ fromDistinctAscList [1..5] +--5 | 4 | | +--3 | | +--2 | +--1