{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -O2 #-}
module Data.Diet.Set.Unboxed
( Set(..)
, singleton
, member
, difference
, intersection
, negate
, aboveInclusive
, belowInclusive
, betweenInclusive
, foldr
, toList
, fromList
, fromListN
) where
import Prelude hiding (lookup,map,foldr,negate)
import Data.Semigroup (Semigroup)
import Data.Functor.Classes (Show2(..))
import Data.Primitive.Types (Prim)
import Data.Primitive.PrimArray (PrimArray)
import qualified GHC.Exts as E
import qualified Data.Semigroup as SG
import qualified Data.Diet.Set.Internal as I
newtype Set a = Set (I.Set PrimArray a)
singleton :: (Ord a, Prim a)
=> a
-> a
-> Set a
singleton lo hi = Set (I.singleton lo hi)
member :: (Ord a, Prim a) => a -> Set a -> Bool
member a (Set s) = I.member a s
instance (Show a, Prim a) => Show (Set a) where
showsPrec p (Set s) = I.showsPrec p s
instance (Eq a, Prim a) => Eq (Set a) where
Set x == Set y = I.equals x y
instance (Ord a, Prim a) => Ord (Set a) where
compare (Set xs) (Set ys) = compare (I.toList xs) (I.toList ys)
instance (Ord a, Enum a, Prim a) => Semigroup (Set a) where
Set x <> Set y = Set (I.append x y)
instance (Ord a, Enum a, Prim a) => Monoid (Set a) where
mempty = Set I.empty
mappend = (SG.<>)
mconcat = Set . I.concat . E.coerce
instance (Ord a, Enum a, Prim a) => E.IsList (Set a) where
type Item (Set a) = (a,a)
fromListN n = Set . I.fromListN n
fromList = Set . I.fromList
toList (Set s) = I.toList s
toList :: Prim a => Set a -> [(a,a)]
toList (Set x) = I.toList x
fromList :: (Ord a, Enum a, Prim a) => [(a,a)] -> Set a
fromList = Set . I.fromList
fromListN :: (Ord a, Enum a, Prim a)
=> Int
-> [(a,a)]
-> Set a
fromListN n = Set . I.fromListN n
difference :: (Ord a, Enum a, Prim a)
=> Set a
-> Set a
-> Set a
difference (Set x) (Set y) = Set (I.difference x y)
intersection :: (Ord a, Enum a, Prim a)
=> Set a
-> Set a
-> Set a
intersection (Set x) (Set y) = Set (I.intersection x y)
negate :: (Ord a, Enum a, Prim a, Bounded a)
=> Set a
-> Set a
negate (Set x) = Set (I.negate x)
foldr :: Prim a => (a -> a -> b -> b) -> b -> Set a -> b
foldr f z (Set arr) = I.foldr f z arr
aboveInclusive :: (Ord a, Prim a)
=> a
-> Set a
-> Set a
aboveInclusive x (Set s) = Set (I.aboveInclusive x s)
belowInclusive :: (Ord a, Prim a)
=> a
-> Set a
-> Set a
belowInclusive x (Set s) = Set (I.belowInclusive x s)
betweenInclusive :: (Ord a, Prim a)
=> a
-> a
-> Set a
-> Set a
betweenInclusive x y (Set s) = Set (I.betweenInclusive x y s)