Maintainer | Jannis Harder <jannis@harderweb.de> |
---|
Data.IntSet.Translatable
Contents
Description
An implementation of integer sets with a constant time translate
operation, where translate
is defined to be
.
translate
x s = map
(+x) s
Since many function names (but not the type name) clash with
Prelude names, this module is usually imported qualified
, e.g.
import Data.IntSet.Translatable (IntSet) import qualified Data.IntSet.Translatable as IntSet
This implementation is based on Finger-Trees storing differences of consecutive entries of the ordered sequence of set elements. With this representation, a translation of all elements can be realized by changing only the leftmost element of the Finger-Tree which is a constant time operation. Together with caching of the accumulated differences most set operations can be implemented efficiently too.
- data IntSet
- (\\) :: IntSet -> IntSet -> IntSet
- null :: IntSet -> Bool
- size :: IntSet -> Int
- member :: Int -> IntSet -> Bool
- notMember :: Int -> IntSet -> Bool
- empty :: IntSet
- singleton :: Int -> IntSet
- insert :: Int -> IntSet -> IntSet
- delete :: Int -> IntSet -> IntSet
- union :: IntSet -> IntSet -> IntSet
- unions :: [IntSet] -> IntSet
- difference :: IntSet -> IntSet -> IntSet
- intersection :: IntSet -> IntSet -> IntSet
- filter :: (Int -> Bool) -> IntSet -> IntSet
- partition :: (Int -> Bool) -> IntSet -> (IntSet, IntSet)
- split :: Int -> IntSet -> (IntSet, IntSet)
- splitMember :: Int -> IntSet -> (IntSet, Bool, IntSet)
- findMin :: IntSet -> Int
- findMax :: IntSet -> Int
- deleteMin :: IntSet -> IntSet
- deleteMax :: IntSet -> IntSet
- deleteFindMin :: IntSet -> (Int, IntSet)
- deleteFindMax :: IntSet -> (Int, IntSet)
- maxView :: IntSet -> Maybe (Int, IntSet)
- minView :: IntSet -> Maybe (Int, IntSet)
- map :: (Int -> Int) -> IntSet -> IntSet
- translate :: Int -> IntSet -> IntSet
- fold :: (Int -> b -> b) -> b -> IntSet -> b
- elems :: IntSet -> [Int]
- toList :: IntSet -> [Int]
- fromList :: [Int] -> IntSet
- toAscList :: IntSet -> [Int]
- fromAscList :: [Int] -> IntSet
- fromDistinctAscList :: [Int] -> IntSet
Set type
Operators
Query
Construction
delete :: Int -> IntSet -> IntSetSource
O(log(n)). Delete a value in the set. Returns the original set when the value was not present.
Combine
union :: IntSet -> IntSet -> IntSetSource
O(m log(n / m)) where m<=n. The union of two sets. O(log m) if all elements of one set are larger than all elements of the other set.
difference :: IntSet -> IntSet -> IntSetSource
O(???). Difference between two sets.
intersection :: IntSet -> IntSet -> IntSetSource
O(???). The intersection of two sets.
Filter
filter :: (Int -> Bool) -> IntSet -> IntSetSource
O(n). Filter all elements that satisfy some predicate.
partition :: (Int -> Bool) -> IntSet -> (IntSet, IntSet)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 :: Int -> IntSet -> (IntSet, IntSet)Source
O(log(min(i,n-i))). 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 :: Int -> IntSet -> (IntSet, Bool, IntSet)Source
O(log(min(i,n-i))). Performs a split
but also returns whether the pivot
element was found in the original set.
Min/Max
deleteFindMin :: IntSet -> (Int, IntSet)Source
O(1). Delete and find the minimal element.
deleteFindMin set = (findMin set, deleteMin set)
deleteFindMax :: IntSet -> (Int, IntSet)Source
O(1). Delete and find the maximal element.
deleteFindMax set = (findMax set, deleteMax set)
maxView :: IntSet -> Maybe (Int, IntSet)Source
O(1). Retrieves the maximal key of the set, and the set
stripped of that element, or Nothing
if passed an empty set.
minView :: IntSet -> Maybe (Int, IntSet)Source
O(1). Retrieves the minimal key of the set, and the set
stripped of that element, or Nothing
if passed an empty set.
Map
map :: (Int -> Int) -> IntSet -> IntSetSource
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
translate :: Int -> IntSet -> IntSetSource
O(1). Add a constant value to all elements of the set.
translate x s == map (+x) s
Fold
fold :: (Int -> b -> b) -> b -> IntSet -> bSource
O(n). Fold over the elements of a set in an unspecified order.
sum set == fold (+) 0 set elems set == fold (:) [] set
Conversion
List
Ordered list
fromAscList :: [Int] -> IntSetSource
O(n). Build a set from an ascending list of elements. The precondition (input list is ascending) is not checked.
fromDistinctAscList :: [Int] -> IntSetSource
O(n). Build a set from an ascending list of distinct elements. The precondition (input list is strictly ascending) is not checked.