uulib-0.9.5: Haskell Utrecht Tools Library

UU.DData.IntMap

Contents

Description

 

Synopsis

Map type

data IntMap a Source

A map of integers to values a.

Instances

Eq a => Eq (IntMap a) 
Show a => Show (IntMap a) 

type Key = IntSource

Operators

(!) :: IntMap a -> Key -> aSource

O(min(n,W)). See find.

(\\) :: IntMap a -> IntMap a -> IntMap aSource

O(n+m). See difference.

Query

isEmpty :: IntMap a -> BoolSource

O(1). Is the map empty?

size :: IntMap a -> IntSource

O(n). Number of elements in the map.

member :: Key -> IntMap a -> BoolSource

O(min(n,W)). Is the key a member of the map?

lookup :: Key -> IntMap a -> Maybe aSource

O(min(n,W)). Lookup the value of a key in the map.

find :: Key -> IntMap a -> aSource

O(min(n,W)). Find the value of a key. Calls error when the element can not be found.

findWithDefault :: a -> Key -> IntMap a -> aSource

O(min(n,W)). The expression (findWithDefault def k map) returns the value of key k or returns def when the key is not an element of the map.

Construction

empty :: IntMap aSource

O(1). The empty map.

single :: Key -> a -> IntMap aSource

O(1). A map of one element.

Insertion

insert :: Key -> a -> IntMap a -> IntMap aSource

O(min(n,W)). Insert a new key/value pair in the map. When the key is already an element of the set, it's value is replaced by the new value, ie. insert is left-biased.

insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap aSource

O(min(n,W)). Insert with a combining function.

insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap aSource

O(min(n,W)). Insert with a combining function.

insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)Source

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 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.

update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap aSource

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 aSource

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)Source

O(min(n,W)). Lookup and update.

Combine

Union

union :: IntMap a -> IntMap a -> IntMap aSource

O(n+m). The (left-biased) union of two sets.

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.

unions :: [IntMap a] -> IntMap aSource

The union of a list of maps.

Difference

difference :: IntMap a -> IntMap a -> IntMap aSource

O(n+m). Difference between two maps (based on keys).

differenceWith :: (a -> a -> Maybe a) -> IntMap a -> IntMap a -> IntMap aSource

O(n+m). Difference with a combining function.

differenceWithKey :: (Key -> a -> a -> Maybe a) -> IntMap a -> IntMap a -> IntMap aSource

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 a -> IntMap aSource

O(n+m). The (left-biased) intersection of two maps (based on keys).

intersectionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap aSource

O(n+m). The intersection with a combining function.

intersectionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap aSource

O(n+m). The intersection with a combining function.

Traversal

Map

map :: (a -> b) -> IntMap a -> IntMap bSource

O(n). Map a function over all values in the 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 mapAccum threads an accumulating argument through the map in an unspecified order.

mapAccumWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)Source

O(n). The function mapAccumWithKey threads an accumulating argument through the map in an unspecified order.

Fold

fold :: (a -> b -> b) -> b -> IntMap a -> bSource

O(n). Fold over the elements of a map in an unspecified order.

 sum map   = fold (+) 0 map
 elems map = fold (:) [] map

foldWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> bSource

O(n). Fold over the elements of a map in an unspecified order.

 keys map = foldWithKey (\k x ks -> k:ks) [] map

Conversion

elems :: IntMap a -> [a]Source

O(n). Return all elements of the map.

keys :: IntMap a -> [Key]Source

O(n). Return all keys of the map.

assocs :: IntMap a -> [(Key, a)]Source

O(n). Return all key/value pairs in the map.

Lists

toList :: IntMap a -> [(Key, a)]Source

O(n). Convert the map to a list of key/value pairs.

fromList :: [(Key, a)] -> IntMap aSource

O(n*min(n,W)). Create a map from a list of key/value pairs.

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.

split :: Key -> IntMap a -> (IntMap a, IntMap a)Source

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.

splitLookup :: Key -> IntMap a -> (Maybe a, IntMap a, IntMap a)Source

O(log n). Performs a split but also returns whether the pivot key was found in the original map.

Subset

subset :: Eq a => IntMap a -> IntMap a -> BoolSource

O(n+m). Is this a subset? Defined as (subset = subsetBy (==)).

subsetBy :: (a -> a -> Bool) -> IntMap a -> IntMap a -> BoolSource

O(n+m). The expression (subsetBy 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.

 subsetBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
 subsetBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
 subsetBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])

But the following are all False:

 subsetBy (==) (fromList [(1,2)]) (fromList [(1,1),(2,2)])
 subsetBy (<) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
 subsetBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])

properSubset :: Eq a => IntMap a -> IntMap a -> BoolSource

O(n+m). Is this a proper subset? (ie. a subset but not equal). Defined as (properSubset = properSubsetBy (==)).

properSubsetBy :: (a -> a -> Bool) -> IntMap a -> IntMap a -> BoolSource

O(n+m). Is this a proper subset? (ie. a subset but not equal). The expression (properSubsetBy 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.

 properSubsetBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
 properSubsetBy (<=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])

But the following are all False:

 properSubsetBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
 properSubsetBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
 properSubsetBy (<)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])

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 (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.