{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RebindableSyntax #-}
module Data.Array.Accelerate.Data.HashSet (
HashSet, Hashable,
fromVector,
size,
member,
insert,
delete,
elems,
toMap,
fromMap,
) where
import Data.Array.Accelerate hiding ( size )
import Data.Array.Accelerate.Data.HashMap ( HashMap, Hashable )
import qualified Data.Array.Accelerate as A
import qualified Data.Array.Accelerate.Data.HashMap as M
data HashSet a = HashSet (HashMap a ())
deriving (Show, Generic, Arrays)
pattern HashSet_
:: Elt a
=> Acc (HashMap a ())
-> Acc (HashSet a)
pattern HashSet_ hm = Pattern hm
{-# COMPLETE HashSet_ #-}
size :: Elt a => Acc (HashSet a) -> Exp Int
size = M.size . toMap
member :: (Eq a, Hashable a) => Exp a -> Acc (HashSet a) -> Exp Bool
member k = M.member k . toMap
insert :: (Eq a, Hashable a)
=> Acc (Vector a)
-> Acc (HashSet a)
-> Acc (HashSet a)
insert xs = fromMap . M.insert (A.map (\x -> T2 x (constant ())) xs) . toMap
delete :: (Eq a, Hashable a)
=> Acc (Vector a)
-> Acc (HashSet a)
-> Acc (HashSet a)
delete xs = fromMap . M.delete xs . toMap
elems :: Elt a => Acc (HashSet a) -> Acc (Vector a)
elems = M.keys . toMap
toMap :: Elt a => Acc (HashSet a) -> Acc (HashMap a ())
toMap (HashSet_ m) = m
fromMap :: Elt a => Acc (HashMap a ()) -> Acc (HashSet a)
fromMap = HashSet_
fromVector :: Hashable a => Acc (Vector a) -> Acc (HashSet a)
fromVector = fromMap . M.fromVector . A.map (\x -> T2 x (constant ()))