{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Map.Unlifted.Lifted
( Map(..)
, empty
, singleton
, lookup
, size
, map
, mapMaybe
, mapMaybeWithKey
, union
, foldlWithKey'
, foldrWithKey'
, foldMapWithKey'
, foldlWithKeyM'
, foldrWithKeyM'
, foldlMapWithKeyM'
, foldrMapWithKeyM'
, traverse
, traverseWithKey
, traverseWithKey_
, fromList
, fromListAppend
, fromListN
, fromListAppendN
, fromSet
, unsafeFreezeZip
) where
import Prelude hiding (lookup,map,traverse)
import Control.Monad.ST (ST)
import Data.Semigroup (Semigroup)
import Data.Primitive.Unlifted.Array (UnliftedArray,MutableUnliftedArray)
import Data.Primitive.Unlifted.Class (PrimUnlifted)
import Data.Primitive (Array,MutableArray)
import Data.Set.Unlifted.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 UnliftedArray Array k v)
instance (PrimUnlifted k, Ord k, Semigroup v) => Semigroup (Map k v) where
Map x <> Map y = Map (I.append x y)
instance (PrimUnlifted k, Ord k, Semigroup v) => Monoid (Map k v) where
mempty = Map I.empty
mappend = (SG.<>)
mconcat = Map . I.concat . E.coerce
instance (PrimUnlifted k, Eq k, Eq v) => Eq (Map k v) where
Map x == Map y = I.equals x y
instance (PrimUnlifted k, Ord k, Ord v) => Ord (Map k v) where
compare (Map x) (Map y) = I.compare x y
instance (PrimUnlifted 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 (PrimUnlifted k, Show k, Show v) => Show (Map k v) where
showsPrec p (Map s) = I.showsPrec p s
lookup :: (PrimUnlifted 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 :: PrimUnlifted k => k -> v -> Map k v
singleton k v = Map (I.singleton k v)
fromList :: (PrimUnlifted k, Ord k) => [(k,v)] -> Map k v
fromList = Map . I.fromList
fromListN :: (PrimUnlifted k, Ord k)
=> Int
-> [(k,v)]
-> Map k v
fromListN n = Map . I.fromListN n
fromListAppend :: (PrimUnlifted k, Ord k, Semigroup v) => [(k,v)] -> Map k v
fromListAppend = Map . I.fromListAppend
fromListAppendN :: (PrimUnlifted k, Ord k, Semigroup v)
=> Int
-> [(k,v)]
-> Map k v
fromListAppendN n = Map . I.fromListAppendN n
fromSet :: PrimUnlifted 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 :: PrimUnlifted k
=> (v -> w)
-> Map k v
-> Map k w
map f (Map m) = Map (I.map f m)
mapMaybe :: PrimUnlifted k
=> (v -> Maybe w)
-> Map k v
-> Map k w
mapMaybe f (Map m) = Map (I.mapMaybe f m)
mapMaybeWithKey :: PrimUnlifted k
=> (k -> v -> Maybe w)
-> Map k v
-> Map k w
mapMaybeWithKey f (Map m) = Map (I.mapMaybeWithKey f m)
traverse :: (Applicative f, PrimUnlifted k)
=> (v -> f b)
-> Map k v
-> f (Map k b)
traverse f (Map m) = Map <$> I.traverse f m
traverseWithKey :: (Applicative f, PrimUnlifted k)
=> (k -> v -> f b)
-> Map k v
-> f (Map k b)
traverseWithKey f (Map m) = Map <$> I.traverseWithKey f m
traverseWithKey_ :: (Applicative f, PrimUnlifted k)
=> (k -> v -> f b)
-> Map k v
-> f ()
traverseWithKey_ f (Map m) = I.traverseWithKey_ f m
foldlWithKeyM' :: (Monad m, PrimUnlifted 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, PrimUnlifted 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, PrimUnlifted k)
=> (k -> v -> m b)
-> Map k v
-> m b
foldlMapWithKeyM' f (Map m) = I.foldlMapWithKeyM' f m
foldrMapWithKeyM' :: (Monad m, Monoid b, PrimUnlifted k)
=> (k -> v -> m b)
-> Map k v
-> m b
foldrMapWithKeyM' f (Map m) = I.foldrMapWithKeyM' f m
foldMapWithKey' :: (Monoid b, PrimUnlifted k)
=> (k -> v -> b)
-> Map k v
-> b
foldMapWithKey' f (Map m) = I.foldMapWithKey' f m
foldlWithKey' :: PrimUnlifted k
=> (b -> k -> v -> b)
-> b
-> Map k v
-> b
foldlWithKey' f b0 (Map m) = I.foldlWithKey' f b0 m
foldrWithKey' :: PrimUnlifted k
=> (k -> v -> b -> b)
-> b
-> Map k v
-> b
foldrWithKey' f b0 (Map m) = I.foldrWithKey' f b0 m
unsafeFreezeZip :: (Ord k, PrimUnlifted k)
=> MutableUnliftedArray s k
-> MutableArray s v
-> ST s (Map k v)
unsafeFreezeZip keys vals = fmap Map (I.unsafeFreezeZip keys vals)
union :: (Ord k, PrimUnlifted k) => Map k v -> Map k v -> Map k v
union (Map a) (Map b) = Map (I.appendWith const a b)