| Copyright | (c) Daan Leijen 2002 (c) Joachim Breitner 2011 |
|---|---|
| License | BSD-style |
| Maintainer | libraries@haskell.org |
| Portability | portable |
| Safe Haskell | Trustworthy |
| Language | Haskell2010 |
Data.IntSet.Internal
Description
WARNING
This module is considered internal.
The Package Versioning Policy does not apply.
The contents of this module may change in any way whatsoever and without any warning between minor versions of this package.
Authors importing this module are expected to track development closely.
Finite Int Sets (internals)
The type represents a set of elements of type IntSetInt. An IntSet
is strict in its elements.
Implementation
The implementation is based on big-endian patricia trees. This data
structure performs especially well on binary operations like union
and intersection. Additionally, benchmarks show that it is also
(much) faster on insertions and deletions when compared to a generic
size-balanced set implementation (see Data.Set).
- Chris Okasaki and Andy Gill, "Fast Mergeable Integer Maps", Workshop on ML, September 1998, pages 77-86, https://web.archive.org/web/20150417234429/https://ittc.ku.edu/~andygill/papers/IntMap98.pdf.
- D.R. Morrison, "PATRICIA -- Practical Algorithm To Retrieve Information Coded In Alphanumeric", Journal of the ACM, 15(4), October 1968, pages 514-534, https://doi.org/10.1145/321479.321481.
Additionally, this implementation places bitmaps in the leaves of the tree. Their size is the natural size of a machine word (32 or 64 bits) and greatly reduces the memory footprint and execution times for dense sets, e.g. sets where it is likely that many values lie close to each other. The asymptotics are not affected by this optimization.
Since: containers-0.5.9
Synopsis
- data IntSet
- type Key = Int
- type BitMap = Word
- (\\) :: IntSet -> IntSet -> IntSet
- null :: IntSet -> Bool
- size :: IntSet -> Int
- member :: Key -> IntSet -> Bool
- notMember :: Key -> IntSet -> Bool
- lookupLT :: Key -> IntSet -> Maybe Key
- lookupGT :: Key -> IntSet -> Maybe Key
- lookupLE :: Key -> IntSet -> Maybe Key
- lookupGE :: Key -> IntSet -> Maybe Key
- isSubsetOf :: IntSet -> IntSet -> Bool
- isProperSubsetOf :: IntSet -> IntSet -> Bool
- disjoint :: IntSet -> IntSet -> Bool
- empty :: IntSet
- singleton :: Key -> IntSet
- fromRange :: (Key, Key) -> IntSet
- insert :: Key -> IntSet -> IntSet
- delete :: Key -> IntSet -> IntSet
- alterF :: Functor f => (Bool -> f Bool) -> Key -> IntSet -> f IntSet
- union :: IntSet -> IntSet -> IntSet
- unions :: Foldable f => f IntSet -> IntSet
- difference :: IntSet -> IntSet -> IntSet
- intersection :: IntSet -> IntSet -> IntSet
- intersections :: NonEmpty IntSet -> IntSet
- symmetricDifference :: IntSet -> IntSet -> IntSet
- newtype Intersection = Intersection {}
- filter :: (Key -> Bool) -> IntSet -> IntSet
- partition :: (Key -> Bool) -> IntSet -> (IntSet, IntSet)
- takeWhileAntitone :: (Key -> Bool) -> IntSet -> IntSet
- dropWhileAntitone :: (Key -> Bool) -> IntSet -> IntSet
- spanAntitone :: (Key -> Bool) -> IntSet -> (IntSet, IntSet)
- split :: Key -> IntSet -> (IntSet, IntSet)
- splitMember :: Key -> IntSet -> (IntSet, Bool, IntSet)
- splitRoot :: IntSet -> [IntSet]
- map :: (Key -> Key) -> IntSet -> IntSet
- mapMonotonic :: (Key -> Key) -> IntSet -> IntSet
- foldr :: (Key -> b -> b) -> b -> IntSet -> b
- foldl :: (a -> Key -> a) -> a -> IntSet -> a
- foldMap :: Monoid a => (Key -> a) -> IntSet -> a
- foldr' :: (Key -> b -> b) -> b -> IntSet -> b
- foldl' :: (a -> Key -> a) -> a -> IntSet -> a
- fold :: (Key -> b -> b) -> b -> IntSet -> b
- lookupMin :: IntSet -> Maybe Key
- lookupMax :: IntSet -> Maybe Key
- findMin :: IntSet -> Key
- findMax :: IntSet -> Key
- deleteMin :: IntSet -> IntSet
- deleteMax :: IntSet -> IntSet
- deleteFindMin :: IntSet -> (Key, IntSet)
- deleteFindMax :: IntSet -> (Key, IntSet)
- maxView :: IntSet -> Maybe (Key, IntSet)
- minView :: IntSet -> Maybe (Key, IntSet)
- elems :: IntSet -> [Key]
- toList :: IntSet -> [Key]
- fromList :: [Key] -> IntSet
- toAscList :: IntSet -> [Key]
- toDescList :: IntSet -> [Key]
- fromAscList :: [Key] -> IntSet
- fromDistinctAscList :: [Key] -> IntSet
- showTree :: IntSet -> String
- showTreeWith :: Bool -> Bool -> IntSet -> String
- suffixBitMask :: Int
- prefixBitMask :: Int
- bitmapOf :: Int -> BitMap
Set type
A set of integers.
Instances
| NFData IntSet Source # | |
Defined in Data.IntSet.Internal | |
| Monoid IntSet Source # |
|
| Semigroup IntSet Source # |
Since: containers-0.5.7 |
| Eq IntSet Source # | |
| Ord IntSet Source # | |
| Data IntSet Source # | |
Defined in Data.IntSet.Internal Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> IntSet -> c IntSet # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c IntSet # toConstr :: IntSet -> Constr # dataTypeOf :: IntSet -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c IntSet) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c IntSet) # gmapT :: (forall b. Data b => b -> b) -> IntSet -> IntSet # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> IntSet -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> IntSet -> r # gmapQ :: (forall d. Data d => d -> u) -> IntSet -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> IntSet -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> IntSet -> m IntSet # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> IntSet -> m IntSet # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> IntSet -> m IntSet # | |
| IsList IntSet Source # | Since: containers-0.5.6.2 |
| Read IntSet Source # | |
| Show IntSet Source # | |
| Lift IntSet Source # | Since: containers-0.6.6 |
| type Item IntSet Source # | |
Defined in Data.IntSet.Internal | |
Operators
(\\) :: IntSet -> IntSet -> IntSet infixl 9 Source #
\(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
See difference.
Query
lookupLT :: Key -> IntSet -> Maybe Key Source #
\(O(\min(n,W))\). Find largest element smaller than the given one.
lookupLT 3 (fromList [3, 5]) == Nothing lookupLT 5 (fromList [3, 5]) == Just 3
lookupGT :: Key -> IntSet -> Maybe Key Source #
\(O(\min(n,W))\). Find smallest element greater than the given one.
lookupGT 4 (fromList [3, 5]) == Just 5 lookupGT 5 (fromList [3, 5]) == Nothing
lookupLE :: Key -> IntSet -> Maybe Key Source #
\(O(\min(n,W))\). 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 :: Key -> IntSet -> Maybe Key Source #
\(O(\min(n,W))\). 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 :: IntSet -> IntSet -> Bool Source #
\(O(\min(n, m \log \frac{2^W}{m})), m \leq n\).
Is this a subset?
(s1 `isSubsetOf` s2) tells whether s1 is a subset of s2.
isProperSubsetOf :: IntSet -> IntSet -> Bool Source #
\(O(\min(n, m \log \frac{2^W}{m})), m \leq n\). Is this a proper subset? (ie. a subset but not equal).
disjoint :: IntSet -> IntSet -> Bool Source #
\(O(\min(n, m \log \frac{2^W}{m})), m \leq n\). 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: containers-0.5.11
Construction
fromRange :: (Key, Key) -> IntSet Source #
\(O(n / W)\). Create a set from a range of integers.
fromRange (low, high) == fromList [low..high]
Since: containers-0.7
insert :: Key -> IntSet -> IntSet Source #
\(O(\min(n,W))\). Add a value to the set. There is no left- or right bias for IntSets.
delete :: Key -> IntSet -> IntSet Source #
\(O(\min(n,W))\). Delete a value in the set. Returns the original set when the value was not present.
Combine
union :: IntSet -> IntSet -> IntSet Source #
\(O(\min(n, m \log \frac{2^W}{m})), m \leq n\). The union of two sets.
difference :: IntSet -> IntSet -> IntSet Source #
\(O(\min(n, m \log \frac{2^W}{m})), m \leq n\). Difference between two sets.
intersection :: IntSet -> IntSet -> IntSet Source #
\(O(\min(n, m \log \frac{2^W}{m})), m \leq n\). The intersection of two sets.
intersections :: NonEmpty IntSet -> IntSet Source #
The intersection of a series of sets. Intersections are performed left-to-right.
Since: containers-0.8
symmetricDifference :: IntSet -> IntSet -> IntSet Source #
\(O(\min(n, m \log \frac{2^W}{m})), m \leq n\). The symmetric difference of two sets.
The result contains elements that appear in exactly one of the two sets.
symmetricDifference (fromList [0,2,4,6]) (fromList [0,3,6,9]) == fromList [2,3,4,9]
Since: containers-0.8
newtype Intersection Source #
IntSets form a Semigroup under intersection.
A Monoid instance is not defined because it would be impractical to
construct mempty, the IntSet containing all Ints.
Since: containers-0.8
Constructors
| Intersection | |
Fields | |
Instances
| Semigroup Intersection Source # | |
Defined in Data.IntSet.Internal Methods (<>) :: Intersection -> Intersection -> Intersection # sconcat :: NonEmpty Intersection -> Intersection # stimes :: Integral b => b -> Intersection -> Intersection # | |
| Eq Intersection Source # | |
Defined in Data.IntSet.Internal | |
| Ord Intersection Source # | |
Defined in Data.IntSet.Internal Methods compare :: Intersection -> Intersection -> Ordering # (<) :: Intersection -> Intersection -> Bool # (<=) :: Intersection -> Intersection -> Bool # (>) :: Intersection -> Intersection -> Bool # (>=) :: Intersection -> Intersection -> Bool # max :: Intersection -> Intersection -> Intersection # min :: Intersection -> Intersection -> Intersection # | |
| Show Intersection Source # | |
Defined in Data.IntSet.Internal Methods showsPrec :: Int -> Intersection -> ShowS # show :: Intersection -> String # showList :: [Intersection] -> ShowS # | |
Filter
filter :: (Key -> Bool) -> IntSet -> IntSet Source #
\(O(n)\). Filter all elements that satisfy some predicate.
partition :: (Key -> Bool) -> IntSet -> (IntSet, IntSet) Source #
\(O(n)\). partition the set according to some predicate.
takeWhileAntitone :: (Key -> Bool) -> IntSet -> IntSet Source #
\(O(\min(n,W))\). Take while a predicate on the elements holds.
The user is responsible for ensuring that for all Ints, j < k ==> p j >= p k.
See note at spanAntitone.
takeWhileAntitone p =fromDistinctAscList.takeWhilep .toListtakeWhileAntitone p =filterp
Since: containers-0.6.7
dropWhileAntitone :: (Key -> Bool) -> IntSet -> IntSet Source #
\(O(\min(n,W))\). Drop while a predicate on the elements holds.
The user is responsible for ensuring that for all Ints, j < k ==> p j >= p k.
See note at spanAntitone.
dropWhileAntitone p =fromDistinctAscList.dropWhilep .toListdropWhileAntitone p =filter(not . p)
Since: containers-0.6.7
spanAntitone :: (Key -> Bool) -> IntSet -> (IntSet, IntSet) Source #
\(O(\min(n,W))\). Divide a set at the point where a predicate on the elements stops holding.
The user is responsible for ensuring that for all Ints, j < k ==> p j >= p k.
spanAntitone p xs = (takeWhileAntitonep xs,dropWhileAntitonep xs) spanAntitone p xs =partitionp xs
Note: if p is not actually antitone, then spanAntitone will split the set
at some unspecified point.
Since: containers-0.6.7
split :: Key -> IntSet -> (IntSet, IntSet) Source #
\(O(\min(n,W))\). 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.
split 3 (fromList [1..5]) == (fromList [1,2], fromList [4,5])
splitMember :: Key -> IntSet -> (IntSet, Bool, IntSet) Source #
\(O(\min(n,W))\). Performs a split but also returns whether the pivot
element was found in the original set.
splitRoot :: IntSet -> [IntSet] 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 submap less than all elements in the second, and so on).
Examples:
splitRoot (fromList [1..120]) == [fromList [1..63],fromList [64..120]] splitRoot empty == []
Note that the current implementation does not return more than two subsets, but you should not depend on this behaviour because it can change in the future without notice. Also, the current version does not continue splitting all the way to individual singleton sets -- it stops at some point.
Map
map :: (Key -> Key) -> IntSet -> IntSet Source #
\(O(n \min(n,W))\).
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 :: (Key -> Key) -> IntSet -> IntSet Source #
\(O(n)\). The
, but works only when mapMonotonic f s == map f sf is strictly increasing.
Semi-formally, we have:
and [x < y ==> f x < f y | x <- ls, y <- ls]
==> mapMonotonic f s == map f s
where ls = toList sWarning: This function should be used only if f is monotonically
strictly increasing. This precondition is not checked. Use map if the
precondition may not hold.
Since: containers-0.6.3.1
Folds
foldMap :: Monoid a => (Key -> a) -> IntSet -> a Source #
\(O(n)\). Map the elements in the set to a monoid and combine with (<>).
Since: containers-0.8
Strict folds
foldr' :: (Key -> b -> b) -> b -> IntSet -> b Source #
\(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 -> Key -> a) -> a -> IntSet -> a Source #
\(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 :: (Key -> b -> b) -> b -> IntSet -> b Source #
Deprecated: Use Data.IntSet.foldr instead
\(O(n)\). Fold the elements in the set using the given right-associative binary operator.
Min/Max
lookupMin :: IntSet -> Maybe Key Source #
\(O(\min(n,W))\). The minimal element of the set. Returns Nothing if the
set is empty.
Since: containers-0.8
lookupMax :: IntSet -> Maybe Key Source #
\(O(\min(n,W))\). The maximal element of the set. Returns Nothing if the
set is empty.
Since: containers-0.8
findMin :: IntSet -> Key Source #
\(O(\min(n,W))\). The minimal element of the set. Calls error if the set
is empty.
findMax :: IntSet -> Key Source #
\(O(\min(n,W))\). The maximal element of the set. Calls error if the set
is empty.
deleteFindMin :: IntSet -> (Key, IntSet) Source #
\(O(\min(n,W))\). Delete and find the minimal element.
deleteFindMin set = (findMin set, deleteMin set)
deleteFindMax :: IntSet -> (Key, IntSet) Source #
\(O(\min(n,W))\). Delete and find the maximal element.
deleteFindMax set = (findMax set, deleteMax set)
maxView :: IntSet -> Maybe (Key, IntSet) Source #
\(O(\min(n,W))\). Retrieves the maximal key of the set, and the set
stripped of that element, or Nothing if passed an empty set.
minView :: IntSet -> Maybe (Key, IntSet) Source #
\(O(\min(n,W))\). Retrieves the minimal key of the set, and the set
stripped of that element, or Nothing if passed an empty set.
Conversion
List
elems :: IntSet -> [Key] Source #
\(O(n)\). An alias of toAscList. The elements of a set in ascending order.
Subject to list fusion.
toList :: IntSet -> [Key] Source #
\(O(n)\). Convert the set to a list of elements. Subject to list fusion.
Ordered list
toAscList :: IntSet -> [Key] Source #
\(O(n)\). Convert the set to an ascending list of elements. Subject to list fusion.
toDescList :: IntSet -> [Key] Source #
\(O(n)\). Convert the set to a descending list of elements. Subject to list fusion.
fromAscList :: [Key] -> IntSet Source #
\(O(n)\). Build a set from an ascending list of elements.
Warning: This function should be used only if the elements are in
non-decreasing order. This precondition is not checked. Use fromList if the
precondition may not hold.
fromDistinctAscList :: [Key] -> IntSet Source #
\(O(n)\). Build a set from an ascending list of distinct elements.
Warning: This function should be used only if the elements are in
strictly increasing order. This precondition is not checked. Use fromList
if the precondition may not hold.
Debugging
showTree :: IntSet -> String Source #
\(O(n \min(n,W))\). Show the tree that implements the set. The tree is shown in a compressed, hanging format.
showTreeWith :: Bool -> Bool -> IntSet -> String Source #
\(O(n \min(n,W))\). The expression () shows
the tree that implements the set. If showTreeWith hang wide maphang is
True, a hanging tree is shown otherwise a rotated tree is shown. If
wide is True, an extra wide version is shown.
Internals
suffixBitMask :: Int Source #
prefixBitMask :: Int Source #