{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -O2 #-}
module Data.Map.Unboxed.Lifted
( Map
, empty
, singleton
, lookup
, size
, map
, mapMaybe
, mapMaybeWithKey
, mapWithKey
, keys
, intersectionWith
, intersectionsWith
, restrict
, appendWithKey
, foldrWithKey
, foldlWithKey'
, foldrWithKey'
, foldMapWithKey
, foldMapWithKey'
, foldlWithKeyM'
, foldrWithKeyM'
, foldlMapWithKeyM'
, foldrMapWithKeyM'
, toList
, fromList
, fromListAppend
, fromListN
, fromListAppendN
, fromSet
, elems
, unsafeFreezeZip
) where
import Prelude hiding (lookup,map)
import Control.DeepSeq (NFData)
import Control.Monad.ST (ST)
import Data.List.NonEmpty (NonEmpty)
import Data.Primitive (PrimArray,Array,MutablePrimArray,MutableArray)
import Data.Primitive.Types (Prim)
import Data.Semigroup (Semigroup)
import Data.Set.Unboxed.Internal (Set(..))
import qualified Control.DeepSeq
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 Array k v)
instance Prim k => Functor (Map k) where
fmap = map
instance (Prim k, NFData k, NFData v) => NFData (Map k v) where
rnf (Map m) = I.rnf m
instance (Prim k, Ord k, Semigroup v) => Semigroup (Map k v) where
Map x <> Map y = Map (I.append x y)
instance (Prim k, Ord k, Semigroup v) => Monoid (Map k v) where
mempty = Map I.empty
mappend = (SG.<>)
mconcat = Map . I.concat . E.coerce
instance (Prim k, Eq k, Eq v) => Eq (Map k v) where
Map x == Map y = I.equals x y
instance (Prim k, Ord k, Ord v) => Ord (Map k v) where
compare (Map x) (Map y) = I.compare x y
instance (Prim k, 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 (Prim k, Show k, Show v) => Show (Map k v) where
showsPrec p (Map s) = I.showsPrec p s
lookup :: (Prim k, Ord k) => k -> Map k v -> Maybe v
lookup a (Map s) = I.lookup a s
empty :: Map k v
empty = Map I.empty
singleton :: Prim k => k -> v -> Map k v
singleton k v = Map (I.singleton k v)
toList :: (Prim k, Ord k) => Map k v -> [(k,v)]
toList (Map m) = I.toList m
fromList :: (Prim k, Ord k) => [(k,v)] -> Map k v
fromList = Map . I.fromList
fromListN :: (Prim k, Ord k)
=> Int
-> [(k,v)]
-> Map k v
fromListN n = Map . I.fromListN n
fromListAppend :: (Prim k, Ord k, Semigroup v) => [(k,v)] -> Map k v
fromListAppend = Map . I.fromListAppend
fromListAppendN :: (Prim k, Ord k, Semigroup v)
=> Int
-> [(k,v)]
-> Map k v
fromListAppendN n = Map . I.fromListAppendN n
fromSet :: Prim k
=> (k -> v)
-> Set k
-> Map k v
fromSet f (Set s) = Map (I.fromSet f s)
size :: Map k v -> Int
size (Map m) = I.size m
map :: Prim k
=> (v -> w)
-> Map k v
-> Map k w
map f (Map m) = Map (I.map f m)
mapMaybe :: Prim k
=> (v -> Maybe w)
-> Map k v
-> Map k w
mapMaybe f (Map m) = Map (I.mapMaybe f m)
mapMaybeWithKey :: Prim k
=> (k -> v -> Maybe w)
-> Map k v
-> Map k w
mapMaybeWithKey f (Map m) = Map (I.mapMaybeWithKey f m)
mapWithKey :: Prim k
=> (k -> v -> w)
-> Map k v
-> Map k w
mapWithKey f (Map m) = Map (I.mapWithKey f m)
appendWithKey :: (Prim k, 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, Prim k)
=> (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)
=> (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)
=> (k -> v -> m b)
-> Map k v
-> m b
foldlMapWithKeyM' f (Map m) = I.foldlMapWithKeyM' f m
foldrMapWithKeyM' :: (Monad m, Monoid b, Prim k)
=> (k -> v -> m b)
-> Map k v
-> m b
foldrMapWithKeyM' f (Map m) = I.foldrMapWithKeyM' f m
foldlWithKey' :: Prim k
=> (b -> k -> v -> b)
-> b
-> Map k v
-> b
foldlWithKey' f b0 (Map m) = I.foldlWithKey' f b0 m
foldrWithKey :: Prim k
=> (k -> v -> b -> b)
-> b
-> Map k v
-> b
foldrWithKey f b0 (Map m) = I.foldrWithKey f b0 m
foldrWithKey' :: Prim k
=> (k -> v -> b -> b)
-> b
-> Map k v
-> b
foldrWithKey' f b0 (Map m) = I.foldrWithKey' f b0 m
foldMapWithKey :: (Monoid b, Prim k)
=> (k -> v -> b)
-> Map k v
-> b
foldMapWithKey f (Map m) = I.foldMapWithKey f m
foldMapWithKey' :: (Monoid b, Prim k)
=> (k -> v -> b)
-> Map k v
-> b
foldMapWithKey' f (Map m) = I.foldMapWithKey' f m
unsafeFreezeZip :: (Ord k, Prim k)
=> MutablePrimArray s k
-> MutableArray s v
-> ST s (Map k v)
unsafeFreezeZip theKeys vals = fmap Map (I.unsafeFreezeZip theKeys vals)
keys :: Map k v -> Set k
keys (Map m) = Set (I.keys m)
intersectionWith :: (Prim k, Ord k)
=> (a -> b -> c)
-> Map k a
-> Map k b
-> Map k c
intersectionWith f (Map a) (Map b) = Map (I.intersectionWith f a b)
intersectionsWith :: (Prim k, Ord k)
=> (v -> v -> v)
-> NonEmpty (Map k v)
-> Map k v
intersectionsWith f xs = Map (I.intersectionsWith f (E.coerce xs))
restrict :: (Prim k, Ord k)
=> Map k v
-> Set k
-> Map k v
restrict (Map m) (Set s) = Map (I.restrict m s)
elems :: Map k v -> Array v
elems (Map m) = I.elems m