unboxed-containers-0.0.2: Self-optimizing unboxed sets using view patterns and data familiesSource codeContentsIndex
Data.Set.Unboxed
Portabilitynon-portable (type families, view patterns, unboxed tuples)
Stabilityexperimental
Maintainerekmett@gmail.com
Contents
Set type
Operators
Query
Construction
Combine
Filter
Map
Fold
Min/Max
Conversion
List
Ordered list
Debugging
Description

An efficient implementation of sets.

Since many function names (but not the type name) clash with Prelude names, this module is usually imported qualified, e.g.

  import Data.Set.Unboxed (USet)
  import qualified Data.Set.Unboxed as USet

The implementation of USet 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.

Modified from Data.Set to use type families for automatic unboxing

Synopsis
class US a where
data USet a
size :: USet a -> Size
null :: USet a -> Bool
newtype Boxed a = Boxed {
getBoxed :: a
}
(\\) :: (US a, Ord a) => USet a -> USet a -> USet a
null :: US a => USet a -> Bool
size :: US a => USet a -> Size
member :: (US a, Ord a) => a -> USet a -> Bool
notMember :: (US a, Ord a) => a -> USet a -> Bool
isSubsetOf :: (US a, Ord a) => USet a -> USet a -> Bool
isProperSubsetOf :: (US a, Ord a) => USet a -> USet a -> Bool
empty :: US a => USet a
singleton :: US a => a -> USet a
insert :: (US a, Ord a) => a -> USet a -> USet a
delete :: (US a, Ord a) => a -> USet a -> USet a
union :: (US a, Ord a) => USet a -> USet a -> USet a
unions :: (US a, Ord a) => [USet a] -> USet a
difference :: (US a, Ord a) => USet a -> USet a -> USet a
intersection :: (US a, Ord a) => USet a -> USet a -> USet a
filter :: (US a, Ord a) => (a -> Bool) -> USet a -> USet a
partition :: (US a, Ord a) => (a -> Bool) -> USet a -> (USet a, USet a)
split :: (US a, Ord a) => a -> USet a -> (USet a, USet a)
splitMember :: (US a, Ord a) => a -> USet a -> (USet a, Bool, USet a)
map :: (US a, US b, Ord a, Ord b) => (a -> b) -> USet a -> USet b
mapMonotonic :: (US a, US b) => (a -> b) -> USet a -> USet b
fold :: US a => (a -> b -> b) -> b -> USet a -> b
findMin :: US a => USet a -> a
findMax :: US a => USet a -> a
deleteMin :: US a => USet a -> USet a
deleteMax :: US a => USet a -> USet a
deleteFindMin :: US a => USet a -> (a, USet a)
deleteFindMax :: US a => USet a -> (a, USet a)
maxView :: US a => USet a -> Maybe (a, USet a)
minView :: US a => USet a -> Maybe (a, USet a)
elems :: US a => USet a -> [a]
toList :: US a => USet a -> [a]
fromList :: (US a, Ord a) => [a] -> USet a
toAscList :: US a => USet a -> [a]
fromAscList :: (US a, Eq a) => [a] -> USet a
fromDistinctAscList :: US a => [a] -> USet a
showTree :: (US a, Show a) => USet a -> String
showTreeWith :: (US a, Show a) => Bool -> Bool -> USet a -> String
valid :: (US a, Ord a) => USet a -> Bool
Set type
class US a whereSource
Associated Types
data USet a Source
Methods
size :: USet a -> SizeSource
O(1). The number of elements in the set.
null :: USet a -> BoolSource
O(1). Is this the empty set?
show/hide Instances
US Char
US Double
US Float
US Int
US Int8
US Int16
US Int32
US Int64
US Integer
US Word8
US Word16
US Word32
US Word64
US (Boxed a)
US ((,) Char Char)
US ((,) Double Double)
US ((,) Float Float)
US ((,) Int Int)
US ((,) Int8 Int8)
US ((,) Int16 Int16)
US ((,) Int32 Int32)
US ((,) Int64 Int64)
US ((,) Integer Integer)
US ((,) Word8 Word8)
US ((,) Word16 Word16)
US ((,) Word32 Word32)
US ((,) Word64 Word64)
US ((,) (Boxed a) (Boxed b))
US ((,,) Char Char Char)
US ((,,) Double Double Double)
US ((,,) Float Float Float)
US ((,,) Int Int Int)
US ((,,) Int8 Int8 Int8)
US ((,,) Int16 Int16 Int16)
US ((,,) Int32 Int32 Int32)
US ((,,) Int64 Int64 Int64)
US ((,,) Integer Integer Integer)
US ((,,) Word8 Word8 Word8)
US ((,,) Word16 Word16 Word16)
US ((,,) Word32 Word32 Word32)
US ((,,) Word64 Word64 Word64)
US ((,,) (Boxed a) (Boxed b) (Boxed c))
US ((,,,) Char Char Char Char)
US ((,,,) Double Double Double Double)
US ((,,,) Float Float Float Float)
US ((,,,) Int Int Int Int)
US ((,,,) Int8 Int8 Int8 Int8)
US ((,,,) Int16 Int16 Int16 Int16)
US ((,,,) Int32 Int32 Int32 Int32)
US ((,,,) Int64 Int64 Int64 Int64)
US ((,,,) Integer Integer Integer Integer)
US ((,,,) Word8 Word8 Word8 Word8)
US ((,,,) Word16 Word16 Word16 Word16)
US ((,,,) Word32 Word32 Word32 Word32)
US ((,,,) Word64 Word64 Word64 Word64)
US ((,,,) (Boxed a) (Boxed b) (Boxed c) (Boxed d))
US ((,,,,) Char Char Char Char Char)
US ((,,,,) Double Double Double Double Double)
US ((,,,,) Float Float Float Float Float)
US ((,,,,) Int Int Int Int Int)
US ((,,,,) Int8 Int8 Int8 Int8 Int8)
US ((,,,,) Int16 Int16 Int16 Int16 Int16)
US ((,,,,) Int32 Int32 Int32 Int32 Int32)
US ((,,,,) Int64 Int64 Int64 Int64 Int64)
US ((,,,,) Integer Integer Integer Integer Integer)
US ((,,,,) Word8 Word8 Word8 Word8 Word8)
US ((,,,,) Word16 Word16 Word16 Word16 Word16)
US ((,,,,) Word32 Word32 Word32 Word32 Word32)
US ((,,,,) Word64 Word64 Word64 Word64 Word64)
US ((,,,,) (Boxed a) (Boxed b) (Boxed c) (Boxed d) (Boxed e))
US ((,,,,,) Char Char Char Char Char Char)
US ((,,,,,) Double Double Double Double Double Double)
US ((,,,,,) Float Float Float Float Float Float)
US ((,,,,,) Int Int Int Int Int Int)
US ((,,,,,) Int8 Int8 Int8 Int8 Int8 Int8)
US ((,,,,,) Int16 Int16 Int16 Int16 Int16 Int16)
US ((,,,,,) Int32 Int32 Int32 Int32 Int32 Int32)
US ((,,,,,) Int64 Int64 Int64 Int64 Int64 Int64)
US ((,,,,,) Integer Integer Integer Integer Integer Integer)
US ((,,,,,) Word8 Word8 Word8 Word8 Word8 Word8)
US ((,,,,,) Word16 Word16 Word16 Word16 Word16 Word16)
US ((,,,,,) Word32 Word32 Word32 Word32 Word32 Word32)
US ((,,,,,) Word64 Word64 Word64 Word64 Word64 Word64)
US ((,,,,,) (Boxed a) (Boxed b) (Boxed c) (Boxed d) (Boxed e) (Boxed f))
US ((,,,,,,) Char Char Char Char Char Char Char)
US ((,,,,,,) Double Double Double Double Double Double Double)
US ((,,,,,,) Float Float Float Float Float Float Float)
US ((,,,,,,) Int Int Int Int Int Int Int)
US ((,,,,,,) Int8 Int8 Int8 Int8 Int8 Int8 Int8)
US ((,,,,,,) Int16 Int16 Int16 Int16 Int16 Int16 Int16)
US ((,,,,,,) Int32 Int32 Int32 Int32 Int32 Int32 Int32)
US ((,,,,,,) Int64 Int64 Int64 Int64 Int64 Int64 Int64)
US ((,,,,,,) Integer Integer Integer Integer Integer Integer Integer)
US ((,,,,,,) Word8 Word8 Word8 Word8 Word8 Word8 Word8)
US ((,,,,,,) Word16 Word16 Word16 Word16 Word16 Word16 Word16)
US ((,,,,,,) Word32 Word32 Word32 Word32 Word32 Word32 Word32)
US ((,,,,,,) Word64 Word64 Word64 Word64 Word64 Word64 Word64)
US ((,,,,,,) (Boxed a) (Boxed b) (Boxed c) (Boxed d) (Boxed e) (Boxed f) (Boxed g))
US ((,,,,,,,) Char Char Char Char Char Char Char Char)
US ((,,,,,,,) Double Double Double Double Double Double Double Double)
US ((,,,,,,,) Float Float Float Float Float Float Float Float)
US ((,,,,,,,) Int Int Int Int Int Int Int Int)
US ((,,,,,,,) Int8 Int8 Int8 Int8 Int8 Int8 Int8 Int8)
US ((,,,,,,,) Int16 Int16 Int16 Int16 Int16 Int16 Int16 Int16)
US ((,,,,,,,) Int32 Int32 Int32 Int32 Int32 Int32 Int32 Int32)
US ((,,,,,,,) Int64 Int64 Int64 Int64 Int64 Int64 Int64 Int64)
US ((,,,,,,,) Integer Integer Integer Integer Integer Integer Integer Integer)
US ((,,,,,,,) Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8)
US ((,,,,,,,) Word16 Word16 Word16 Word16 Word16 Word16 Word16 Word16)
US ((,,,,,,,) Word32 Word32 Word32 Word32 Word32 Word32 Word32 Word32)
US ((,,,,,,,) Word64 Word64 Word64 Word64 Word64 Word64 Word64 Word64)
US ((,,,,,,,) (Boxed a) (Boxed b) (Boxed c) (Boxed d) (Boxed e) (Boxed f) (Boxed g) (Boxed h))
US ((,,,,,,,,) Char Char Char Char Char Char Char Char Char)
US ((,,,,,,,,) Double Double Double Double Double Double Double Double Double)
US ((,,,,,,,,) Float Float Float Float Float Float Float Float Float)
US ((,,,,,,,,) Int Int Int Int Int Int Int Int Int)
US ((,,,,,,,,) Int8 Int8 Int8 Int8 Int8 Int8 Int8 Int8 Int8)
US ((,,,,,,,,) Int16 Int16 Int16 Int16 Int16 Int16 Int16 Int16 Int16)
US ((,,,,,,,,) Int32 Int32 Int32 Int32 Int32 Int32 Int32 Int32 Int32)
US ((,,,,,,,,) Int64 Int64 Int64 Int64 Int64 Int64 Int64 Int64 Int64)
US ((,,,,,,,,) Integer Integer Integer Integer Integer Integer Integer Integer Integer)
US ((,,,,,,,,) Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8)
US ((,,,,,,,,) Word16 Word16 Word16 Word16 Word16 Word16 Word16 Word16 Word16)
US ((,,,,,,,,) Word32 Word32 Word32 Word32 Word32 Word32 Word32 Word32 Word32)
US ((,,,,,,,,) Word64 Word64 Word64 Word64 Word64 Word64 Word64 Word64 Word64)
US ((,,,,,,,,) (Boxed a) (Boxed b) (Boxed c) (Boxed d) (Boxed e) (Boxed f) (Boxed g) (Boxed h) (Boxed i))
newtype Boxed a Source
Constructors
Boxed
getBoxed :: a
show/hide Instances
Bounded a => Bounded (Boxed a)
Eq a => Eq (Boxed a)
Ord a => Ord (Boxed a)
Read a => Read (Boxed a)
Show a => Show (Boxed a)
US (Boxed a)
Operators
(\\) :: (US a, Ord a) => USet a -> USet a -> USet aSource
O(n+m). See difference.
Query
null :: US a => USet a -> BoolSource
O(1). Is this the empty set?
size :: US a => USet a -> SizeSource
O(1). The number of elements in the set.
member :: (US a, Ord a) => a -> USet a -> BoolSource
O(log n). Is the element in the set?
notMember :: (US a, Ord a) => a -> USet a -> BoolSource
O(log n). Is the element not in the set?
isSubsetOf :: (US a, Ord a) => USet a -> USet a -> BoolSource
O(n+m). Is this a subset? (s1 isSubsetOf s2) tells whether s1 is a subset of s2.
isProperSubsetOf :: (US a, Ord a) => USet a -> USet a -> BoolSource
O(n+m). Is this a proper subset? (ie. a subset but not equal).
Construction
empty :: US a => USet aSource
O(1). The empty set.
singleton :: US a => a -> USet aSource
O(1). Create a singleton set.
insert :: (US a, Ord a) => a -> USet a -> USet 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.
delete :: (US a, Ord a) => a -> USet a -> USet aSource
O(log n). Delete an element from a set.
Combine
union :: (US a, Ord a) => USet a -> USet a -> USet 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. Hedge-union is more efficient on (bigset union smallset).
unions :: (US a, Ord a) => [USet a] -> USet aSource
The union of a list of sets: (unions == foldl union empty).
difference :: (US a, Ord a) => USet a -> USet a -> USet aSource
O(n+m). Difference of two sets. The implementation uses an efficient hedge algorithm comparable with hedge-union.
intersection :: (US a, Ord a) => USet a -> USet a -> USet aSource

