| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
H2JVM.Internal.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, since the constant pool is indexed by integers, and we often need to check if a value is already in the constant pool before inserting it. Because of the specialised nature, its indexes start at 1, not 0. I would apologise but I'm not sorry.
Synopsis
- data IndexedMap a
- data Index
- indexValue :: Index -> Word32
- empty :: IndexedMap a
- singleton :: Ord a => a -> IndexedMap a
- lookup :: Index -> IndexedMap a -> Maybe a
- lookupIndex :: Ord a => a -> IndexedMap a -> Maybe Index
- lookupIndexWhere :: (a -> Bool) -> IndexedMap a -> Maybe Index
- isEmpty :: IndexedMap a -> Bool
- insert :: Ord a => a -> IndexedMap a -> (Index, IndexedMap a)
- lookupOrInsert :: Ord a => a -> IndexedMap a -> (Index, IndexedMap a)
- lookupOrInsertM :: forall a (r :: [Effect]). (State (IndexedMap a) :> r, Ord a) => a -> Eff r Index
- lookupOrInsertMOver :: forall a (r :: [Effect]) b. (State a :> r, Ord b) => Lens' a (IndexedMap b) -> b -> Eff r Index
- toVector :: IndexedMap a -> Vector a
Types
data IndexedMap a Source #
An indexed map is a map from integer indexes to values, with a reverse map from values to indexes.
Instances
An index into the map.
indexValue :: Index -> Word32 Source #
Get the raw value of an index.
Construction
empty :: IndexedMap a Source #
An empty indexed map.
>>>lookup @String 1 emptyNothing
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
Lookup
lookup :: Index -> IndexedMap a -> Maybe a Source #
Lookup a value in the map by its index.
>>>lookup @String 1 (singleton "hello")Just "hello"
lookupIndex :: Ord a => a -> IndexedMap a -> Maybe Index Source #
Lookup a value in the map, returning its index if it exists.
>>>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 Index 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
isEmpty :: IndexedMap a -> Bool Source #
Check if the map is empty
>>>isEmpty emptyTrue>>>isEmpty (singleton "hello")False
Insertion
insert :: Ord a => a -> IndexedMap a -> (Index, IndexedMap a) Source #
Insert a value into the map without checking if it already exists. In other words, this will insert a duplicate value if it already exists in the map, and return a new index for it.
>>>insert "hello" empty(Index 1,fromList [(1,"hello")])
>>>insert "world" (singleton "hello")(Index 2,fromList [(1,"hello"),(2,"world")])>>>insert "hello" (singleton "hello")(Index 2,fromList [(1,"hello"),(2,"hello")])
lookupOrInsert :: Ord a => a -> IndexedMap a -> (Index, IndexedMap a) Source #
Lookup a value in the map, or insert it if it doesn't exist. If the value already exists, this will return the existing index and the original map.
>>>lookupOrInsert "hello" (singleton "hello")(Index 1,fromList [(1,"hello")])>>>lookupOrInsert "world" (singleton "hello")(Index 2,fromList [(1,"hello"),(2,"world")])
lookupOrInsertM :: forall a (r :: [Effect]). (State (IndexedMap a) :> r, Ord a) => a -> Eff r Index Source #
A monadic version of lookupOrInsert that can be used in a state monad.
lookupOrInsertMOver :: forall a (r :: [Effect]) b. (State a :> r, Ord b) => Lens' a (IndexedMap b) -> b -> Eff r Index Source #
A more general version of lookupOrInsertM that allows you to specify which lens to use for the state.
This is useful if you have an IndexedMap within a larger state, and you want to avoid having to manually get and put the IndexedMap every time you want to lookup or insert a value.
Conversion
toVector :: IndexedMap a -> Vector a Source #
\(O(n)\) conversion to a Vector. Duplicates are not removed, and the order of the vector is the order of the indexes in the map.
This relies on the fact that IndexedMap is strictly increasing in the key, so we can just generate a vector of the appropriate length and fill it with the values from the map.
>>>toVector (singleton @Int 1)[1]
>>>toVector (singleton @Int 1 <> singleton 2)[1,2]
>>>toVector (singleton @Int 1 <> singleton 2 <> singleton 1)[1,2]