containers-verified-0.6.0.1: Formally verified drop-in replacement of containers

Safe HaskellSafe
LanguageHaskell2010

Data.Set

Contents

Description

Please see the documentation of containers for details.

Synopsis

Set type

data Set a :: * -> * #

A set of values a.

Instances

Foldable Set 

Methods

fold :: Monoid m => Set m -> m #

foldMap :: Monoid m => (a -> m) -> Set a -> m #

foldr :: (a -> b -> b) -> b -> Set a -> b #

foldr' :: (a -> b -> b) -> b -> Set a -> b #

foldl :: (b -> a -> b) -> b -> Set a -> b #

foldl' :: (b -> a -> b) -> b -> Set a -> b #

foldr1 :: (a -> a -> a) -> Set a -> a #

foldl1 :: (a -> a -> a) -> Set a -> a #

toList :: Set a -> [a] #

null :: Set a -> Bool #

length :: Set a -> Int #

elem :: Eq a => a -> Set a -> Bool #

maximum :: Ord a => Set a -> a #

minimum :: Ord a => Set a -> a #

sum :: Num a => Set a -> a #

product :: Num a => Set a -> a #

Eq1 Set

Since: 0.5.9

Methods

liftEq :: (a -> b -> Bool) -> Set a -> Set b -> Bool #

Ord1 Set

Since: 0.5.9

Methods

liftCompare :: (a -> b -> Ordering) -> Set a -> Set b -> Ordering #

Show1 Set

Since: 0.5.9

Methods

liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Set a -> ShowS #

liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [Set a] -> ShowS #

Ord a => IsList (Set a)

Since: 0.5.6.2

Associated Types

type Item (Set a) :: * #

Methods

fromList :: [Item (Set a)] -> Set a #

fromListN :: Int -> [Item (Set a)] -> Set a #

toList :: Set a -> [Item (Set a)] #

Eq a => Eq (Set a) 

Methods

(==) :: Set a -> Set a -> Bool #

(/=) :: Set a -> Set a -> Bool #

(Data a, Ord a) => Data (Set a) 

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Set a -> c (Set a) #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Set a) #

toConstr :: Set a -> Constr #

dataTypeOf :: Set a -> DataType #

dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c (Set a)) #

dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Set a)) #

gmapT :: (forall b. Data b => b -> b) -> Set a -> Set a #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Set a -> r #

gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Set a -> r #

gmapQ :: (forall d. Data d => d -> u) -> Set a -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> Set a -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Set a -> m (Set a) #

Ord a => Ord (Set a) 

Methods

compare :: Set a -> Set a -> Ordering #

(<) :: Set a -> Set a -> Bool #

(<=) :: Set a -> Set a -> Bool #

(>) :: Set a -> Set a -> Bool #

(>=) :: Set a -> Set a -> Bool #

max :: Set a -> Set a -> Set a #

min :: Set a -> Set a -> Set a #

(Read a, Ord a) => Read (Set a) 
Show a => Show (Set a) 

Methods

showsPrec :: Int -> Set a -> ShowS #

show :: Set a -> String #

showList :: [Set a] -> ShowS #

Ord a => Semigroup (Set a)

Since: 0.5.7

Methods

(<>) :: Set a -> Set a -> Set a #

sconcat :: NonEmpty (Set a) -> Set a #

stimes :: Integral b => b -> Set a -> Set a #

Ord a => Monoid (Set a) 

Methods

mempty :: Set a #

mappend :: Set a -> Set a -> Set a #

mconcat :: [Set a] -> Set a #

NFData a => NFData (Set a) 

Methods

rnf :: Set a -> () #

type Item (Set a) 
type Item (Set a) = a

Operators

Query

null :: Set a -> Bool #

O(1). Is this the empty set?

size :: Set a -> Int #

O(1). The number of elements in the set.

member :: Ord a => a -> Set a -> Bool #

O(log n). Is the element in the set?

notMember :: Ord a => a -> Set a -> Bool #

O(log n). Is the element not in the set?

isSubsetOf :: Ord a => Set a -> Set a -> Bool #

O(n+m). Is this a subset? (s1 `isSubsetOf` s2) tells whether s1 is a subset of s2.

disjoint :: Ord a => Set a -> Set a -> Bool #

O(n+m). Check whether two sets are disjoint (i.e. their intersection is empty).

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

Since: 0.5.11

Construction

empty :: Set a #

O(1). The empty set.

singleton :: a -> Set a #

