Portability | portable |
---|---|
Stability | provisional |
Maintainer | libraries@haskell.org |
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).
- 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)
- alter :: (Maybe a -> Maybe a) -> Key -> IntMap 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
- maxView :: Monad m => IntMap b -> m (b, IntMap b)
- minView :: Monad m => IntMap b -> m (b, IntMap b)
- findMin :: IntMap b -> b
- findMax :: IntMap b -> b
- deleteMin :: IntMap b -> IntMap b
- deleteMax :: IntMap b -> IntMap b
- deleteFindMin :: IntMap b -> (b, IntMap b)
- deleteFindMax :: IntMap b -> (b, IntMap b)
- 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
.
Operators
(!) :: IntMap a -> Key -> aSource
O(min(n,W)). Find the value at a key.
Calls error
when the element can not be found.
Query
findWithDefault :: a -> Key -> IntMap a -> aSource
O(min(n,W)). The expression (
returns the value at key findWithDefault
def k map)k
or returns def
when the key is not an
element of the map.
Construction
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.
will insert the pair (key, value) into insertWith
f key value mpmp
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.
will insert the pair (key, value) into insertWithKey
f key value mpmp
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 (
)
is a pair where the first element is equal to (insertLookupWithKey
f k x map
)
and the second element equal to (lookup
k map
).
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.
updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a, IntMap a)Source
O(min(n,W)). Lookup and update.
Combine
Union
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.
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.
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
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
threads an accumulating
argument through the map in ascending order of keys.
mapAccum
mapAccumWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)Source
O(n). The function
threads an accumulating
argument through the map in ascending order of keys.
mapAccumWithKey
Fold
foldWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> bSource
O(n). Fold the keys and values in the map, such that
.
For example,
foldWithKey
f z == Prelude.foldr
(uncurry
f) z . toAscList
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.
assocs :: IntMap a -> [(Key, a)]Source
O(n). Return all key/value pairs in the map in ascending key order.
Lists
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.
split :: Key -> IntMap a -> (IntMap a, IntMap a)Source
O(log n). The expression (
) is a pair split
k map(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 (
) returns isSubmapOfBy
f m1 m2True
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(log n). Retrieves the maximal key of the map, and the map stripped from that element.
fail
s (in the monad) when passed an empty map.
O(log n). Retrieves the minimal key of the map, and the map stripped from that element.
fail
s (in the monad) when passed an empty map.
O(log n). Delete and find the maximal element.
O(log n). Delete and find the minimal element.
O(log n). The minimal key of the map.
O(log n). The maximal key of the map.
O(log n). Delete the minimal key.
O(log n). Delete the maximal key.
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 (
) returns isProperSubmapOfBy
f m1 m2True
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
deleteFindMin :: IntMap b -> (b, IntMap b)Source
deleteFindMax :: IntMap b -> (b, IntMap b)Source
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.
fail
s (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.
fail
s (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 (
) shows
the tree that implements the map. If showTreeWith
hang wide maphang
is
True
, a hanging tree is shown otherwise a rotated tree is shown. If
wide
is True
, an extra wide version is shown.