| Haskell Hierarchical Libraries (base package) | Source code | Contents | Index
Data.IntMap |
Portability | portable
Stability | provisional
Maintainer | libraries@haskell.org
Contents
Map type Operators Query Construction Insertion Delete/Update Combine Union Difference Intersection Traversal Map Fold Conversion Lists Ordered lists Filter Submap 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 and Data.FiniteMap). Chris Okasaki and Andy Gill, "Fast Mergeable Integer Maps", Workshop on ML, September 1998, pages 77-86, http://www.cse.ogi.edu/~andy/pub/finite.htm 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
showTree :: Show a => IntMap a -> String
showTreeWith :: Show a => Bool -> Bool -> IntMap a -> String
Map type
data IntMap a
A map of integers to values `a`.
Instances
Foldable IntMap
Functor IntMap
Typeable1 IntMap
Data a => Data (IntMap a)
Eq a => Eq (IntMap a)
Monoid (IntMap a)
Ord a => Ord (IntMap a)
Read e => Read (IntMap e)
Show a => Show (IntMap a)
type Key = Int
Operators
(!) :: IntMap a -> Key -> a
O(min(n,W)). Find the value at a key. Calls `error` when the element can not be found.
(\\) :: IntMap a -> IntMap b -> IntMap a
O(n+m). See `difference`.
Query
null :: IntMap a -> Bool
O(1). Is the map empty?
size :: IntMap a -> Int
O(n). Number of elements in the map.
member :: Key -> IntMap a -> Bool
O(min(n,W)). Is the key a member of the map?
notMember :: Key -> IntMap a -> Bool
O(log n). Is the key not a member of the map?
lookup :: Monad m => Key -> IntMap a -> m a
O(min(n,W)). Lookup the value at a key in the map.
findWithDefault :: a -> Key -> IntMap a -> a
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 a
O(1). The empty map.
singleton :: Key -> a -> IntMap a
O(1). A map of one element.
Insertion
insert :: Key -> a -> IntMap a -> IntMap a
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 a
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 a
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)
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 a
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 a
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 a
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 a
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 a
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)
O(min(n,W)). Lookup and update.
Combine
Union
union :: IntMap a -> IntMap a -> IntMap a
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 a
O(n+m). The union with a combining function.
unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
O(n+m). The union with a combining function.
unions :: [IntMap a] -> IntMap a
The union of a list of maps.
unionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap a
The union of a list of maps, with a combining operation
Difference
difference :: IntMap a -> IntMap b -> IntMap a
O(n+m). Difference between two maps (based on keys).
differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
O(n+m). Difference with a combining function.
differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a
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 a
O(n+m). The (left-biased) intersection of two maps (based on keys).
intersectionWith :: (a -> b -> a) -> IntMap a -> IntMap b -> IntMap a
O(n+m). The intersection with a combining function.
intersectionWithKey :: (Key -> a -> b -> a) -> IntMap a -> IntMap b -> IntMap a
O(n+m). The intersection with a combining function.
Traversal
Map
map :: (a -> b) -> IntMap a -> IntMap b
O(n). Map a function over all values in the map.
mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap b
O(n). Map a function over all values in the map.
mapAccum :: (a -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)
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)
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 -> b
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 -> b
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]
O(n). Return all elements of the map in the ascending order of their keys.
keys :: IntMap a -> [Key]
O(n). Return all keys of the map in ascending order.
keysSet :: IntMap a -> IntSet
O(n*min(n,W)). The set of all keys of the map.
assocs :: IntMap a -> [(Key, a)]
O(n). Return all key/value pairs in the map in ascending key order.
Lists
toList :: IntMap a -> [(Key, a)]
O(n). Convert the map to a list of key/value pairs.
fromList :: [(Key, a)] -> IntMap a
O(n*min(n,W)). Create a map from a list of key/value pairs.
fromListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a
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 a
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)]
O(n). Convert the map to a list of key/value pairs where the keys are in ascending order.
fromAscList :: [(Key, a)] -> IntMap a
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 a
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 a
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 a
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 a
O(n). Filter all values that satisfy some predicate.
filterWithKey :: (Key -> a -> Bool) -> IntMap a -> IntMap a
O(n). Filter all keys/values that satisfy some predicate.
partition :: (a -> Bool) -> IntMap a -> (IntMap a, IntMap a)
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)
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 b
O(n). Map values and collect the `Just` results.
mapMaybeWithKey :: (Key -> a -> Maybe b) -> IntMap a -> IntMap b
O(n). Map keys/values and collect the `Just` results.
mapEither :: (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
O(n). Map values and separate the `Left` and `Right` results.
mapEitherWithKey :: (Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)
O(n). Map keys/values and separate the `Left` and `Right` results.
split :: Key -> IntMap a -> (IntMap a, IntMap a)
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)
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 -> Bool
O(n+m). Is this a submap? Defined as (``isSubmapOf` = `isSubmapOfBy` (==)`).
isSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool
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 -> Bool
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 -> Bool
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)])
Debugging
showTree :: Show a => IntMap a -> String
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 -> String
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