| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Data.IndexedMap
Description
An indexed map is an efficient map with integer keys, that can efficiently retrieve the key from a value. This is used to efficiently build up a constant pool without duplicating entries. Because of the specialised nature, its indexes start at 1, not 0. I would apologise but I'm not sorry.
Synopsis
- data IndexedMap a = IndexedMap !(IntMap a) !(Map a Int)
- empty :: IndexedMap a
- singleton :: Ord a => a -> IndexedMap a
- lookup :: Int -> IndexedMap a -> Maybe a
- lookupIndex :: Ord a => a -> IndexedMap a -> Maybe Int
- lookupIndexWhere :: (a -> Bool) -> IndexedMap a -> Maybe Int
- insert :: Ord a => a -> IndexedMap a -> (Int, IndexedMap a)
- lookupOrInsert :: Ord a => a -> IndexedMap a -> (Int, IndexedMap a)
- lookupOrInsertM :: forall a (r :: [Effect]). (State (IndexedMap a) :> r, Ord a) => a -> Eff r Int
- lookupOrInsertMOver :: forall a (r :: [Effect]) b. (State a :> r, Ord b) => Lens' a (IndexedMap b) -> b -> Eff r Int
- isEmpty :: IndexedMap a -> Bool
- toVector :: IndexedMap a -> Vector a
Documentation
data IndexedMap a Source #
Constructors
| IndexedMap !(IntMap a) !(Map a Int) |
Instances
empty :: IndexedMap a Source #
An empty indexed map >>> lookup @String 1 empty Nothing
singleton :: Ord a => a -> IndexedMap a Source #
Create an indexed map with a single element
>>> lookup String 1 (singleton "hello")
Just "hello"
>>> lookup String 2 (singleton "hello")
Nothing
lookupIndex :: Ord a => a -> IndexedMap a -> Maybe Int Source #
Lookup a value in the map
>>> lookupIndex String "hello" (singleton "hello")
Just 1
>>> lookupIndex String "hello" (singleton "world")
Nothing
>>> lookupIndex @String "hello" (singleton "world" <> singleton "hello")
Just 2
lookupIndexWhere :: (a -> Bool) -> IndexedMap a -> Maybe Int Source #
Find the index of the first element that satisfies the predicate, if any >>> lookupIndexWhere (== "hello") (singleton "hello") Just 1
>>>lookupIndexWhere (== "hello") (singleton "world")Nothing
insert :: Ord a => a -> IndexedMap a -> (Int, IndexedMap a) Source #
Insert a value into the map without checking if it already exists
lookupOrInsert :: Ord a => a -> IndexedMap a -> (Int, IndexedMap a) Source #
Lookup a value in the map, or insert it if it doesn't exist
lookupOrInsertM :: forall a (r :: [Effect]). (State (IndexedMap a) :> r, Ord a) => a -> Eff r Int Source #
lookupOrInsertMOver :: forall a (r :: [Effect]) b. (State a :> r, Ord b) => Lens' a (IndexedMap b) -> b -> Eff r Int Source #
isEmpty :: IndexedMap a -> Bool Source #
toVector :: IndexedMap a -> Vector a Source #
O(n) conversion to a vector This relies on the fact that IndexedMap is strictly increasing in the key
>>>toVector (singleton @Int 1)[1]
>>>toVector (singleton @Int 1 <> singleton 2)[1,2]
>>>toVector (singleton @Int 1 <> singleton 2 <> singleton 1)[1,2]