Movatterモバイル変換


[0]ホーム

URL:


containers-0.6.0.1: Assorted concrete container types

Copyright(c) Daan Leijen 2002
LicenseBSD-style
Maintainerlibraries@haskell.org
Portabilityportable
Safe HaskellSafe
LanguageHaskell98

Data.Set

Contents

Description

Finite Sets

TheSet e type represents a set of elements of typee. Most operations require thate be an instance of theOrd class. ASet is strict in its elements.

For a walkthrough of the most commonly used functions see thesets introduction.

Note that the implementation is generallyleft-biased. Functions that take two sets as arguments and combine them, such asunion andintersection, 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 exceedmaxBound::Int. Violation of this condition is not detected and if the size limit is exceeded, its behaviour is undefined.

Implementation

The implementation ofSet is based onsize balanced binary trees (or trees ofbounded 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 forunion,intersection, anddifference are as given by

Synopsis

Set type

dataSet aSource#

A set of valuesa.

Instances
FoldableSetSource# 
Instance details

Defined inData.Set.Internal

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#

Eq1SetSource#

Since: containers-0.5.9

Instance details

Defined inData.Set.Internal

Methods

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

Ord1SetSource#

Since: containers-0.5.9

Instance details

Defined inData.Set.Internal

Methods

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

Show1SetSource#

Since: containers-0.5.9

Instance details

Defined inData.Set.Internal

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)Source#

Since: containers-0.5.6.2

Instance details

Defined inData.Set.Internal

Associated Types

typeItem (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)Source# 
Instance details

Defined inData.Set.Internal

Methods

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

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

(Data a,Ord a) =>Data (Set a)Source# 
Instance details

Defined inData.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)#

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)Source# 
Instance details

Defined inData.Set.Internal

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)Source# 
Instance details

Defined inData.Set.Internal

Show a =>Show (Set a)Source# 
Instance details

Defined inData.Set.Internal

Methods

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

show ::Set a ->String#

showList :: [Set a] ->ShowS#

Ord a =>Semigroup (Set a)Source#

Since: containers-0.5.7

Instance details

Defined inData.Set.Internal

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)Source# 
Instance details

Defined inData.Set.Internal

Methods

mempty ::Set a#

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

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

NFData a =>NFData (Set a)Source# 
Instance details

Defined inData.Set.Internal

Methods

rnf ::Set a -> ()#

typeItem (Set a)Source# 
Instance details

Defined inData.Set.Internal

typeItem (Set a) = a

Construction

empty ::Set aSource#

O(1). The empty set.

singleton :: a ->Set aSource#

O(1). Create a singleton set.

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

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 tofromDistinctAscList.

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

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 aSource#

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

Since: containers-0.5.8

fromDistinctAscList :: [a] ->Set aSource#

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 aSource#

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.

powerSet ::Set a ->Set (Set a)Source#

Calculate the power set of a set: the set of all its subsets.

t `member` powerSet s == t `isSubsetOf` s

Example:

powerSet (fromList [1,2,3]) =  fromList [[], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]]

Since: containers-0.5.11

Insertion

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

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.

Deletion

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

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

Query

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

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

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

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

lookupLT ::Ord a => a ->Set a ->Maybe aSource#

O(log n). Find largest element smaller than the given one.

lookupLT 3 (fromList [3, 5]) == NothinglookupLT 5 (fromList [3, 5]) == Just 3

lookupGT ::Ord a => a ->Set a ->Maybe aSource#

O(log n). Find smallest element greater than the given one.

lookupGT 4 (fromList [3, 5]) == Just 5lookupGT 5 (fromList [3, 5]) == Nothing

lookupLE ::Ord a => a ->Set a ->Maybe aSource#

O(log n). Find largest element smaller or equal to the given one.

lookupLE 2 (fromList [3, 5]) == NothinglookupLE 4 (fromList [3, 5]) == Just 3lookupLE 5 (fromList [3, 5]) == Just 5

lookupGE ::Ord a => a ->Set a ->Maybe aSource#

O(log n). Find smallest element greater or equal to the given one.

lookupGE 3 (fromList [3, 5]) == Just 3lookupGE 4 (fromList [3, 5]) == Just 5lookupGE 6 (fromList [3, 5]) == Nothing

null ::Set a ->BoolSource#

O(1). Is this the empty set?

size ::Set a ->IntSource#

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

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

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

isProperSubsetOf ::Ord a =>Set a ->Set a ->BoolSource#

O(n+m). Is this a proper subset? (ie. a subset but not equal).

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

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

disjoint (fromList [2,4,6])   (fromList [1,3])     == Truedisjoint (fromList [2,4,6,8]) (fromList [2,3,5,7]) == Falsedisjoint (fromList [1,2])     (fromList [1,2,3,4]) == Falsedisjoint (fromList [])        (fromList [])        == True

Since: containers-0.5.11

Combine

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

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 aSource#

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

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

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

(\\) ::Ord a =>Set a ->Set a ->Set ainfixl 9Source#

O(m*log(n/m+1)), m <= n. Seedifference.

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

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 Sdata AB = A | B deriving Showinstance Ord AB where compare _ _ = EQinstance Eq AB where _ == _ = Truemain = 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#

Calculate the Cartesian product of two sets.

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#

Calculate the disjoint union of two sets.

 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

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

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

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

O(log n). Take while a predicate on the elements holds. The user is responsible for ensuring that for all elementsj andk in the set,j < k ==> p j >= p k. See note atspanAntitone.

takeWhileAntitone p =fromDistinctAscList .takeWhile p .toListtakeWhileAntitone p =filter p

Since: containers-0.5.8

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

O(log n). Drop while a predicate on the elements holds. The user is responsible for ensuring that for all elementsj andk in the set,j < k ==> p j >= p k. See note atspanAntitone.

