containers-0.1.0.1: Assorted concrete container typesSource codeContentsIndex
Data.IntMap
Portabilityportable
Stabilityprovisional
Maintainerlibraries@haskell.org
Contents
Map type
Operators
Query
Construction
Insertion
Delete/Update
Combine
Union
Difference
Intersection
Traversal
Map
Fold
Conversion
Lists
Ordered lists
Filter
Submap
Min/Max
Debugging
Description

An efficient implementation of maps from integer keys to values.

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

  import Data.IntMap (IntMap)
  import qualified Data.IntMap as IntMap

The implementation is based on big-endian patricia trees. This data structure performs especially well on binary operations like union and intersection. However, my benchmarks show that it is also (much) faster on insertions and deletions when compared to a generic size-balanced map implementation (see Data.Map).

  • Chris Okasaki and Andy Gill, "Fast Mergeable Integer Maps", Workshop on ML, September 1998, pages 77-86, http://citeseer.ist.psu.edu/okasaki98fast.html
  • D.R. Morrison, "/PATRICIA -- Practical Algorithm To Retrieve Information Coded In Alphanumeric/", Journal of the ACM, 15(4), October 1968, pages 514-534.

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).

Synopsis
data IntMap a
type Key = Int
(!) :: IntMap a -> Key -> a
(\\) :: IntMap a -> IntMap b -> IntMap a
null :: IntMap a -> Bool
size :: IntMap a -> Int
member :: Key -> IntMap a -> Bool
notMember :: Key -> IntMap a -> Bool
lookup :: Monad m => Key -> IntMap a -> m a
findWithDefault :: a -> Key -> IntMap a -> a
empty :: IntMap a
singleton :: Key -> a -> IntMap a
insert :: Key -> a -> IntMap a -> IntMap a
insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a
insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)
delete :: Key -> IntMap a -> IntMap a
adjust :: (a -> a) -> Key -> IntMap a -> IntMap a
adjustWithKey :: (Key -> a -> a) -> Key -> IntMap a -> IntMap a
update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap a
updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a
updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a, IntMap a)
union :: IntMap a -> IntMap a -> IntMap a
unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
unions :: [IntMap a] -> IntMap a
unionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap a
difference :: IntMap a -> IntMap b -> IntMap a
differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
intersection :: IntMap a -> IntMap b -> IntMap a
intersectionWith :: (a -> b -> a) -> IntMap a -> IntMap b -> IntMap a
intersectionWithKey :: (Key -> a -> b -> a) -> IntMap a -> IntMap b -> IntMap a
map :: (a -> b) -> IntMap a -> IntMap b
mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap b
mapAccum :: (a -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
mapAccumWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
fold :: (a -> b -> b) -> b -> IntMap a -> b
foldWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> b
elems :: IntMap a -> [a]
keys :: IntMap a -> [Key]
keysSet :: IntMap a -> IntSet
assocs :: IntMap a -> [(Key, a)]
toList :: IntMap a -> [(Key, a)]
fromList :: [(Key, a)] -> IntMap a
fromListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a
fromListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
toAscList :: IntMap a -> [(Key, a)]
fromAscList :: [(Key, a)] -> IntMap a
fromAscListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a
fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a
fromDistinctAscList :: [(Key, a)] -> IntMap a
filter :: (a -> Bool) -> IntMap a -> IntMap a
filterWithKey :: (Key -> a -> Bool) -> IntMap a -> IntMap a
partition :: (a -> Bool) -> IntMap a -> (IntMap a, IntMap a)
partitionWithKey :: (Key -> a -> Bool) -> IntMap a -> (IntMap a, IntMap a)
mapMaybe :: (a -> Maybe b) -> IntMap a -> IntMap b
mapMaybeWithKey :: (Key -> a -> Maybe b) -> IntMap a -> IntMap b
mapEither :: (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
mapEitherWithKey :: (Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
split :: Key -> IntMap a -> (IntMap a, IntMap a)
splitLookup :: Key -> IntMap a -> (IntMap a, Maybe a, IntMap a)
isSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool
isSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
isProperSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool
isProperSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
updateMin :: (a -> a) -> IntMap a -> IntMap a
updateMax :: (a -> a) -> IntMap a -> IntMap a
updateMinWithKey :: (Key -> a -> a) -> IntMap a -> IntMap a
updateMaxWithKey :: (Key -> a -> a) -> IntMap a -> IntMap a
minViewWithKey :: Monad m => IntMap a -> m ((Key, a), IntMap a)
maxViewWithKey :: Monad m => IntMap a -> m ((Key, a), IntMap a)
showTree :: Show a => IntMap a -> String
showTreeWith :: Show a => Bool -> Bool -> IntMap a -> String
Map type
data IntMap aSource
A map of integers to values a.
show/hide Instances
type Key = IntSource
Operators
(!) :: IntMap a -> Key -> aSource
O(min(n,W)). Find the value at a key. Calls error when the element can not be found.
(\\) :: IntMap a -> IntMap b -> IntMap aSource
O(n+m). See difference.
Query
null :: IntMap a -> BoolSource
O(1). Is the map empty?
size :: IntMap a -> IntSource
O(n). Number of elements in the map.
member :: Key -> IntMap a -> BoolSource
O(min(n,W)). Is the key a member of the map?
notMember :: Key -> IntMap a -> BoolSource
O(log n). Is the key not a member of the map?
lookup :: Monad m => Key -> IntMap a -> m aSource
O(min(n,W)). Lookup the value at a key in the map.
findWithDefault :: a -> Key -> IntMap a -> aSource
O(min(n,W)). The expression (findWithDefault def k map) returns the value at key k or returns def when the key is not an element of the map.
Construction
empty :: IntMap aSource
O(1). The empty map.
singleton :: Key -> a -> IntMap aSource
O(1). A map of one element.
Insertion
insert :: Key -> a -> IntMap a -> IntMap aSource
O(min(n,W)). Insert a new key/value pair in the map. If the key is already present in the map, the associated value is replaced with the supplied value, i.e. insert is equivalent to insertWith const.
insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap aSource
O(min(n,W)). Insert with a combining function. insertWith f key value mp will insert the pair (key, value) into mp if key does not exist in the map. If the key does exist, the function will insert f new_value old_value.
insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap aSource
O(min(n,W)). Insert with a combining function. insertWithKey f key value mp will insert the pair (key, value) into mp if key does not exist in the map. If the key does exist, the function will insert f key new_value old_value.
insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)Source
O(min(n,W)). The expression (insertLookupWithKey f k x map) is a pair where the first element is equal to (lookup k map) and the second element equal to (insertWithKey f k x map).
Delete/Update
delete :: Key -> IntMap a -> IntMap aSource
O(min(n,W)). Delete a key and its value from the map. When the key is not a member of the map, the original map is returned.
adjust :: (a -> a) -> Key -> IntMap a -> IntMap aSource
O(min(n,W)). Adjust a value at a specific key. When the key is not a member of the map, the original map is returned.
adjustWithKey :: (Key -> a -> a) -> Key -> IntMap a -> IntMap aSource
O(min(n,W)). Adjust a value at a specific key. When the key is not a member of the map, the original map is returned.
update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap aSource
O(min(n,W)). The expression (update f k map) updates the value x at k (if it is in the map). If (f x) is Nothing, the element is deleted. If it is (Just y), the key k is bound to the new value y.
updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap aSource
O(min(n,W)). The expression (update f k map) updates the value x at k (if it is in the map). If (f k x) is Nothing, the element is deleted. If it is (Just y), the key k is bound to the new value y.
updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a, IntMap a)Source
O(min(n,W)). Lookup and update.
Combine
Union
union :: IntMap a -> IntMap a -> IntMap aSource
O(n+m). The (left-biased) union of two maps. It prefers the first map when duplicate keys are encountered, i.e. (union == unionWith const).
unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap aSource
O(n+m). The union with a combining function.
unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap aSource
O(n+m). The union with a combining function.
unions :: [IntMap a] -> IntMap aSource
The union of a list of maps.
unionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap aSource
The union of a list of maps, with a combining operation
Difference
difference :: IntMap a -> IntMap b -> IntMap aSource
O(n+m). Difference between two maps (based on keys).
differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap aSource
O(n+m). Difference with a combining function.
differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap aSource
O(n+m). Difference with a combining function. When two equal keys are encountered, the combining function is applied to the key and both values. If it returns Nothing, the element is discarded (proper set difference). If it returns (Just y), the element is updated with a new value y.
Intersection
intersection :: IntMap a -> IntMap b -> IntMap aSource
O(n+m). The (left-biased) intersection of two maps (based on keys).
intersectionWith :: (a -> b -> a) -> IntMap a -> IntMap b -> IntMap aSource
O(n+m). The intersection with a combining function.
intersectionWithKey :: (Key -> a -> b -> a) -> IntMap a -> IntMap b -> IntMap aSource
O(n+m). The intersection with a combining function.
Traversal
Map
map :: (a -> b) -> IntMap a -> IntMap bSource
O(n). Map a function over all values in the map.
mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap bSource
O(n). Map a function over all values in the map.
mapAccum :: (a -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)Source
O(n). The function mapAccum threads an accumulating argument through the map in ascending order of keys.
mapAccumWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)Source
O(n). The function mapAccumWithKey threads an accumulating argument through the map in ascending order of keys.
Fold
fold :: (a -> b -> b) -> b -> IntMap a -> bSource

