{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -O2 #-}
module Data.Map.Unboxed.Unlifted
( Map
, empty
, singleton
, lookup
, size
, map
, mapMaybe
, mapMaybeP
, mapMaybeWithKey
, adjustMany
, foldlWithKey'
, foldrWithKey'
, foldMapWithKey'
, foldlWithKeyM'
, foldrWithKeyM'
, foldlMapWithKeyM'
, foldrMapWithKeyM'
, traverse
, fromList
, fromListAppend
, fromListN
, fromListAppendN
, fromSet
, fromSetP
, unsafeFreezeZip
) where
import Prelude hiding (lookup,map,traverse)
import Control.Monad.Primitive (PrimMonad)
import Control.Monad.ST (ST)
import Data.Primitive (PrimArray,MutablePrimArray)
import Data.Primitive.Types (Prim)
import Data.Primitive.UnliftedArray (PrimUnlifted,UnliftedArray,MutableUnliftedArray)
import Data.Semigroup (Semigroup)
import Data.Set.Unboxed.Internal (Set(..))
import qualified Data.Map.Internal as I
import qualified Data.Semigroup as SG
import qualified GHC.Exts as E
newtype Map k v = Map (I.Map PrimArray UnliftedArray k v)
instance (Prim k, Ord k, PrimUnlifted v, Semigroup v) => Semigroup (Map k v) where
Map x <> Map y = Map (I.append x y)
instance (Prim k, Ord k, PrimUnlifted v, Semigroup v) => Monoid (Map k v) where
mempty = Map I.empty
mappend = (SG.<>)
mconcat = Map . I.concat . E.coerce
instance (Prim k, Eq k, PrimUnlifted v, Eq v) => Eq (Map k v) where
Map x == Map y = I.equals x y
instance (Prim k, Ord k, PrimUnlifted v, Ord v) => Ord (Map k v) where
compare (Map x) (Map y) = I.compare x y
instance (Prim k, Ord k, PrimUnlifted v) => 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 (Prim k, Show k, PrimUnlifted v, Show v) => Show (Map k v) where
showsPrec p (Map s) = I.showsPrec p s
empty :: Map k v
empty = Map I.empty
lookup :: (Prim k, Ord k, PrimUnlifted v) => k -> Map k v -> Maybe v
lookup a (Map s) = I.lookup a s
singleton :: (Prim k, PrimUnlifted v) => k -> v -> Map k v
singleton k v = Map (I.singleton k v)
fromList :: (Prim k, Ord k, PrimUnlifted v) => [(k,v)] -> Map k v
fromList = Map . I.fromList
fromListN :: (Prim k, Ord k, PrimUnlifted v)
=> Int
-> [(k,v)]
-> Map k v
fromListN n = Map . I.fromListN n
fromListAppend :: (Prim k, Ord k, PrimUnlifted v, Semigroup v) => [(k,v)] -> Map k v
fromListAppend = Map . I.fromListAppend
fromListAppendN :: (Prim k, Ord k, PrimUnlifted v, Semigroup v)
=> Int
-> [(k,v)]
-> Map k v
fromListAppendN n = Map . I.fromListAppendN n
fromSet :: (Prim k, PrimUnlifted v)
=> (k -> v)
-> Set k
-> Map k v
{-# INLINE fromSet #-}
fromSet f (Set s) = Map (I.fromSet f s)
fromSetP :: (PrimMonad m, Prim k, PrimUnlifted v)
=> (k -> m v)
-> Set k
-> m (Map k v)
{-# INLINE fromSetP #-}
fromSetP f (Set s) = fmap Map (I.fromSetP f s)
size :: PrimUnlifted v => Map k v -> Int
size (Map m) = I.size m
map :: (Prim k, PrimUnlifted v, PrimUnlifted w)
=> (v -> w)
-> Map k v
-> Map k w
map f (Map m) = Map (I.map f m)
mapMaybe :: (Prim k, PrimUnlifted v, PrimUnlifted w)
=> (v -> Maybe w)
-> Map k v
-> Map k w
{-# INLINE mapMaybe #-}
mapMaybe f (Map m) = Map (I.mapMaybe f m)
mapMaybeP :: (PrimMonad m, Prim k, PrimUnlifted v, PrimUnlifted w)
=> (v -> m (Maybe w))
-> Map k v
-> m (Map k w)
{-# INLINE mapMaybeP #-}
mapMaybeP f (Map m) = fmap Map (I.mapMaybeP f m)
mapMaybeWithKey :: (Prim k, PrimUnlifted v, PrimUnlifted w)
=> (k -> v -> Maybe w)
-> Map k v
-> Map k w
mapMaybeWithKey f (Map m) = Map (I.mapMaybeWithKey f m)
adjustMany :: (Prim k, PrimUnlifted v, PrimMonad m, Ord k)
=> ((k -> (v -> m v) -> m ()) -> m a)
-> Map k v
-> m (Map k v, a)
adjustMany f (Map m) = do
(r,a) <- I.adjustMany f m
pure (Map r, a)
foldlWithKeyM' :: (Monad m, Prim k, PrimUnlifted v)
=> (b -> k -> v -> m b)
-> b
-> Map k v
-> m b
foldlWithKeyM' f b0 (Map m) = I.foldlWithKeyM' f b0 m
foldrWithKeyM' :: (Monad m, Prim k, PrimUnlifted v)
=> (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, Prim k, PrimUnlifted v)
=> (k -> v -> m b)
-> Map k v
-> m b
foldlMapWithKeyM' f (Map m) = I.foldlMapWithKeyM' f m
foldrMapWithKeyM' :: (Monad m, Monoid b, Prim k, PrimUnlifted v)
=> (k -> v -> m b)
-> Map k v
-> m b
foldrMapWithKeyM' f (Map m) = I.foldrMapWithKeyM' f m
traverse :: (Applicative m, Prim k, PrimUnlifted v, PrimUnlifted w)
=> (v -> m w)
-> Map k v
-> m (Map k w)
traverse f (Map m) = fmap Map (I.traverse f m)
foldMapWithKey' :: (Monoid b, Prim k, PrimUnlifted v)
=> (k -> v -> b)
-> Map k v
-> b
foldMapWithKey' f (Map m) = I.foldMapWithKey' f m
foldlWithKey' :: (Prim k, PrimUnlifted v)
=> (b -> k -> v -> b)
-> b
-> Map k v
-> b
foldlWithKey' f b0 (Map m) = I.foldlWithKey' f b0 m
foldrWithKey' :: (Prim k, PrimUnlifted v)
=> (k -> v -> b -> b)
-> b
-> Map k v
-> b
foldrWithKey' f b0 (Map m) = I.foldrWithKey' f b0 m
unsafeFreezeZip :: (Ord k, Prim k, PrimUnlifted v)
=> MutablePrimArray s k
-> MutableUnliftedArray s v
-> ST s (Map k v)
unsafeFreezeZip keys vals = fmap Map (I.unsafeFreezeZip keys vals)