|
Data.IntMultiSet | Portability | portable | Stability | provisional | Maintainer | libraries@haskell.org |
|
|
|
|
|
Description |
An efficient implementation of multisets of integers, also somtimes called bags.
A multiset is like a set, but it can contain multiple copies of the same element.
Since many function names (but not the type name) clash with
Prelude names, this module is usually imported qualified, e.g.
import Data.MultiSet (MultiSet)
import qualified Data.MultiSet as MultiSet
The implementation of MultiSet is based on the Data.IntMap module.
Many operations have a worst-case complexity of O(min(n,W)).
This means that the operation can become linear in the number of
elements with a maximum of W -- the number of bits in an Int
(32 or 64). Here n refers to the number of distinct elements,
t is the total number of elements.
|
|
Synopsis |
|
data IntMultiSet | | type Key = Int | | type Occur = Int | | (\\) :: IntMultiSet -> IntMultiSet -> IntMultiSet | | null :: IntMultiSet -> Bool | | size :: IntMultiSet -> Int | | distinctSize :: IntMultiSet -> Int | | member :: Key -> IntMultiSet -> Bool | | notMember :: Key -> IntMultiSet -> Bool | | occur :: Key -> IntMultiSet -> Int | | isSubsetOf :: IntMultiSet -> IntMultiSet -> Bool | | isProperSubsetOf :: IntMultiSet -> IntMultiSet -> Bool | | empty :: IntMultiSet | | singleton :: Key -> IntMultiSet | | insert :: Key -> IntMultiSet -> IntMultiSet | | insertMany :: Key -> Occur -> IntMultiSet -> IntMultiSet | | delete :: Key -> IntMultiSet -> IntMultiSet | | deleteMany :: Key -> Occur -> IntMultiSet -> IntMultiSet | | deleteAll :: Key -> IntMultiSet -> IntMultiSet | | union :: IntMultiSet -> IntMultiSet -> IntMultiSet | | unions :: [IntMultiSet] -> IntMultiSet | | difference :: IntMultiSet -> IntMultiSet -> IntMultiSet | | intersection :: IntMultiSet -> IntMultiSet -> IntMultiSet | | filter :: (Key -> Bool) -> IntMultiSet -> IntMultiSet | | partition :: (Key -> Bool) -> IntMultiSet -> (IntMultiSet, IntMultiSet) | | split :: Int -> IntMultiSet -> (IntMultiSet, IntMultiSet) | | splitOccur :: Int -> IntMultiSet -> (IntMultiSet, Int, IntMultiSet) | | map :: (Key -> Key) -> IntMultiSet -> IntMultiSet | | mapMonotonic :: (Key -> Key) -> IntMultiSet -> IntMultiSet | | mapMaybe :: (Key -> Maybe Key) -> IntMultiSet -> IntMultiSet | | mapEither :: (Key -> Either Key Key) -> IntMultiSet -> (IntMultiSet, IntMultiSet) | | concatMap :: (Key -> [Key]) -> IntMultiSet -> IntMultiSet | | unionsMap :: (Key -> IntMultiSet) -> IntMultiSet -> IntMultiSet | | bind :: IntMultiSet -> (Key -> IntMultiSet) -> IntMultiSet | | join :: MultiSet IntMultiSet -> IntMultiSet | | fold :: (Key -> b -> b) -> b -> IntMultiSet -> b | | foldOccur :: (Key -> Occur -> b -> b) -> b -> IntMultiSet -> b | | findMin :: IntMultiSet -> Key | | findMax :: IntMultiSet -> Key | | deleteMin :: IntMultiSet -> IntMultiSet | | deleteMax :: IntMultiSet -> IntMultiSet | | deleteMinAll :: IntMultiSet -> IntMultiSet | | deleteMaxAll :: IntMultiSet -> IntMultiSet | | deleteFindMin :: IntMultiSet -> (Key, IntMultiSet) | | deleteFindMax :: IntMultiSet -> (Key, IntMultiSet) | | maxView :: Monad m => IntMultiSet -> m (Key, IntMultiSet) | | minView :: Monad m => IntMultiSet -> m (Key, IntMultiSet) | | elems :: IntMultiSet -> [Key] | | distinctElems :: IntMultiSet -> [Key] | | toList :: IntMultiSet -> [Key] | | fromList :: [Int] -> IntMultiSet | | toAscList :: IntMultiSet -> [Key] | | fromAscList :: [Int] -> IntMultiSet | | fromDistinctAscList :: [Int] -> IntMultiSet | | toOccurList :: IntMultiSet -> [(Int, Int)] | | toAscOccurList :: IntMultiSet -> [(Int, Int)] | | fromOccurList :: [(Int, Int)] -> IntMultiSet | | fromAscOccurList :: [(Int, Int)] -> IntMultiSet | | fromDistinctAscOccurList :: [(Int, Int)] -> IntMultiSet | | toMap :: IntMultiSet -> IntMap Int | | fromMap :: IntMap Int -> IntMultiSet | | fromOccurMap :: IntMap Int -> IntMultiSet | | toSet :: IntMultiSet -> IntSet | | fromSet :: IntSet -> IntMultiSet | | showTree :: IntMultiSet -> String | | showTreeWith :: Bool -> Bool -> IntMultiSet -> String |
|
|
|
MultiSet type
|
|
|
A multiset of integers.
The same value can occur multiple times.
| Instances | |
|
|
|
|
|
The number of occurences of an element
|
|
Operators
|
|
|
O(n+m). See difference.
|
|
Query
|
|
|
O(1). Is this the empty multiset?
|
|
|
O(n). The number of elements in the multiset.
|
|
|
O(1). The number of distinct elements in the multiset.
|
|
|
O(min(n,W)). Is the element in the multiset?
|
|
|
O(min(n,W)). Is the element not in the multiset?
|
|
|
O(min(n,W)). The number of occurences of an element in a multiset.
|
|
|
O(n+m). Is this a subset?
(s1 `isSubsetOf` s2) tells whether s1 is a subset of s2.
|
|
|
O(n+m). Is this a proper subset? (ie. a subset but not equal).
|
|
Construction
|
|
|
O(1). The empty mutli set.
|
|
|
O(1). Create a singleton mutli set.
|
|
|
O(min(n,W)). Insert an element in a multiset.
|
|
|
O(min(n,W)). Insert an element in a multiset a given number of times.
Negative numbers remove occurences of the given element.
|
|
|
O(min(n,W)). Delete a single element from a multiset.
|
|
|
O(min(n,W)). Delete an element from a multiset a given number of times.
Negative numbers add occurences of the given element.
|
|
|
O(min(n,W)). Delete all occurences of an element from a multiset.
|
|
Combine
|
|
|
O(n+m). The union of two multisets, preferring the first multiset when
equal elements are encountered.
The implementation uses the efficient hedge-union algorithm.
Hedge-union is more efficient on (bigset union smallset).
|
|
|
The union of a list of multisets: (unions == foldl union empty).
|
|
|
O(n+m). Difference of two multisets.
The implementation uses an efficient hedge algorithm comparable with hedge-union.
|
|
|
O(n+m). The intersection of two multisets.
Elements of the result come from the first multiset, so for example
import qualified Data.MultiSet as MS
data AB = A | B deriving Show
instance Ord AB where compare _ _ = EQ
instance Eq AB where _ == _ = True
main = print (MS.singleton A `MS.intersection` MS.singleton B,
MS.singleton B `MS.intersection` MS.singleton A)
prints (fromList [A],fromList [B]).
|
|
Filter
|
|
|
O(n). Filter all elements that satisfy the predicate.
|
|
|
O(n). Partition the multiset into two multisets, one with all elements that satisfy
the predicate and one with all elements that don't satisfy the predicate.
See also split.
|
|
|
O(log n). The expression (split x set) is a pair (set1,set2)
where all elements in set1 are lower than x and all elements in
set2 larger than x. x is not found in neither set1 nor set2.
|
|
|
O(log n). Performs a split but also returns the number of
occurences of the pivot element in the original set.
|
|
Map
|
|
|
O(n*log n).
map f s is the multiset obtained by applying f to each element of s.
|
|
|
O(n). The
mapMonotonic f s == map f s, but works only when f is strictly 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
|
|
|
O(n). Map and collect the Just results.
|
|
|
O(n). Map and separate the Left and Right results.
|
|
|
O(n). Apply a function to each element, and take the union of the results
|
|
|
O(n). Apply a function to each element, and take the union of the results
|
|
Monadic
|
|
|
O(n). The monad bind operation, (>>=), for multisets.
|
|
|
O(n). The monad join operation for multisets.
|
|
Fold
|
|
|
O(t). Fold over the elements of a multiset in an unspecified order.
|
|
|
O(n). Fold over the elements of a multiset with their occurences.
|
|
Min/Max
|
|
|
O(log n). The minimal element of a multiset.
|
|
|
O(log n). The maximal element of a multiset.
|
|
|
O(log n). Delete the minimal element.
|
|
|
O(log n). Delete the maximal element.
|
|
|
O(log n). Delete all occurences of the minimal element.
|
|
|
O(log n). Delete all occurences of the maximal element.
|
|
|
O(log n). Delete and find the minimal element.
deleteFindMin set = (findMin set, deleteMin set)
|
|
|
O(log n). Delete and find the maximal element.
deleteFindMax set = (findMax set, deleteMax set)
|
|
|
O(log n). Retrieves the maximal element of the multiset, and the set stripped from that element
fails (in the monad) when passed an empty multiset.
|
|
|
O(log n). Retrieves the minimal element of the multiset, and the set stripped from that element
fails (in the monad) when passed an empty multiset.
|
|
Conversion
|
|
List
|
|
|
O(t). The elements of a multiset.
|
|
|
O(n). The distinct elements of a multiset, each element occurs only once in the list.
distinctElems = map fst . toOccurList
|
|
|
O(t). Convert the multiset to a list of elements.
|
|
|
O(t*min(n,W)). Create a multiset from a list of elements.
|
|
Ordered list
|
|
|
O(t). Convert the multiset to an ascending list of elements.
|
|
|
O(t). Build a multiset from an ascending list in linear time.
The precondition (input list is ascending) is not checked.
|
|
|
O(n). Build a multiset from an ascending list of distinct elements in linear time.
The precondition (input list is strictly ascending) is not checked.
|
|
Occurrence lists
|
|
|
O(n). Convert the multiset to a list of element/occurence pairs.
|
|
|
O(n). Convert the multiset to an ascending list of element/occurence pairs.
|
|
|
O(n*min(n,W)). Create a multiset from a list of element/occurence pairs.
|
|
|
O(n). Build a multiset from an ascending list of element/occurence pairs in linear time.
The precondition (input list is ascending) is not checked.
|
|
|
O(n). Build a multiset from an ascending list of elements/occurence pairs where each elements appears only once.
The precondition (input list is strictly ascending) is not checked.
|
|
Map
|
|
|
O(1). Convert a multiset to an IntMap from elements to number of occurrences.
|
|
|
O(n). Convert an IntMap from elements to occurrences to a multiset.
|
|
|
O(1). Convert an IntMap from elements to occurrences to a multiset.
Assumes that the IntMap contains only values larger than one.
The precondition (all elements > 1) is not checked.
|
|
Set
|
|
|
O(n). Convert a multiset to an IntMap, removing duplicates.
|
|
|
O(n). Convert an IntMap to a multiset.
|
|
Debugging
|
|
|
O(n). Show the tree that implements the set. The tree is shown
in a compressed, hanging format.
|
|
|
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,1,2,3,4,5]
(1*) 4
+--(1*) 2
| +--(2*) 1
| +--(1*) 3
+--(1*) 5
Set> putStrLn $ showTreeWith True True $ fromDistinctAscList [1,1,2,3,4,5]
(1*) 4
|
+--(1*) 2
| |
| +--(2*) 1
| |
| +--(1*) 3
|
+--(1*) 5
Set> putStrLn $ showTreeWith False True $ fromDistinctAscList [1,1,2,3,4,5]
+--(1*) 5
|
(1*) 4
|
| +--(1*) 3
| |
+--(1*) 2
|
+--(2*) 1
|
|
Produced by Haddock version 2.4.2 |