O(n). Fold the values in the map, such that fold f z == foldr f z . elems. For example,

 elems map = fold (:) [] map
foldWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> bSource

O(n). Fold the keys and values in the map, such that foldWithKey f z == foldr (uncurry f) z . toAscList. For example,

 keys map = foldWithKey (\k x ks -> k:ks) [] map
Conversion
elems :: IntMap a -> [a]Source
O(n). Return all elements of the map in the ascending order of their keys.
keys :: IntMap a -> [Key]Source
O(n). Return all keys of the map in ascending order.
keysSet :: IntMap a -> IntSetSource
O(n*min(n,W)). The set of all keys of the map.
assocs :: IntMap a -> [(Key, a)]Source
O(n). Return all key/value pairs in the map in ascending key order.
Lists
toList :: IntMap a -> [(Key, a)]Source
O(n). Convert the map to a list of key/value pairs.
fromList :: [(Key, a)] -> IntMap aSource
O(n*min(n,W)). Create a map from a list of key/value pairs.
fromListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap aSource
O(n*min(n,W)). Create a map from a list of key/value pairs with a combining function. See also fromAscListWith.
fromListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap aSource
O(n*min(n,W)). Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey'.
Ordered lists
toAscList :: IntMap a -> [(Key, a)]Source
O(n). Convert the map to a list of key/value pairs where the keys are in ascending order.
fromAscList :: [(Key, a)] -> IntMap aSource
O(n*min(n,W)). Build a map from a list of key/value pairs where the keys are in ascending order.
fromAscListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap aSource
O(n*min(n,W)). Build a map from a list of key/value pairs where the keys are in ascending order, with a combining function on equal keys.
fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap aSource
O(n*min(n,W)). Build a map from a list of key/value pairs where the keys are in ascending order, with a combining function on equal keys.
fromDistinctAscList :: [(Key, a)] -> IntMap aSource
O(n*min(n,W)). Build a map from a list of key/value pairs where the keys are in ascending order and all distinct.
Filter
filter :: (a -> Bool) -> IntMap a -> IntMap aSource
O(n). Filter all values that satisfy some predicate.
filterWithKey :: (Key -> a -> Bool) -> IntMap a -> IntMap aSource
O(n). Filter all keys/values that satisfy some predicate.
partition :: (a -> Bool) -> IntMap a -> (IntMap a, IntMap a)Source
O(n). partition the map according to some predicate. The first map contains all elements that satisfy the predicate, the second all elements that fail the predicate. See also split.
partitionWithKey :: (Key -> a -> Bool) -> IntMap a -> (IntMap a, IntMap a)Source
O(n). partition the map according to some predicate. The first map contains all elements that satisfy the predicate, the second all elements that fail the predicate. See also split.
mapMaybe :: (a -> Maybe b) -> IntMap a -> IntMap bSource
O(n). Map values and collect the Just results.
mapMaybeWithKey :: (Key -> a -> Maybe b) -> IntMap a -> IntMap bSource
O(n). Map keys/values and collect the Just results.
mapEither :: (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)Source
O(n). Map values and separate the Left and Right results.
mapEitherWithKey :: (Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)Source
O(n). Map keys/values and separate the Left and Right results.
split :: Key -> IntMap a -> (IntMap a, IntMap a)Source
O(log n). The expression (split k map) is a pair (map1,map2) where all keys in map1 are lower than k and all keys in map2 larger than k. Any key equal to k is found in neither map1 nor map2.
splitLookup :: Key -> IntMap a -> (IntMap a, Maybe a, IntMap a)Source
O(log n). Performs a split but also returns whether the pivot key was found in the original map.
Submap
isSubmapOf :: Eq a => IntMap a -> IntMap a -> BoolSource
O(n+m). Is this a submap? Defined as (isSubmapOf = isSubmapOfBy (==)).
isSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> BoolSource

