|
Data.IntMap | Portability | portable | Stability | provisional | Maintainer | libraries@haskell.org |
|
|
|
|
|
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
|
|
|
A map of integers to values a.
| Instances | |
|
|
|
|
Operators
|
|
|
O(min(n,W)). Find the value at a key.
Calls error when the element can not be found.
|
|
|
O(n+m). See difference.
|
|
Query
|
|
|
O(1). Is the map empty?
|
|
|
O(n). Number of elements in the map.
|
|
|
O(min(n,W)). Is the key a member of the map?
|
|
|
O(log n). Is the key not a member of the map?
|
|
|
O(min(n,W)). Lookup the value at a key in the map.
|
|
|
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
|
|
|
O(1). The empty map.
|
|
|
O(1). A map of one element.
|
|
Insertion
|
|
|
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.
|
|
|
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.
|
|
|
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.
|
|
|
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
|
|
|
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.
|
|
|
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.
|
|
|
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.
|
|
|
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.
|
|
|
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.
|
|
|
O(min(n,W)). Lookup and update.
|
|
Combine
|
|
Union
|
|
|
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).
|
|
|
O(n+m). The union with a combining function.
|
|
|
O(n+m). The union with a combining function.
|
|
|
The union of a list of maps.
|
|
|
The union of a list of maps, with a combining operation
|
|
Difference
|
|
|
O(n+m). Difference between two maps (based on keys).
|
|
|
O(n+m). Difference with a combining function.
|
|
|
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
|
|
|
O(n+m). The (left-biased) intersection of two maps (based on keys).
|
|
|
O(n+m). The intersection with a combining function.
|
|
|
O(n+m). The intersection with a combining function.
|
|
Traversal
|
|
Map
|
|
|
O(n). Map a function over all values in the map.
|
|
|
O(n). Map a function over all values in the map.
|
|
|
O(n). The function mapAccum threads an accumulating
argument through the map in ascending order of keys.
|
|
|
O(n). The function mapAccumWithKey threads an accumulating
argument through the map in ascending order of keys.
|
|
Fold
|
|
|
O(n). Fold the values in the map, such that
fold f z == foldr f z . elems.
For example,
elems map = fold (:) [] map
|
|
|
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
|
|
|
O(n).
Return all elements of the map in the ascending order of their keys.
|
|
|
O(n). Return all keys of the map in ascending order.
|
|
|
O(n*min(n,W)). The set of all keys of the map.
|
|
|
O(n). Return all key/value pairs in the map in ascending key order.
|
|
Lists
|
|
|
O(n). Convert the map to a list of key/value pairs.
|
|
|
O(n*min(n,W)). Create a map from a list of key/value pairs.
|
|
|
O(n*min(n,W)). Create a map from a list of key/value pairs with a combining function. See also fromAscListWith.
|
|
|
O(n*min(n,W)). Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey'.
|
|
Ordered lists
|
|
|
O(n). Convert the map to a list of key/value pairs where the
keys are in ascending order.
|
|
|
O(n*min(n,W)). Build a map from a list of key/value pairs where
the keys are in ascending order.
|
|
|
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.
|
|
|
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.
|
|
|
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
|
|
|
O(n). Filter all values that satisfy some predicate.
|
|
|
O(n). Filter all keys/values that satisfy some predicate.
|
|
|
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.
|
|
|
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.
|
|
|
O(n). Map values and collect the Just results.
|
|
|
O(n). Map keys/values and collect the Just results.
|
|
|
O(n). Map values and separate the Left and Right results.
|
|
|
O(n). Map keys/values and separate the Left and Right results.
|
|
|
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.
|
|
|
O(log n). Performs a split but also returns whether the pivot
key was found in the original map.
|
|
Submap
|
|
|
O(n+m). Is this a submap?
Defined as (isSubmapOf = isSubmapOfBy (==)).
|
|
|
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)])
|
|
|
O(n+m). Is this a proper submap? (ie. a submap but not equal).
Defined as (isProperSubmapOf = isProperSubmapOfBy (==)).
|
|
|
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
|
|
|
O(log n). Update the value at the minimal key.
|
|
|
O(log n). Update the value at the maximal key.
|
|
|
O(log n). Update the value at the minimal key.
|
|
|
O(log n). Update the value at the maximal key.
|
|
|
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.
|
|
|
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
|
|
|
O(n). Show the tree that implements the map. The tree is shown
in a compressed, hanging format.
|
|
|
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 |