O(n+m). The intersection of two sets. 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
filter :: (US a, Ord a) => (a -> Bool) -> USet a -> USet aSource
O(n). Filter all elements that satisfy the predicate.
partition :: (US a, Ord a) => (a -> Bool) -> USet a -> (USet a, USet 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 :: (US a, Ord a) => a -> USet a -> (USet a, USet a)Source
O(log n). The expression (split x set) is a pair (set1,set2) where set1 comprises the elements of set less than x and set2 comprises the elements of set greater than x.
splitMember :: (US a, Ord a) => a -> USet a -> (USet a, Bool, USet a)Source
O(log n). Performs a split but also returns whether the pivot element was found in the original set.
Map
map :: (US a, US b, Ord a, Ord b) => (a -> b) -> USet a -> USet bSource

O(n*log n). map f s is the set obtained by applying f 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 :: (US a, US b) => (a -> b) -> USet a -> USet bSource

O(n). The

mapMonotonic f s == map f s, but works only when f 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
Fold
fold :: US a => (a -> b -> b) -> b -> USet a -> bSource
O(n). Fold over the elements of a set in an unspecified order.
Min/Max
findMin :: US a => USet a -> aSource
O(log n). The minimal element of a set.
findMax :: US a => USet a -> aSource
O(log n). The maximal element of a set.
deleteMin :: US a => USet a -> USet aSource
O(log n). Delete the minimal element.
deleteMax :: US a => USet a -> USet aSource
O(log n). Delete the maximal element.
deleteFindMin :: US a => USet a -> (a, USet a)Source

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

 deleteFindMin set = (findMin set, deleteMin set)
deleteFindMax :: US a => USet a -> (a, USet a)Source

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

 deleteFindMax set = (findMax set, deleteMax set)
maxView :: US a => USet a -> Maybe (a, USet 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 :: US a => USet a -> Maybe (a, USet 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
elems :: US a => USet a -> [a]Source
O(n). The elements of a set.
toList :: US a => USet a -> [a]Source
O(n). Convert the set to a list of elements.
fromList :: (US a, Ord a) => [a] -> USet aSource
O(n*log n). Create a set from a list of elements.
Ordered list
toAscList :: US a => USet a -> [a]Source
O(n). Convert the set to an ascending list of elements.
fromAscList :: (US a, Eq a) => [a] -> USet aSource
O(n). Build a set from an ascending list in linear time. The precondition (input list is ascending) is not checked.
fromDistinctAscList :: US a => [a] -> USet 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 :: (US a, Show a) => USet a -> StringSource
O(n). Show the tree that implements the set. The tree is shown in a compressed, hanging format.
showTreeWith :: (US a, Show a) => Bool -> Bool -> USet 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
valid :: (US a, Ord a) => USet a -> BoolSource
O(n). Test if the internal set structure is valid.
Produced by Haddock version 2.3.0