| Copyright | (c) 2017-2022 Kowainik |
|---|---|
| License | MPL-2.0 |
| Maintainer | Kowainik <xrom.xkov@gmail.com> |
| Stability | Stable |
| Portability | Portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Data.TMap
Description
TMap is a heterogeneous data structure similar in its essence to
Map with types as keys, where each value has the type of its key.
Here is an example of a TMap with a comparison to Map:
MapStringStringTMap-------------------- ----------------- "Int" -> "5"Int-> 5 "Bool" -> "True"Bool->True"Char" -> "'x'"Char-> 'x'
The runtime representation of TMap is an array, not a tree. This makes
lookup significantly more efficient.
Synopsis
- type TMap = TypeRepMap Identity
- empty :: TMap
- one :: forall a. Typeable a => a -> TMap
- insert :: forall a. Typeable a => a -> TMap -> TMap
- delete :: forall a. Typeable a => TMap -> TMap
- unionWith :: (forall x. Typeable x => x -> x -> x) -> TMap -> TMap -> TMap
- union :: TMap -> TMap -> TMap
- intersectionWith :: (forall x. Typeable x => x -> x -> x) -> TMap -> TMap -> TMap
- intersection :: TMap -> TMap -> TMap
- map :: (forall a. Typeable a => a -> a) -> TMap -> TMap
- adjust :: Typeable a => (a -> a) -> TMap -> TMap
- alter :: Typeable a => (Maybe a -> Maybe a) -> TMap -> TMap
- lookup :: forall a. Typeable a => TMap -> Maybe a
- member :: forall a. Typeable a => TMap -> Bool
- size :: TMap -> Int
- keys :: TMap -> [SomeTypeRep]
- keysWith :: (forall a. TypeRep a -> r) -> TMap -> [r]
- toListWith :: (forall a. Typeable a => a -> r) -> TMap -> [r]
Map type
type TMap = TypeRepMap Identity Source #
TMap is a special case of TypeRepMap when the interpretation is
Identity.
Construction
Modification
insert :: forall a. Typeable a => a -> TMap -> TMap Source #
Insert a value into a TMap.
TMap optimizes for fast reads rather than inserts, as a trade-off inserts are O(n).
size (insert v tm) >= size tm
member @a (insert (x :: a) tm) == True
delete :: forall a. Typeable a => TMap -> TMap Source #
Delete a value from a TMap.
TMap optimizes for fast reads rather than modifications, as a trade-off deletes are O(n),
with an O(log(n)) optimization for when the element is already missing.
size (delete @a tm) <= size tm
member @a (delete @a tm) == False
>>>tm = delete @Bool $ insert True $ one 'a'>>>size tm1>>>member @Bool tmFalse>>>member @Char tmTrue
unionWith :: (forall x. Typeable x => x -> x -> x) -> TMap -> TMap -> TMap Source #
The union of two TMaps using a combining function.
intersectionWith :: (forall x. Typeable x => x -> x -> x) -> TMap -> TMap -> TMap Source #
The intersection of two TMaps using a combining function.
O(n + m)
intersection :: TMap -> TMap -> TMap Source #
The intersection of two TMaps.
It keeps all values from the first map whose keys are present in the second.
O(n + m)
adjust :: Typeable a => (a -> a) -> TMap -> TMap Source #
Update a value with the result of the provided function.
alter :: Typeable a => (Maybe a -> Maybe a) -> TMap -> TMap Source #
Updates a value at a specific key, whether or not it exists. This can be used to insert, delete, or update a value of a given type in the map.
Query
lookup :: forall a. Typeable a => TMap -> Maybe a Source #
Lookup a value of the given type in a TMap.
>>>x = lookup $ insert (11 :: Int) empty>>>x :: Maybe IntJust 11>>>x :: Maybe ()Nothing
member :: forall a. Typeable a => TMap -> Bool Source #
Check if a value of the given type is present in a TMap.
>>>member @Char $ one 'a'True>>>member @Bool $ one 'a'False
keys :: TMap -> [SomeTypeRep] Source #
Returns the list of SomeTypeReps from keys.
keysWith :: (forall a. TypeRep a -> r) -> TMap -> [r] Source #
Return the list of keys by wrapping them with a user-provided function.
toListWith :: (forall a. Typeable a => a -> r) -> TMap -> [r] Source #
Return the list of key-value pairs by wrapping them with a user-provided function.