Copyright | (c) Daan Leijen 2002 |
---|---|
License | BSD-style |
Maintainer | libraries@haskell.org |
Stability | provisional |
Portability | portable |
Safe Haskell | Safe |
Language | Haskell98 |
An efficient implementation of sets.
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
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.
Note that the implementation is left-biased -- the elements of a
first argument are always preferred to the second, for example in
union
or insert
. Of course, left-biasing can only be observed
when equality is an equivalence relation instead of structural
equality.
- data Set a
- (\\) :: Ord a => Set a -> Set a -> Set a
- null :: Set a -> Bool
- size :: Set a -> Int
- member :: Ord a => a -> Set a -> Bool
- notMember :: Ord a => a -> Set a -> Bool
- lookupLT :: Ord a => a -> Set a -> Maybe a
- lookupGT :: Ord a => a -> Set a -> Maybe a
- lookupLE :: Ord a => a -> Set a -> Maybe a
- lookupGE :: Ord a => a -> Set a -> Maybe a
- isSubsetOf :: Ord a => Set a -> Set a -> Bool
- isProperSubsetOf :: Ord a => Set a -> Set a -> Bool
- empty :: Set a
- singleton :: a -> Set a
- insert :: Ord a => a -> Set a -> Set a
- delete :: Ord a => a -> Set a -> Set a
- union :: Ord a => Set a -> Set a -> Set a
- unions :: Ord a => [Set a] -> Set a
- difference :: Ord a => Set a -> Set a -> Set a
- intersection :: Ord a => Set a -> Set a -> Set a
- filter :: (a -> Bool) -> Set a -> Set a
- partition :: (a -> Bool) -> Set a -> (Set a, Set a)
- split :: Ord a => a -> Set a -> (Set a, Set a)
- splitMember :: Ord a => a -> Set a -> (Set a, Bool, Set a)
- splitRoot :: Set a -> [Set a]
- lookupIndex :: Ord a => a -> Set a -> Maybe Int
- findIndex :: Ord a => a -> Set a -> Int
- elemAt :: Int -> Set a -> a
- deleteAt :: Int -> Set a -> Set a
- map :: Ord b => (a -> b) -> Set a -> Set b
- mapMonotonic :: (a -> b) -> Set a -> Set b
- foldr :: (a -> b -> b) -> b -> Set a -> b
- foldl :: (a -> b -> a) -> a -> Set b -> a
- foldr' :: (a -> b -> b) -> b -> Set a -> b
- foldl' :: (a -> b -> a) -> a -> Set b -> a
- fold :: (a -> b -> b) -> b -> Set a -> b
- findMin :: Set a -> a
- findMax :: Set a -> a
- deleteMin :: Set a -> Set a
- deleteMax :: Set a -> Set a
- deleteFindMin :: Set a -> (a, Set a)
- deleteFindMax :: Set a -> (a, Set a)
- maxView :: Set a -> Maybe (a, Set a)
- minView :: Set a -> Maybe (a, Set a)
- elems :: Set a -> [a]
- toList :: Set a -> [a]
- fromList :: Ord a => [a] -> Set a
- toAscList :: Set a -> [a]
- toDescList :: Set a -> [a]
- fromAscList :: Eq a => [a] -> Set a
- fromDistinctAscList :: [a] -> Set a
- showTree :: Show a => Set a -> String
- showTreeWith :: Show a => Bool -> Bool -> Set a -> String
- valid :: Ord a => Set a -> Bool
Strictness properties
This module satisfies the following strictness property:
- Key arguments are evaluated to WHNF
Here are some examples that illustrate the property:
delete undefined s == undefined
Set type
A set of values a
.
Operators
Query
lookupLT :: Ord a => a -> Set a -> Maybe aSource
O(log n). Find largest element smaller than the given one.
lookupLT 3 (fromList [3, 5]) == Nothing lookupLT 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 5 lookupGT 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]) == Nothing lookupLE 4 (fromList [3, 5]) == Just 3 lookupLE 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 3 lookupGE 4 (fromList [3, 5]) == Just 5 lookupGE 6 (fromList [3, 5]) == Nothing
isSubsetOf :: Ord a => Set a -> Set a -> BoolSource
O(n+m). Is this a subset?
(s1
tells whether isSubsetOf
s2)s1
is a subset of s2
.
isProperSubsetOf :: Ord a => Set a -> Set a -> BoolSource
O(n+m). Is this a proper subset? (ie. a subset but not equal).
Construction
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.
Combine
union :: Ord a => Set a -> Set a -> Set aSource
O(n+m). The union of two sets, preferring the first set when equal elements are encountered. The implementation uses the efficient hedge-union algorithm.
difference :: Ord a => Set a -> Set a -> Set aSource
O(n+m). Difference of two sets. The implementation uses an efficient hedge algorithm comparable with hedge-union.
intersection :: Ord a => Set a -> Set a -> Set aSource
O(n+m). The intersection of two sets. The implementation uses an efficient hedge algorithm comparable with hedge-union. 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
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 also split
.
split :: Ord a => a -> Set a -> (Set a, Set a)Source
O(log n). The expression (
) 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
O(log n). Performs a split
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.
Indexed
lookupIndex :: Ord a => a -> Set a -> Maybe IntSource
O(log n). Lookup the index of an element, which is its zero-based index in
the sorted sequence of elements. The index is a number from 0 up to, but not
including, the 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
findIndex :: Ord a => a -> Set a -> IntSource
O(log n). Return the index of an element, which is its zero-based
index in the sorted sequence of elements. The index is a number from 0 up
to, but not including, the 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
elemAt :: Int -> Set a -> aSource
O(log n). Retrieve an element by its index, i.e. by its zero-based
index in the sorted sequence of elements. If the index is out of range (less
than zero, greater or equal to 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
deleteAt :: Int -> Set a -> Set aSource
O(log n). Delete the element at index, i.e. by its zero-based index in
the sorted sequence of elements. If the index is out of range (less than zero,
greater or equal to 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
Map
map :: Ord b => (a -> b) -> Set a -> Set bSource
O(n*log n).
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 bSource
O(n). The
, but works only when mapMonotonic
f s == map
f sf
is monotonic.
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 -> bSource
O(n). A strict version of 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 -> aSource
O(n). A strict version of 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 -> bSource
O(n). Fold the elements in the set using the given right-associative
binary operator. This function is an equivalent of 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 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, or Nothing
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, or Nothing
if passed an empty set.
Conversion
List
O(n). An alias of toAscList
. The elements of a set in ascending order.
Subject to list fusion.
fromList :: Ord a => [a] -> Set aSource
O(n*log n). Create a set from a list of elements.
If the elemens are ordered, linear-time implementation is used,
with the performance equal to fromDistinctAscList
.
Ordered list
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.
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.
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.
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. 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