hashmap-1.3.3: Persistent containers Map and Set based on hashing.

Data.HashMap

Description

Persistent Map based on hashing, which is defined as

  data Map k v = IntMap (Some k v)


is an IntMap indexed by hash values of keys, containing a value of Some e. That contains either one (k, v) pair or a Map k v with keys of the same hash values.

The interface of a Map is a suitable subset of IntMap and can be used as a drop-in replacement of Map.

The complexity of operations is determined by the complexities of IntMap and Map operations. See the sources of Map to see which operations from containers package are used.

Synopsis

# Documentation

data Map k v Source #

The abstract type of a Map. Its interface is a suitable subset of IntMap.

Instances

type HashMap k v = Map k v Source #

The HashMap is a type synonym for Map for backward compatibility. It is deprecated and will be removed in furture releases.

# Operators

(!) :: (Hashable k, Ord k) => Map k a -> k -> a Source #

Find the value at a key. Calls error when the element can not be found.

(\\) :: Ord k => Map k a -> Map k b -> Map k a Source #

Same as difference.

# Query

null :: Map k a -> Bool Source #

Is the map empty?

size :: Map k a -> Int Source #

Number of elements in the map.

member :: (Hashable k, Ord k) => k -> Map k a -> Bool Source #

Is the key a member of the map?

notMember :: (Hashable k, Ord k) => k -> Map k a -> Bool Source #

Is the key not a member of the map?

lookup :: (Hashable k, Ord k) => k -> Map k a -> Maybe a Source #

Lookup the value at a key in the map.

findWithDefault :: (Hashable k, Ord k) => a -> k -> Map k a -> a Source #

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 :: Map k a Source #

The empty map.

singleton :: Hashable k => k -> a -> Map k a Source #

A map of one element.

## Insertion

insert :: (Hashable k, Ord k) => k -> a -> Map k a -> Map k a Source #

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 :: (Hashable k, Ord k) => (a -> a -> a) -> k -> a -> Map k a -> Map k a Source #

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 :: (Hashable k, Ord k) => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a Source #

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 :: (Hashable k, Ord k) => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a) Source #

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 :: (Hashable k, Ord k) => k -> Map k a -> Map k a Source #

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 :: (Hashable k, Ord k) => (a -> a) -> k -> Map k a -> Map k a Source #

Adjust a value at a specific key. When the key is not a member of the map, the original map is returned.

adjustWithKey :: (Hashable k, Ord k) => (k -> a -> a) -> k -> Map k a -> Map k a Source #

Adjust a value at a specific key. When the key is not a member of the map, the original map is returned.

update :: (Hashable k, Ord k) => (a -> Maybe a) -> k -> Map k a -> Map k a Source #

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 :: (Hashable k, Ord k) => (k -> a -> Maybe a) -> k -> Map k a -> Map k a Source #

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 :: (Hashable k, Ord k) => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a) Source #

Lookup and update. The function returns original value, if it is updated. This is different behavior than updateLookupWithKey. Returns the original key value if the map entry is deleted.

alter :: (Hashable k, Ord k) => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a Source #

The expression (alter f k map) alters the value x at k, or absence thereof. alter can be used to insert, delete, or update a value in an Map.

# Combine

## Union

union :: Ord k => Map k a -> Map k a -> Map k a Source #

The (left-biased) union of two maps. It prefers the first map when duplicate keys are encountered, i.e. (union == unionWith const).

unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a Source #

The union with a combining function.

unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a Source #

The union with a combining function.

unions :: Ord k => [Map k a] -> Map k a Source #

The union of a list of maps.

unionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a Source #

The union of a list of maps, with a combining operation.

## Difference

difference :: Ord k => Map k a -> Map k b -> Map k a Source #

Difference between two maps (based on keys).

differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a Source #

Difference with a combining function.

differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a Source #

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 :: Ord k => Map k a -> Map k b -> Map k a Source #

The (left-biased) intersection of two maps (based on keys).

intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c Source #

The intersection with a combining function.

intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c Source #

The intersection with a combining function.

# Traversal

## Map

map :: (a -> b) -> Map k a -> Map k b Source #

Map a function over all values in the map.

mapWithKey :: (k -> a -> b) -> Map k a -> Map k b Source #

Map a function over all values in the map.

mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c) Source #

The function mapAccum threads an accumulating argument through the map in unspecified order of keys.

mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c) Source #

The function mapAccumWithKey threads an accumulating argument through the map in unspecified order of keys.

## Fold

fold :: (a -> b -> b) -> b -> Map k a -> b Source #

Fold the values in the map, such that fold f z == foldr f z . elems.

foldWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b Source #

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

# Conversion

elems :: Map k a -> [a] Source #

Return all elements of the map in arbitrary order of their keys.

keys :: Map k a -> [k] Source #

Return all keys of the map in arbitrary order.

keysSet :: Ord k => Map k a -> Set k Source #

The set of all keys of the map.

assocs :: Map k a -> [(k, a)] Source #

Return all key/value pairs in the map in arbitrary key order.

## Lists

toList :: Map k a -> [(k, a)] Source #

Convert the map to a list of key/value pairs.

fromList :: (Hashable k, Ord k) => [(k, a)] -> Map k a Source #

Create a map from a list of key/value pairs.

fromListWith :: (Hashable k, Ord k) => (a -> a -> a) -> [(k, a)] -> Map k a Source #

Create a map from a list of key/value pairs with a combining function.

fromListWithKey :: (Hashable k, Ord k) => (k -> a -> a -> a) -> [(k, a)] -> Map k a Source #

Build a map from a list of key/value pairs with a combining function.

# Filter

filter :: Ord k => (a -> Bool) -> Map k a -> Map k a Source #

Filter all values that satisfy some predicate.

filterWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> Map k a Source #

Filter all keys/values that satisfy some predicate.

partition :: Ord k => (a -> Bool) -> Map k a -> (Map k a, Map k a) Source #

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.

partitionWithKey :: Ord k => (k -> a -> Bool) -> Map k a -> (Map k a, Map k a) Source #

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.

mapMaybe :: Ord k => (a -> Maybe b) -> Map k a -> Map k b Source #

Map values and collect the Just results.

mapMaybeWithKey :: Ord k => (k -> a -> Maybe b) -> Map k a -> Map k b Source #

Map keys/values and collect the Just results.

mapEither :: Ord k => (a -> Either b c) -> Map k a -> (Map k b, Map k c) Source #

Map values and separate the Left and Right results.

mapEitherWithKey :: Ord k => (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c) Source #

Map keys/values and separate the Left and Right results.

# Submap

isSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool Source #

Is this a submap?

isSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool Source #

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.

isProperSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool Source #

Is this a proper submap? (ie. a submap but not equal).

isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool Source #

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.