dropWhileAntitone p =fromDistinctAscList .dropWhile p .toListdropWhileAntitone p =filter (not . p)

Since: containers-0.5.8

spanAntitone :: (a ->Bool) ->Set a -> (Set a,Set a)Source#

O(log n). Divide a set at the point where a predicate on the elements stops holding. The user is responsible for ensuring that for all elementsj andk 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: ifp is not actually antitone, thenspanAntitone will split the set at someunspecified 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#

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 alsosplit.

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

O(log n). The expression (split x set) is a pair(set1,set2) whereset1 comprises the elements ofset less thanx andset2 comprises the elements ofset greater thanx.

splitMember ::Ord a => a ->Set a -> (Set a,Bool,Set a)Source#

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

splitRoot ::Set a -> [Set a]Source#

O(1). Decompose a set into pieces based on the structure of the underlying tree. This function is useful for consuming a set in parallel.

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 ->MaybeIntSource#

O(log n). Lookup theindex of an element, which is its zero-based index in the sorted sequence of elements. The index is a number from0 up to, but not including, thesize of the set.

isJust   (lookupIndex 2 (fromList [5,3])) == FalsefromJust (lookupIndex 3 (fromList [5,3])) == 0fromJust (lookupIndex 5 (fromList [5,3])) == 1isJust   (lookupIndex 6 (fromList [5,3])) == False

Since: containers-0.5.4

findIndex ::Ord a => a ->Set a ->IntSource#

O(log n). Return theindex of an element, which is its zero-based index in the sorted sequence of elements. The index is a number from0 up to, but not including, thesize of the set. Callserror when the element is not amember of the set.

findIndex 2 (fromList [5,3])    Error: element is not in the setfindIndex 3 (fromList [5,3]) == 0findIndex 5 (fromList [5,3]) == 1findIndex 6 (fromList [5,3])    Error: element is not in the set

Since: containers-0.5.4

elemAt ::Int ->Set a -> aSource#

O(log n). Retrieve an element by itsindex, i.e. by its zero-based index in the sorted sequence of elements. If theindex is out of range (less than zero, greater or equal tosize of the set),error is called.

elemAt 0 (fromList [5,3]) == 3elemAt 1 (fromList [5,3]) == 5elemAt 2 (fromList [5,3])    Error: index out of range

Since: containers-0.5.4

deleteAt ::Int ->Set a ->Set aSource#

O(log n). Delete the element atindex, i.e. by its zero-based index in the sorted sequence of elements. If theindex is out of range (less than zero, greater or equal tosize of the set),error is called.

deleteAt 0    (fromList [5,3]) == singleton 5deleteAt 1    (fromList [5,3]) == singleton 3deleteAt 2    (fromList [5,3])    Error: index out of rangedeleteAt (-1) (fromList [5,3])    Error: index out of range

Since: containers-0.5.4

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

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

take n =fromDistinctAscList .take n .toAscList

Since: containers-0.5.8

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

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

drop n =fromDistinctAscList .drop n .toAscList

Since: containers-0.5.8

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

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

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

Map

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

O(n*log n).map f s is the set obtained by applyingf to each element ofs.

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 bSource#

O(n). The

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

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

For example,

toAscList set = foldr (:) [] set

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

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

For example,

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

Strict folds

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

O(n). A strict version offoldr. 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 -> aSource#

O(n). A strict version offoldl. 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 -> bSource#

O(n). Fold the elements in the set using the given right-associative binary operator. This function is an equivalent offoldr and is present for compatibility only.

Please note that fold will be deprecated in the future and removed.

Min/Max

lookupMin ::Set a ->Maybe aSource#

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

Since: containers-0.5.9

lookupMax ::Set a ->Maybe aSource#

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

Since: containers-0.5.9

findMin ::Set a -> aSource#

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

findMax ::Set a -> aSource#

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

deleteMin ::Set a ->Set aSource#

O(log n). Delete the minimal element. Returns an empty set if the set is empty.

deleteMax ::Set a ->Set aSource#

O(log n). Delete the maximal element. Returns an empty set if the set is empty.

deleteFindMin ::Set a -> (a,Set a)Source#

O(log n). Delete and find the minimal element.

deleteFindMin set = (findMin set, deleteMin set)

deleteFindMax ::Set a -> (a,Set a)Source#

O(log n). Delete and find the maximal element.

deleteFindMax set = (findMax set, deleteMax set)

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

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

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

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

Conversion

List

elems ::Set a -> [a]Source#

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

toList ::Set a -> [a]Source#

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

toAscList ::Set a -> [a]Source#

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

toDescList ::Set a -> [a]Source#

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

Debugging

showTree ::Show a =>Set a ->StringSource#

O(n). Show the tree that implements the set. The tree is shown in a compressed, hanging format.

showTreeWith ::Show a =>Bool ->Bool ->Set a ->StringSource#

O(n). The expression (showTreeWith hang wide map) shows the tree that implements the set. Ifhang isTrue, ahanging tree is shown otherwise a rotated tree is shown. Ifwide isTrue, an extra wide version is shown.

Set> putStrLn $ showTreeWith True False $ fromDistinctAscList [1..5]4+--2|  +--1|  +--3+--5Set> putStrLn $ showTreeWith True True $ fromDistinctAscList [1..5]4|+--2|  ||  +--1|  ||  +--3|+--5Set> putStrLn $ showTreeWith False True $ fromDistinctAscList [1..5]+--5|4||  +--3|  |+--2   |   +--1

valid ::Ord a =>Set a ->BoolSource#

O(n). Test if the internal set structure is valid.

Produced byHaddock version 2.20.0


[8]ページ先頭

©2009-2025 Movatter.jp