{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -O2 -Wall #-}
module Data.Map.Lifted.Lifted
( Map(..)
, empty
, singleton
, lookup
, size
, map
, mapMaybe
, mapMaybeWithKey
, appendWithKey
, union
, foldlWithKey'
, foldrWithKey'
, foldMapWithKey'
, foldlWithKeyM'
, foldrWithKeyM'
, foldlMapWithKeyM'
, foldrMapWithKeyM'
, toList
, fromList
, fromListAppend
, fromListN
, fromListAppendN
, fromSet
, keys
, elems
) where
import Prelude hiding (lookup,map)
import Data.Semigroup (Semigroup)
import Data.Primitive.Array (Array)
import Data.Set.Lifted.Internal (Set(..))
import qualified GHC.Exts as E
import qualified Data.Semigroup as SG
import qualified Data.Map.Internal as I
newtype Map k v = Map (I.Map Array Array k v)
instance Functor (Map k) where
fmap = map
instance (Ord k, Semigroup v) => Semigroup (Map k v) where
Map x <> Map y = Map (I.append x y)
instance (Ord k, Semigroup v) => Monoid (Map k v) where
mempty = Map I.empty
mappend = (SG.<>)
mconcat = Map . I.concat . E.coerce
instance (Eq k, Eq v) => Eq (Map k v) where
Map x == Map y = I.equals x y
instance (Ord k, Ord v) => Ord (Map k v) where
compare (Map x) (Map y) = I.compare x y
instance Ord k => E.IsList (Map k v) where
type Item (Map k v) = (k,v)
fromListN n = Map . I.fromListN n
fromList = Map . I.fromList
toList (Map s) = I.toList s
instance (Show k, Show v) => Show (Map k v) where
showsPrec p (Map s) = I.showsPrec p s
empty :: Map k v
empty = Map I.empty
lookup :: Ord k => k -> Map k v -> Maybe v
lookup a (Map s) = I.lookup a s
singleton :: k -> v -> Map k v
singleton k v = Map (I.singleton k v)
toList :: Ord k => Map k v -> [(k,v)]
toList (Map m) = I.toList m
fromList :: Ord k => [(k,v)] -> Map k v
fromList = Map . I.fromList
fromListN :: Ord k
=> Int
-> [(k,v)]
-> Map k v
fromListN n = Map . I.fromListN n
fromListAppend :: (Ord k, Semigroup v) => [(k,v)] -> Map k v
fromListAppend = Map . I.fromListAppend
fromSet ::
(k -> v)
-> Set k
-> Map k v
fromSet f (Set s) = Map (I.fromSet f s)
fromListAppendN :: (Ord k, Semigroup v)
=> Int
-> [(k,v)]
-> Map k v
fromListAppendN n = Map . I.fromListAppendN n
size :: Map k v -> Int
size (Map m) = I.size m
map ::
(v -> w)
-> Map k v
-> Map k w
map f (Map m) = Map (I.map f m)
mapMaybe ::
(v -> Maybe w)
-> Map k v
-> Map k w
mapMaybe f (Map m) = Map (I.mapMaybe f m)
mapMaybeWithKey ::
(k -> v -> Maybe w)
-> Map k v
-> Map k w
mapMaybeWithKey f (Map m) = Map (I.mapMaybeWithKey f m)
appendWithKey :: Ord k
=> (k -> v -> v -> v)
-> Map k v
-> Map k v
-> Map k v
appendWithKey f (Map m) (Map n) = Map (I.appendWithKey f m n)
foldlWithKeyM' :: Monad m
=> (b -> k -> v -> m b)
-> b
-> Map k v
-> m b
foldlWithKeyM' f b0 (Map m) = I.foldlWithKeyM' f b0 m
foldrWithKeyM' :: Monad m
=> (k -> v -> b -> m b)
-> b
-> Map k v
-> m b
foldrWithKeyM' f b0 (Map m) = I.foldrWithKeyM' f b0 m
foldlMapWithKeyM' :: (Monad m, Monoid b)
=> (k -> v -> m b)
-> Map k v
-> m b
foldlMapWithKeyM' f (Map m) = I.foldlMapWithKeyM' f m
foldrMapWithKeyM' :: (Monad m, Monoid b)
=> (k -> v -> m b)
-> Map k v
-> m b
foldrMapWithKeyM' f (Map m) = I.foldrMapWithKeyM' f m
foldMapWithKey' :: Monoid b
=> (k -> v -> b)
-> Map k v
-> b
foldMapWithKey' f (Map m) = I.foldMapWithKey' f m
foldlWithKey' ::
(b -> k -> v -> b)
-> b
-> Map k v
-> b
foldlWithKey' f b0 (Map m) = I.foldlWithKey' f b0 m
foldrWithKey' ::
(k -> v -> b -> b)
-> b
-> Map k v
-> b
foldrWithKey' f b0 (Map m) = I.foldrWithKey' f b0 m
union :: Ord k => Map k v -> Map k v -> Map k v
union (Map a) (Map b) = Map (I.appendWith const a b)
keys :: Map k v -> Set k
keys (Map m) = Set (I.keys m)
elems :: Map k v -> Array v
elems (Map m) = I.elems m