{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -O2 #-}
module Data.Diet.Map.Strict.Unboxed.Lifted
( Map
, empty
, singleton
, lookup
, mapBijection
, fromSet
, fromList
, fromListAppend
, fromListN
, fromListAppendN
) where
import Prelude hiding (lookup,map)
import Data.Diet.Set.Unboxed (Set(..))
import Data.Functor.Classes (Show2(..))
import Data.Primitive.Array (Array)
import Data.Primitive.PrimArray (PrimArray)
import Data.Primitive.Types (Prim)
import Data.Semigroup (Semigroup)
import qualified GHC.Exts as E
import qualified Data.Semigroup as SG
import qualified Data.Diet.Map.Strict.Internal as I
newtype Map k v = Map (I.Map PrimArray Array k v)
empty :: Map k v
empty = Map I.empty
singleton :: (Prim k,Ord k)
=> k
-> k
-> v
-> Map k v
singleton lo hi v = Map (I.singleton lo hi v)
lookup :: (Prim k, Ord k) => k -> Map k v -> Maybe v
lookup a (Map s) = I.lookup a s
instance (Prim k, Show k, Show v) => Show (Map k v) where
showsPrec p (Map m) = I.showsPrec p m
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, Enum k, Semigroup v, Eq v) => Semigroup (Map k v) where
Map x <> Map y = Map (I.append x y)
instance (Prim k, Ord k, Enum k, Semigroup v, Eq v) => Monoid (Map k v) where
mempty = Map I.empty
mappend = (SG.<>)
mconcat = Map . I.concat . E.coerce
instance (Prim k, Ord k, Enum k, Eq v) => E.IsList (Map k v) where
type Item (Map k v) = (k,k,v)
fromListN n = Map . I.fromListN n
fromList = Map . I.fromList
toList (Map s) = I.toList s
fromList :: (Ord k, Enum k, Prim k, Eq v) => [(k,k,v)] -> Map k v
fromList = Map . I.fromList
fromListN :: (Ord k, Enum k, Prim k, Eq v)
=> Int
-> [(k,k,v)]
-> Map k v
fromListN n = Map . I.fromListN n
fromListAppend :: (Ord k, Enum k, Prim k, Semigroup v, Eq v) => [(k,k,v)] -> Map k v
fromListAppend = Map . I.fromListAppend
fromListAppendN :: (Ord k, Enum k, Prim k, Semigroup v, Eq v)
=> Int
-> [(k,k,v)]
-> Map k v
fromListAppendN n = Map . I.fromListAppendN n
mapBijection :: (Prim k, Ord k)
=> (v -> w)
-> Map k v
-> Map k w
mapBijection f (Map m) = Map (I.map f m)
fromSet :: Prim k
=> (k -> k -> v)
-> Set k
-> Map k v
fromSet f (Set s) = Map (I.fromSet f s)