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

{-# OPTIONS_GHC -O2 #-}
module Data.Set.NonEmpty.Unlifted
  ( Set
  , singleton
  , member
  , size
    -- * Conversion
  , toArray
  , toList
  , fromNonEmpty
  , toSet
  , fromSet
    -- * Folds
  , foldr
  , foldMap
  , foldl'
  , foldr'
  , foldMap'
    -- * Traversals
  , traverse_
  , itraverse_
  ) where

import Prelude hiding (foldr,foldMap,null)

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

import qualified Data.Foldable as F
import qualified Data.Hashable as H
import qualified Data.List.NonEmpty as NE
import qualified Data.Semigroup as SG
import qualified Data.Set.Internal as I
import qualified GHC.Exts as E
import qualified Data.Set.Unlifted as S
import qualified Data.Set.Unlifted.Internal as SI

newtype Set a = Set (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 => b -> a -> a
SG.stimesIdempotent
  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, 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

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.
fromNonEmpty :: (PrimUnlifted a, Ord a) => NonEmpty a -> Set a
fromNonEmpty :: forall a. (PrimUnlifted a, Ord a) => NonEmpty a -> Set a
fromNonEmpty = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NonEmpty a -> [a]
NE.toList

-- | /O(1)/ Convert a set to a non-empty set. This returns @Nothing@ if
-- the set is empty. The resulting non-empty set shares internal
-- represention as the argument.
fromSet :: SI.Set a -> Maybe (Set a)
fromSet :: forall a. Set a -> Maybe (Set a)
fromSet s :: Set a
s@(SI.Set Set UnliftedArray a
x) = if forall a. Set a -> Bool
S.null Set a
s
  then forall a. Maybe a
Nothing
  else forall a. a -> Maybe a
Just (forall a. Set UnliftedArray a -> Set a
Set Set UnliftedArray a
x)

-- | /O(0)/ Convert a non-empty set to a set. The resulting set shares
-- the internal representation with the argument.
toSet :: Set a -> SI.Set a
toSet :: forall a. Set a -> Set a
toSet = coerce :: forall a b. Coercible a b => a -> b
E.coerce

-- | Test for membership in the set.
member :: (PrimUnlifted a, Ord a) => a -> Set a -> Bool
member :: forall a. (PrimUnlifted a, Ord a) => a -> Set a -> Bool
member a
a (Set Set UnliftedArray a
s) = forall (arr :: * -> *) a.
(Contiguous arr, Element arr a, Ord a) =>
a -> Set arr a -> Bool
I.member a
a Set UnliftedArray a
s

-- | Construct a set with a single element.
singleton :: PrimUnlifted a => a -> Set a
singleton :: forall a. PrimUnlifted a => a -> Set a
singleton = forall a. Set UnliftedArray a -> Set a
Set forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
a -> Set arr a
I.singleton

-- | The number of elements in the set.
size :: PrimUnlifted a => Set a -> Int
size :: forall a. PrimUnlifted a => Set a -> Int
size (Set Set UnliftedArray a
s) = forall (arr :: * -> *) a.
(Contiguous arr, Element arr a) =>
Set arr a -> Int
I.size Set UnliftedArray a
s

-- | /O(1)/ Convert a set to an array. The elements are given in ascending
-- order. This function is zero-cost.
toArray :: Set a -> UnliftedArray a
toArray :: forall a. Set a -> UnliftedArray a
toArray (Set Set UnliftedArray a
s) = forall (arr :: * -> *) a. Set arr a -> arr a
I.toArray Set UnliftedArray a
s

-- | Right fold over the elements in the set. This is lazy in the accumulator.
foldr :: PrimUnlifted a
  => (a -> b -> b)
  -> b
  -> Set a
  -> b
foldr :: forall a b. PrimUnlifted a => (a -> b -> b) -> b -> Set a -> b
foldr a -> b -> b
f b
b0 (Set Set UnliftedArray a
s) = forall (arr :: * -> *) a b.
(Contiguous arr, Element arr a) =>
(a -> b -> b) -> b -> Set arr a -> b
I.foldr a -> b -> b
f b
b0 Set UnliftedArray a
s

-- | Monoidal fold over the elements in the set. This is lazy in the accumulator.
foldMap :: (PrimUnlifted a, Monoid m)
  => (a -> m)
  -> Set a
  -> m
foldMap :: forall a m. (PrimUnlifted a, Monoid m) => (a -> m) -> Set a -> m
foldMap a -> m
f (Set Set UnliftedArray a
s) = forall (arr :: * -> *) a m.
(Contiguous arr, Element arr a, Monoid m) =>
(a -> m) -> Set arr a -> m
I.foldMap a -> m
f Set UnliftedArray a
s

-- | Strict left fold over the elements in the set.
foldl' :: PrimUnlifted a
  => (b -> a -> b)
  -> b
  -> Set a
  -> b
foldl' :: forall a b. PrimUnlifted a => (b -> a -> b) -> b -> Set a -> b
foldl' b -> a -> b
f b
b0 (Set Set UnliftedArray a
s) = forall (arr :: * -> *) a b.
(Contiguous arr, Element arr a) =>
(b -> a -> b) -> b -> Set arr a -> b
I.foldl' b -> a -> b
f b
b0 Set UnliftedArray a
s

-- | Strict right fold over the elements in the set.
foldr' :: PrimUnlifted a
  => (a -> b -> b)
  -> b
  -> Set a
  -> b
foldr' :: forall a b. PrimUnlifted a => (a -> b -> b) -> b -> Set a -> b
foldr' a -> b -> b
f b
b0 (Set Set UnliftedArray a
s) = forall (arr :: * -> *) a b.
(Contiguous arr, Element arr a) =>
(a -> b -> b) -> b -> Set arr a -> b
I.foldr' a -> b -> b
f b
b0 Set UnliftedArray a
s

-- | Strict monoidal fold over the elements in the set.
foldMap' :: (PrimUnlifted a, Monoid m)
  => (a -> m)
  -> Set a
  -> m
foldMap' :: forall a m. (PrimUnlifted a, Monoid m) => (a -> m) -> Set a -> m
foldMap' a -> m
f (Set Set UnliftedArray a
arr) = forall (arr :: * -> *) a m.
(Contiguous arr, Element arr a, Monoid m) =>
(a -> m) -> Set arr a -> m
I.foldMap' a -> m
f Set UnliftedArray a
arr

-- | Traverse a set, discarding the result.
traverse_ :: (Applicative m, PrimUnlifted a)
  => (a -> m b)
  -> Set a
  -> m ()
traverse_ :: forall (m :: * -> *) a b.
(Applicative m, PrimUnlifted a) =>
(a -> m b) -> Set a -> m ()
traverse_ a -> m b
f (Set Set UnliftedArray a
arr) = forall (arr :: * -> *) a (m :: * -> *) b.
(Contiguous arr, Element arr a, Applicative m) =>
(a -> m b) -> Set arr a -> m ()
I.traverse_ a -> m b
f Set UnliftedArray a
arr

-- | Traverse a set with the indices, discarding the result.
itraverse_ :: (Applicative m, PrimUnlifted a)
  => (Int -> a -> m b)
  -> Set a
  -> m ()
itraverse_ :: forall (m :: * -> *) a b.
(Applicative m, PrimUnlifted a) =>
(Int -> a -> m b) -> Set a -> m ()
itraverse_ Int -> a -> m b
f (Set Set UnliftedArray a
arr) = forall (arr :: * -> *) a (m :: * -> *) b.
(Contiguous arr, Element arr a, Applicative m) =>
(Int -> a -> m b) -> Set arr a -> m ()
I.itraverse_ Int -> a -> m b
f Set UnliftedArray a
arr