O(1). Create a singleton set.

insert :: Ord a => a -> Set a -> Set a #

O(log n). Insert an element in a set. If the set already contains an element equal to the given value, it is replaced with the new value.

delete :: Ord a => a -> Set a -> Set a #

O(log n). Delete an element from a set.

Combine

union :: Ord a => Set a -> Set a -> Set a #

O(m*log(n/m + 1)), m <= n. The union of two sets, preferring the first set when equal elements are encountered.

unions :: (Foldable f, Ord a) => f (Set a) -> Set a #

The union of a list of sets: (unions == foldl union empty).

difference :: Ord a => Set a -> Set a -> Set a #

O(m*log(n/m + 1)), m <= n. Difference of two sets.

intersection :: Ord a => Set a -> Set a -> Set a #

O(m*log(n/m + 1)), m <= n. The intersection of two sets. Elements of the result come from the first set, so for example

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]).

Filter

filter :: (a -> Bool) -> Set a -> Set a #

O(n). Filter all elements that satisfy the predicate.

partition :: (a -> Bool) -> Set a -> (Set a, Set a) #

O(n). Partition the set into two sets, one with all elements that satisfy the predicate and one with all elements that don't satisfy the predicate. See also split.

split :: Ord a => a -> Set a -> (Set a, Set a) #

O(log n). The expression (split x set) is a pair (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) #

O(log n). Performs a split but also returns whether the pivot element was found in the original set.

take :: Int -> Set a -> Set a #

Take a given number of elements in order, beginning with the smallest ones.

take n = fromDistinctAscList . take n . toAscList

Since: 0.5.8

drop :: Int -> Set a -> Set a #

Drop a given number of elements in order, beginning with the smallest ones.

drop n = fromDistinctAscList . drop n . toAscList

Since: 0.5.8

splitAt :: Int -> Set a -> (Set a, Set a) #

O(log n). Split a set at a particular index.

splitAt !n !xs = (take n xs, drop n xs)

map :: Ord b => (a -> b) -> Set a -> Set b #

O(n*log n). map f s is the set obtained by applying f 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 #

O(n). The

mapMonotonic f s == map f s, but works only when f 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

foldr :: (a -> b -> b) -> b -> Set a -> b #

O(n). Fold the elements in the set using the given right-associative binary operator, such that foldr f z == foldr f z . toAscList.

For example,

toAscList set = foldr (:) [] set

foldl :: (a -> b -> a) -> a -> Set b -> a #

O(n). Fold the elements in the set using the given left-associative binary operator, such that foldl f z == foldl f z . toAscList.

For example,

toDescList set = foldl (flip (:)) [] set

lookupMin :: Set a -> Maybe a #

O(log n). The minimal element of a set.

Since: 0.5.9

lookupMax :: Set a -> Maybe a #

O(log n). The maximal element of a set.

Since: 0.5.9

maxView :: Set a -> Maybe (a, Set a) #

O(log n). Retrieves the maximal key of the set, and the set stripped of that element, or Nothing if passed an empty set.

minView :: Set a -> Maybe (a, Set a) #

O(log n). Retrieves the minimal key of the set, and the set stripped of that element, or Nothing if passed an empty set.

elems :: Set a -> [a] #

O(n). An alias of toAscList. The elements of a set in ascending order. Subject to list fusion.

toList :: Set a -> [a] #

O(n). Convert the set to a list of elements. Subject to list fusion.

fromList :: Ord a => [a] -> Set a #

O(n*log n). Create a set from a list of elements.

If the elements are ordered, a linear-time implementation is used, with the performance equal to fromDistinctAscList.

toAscList :: Set a -> [a] #

O(n). Convert the set to an ascending list of elements. Subject to list fusion.

toDescList :: Set a -> [a] #

O(n). Convert the set to a descending list of elements. Subject to list fusion.

fromAscList :: Eq a => [a] -> Set a #

O(n). Build a set from an ascending list in linear time. The precondition (input list is ascending) is not checked.

fromDescList :: Eq a => [a] -> Set a #

O(n). Build a set from a descending list in linear time. The precondition (input list is descending) is not checked.

Since: 0.5.8

fromDistinctAscList :: [a] -> Set a #

O(n). Build a set from an ascending list of distinct elements in linear time. The precondition (input list is strictly ascending) is not checked.

fromDistinctDescList :: [a] -> Set a #

O(n). Build a set from a descending list of distinct elements in linear time. The precondition (input list is strictly descending) is not checked.