| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Mikan.Utils.BiMap
Description
Partly invertible finite maps.
Time complexities are given under the assumption that all relevant instance functions, as well as arguments of function type, take constant time, and "n" is the number of keys involved in the operation.
Synopsis
- data BiMap k v = BiMap {
- biMapThere :: !(Map k v)
- biMapBack :: !(Map (Tag v) k)
- class HasTag a where
- source :: Ord k => k -> BiMap k v -> Bool
- target :: Ord (Tag v) => Tag v -> BiMap k v -> Bool
- lookup :: Ord k => k -> BiMap k v -> Maybe v
- invLookup :: Ord (Tag v) => Tag v -> BiMap k v -> Maybe k
- singleton :: HasTag v => k -> v -> BiMap k v
- insert :: (Ord k, HasTag v, Ord (Tag v)) => k -> v -> BiMap k v -> BiMap k v
- insertLookupWithKey :: (Ord k, Ord (Tag v), HasTag v) => (k -> v -> v -> v) -> k -> v -> BiMap k v -> (Maybe v, BiMap k v)
- alter :: (Ord k, Ord (Tag v), HasTag v) => (Maybe v -> Maybe v) -> k -> BiMap k v -> BiMap k v
- alterM :: (Ord k, Ord (Tag v), HasTag v, Monad m) => (Maybe v -> m (Maybe v)) -> k -> BiMap k v -> m (BiMap k v)
- update :: (Ord k, Ord (Tag v), HasTag v) => (v -> Maybe v) -> k -> BiMap k v -> BiMap k v
- adjust :: (Ord k, Ord (Tag v), HasTag v) => (v -> v) -> k -> BiMap k v -> BiMap k v
- union :: (Ord k, Ord (Tag v)) => BiMap k v -> BiMap k v -> BiMap k v
- mapWithKey :: (Ord k, Ord (Tag v), HasTag v) => (k -> v -> v) -> BiMap k v -> BiMap k v
- mapWithKeyFixedTags :: (k -> v -> v) -> BiMap k v -> BiMap k v
- fromList :: (Ord k, Ord (Tag v), HasTag v) => [(k, v)] -> BiMap k v
- fromDistinctAscendingLists :: ([(k, v)], [(Tag v, k)]) -> BiMap k v
- toList :: BiMap k v -> [(k, v)]
- toDistinctAscendingLists :: BiMap k v -> ([(k, v)], [(Tag v, k)])
- keys :: BiMap k v -> [k]
- elems :: BiMap k v -> [v]
- tagInjectiveFor :: (Eq v, Eq (Tag v), HasTag v) => [v] -> Bool
- biMapInvariant :: (Eq k, Eq v, Ord (Tag v), HasTag v) => BiMap k v -> Bool
- insertPrecondition :: (Eq k, Eq (Tag v), HasTag v) => k -> v -> BiMap k v -> Bool
- insertLookupWithKeyPrecondition :: (Ord k, Eq (Tag v), HasTag v) => (k -> v -> v -> v) -> k -> v -> BiMap k v -> Bool
- alterPrecondition :: (Ord k, Eq (Tag v), HasTag v) => (Maybe v -> Maybe v) -> k -> BiMap k v -> Bool
- updatePrecondition :: (Ord k, Eq (Tag v), HasTag v) => (v -> Maybe v) -> k -> BiMap k v -> Bool
- adjustPrecondition :: (Ord k, Eq (Tag v), HasTag v) => (v -> v) -> k -> BiMap k v -> Bool
- unionPrecondition :: (Ord k, Eq v, Eq (Tag v), HasTag v) => BiMap k v -> BiMap k v -> Bool
- mapWithKeyPrecondition :: (Eq k, Eq v, Eq (Tag v), HasTag v) => (k -> v -> v) -> BiMap k v -> Bool
- mapWithKeyFixedTagsPrecondition :: (Eq (Tag v), HasTag v) => (k -> v -> v) -> BiMap k v -> Bool
- fromListPrecondition :: (Eq k, Eq v, Eq (Tag v), HasTag v) => [(k, v)] -> Bool
- fromDistinctAscendingListsPrecondition :: (Ord k, Eq v, Ord (Tag v), HasTag v) => ([(k, v)], [(Tag v, k)]) -> Bool
Documentation
Finite maps from k to v, with a way to quickly get from v
to k for certain values of type v (those for which tag is
defined).
Every value of this type must satisfy biMapInvariant.
Instances
Partial injections from a type to some tag type.
The idea is that tag should be injective on its domain: if
, then tag x = tag y = Just ix = y. However, this
property does not need to hold globally. The preconditions of the
BiMap operations below specify for which sets of values tag
must be injective.
Instances
| HasTag ModuleNameHash Source # | |||||
Defined in Mikan.Syntax.TopLevelModuleName.Boot Associated Types
Methods tag :: ModuleNameHash -> Maybe (Tag ModuleNameHash) Source # | |||||
| HasTag InteractionPoint Source # | |||||
Defined in Mikan.TypeChecking.Monad.Base Associated Types
Methods tag :: InteractionPoint -> Maybe (Tag InteractionPoint) Source # | |||||
| HasTag (TopLevelModuleName' range) Source # | |||||
Defined in Mikan.Syntax.TopLevelModuleName.Boot Associated Types
Methods tag :: TopLevelModuleName' range -> Maybe (Tag (TopLevelModuleName' range)) Source # | |||||
Queries
source :: Ord k => k -> BiMap k v -> Bool Source #
\(\mathcal{O}(\log n)\). Is the value a source key?
target :: Ord (Tag v) => Tag v -> BiMap k v -> Bool Source #
\(\mathcal{O}(\log n)\). Is the value a target key?
invLookup :: Ord (Tag v) => Tag v -> BiMap k v -> Maybe k Source #
\(\mathcal{O}(\log n)\). Inverse key lookup.
Creation
Insertion
insert :: (Ord k, HasTag v, Ord (Tag v)) => k -> v -> BiMap k v -> BiMap k v Source #
\(\mathcal{O}(\log n)\). Insert an element into a map. Overwrites existing values.
Precondition: See insertPrecondition.
insertLookupWithKey :: (Ord k, Ord (Tag v), HasTag v) => (k -> v -> v -> v) -> k -> v -> BiMap k v -> (Maybe v, BiMap k v) Source #
Inserts a binding into the map. If a binding for the key already exists, then the value obtained by applying the function to the key, the new value and the old value is inserted, and the old value is returned.
Precondition: See insertLookupWithKeyPrecondition.
Update
alter :: (Ord k, Ord (Tag v), HasTag v) => (Maybe v -> Maybe v) -> k -> BiMap k v -> BiMap k v Source #
\(\mathcal{O}(\log n)\). Modifies the value at the given position, if any. If the function
returns Nothing, then the value is removed.
Precondition: See alterPrecondition.
alterM :: (Ord k, Ord (Tag v), HasTag v, Monad m) => (Maybe v -> m (Maybe v)) -> k -> BiMap k v -> m (BiMap k v) Source #
(mathcal{O}(log n).) Modifies the value at the given position, if any. If the function
returns Nothing, then the value is removed.
The precondition for is that, if the value alterM f k mv is
inserted into m, and is defined, then no key other than
tag vk may map to a value v' for which .tag v' = tag v
update :: (Ord k, Ord (Tag v), HasTag v) => (v -> Maybe v) -> k -> BiMap k v -> BiMap k v Source #
\(\mathcal{O}(\log n)\). Modifies the value at the given position, if any. If the function
returns Nothing, then the value is removed.
Precondition: See updatePrecondition.
adjust :: (Ord k, Ord (Tag v), HasTag v) => (v -> v) -> k -> BiMap k v -> BiMap k v Source #
\(\mathcal{O}(\log n)\). Modifies the value at the given position, if any.
Precondition: See adjustPrecondition.
Combining
union :: (Ord k, Ord (Tag v)) => BiMap k v -> BiMap k v -> BiMap k v Source #
Left-biased union. For the time complexity, see union.
Precondition: See unionPrecondition.
Traversal
mapWithKey :: (Ord k, Ord (Tag v), HasTag v) => (k -> v -> v) -> BiMap k v -> BiMap k v Source #
\(\mathcal{O}(n \log n)\). Map a function over all values in the map.
Precondition: See mapWithKeyPrecondition.
mapWithKeyFixedTags :: (k -> v -> v) -> BiMap k v -> BiMap k v Source #
\(\mathcal{O}(n)\). Changes all the values using the given function, which is also given access to keys.
Precondition: See mapWithKeyFixedTagsPrecondition. Note that tags
must not change.
Conversion
fromList :: (Ord k, Ord (Tag v), HasTag v) => [(k, v)] -> BiMap k v Source #
\(\mathcal{O}(n \log n)\). Conversion from lists of pairs. Later entries take precedence over earlier ones.
Precondition: See fromListPrecondition.
fromDistinctAscendingLists :: ([(k, v)], [(Tag v, k)]) -> BiMap k v Source #
\(\mathcal{O}(n)\). Conversion from two lists that contain distinct keys/tags, with the keys/tags in ascending order.
Precondition: See fromDistinctAscendingListsPrecondition.
toList :: BiMap k v -> [(k, v)] Source #
\(\mathcal{O}(n)\). Conversion to lists of pairs, with the keys in ascending order.
toDistinctAscendingLists :: BiMap k v -> ([(k, v)], [(Tag v, k)]) Source #
\(\mathcal{O}(n)\). Generates input suitable for fromDistinctAscendingLists.
elems :: BiMap k v -> [v] Source #
\(\mathcal{O}(n)\). The values, ordered according to the corresponding keys.
Debugging
tagInjectiveFor :: (Eq v, Eq (Tag v), HasTag v) => [v] -> Bool Source #
Checks if the function tag is injective for the values in the
given list for which the function is defined.
biMapInvariant :: (Eq k, Eq v, Ord (Tag v), HasTag v) => BiMap k v -> Bool Source #
The invariant for BiMap.
insertLookupWithKeyPrecondition :: (Ord k, Eq (Tag v), HasTag v) => (k -> v -> v -> v) -> k -> v -> BiMap k v -> Bool Source #
The precondition for is that, if
the value insertLookupWithKey f k v mv' is inserted into m, and is defined,
then no key other than tag v'k may map to a value v'' for which
.tag v'' = tag v'
alterPrecondition :: (Ord k, Eq (Tag v), HasTag v) => (Maybe v -> Maybe v) -> k -> BiMap k v -> Bool Source #
updatePrecondition :: (Ord k, Eq (Tag v), HasTag v) => (v -> Maybe v) -> k -> BiMap k v -> Bool Source #
mapWithKeyPrecondition :: (Eq k, Eq v, Eq (Tag v), HasTag v) => (k -> v -> v) -> BiMap k v -> Bool Source #
The precondition for : For any two distinct
mappings mapWithKey f mk₁ ↦ v₁, k₂ ↦ v₂ in m for which the tags of
f k₁ v₁ and f k₂ v₂ are defined the values of f must be
distinct (f k₁ v₁ ≠ f k₂ v₂). Furthermore tag must be injective
for { f k v | (k, v) ∈ m }.
mapWithKeyFixedTagsPrecondition :: (Eq (Tag v), HasTag v) => (k -> v -> v) -> BiMap k v -> Bool Source #
The precondition for is that, if mapWithKeyFixedTags f mm
maps k to v, then .tag (f k v) == tag v
fromDistinctAscendingListsPrecondition :: (Ord k, Eq v, Ord (Tag v), HasTag v) => ([(k, v)], [(Tag v, k)]) -> Bool Source #
The precondition for : The
lists must contain distinct keys/tags, and must be sorted according
to the keys/tags. Furthermore, for every pair fromDistinctAscendingLists (kvs, kks)(k, v) in the first
list for which there must be a pair tag v = Just k'(k', k)
in the second list, and there must not be any other pairs in that
list. Finally tag must be injective for {v | (_, v) ∈ kvs }.