{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}

module Data.Set.Unlifted.Internal
  ( Set(..)
  , toList
  , fromList
  ) where

import Prelude hiding (foldr)

import Data.Hashable (Hashable)
import Data.Primitive.Unlifted.Array (UnliftedArray)
import Data.Primitive.Unlifted.Class (PrimUnlifted)
import Data.Primitive (Array)
import Data.Semigroup (Semigroup)

import qualified Data.Foldable as F
import qualified Data.Hashable as H
import qualified Data.Semigroup as SG
import qualified Data.Set.Internal as I
import qualified GHC.Exts as E

newtype Set a = Set { forall a. Set a -> Set UnliftedArray a
getSet :: I.Set UnliftedArray a }

instance (Ord a, PrimUnlifted a) => Semigroup (Set a) where
  Set Set UnliftedArray a
x <> :: Set a -> Set a -> Set a
<> Set Set UnliftedArray a
y = forall a. Set UnliftedArray a -> Set a
Set (forall (arr :: * -> *) a.
(ContiguousU arr, Element arr a, Ord a) =>
Set arr a -> Set arr a -> Set arr a
I.append Set UnliftedArray a
x Set UnliftedArray a
y)
  stimes :: forall b. Integral b => b -> Set a -> Set a
stimes = forall b a. (Integral b, Monoid a) => b -> a -> a
SG.stimesIdempotentMonoid
  sconcat :: NonEmpty (Set a) -> Set a
sconcat NonEmpty (Set a)
xs = forall a. Set UnliftedArray a -> Set a
Set (forall (arr :: * -> *) a.
(ContiguousU arr, Element arr a, Ord a) =>
[Set arr a] -> Set arr a
I.concat (coerce :: forall a b. Coercible a b => a -> b
E.coerce (forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList NonEmpty (Set a)
xs)))

instance (Hashable a, PrimUnlifted a) => Hashable (Set a) where
  hashWithSalt :: Int -> Set a -> Int
hashWithSalt Int
s (Set Set UnliftedArray a
arr) = forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
(Int -> a -> Int) -> Int -> Set arr a -> Int
I.liftHashWithSalt forall a. Hashable a => Int -> a -> Int
H.hashWithSalt Int
s Set UnliftedArray a
arr

instance (PrimUnlifted a, Ord a) => Monoid (Set a) where
  mempty :: Set a
mempty = forall a. Set UnliftedArray a -> Set a
Set forall (arr :: * -> *) a. Contiguous arr => Set arr a
I.empty
  mappend :: Set a -> Set a -> Set a
mappend = forall a. Semigroup a => a -> a -> a
(SG.<>)
  mconcat :: [Set a] -> Set a
mconcat [Set a]
xs = forall a. Set UnliftedArray a -> Set a
Set (forall (arr :: * -> *) a.
(ContiguousU arr, Element arr a, Ord a) =>
[Set arr a] -> Set arr a
I.concat (coerce :: forall a b. Coercible a b => a -> b
E.coerce [Set a]
xs))

instance (PrimUnlifted a, Eq a) => Eq (Set a) where
  Set Set UnliftedArray a
x == :: Set a -> Set a -> Bool
== Set Set UnliftedArray a
y = forall (arr :: * -> *) a.
(Contiguous arr, Element arr a, Eq a) =>
Set arr a -> Set arr a -> Bool
I.equals Set UnliftedArray a
x Set UnliftedArray a
y

instance (PrimUnlifted a, Ord a) => Ord (Set a) where
  compare :: Set a -> Set a -> Ordering
compare (Set Set UnliftedArray a
x) (Set Set UnliftedArray a
y) = forall (arr :: * -> *) a.
(Contiguous arr, Element arr a, Ord a) =>
Set arr a -> Set arr a -> Ordering
I.compare Set UnliftedArray a
x Set UnliftedArray a
y

-- | The functions that convert a list to a 'Set' are asymptotically
-- better that using @'foldMap' 'singleton'@, with a cost of /O(n*log n)/
-- rather than /O(n^2)/. If the input list is sorted, even if duplicate
-- elements are present, the algorithm further improves to /O(n)/. The
-- fastest option available is calling 'fromListN' on a presorted list
-- and passing the correct size size of the resulting 'Set'. However, even
-- if an incorrect size is given to this function,
-- it will still correctly convert the list into a 'Set'.
instance (PrimUnlifted a, Ord a) => E.IsList (Set a) where
  type Item (Set a) = a
  fromListN :: Int -> [Item (Set a)] -> Set a
fromListN Int
n = forall a. Set UnliftedArray a -> Set a
Set forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (arr :: * -> *) a.
(ContiguousU arr, Element arr a, Ord a) =>
Int -> [a] -> Set arr a
I.fromListN Int
n
  fromList :: [Item (Set a)] -> Set a
fromList = forall a. Set UnliftedArray a -> Set a
Set forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (arr :: * -> *) a.
(ContiguousU arr, Element arr a, Ord a) =>
[a] -> Set arr a
I.fromList
  toList :: Set a -> [Item (Set a)]
toList = forall a. PrimUnlifted a => Set a -> [a]
toList

instance (PrimUnlifted a, Show a) => Show (Set a) where
  showsPrec :: Int -> Set a -> ShowS
showsPrec Int
p (Set Set UnliftedArray a
s) = forall (arr :: * -> *) a.
(Contiguous arr, Element arr a, Show a) =>
Int -> Set arr a -> ShowS
I.showsPrec Int
p Set UnliftedArray a
s

-- | /O(n)/ Convert a set to a list. The elements are given in ascending order.
toList :: PrimUnlifted a => Set a -> [a]
toList :: forall a. PrimUnlifted a => Set a -> [a]
toList (Set Set UnliftedArray a
s) = forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
Set arr a -> [a]
I.toList Set UnliftedArray a
s

-- | /O(n*log n)/ Convert a list to a set.
fromList :: (PrimUnlifted a, Ord a) => [a] -> Set a
fromList :: forall a. (PrimUnlifted a, Ord a) => [a] -> Set a
fromList = forall a. Set UnliftedArray a -> Set a
Set forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (arr :: * -> *) a.
(ContiguousU arr, Element arr a, Ord a) =>
[a] -> Set arr a
I.fromList