Data.HashMap.Strict
A map from hashable keys to values. A map cannot contain
duplicate keys; each key can map to at most one value. A HashMap
makes no guarantees as to the order of its elements.
The implementation is based on hash array mapped tries. A
HashMap
is often faster than other tree-based set types,
especially when key comparison is expensive, as in the case of
strings.
Many operations have a average-case complexity of
Strictness properties
This module satisfies the following strictness properties:
- Key arguments are evaluated to WHNF;
- Keys and values are evaluated to WHNF before they are stored in the map.
A map from keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
Instances
Bifoldable HashMap Source # | Since: 0.2.11 |
Defined in Data.HashMap.Internal | |
Eq2 HashMap Source # | |
Ord2 HashMap Source # | |
Defined in Data.HashMap.Internal | |
Show2 HashMap Source # | |
Defined in Data.HashMap.Internal | |
NFData2 HashMap Source # | Since: 0.2.14.0 |
Defined in Data.HashMap.Internal | |
Hashable2 HashMap Source # | |
(Lift k, Lift v) => Lift (HashMap k v :: Type) Source # | Since: 0.2.17.0 |
Foldable (HashMap k) Source # | |
Defined in Data.HashMap.Internal Methods fold :: Monoid m => HashMap k m -> m Source # foldMap :: Monoid m => (a -> m) -> HashMap k a -> m Source # foldMap' :: Monoid m => (a -> m) -> HashMap k a -> m Source # foldr :: (a -> b -> b) -> b -> HashMap k a -> b Source # foldr' :: (a -> b -> b) -> b -> HashMap k a -> b Source # foldl :: (b -> a -> b) -> b -> HashMap k a -> b Source # foldl' :: (b -> a -> b) -> b -> HashMap k a -> b Source # foldr1 :: (a -> a -> a) -> HashMap k a -> a Source # foldl1 :: (a -> a -> a) -> HashMap k a -> a Source # toList :: HashMap k a -> [a] Source # null :: HashMap k a -> Bool Source # length :: HashMap k a -> Int Source # elem :: Eq a => a -> HashMap k a -> Bool Source # maximum :: Ord a => HashMap k a -> a Source # minimum :: Ord a => HashMap k a -> a Source # | |
Eq k => Eq1 (HashMap k) Source # | |
Ord k => Ord1 (HashMap k) Source # | |
Defined in Data.HashMap.Internal | |
(Eq k, Hashable k, Read k) => Read1 (HashMap k) Source # | |
Defined in Data.HashMap.Internal Methods liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (HashMap k a) Source # liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [HashMap k a] Source # liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (HashMap k a) Source # liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [HashMap k a] Source # | |
Show k => Show1 (HashMap k) Source # | |
Traversable (HashMap k) Source # | |
Defined in Data.HashMap.Internal Methods traverse :: Applicative f => (a -> f b) -> HashMap k a -> f (HashMap k b) Source # sequenceA :: Applicative f => HashMap k (f a) -> f (HashMap k a) Source # mapM :: Monad m => (a -> m b) -> HashMap k a -> m (HashMap k b) Source # sequence :: Monad m => HashMap k (m a) -> m (HashMap k a) Source # | |
Functor (HashMap k) Source # | |
NFData k => NFData1 (HashMap k) Source # | Since: 0.2.14.0 |
Defined in Data.HashMap.Internal | |
Hashable k => Hashable1 (HashMap k) Source # | |
Defined in Data.HashMap.Internal | |
(Data k, Data v, Eq k, Hashable k) => Data (HashMap k v) Source # | |
Defined in Data.HashMap.Internal Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> HashMap k v -> c (HashMap k v) Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (HashMap k v) Source # toConstr :: HashMap k v -> Constr Source # dataTypeOf :: HashMap k v -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (HashMap k v)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (HashMap k v)) Source # gmapT :: (forall b. Data b => b -> b) -> HashMap k v -> HashMap k v Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> HashMap k v -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> HashMap k v -> r Source # gmapQ :: (forall d. Data d => d -> u) -> HashMap k v -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> HashMap k v -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> HashMap k v -> m (HashMap k v) Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> HashMap k v -> m (HashMap k v) Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> HashMap k v -> m (HashMap k v) Source # | |
(Eq k, Hashable k) => Monoid (HashMap k v) Source # | If a key occurs in both maps, the mapping from the first will be the mapping in the result. Examples
|
(Eq k, Hashable k) => Semigroup (HashMap k v) Source # | If a key occurs in both maps, the mapping from the first will be the mapping in the result. Examples
|
(Eq k, Hashable k) => IsList (HashMap k v) Source # | |
(Eq k, Hashable k, Read k, Read e) => Read (HashMap k e) Source # | |
(Show k, Show v) => Show (HashMap k v) Source # | |
(NFData k, NFData v) => NFData (HashMap k v) Source # | |
Defined in Data.HashMap.Internal | |
(Eq k, Eq v) => Eq (HashMap k v) Source # | Note that, in the presence of hash collisions, equal
In general, the lack of substitutivity can be observed with any function that depends on the key ordering, such as folds and traversals. |
(Ord k, Ord v) => Ord (HashMap k v) Source # | The ordering is total and consistent with the |
Defined in Data.HashMap.Internal Methods compare :: HashMap k v -> HashMap k v -> Ordering Source # (<) :: HashMap k v -> HashMap k v -> Bool Source # (<=) :: HashMap k v -> HashMap k v -> Bool Source # (>) :: HashMap k v -> HashMap k v -> Bool Source # (>=) :: HashMap k v -> HashMap k v -> Bool Source # | |
(Hashable k, Hashable v) => Hashable (HashMap k v) Source # | |
type Item (HashMap k v) Source # | |
Defined in Data.HashMap.Internal |
Construction
Basic interface
lookup :: (Eq k, Hashable k) => k -> HashMap k v -> Maybe v Source #
Nothing
if this map contains no mapping for the key.
Since: 0.2.11
DEPRECATED: lookupDefault is deprecated as of version 0.2.11, replaced
by findWithDefault
.
(!) :: (Eq k, Hashable k, HasCallStack) => HashMap k v -> k -> v infixl 9 Source #
error
if this map contains no mapping for the key.
insert :: (Eq k, Hashable k) => k -> v -> HashMap k v -> HashMap k v Source #
insertWith :: (Eq k, Hashable k) => (v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v Source #
insertWith f k v map where f new old = new + old
delete :: (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v Source #
adjust :: (Eq k, Hashable k) => (v -> v) -> k -> HashMap k v -> HashMap k v Source #
alterF :: (Functor f, Eq k, Hashable k) => (Maybe v -> f (Maybe v)) -> k -> HashMap k v -> f (HashMap k v) Source #
) alters the value alterF
f k mapx
at
k
, or absence thereof.
alterF
can be used to insert, delete, or update a value in a map.
Note: alterF
is a flipped version of the at
combinator from
Control.Lens.At.
Since: 0.2.10
isSubmapOf :: (Eq k, Hashable k, Eq v) => HashMap k v -> HashMap k v -> Bool Source #
isSubmapOf m1 m2 = keys m1 `isSubsetOf` keys m2 && and [ v1 == v2 | (k1,v1) <- toList m1; let v2 = m2 ! k1 ]
Examples
>>>
fromList [(1,'a')] `isSubmapOf` fromList [(1,'a'),(2,'b')]
True
>>>
fromList [(1,'a'),(2,'b')] `isSubmapOf` fromList [(1,'a')]
False
Since: 0.2.12
isSubmapOfBy :: (Eq k, Hashable k) => (v1 -> v2 -> Bool) -> HashMap k v1 -> HashMap k v2 -> Bool Source #
isSubmapOfBy cmpV m1 m2 = keys m1 `isSubsetOf` keys m2 && and [ v1 `cmpV` v2 | (k1,v1) <- toList m1; let v2 = m2 ! k1 ]
Examples
>>>
isSubmapOfBy (<=) (fromList [(1,'a')]) (fromList [(1,'b'),(2,'c')])
True
>>>
isSubmapOfBy (<=) (fromList [(1,'b')]) (fromList [(1,'a'),(2,'c')])
False
Since: 0.2.12
Combine
Union
union :: (Eq k, Hashable k) => HashMap k v -> HashMap k v -> HashMap k v Source #
Examples
>>>
union (fromList [(1,'a'),(2,'b')]) (fromList [(2,'c'),(3,'d')])
fromList [(1,'a'),(2,'b'),(3,'d')]
unionWith :: (Eq k, Hashable k) => (v -> v -> v) -> HashMap k v -> HashMap k v -> HashMap k v Source #
unionWithKey :: (Eq k, Hashable k) => (k -> v -> v -> v) -> HashMap k v -> HashMap k v -> HashMap k v Source #
unions :: (Eq k, Hashable k) => [HashMap k v] -> HashMap k v Source #
Construct a set containing all elements from a list of sets.
Compose
compose :: (Eq b, Hashable b) => HashMap b c -> HashMap a b -> HashMap a c Source #
Relate the keys of one map to the values of the other, by using the values of the former as keys for lookups in the latter.
Complexity:
>>>
compose (fromList [('a', "A"), ('b', "B")]) (fromList [(1,'a'),(2,'b'),(3,'z')])
fromList [(1,"A"),(2,"B")]
(compose
bc ab!?
) = (bc!?
) <=< (ab!?
)
Since: 0.2.13.0
Transformations
map :: (v1 -> v2) -> HashMap k v1 -> HashMap k v2 Source #
mapWithKey :: (k -> v1 -> v2) -> HashMap k v1 -> HashMap k v2 Source #
traverseWithKey :: Applicative f => (k -> v1 -> f v2) -> HashMap k v1 -> f (HashMap k v2) Source #
Applicative
action for each key-value pair
in a HashMap
and produce a HashMap
of all the results. Each HashMap
will be strict in all its values.
traverseWithKey f = fmap (map
id) . Data.HashMap.Lazy.traverseWithKey
f
Note: the order in which the actions occur is unspecified. In particular, when the map contains hash collisions, the order in which the actions associated with the keys involved will depend in an unspecified way on their insertion order.
mapKeys :: (Eq k2, Hashable k2) => (k1 -> k2) -> HashMap k1 v -> HashMap k2 v Source #
is the map obtained by applying mapKeys
f sf
to each key of s
.
The size of the result may be smaller if f
maps two or more distinct
keys to the same new key. In this case there is no guarantee which of the
associated values is chosen for the conflicting key.
>>>
mapKeys (+ 1) (fromList [(5,"a"), (3,"b")])
fromList [(4,"b"),(6,"a")]>>>
mapKeys (\ _ -> 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")])
fromList [(1,"c")]>>>
mapKeys (\ _ -> 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")])
fromList [(3,"c")]
Since: 0.2.14.0
Difference and intersection
difference :: (Eq k, Hashable k) => HashMap k v -> HashMap k w -> HashMap k v Source #
differenceWith :: (Eq k, Hashable k) => (v -> w -> Maybe v) -> HashMap k v -> HashMap k w -> HashMap k v Source #
intersection :: (Eq k, Hashable k) => HashMap k v -> HashMap k w -> HashMap k v Source #
intersectionWith :: (Eq k, Hashable k) => (v1 -> v2 -> v3) -> HashMap k v1 -> HashMap k v2 -> HashMap k v3 Source #
intersectionWithKey :: (Eq k, Hashable k) => (k -> v1 -> v2 -> v3) -> HashMap k v1 -> HashMap k v2 -> HashMap k v3 Source #
Folds
foldMapWithKey :: Monoid m => (k -> v -> m) -> HashMap k v -> m Source #
foldr :: (v -> a -> a) -> a -> HashMap k v -> a Source #
foldl :: (a -> v -> a) -> a -> HashMap k v -> a Source #
foldr' :: (v -> a -> a) -> a -> HashMap k v -> a Source #
foldl' :: (a -> v -> a) -> a -> HashMap k v -> a Source #
foldrWithKey' :: (k -> v -> a -> a) -> a -> HashMap k v -> a Source #
foldlWithKey' :: (a -> k -> v -> a) -> a -> HashMap k v -> a Source #
foldrWithKey :: (k -> v -> a -> a) -> a -> HashMap k v -> a Source #
foldlWithKey :: (a -> k -> v -> a) -> a -> HashMap k v -> a Source #
Filter
filter :: (v -> Bool) -> HashMap k v -> HashMap k v Source #
filterWithKey :: forall k v. (k -> v -> Bool) -> HashMap k v -> HashMap k v Source #
mapMaybe :: (v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2 Source #
mapMaybeWithKey :: (k -> v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2 Source #
Conversions
elems :: HashMap k v -> [v] Source #
Lists
toList :: HashMap k v -> [(k, v)] Source #
fromList :: (Eq k, Hashable k) => [(k, v)] -> HashMap k v Source #
fromListWith :: (Eq k, Hashable k) => (v -> v -> v) -> [(k, v)] -> HashMap k v Source #
f
to merge duplicate entries with
(f newVal oldVal)
.
Examples
Given a list xs
, create a map with the number of occurrences of each
element in xs
:
let xs = ['a', 'b', 'a'] in fromListWith (+) [ (x, 1) | x <- xs ] = fromList [('a', 2), ('b', 1)]
Given a list of key-value pairs xs :: [(k, v)]
, group all values by their
keys and return a HashMap k [v]
.
let xs = ('a', 1), ('b', 2), ('a', 3)] in fromListWith (++) [ (k, [v]) | (k, v) <- xs ] = fromList [('a', [3, 1]), ('b', [2])]
Note that the lists in the resulting map contain elements in reverse order from their occurences in the original list.
More generally, duplicate entries are accumulated as follows;
this matters when f
is not commutative or not associative.
fromListWith f [(k, a), (k, b), (k, c), (k, d)] = fromList [(k, f d (f c (f b a)))]
fromListWithKey :: (Eq k, Hashable k) => (k -> v -> v -> v) -> [(k, v)] -> HashMap k v Source #
Examples
Given a list of key-value pairs where the keys are of different flavours, e.g:
data Key = Div | Sub
and the values need to be combined differently when there are duplicates, depending on the key:
combine Div = div combine Sub = (-)
then fromListWithKey
can be used as follows:
fromListWithKey combine [(Div, 2), (Div, 6), (Sub, 2), (Sub, 3)] = fromList [(Div, 3), (Sub, 1)]
More generally, duplicate entries are accumulated as follows;
fromListWith f [(k, a), (k, b), (k, c), (k, d)] = fromList [(k, f k d (f k c (f k b a)))]
Since: 0.2.11