h2jvm
Safe HaskellNone
LanguageGHC2021

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

Documentation

data IndexedMap a Source #

Constructors

IndexedMap !(IntMap a) !(Map a Int) 

Instances

Instances details
Foldable IndexedMap Source # 
Instance details

Defined in Data.IndexedMap

Methods

fold :: Monoid m => IndexedMap m -> m #

foldMap :: Monoid m => (a -> m) -> IndexedMap a -> m #

foldMap' :: Monoid m => (a -> m) -> IndexedMap a -> m #

foldr :: (a -> b -> b) -> b -> IndexedMap a -> b #

foldr' :: (a -> b -> b) -> b -> IndexedMap a -> b #

foldl :: (b -> a -> b) -> b -> IndexedMap a -> b #

foldl' :: (b -> a -> b) -> b -> IndexedMap a -> b #

foldr1 :: (a -> a -> a) -> IndexedMap a -> a #

foldl1 :: (a -> a -> a) -> IndexedMap a -> a #

toList :: IndexedMap a -> [a] #

null :: IndexedMap a -> Bool #

length :: IndexedMap a -> Int #

elem :: Eq a => a -> IndexedMap a -> Bool #

maximum :: Ord a => IndexedMap a -> a #

minimum :: Ord a => IndexedMap a -> a #

sum :: Num a => IndexedMap a -> a #

product :: Num a => IndexedMap a -> a #

Ord a => Monoid (IndexedMap a) Source #

Monoid instance for IndexedMap

Instance details

Defined in Data.IndexedMap

Ord a => Semigroup (IndexedMap a) Source #

Semigroup instance for IndexedMap | This is a left-biased union of the two maps

Instance details

Defined in Data.IndexedMap

Ord a => IsList (IndexedMap a) Source # 
Instance details

Defined in Data.IndexedMap

Associated Types

type Item (IndexedMap a) 
Instance details

Defined in Data.IndexedMap

type Item (IndexedMap a) = a
Show a => Show (IndexedMap a) Source # 
Instance details

Defined in Data.IndexedMap

Eq a => Eq (IndexedMap a) Source # 
Instance details

Defined in Data.IndexedMap

Methods

(==) :: IndexedMap a -> IndexedMap a -> Bool #

(/=) :: IndexedMap a -> IndexedMap a -> Bool #

Ord a => Ord (IndexedMap a) Source # 
Instance details

Defined in Data.IndexedMap

type Item (IndexedMap a) Source # 
Instance details

Defined in Data.IndexedMap

type Item (IndexedMap a) = a

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 #

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]