O(n+m). The expression (isSubmapOfBy f m1 m2) returns True if all keys in m1 are in m2, and when f returns True when applied to their respective values. For example, the following expressions are all True:

 isSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
 isSubmapOfBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
 isSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])

But the following are all False:

 isSubmapOfBy (==) (fromList [(1,2)]) (fromList [(1,1),(2,2)])
 isSubmapOfBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
 isSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
isProperSubmapOf :: Eq a => IntMap a -> IntMap a -> BoolSource
O(n+m). Is this a proper submap? (ie. a submap but not equal). Defined as (isProperSubmapOf = isProperSubmapOfBy (==)).
isProperSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> BoolSource

O(n+m). Is this a proper submap? (ie. a submap but not equal). The expression (isProperSubmapOfBy f m1 m2) returns True when m1 and m2 are not equal, all keys in m1 are in m2, and when f returns True when applied to their respective values. For example, the following expressions are all True:

 isProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
 isProperSubmapOfBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])

But the following are all False:

 isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
 isProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
 isProperSubmapOfBy (<)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
Min/Max
updateMin :: (a -> a) -> IntMap a -> IntMap aSource
O(log n). Update the value at the minimal key.
updateMax :: (a -> a) -> IntMap a -> IntMap aSource
O(log n). Update the value at the maximal key.
updateMinWithKey :: (Key -> a -> a) -> IntMap a -> IntMap aSource
O(log n). Update the value at the minimal key.
updateMaxWithKey :: (Key -> a -> a) -> IntMap a -> IntMap aSource
O(log n). Update the value at the maximal key.
minViewWithKey :: Monad m => IntMap a -> m ((Key, a), IntMap a)Source
O(log n). Retrieves the minimal (key,value) couple of the map, and the map stripped from that element. fails (in the monad) when passed an empty map.
maxViewWithKey :: Monad m => IntMap a -> m ((Key, a), IntMap a)Source
O(log n). Retrieves the maximal (key,value) couple of the map, and the map stripped from that element. fails (in the monad) when passed an empty map.
Debugging
showTree :: Show a => IntMap a -> StringSource
O(n). Show the tree that implements the map. The tree is shown in a compressed, hanging format.
showTreeWith :: Show a => Bool -> Bool -> IntMap a -> StringSource
O(n). The expression (showTreeWith hang wide map) shows the tree that implements the map. 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.
Produced by Haddock version 0.8