{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveLift #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_HADDOCK not-home #-}
module Data.HashSet.Internal
(
HashSet(..)
, empty
, singleton
, null
, size
, member
, insert
, delete
, isSubsetOf
, map
, union
, unions
, difference
, intersection
, foldr
, foldr'
, foldl
, foldl'
, filter
, toList
, fromList
, toMap
, fromMap
, keysSet
) where
import Control.DeepSeq (NFData (..), NFData1 (..), liftRnf2)
import Data.Data (Constr, Data (..), DataType)
import Data.Functor.Classes
import Data.Hashable (Hashable (hashWithSalt))
import Data.Hashable.Lifted (Hashable1 (..), Hashable2 (..))
import Data.HashMap.Internal (HashMap, equalKeys, equalKeys1, foldMapWithKey,
foldlWithKey, foldrWithKey)
import Data.Semigroup (Semigroup (..), stimesIdempotentMonoid)
import Prelude hiding (filter, foldl, foldr, map, null)
import Text.Read
import qualified Data.Data as Data
import qualified Data.Foldable as Foldable
import qualified Data.HashMap.Internal as H
import qualified Data.List as List
import qualified GHC.Exts as Exts
import qualified Language.Haskell.TH.Syntax as TH
newtype HashSet a = HashSet {
forall a. HashSet a -> HashMap a ()
asMap :: HashMap a ()
}
type role HashSet nominal
deriving instance TH.Lift a => TH.Lift (HashSet a)
instance (NFData a) => NFData (HashSet a) where
rnf :: HashSet a -> ()
rnf = forall a. NFData a => a -> ()
rnf forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
{-# INLINE rnf #-}
instance NFData1 HashSet where
liftRnf :: forall a. (a -> ()) -> HashSet a -> ()
liftRnf a -> ()
rnf1 = forall (p :: * -> * -> *) a b.
NFData2 p =>
(a -> ()) -> (b -> ()) -> p a b -> ()
liftRnf2 a -> ()
rnf1 forall a. NFData a => a -> ()
rnf forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
instance (Eq a) => Eq (HashSet a) where
HashSet HashMap a ()
a == :: HashSet a -> HashSet a -> Bool
== HashSet HashMap a ()
b = forall k v v'. Eq k => HashMap k v -> HashMap k v' -> Bool
equalKeys HashMap a ()
a HashMap a ()
b
{-# INLINE (==) #-}
instance Eq1 HashSet where
liftEq :: forall a b. (a -> b -> Bool) -> HashSet a -> HashSet b -> Bool
liftEq a -> b -> Bool
eq (HashSet HashMap a ()
a) (HashSet HashMap b ()
b) = forall k k' v v'.
(k -> k' -> Bool) -> HashMap k v -> HashMap k' v' -> Bool
equalKeys1 a -> b -> Bool
eq HashMap a ()
a HashMap b ()
b
instance (Ord a) => Ord (HashSet a) where
compare :: HashSet a -> HashSet a -> Ordering
compare (HashSet HashMap a ()
a) (HashSet HashMap a ()
b) = forall a. Ord a => a -> a -> Ordering
compare HashMap a ()
a HashMap a ()
b
{-# INLINE compare #-}
instance Ord1 HashSet where
liftCompare :: forall a b.
(a -> b -> Ordering) -> HashSet a -> HashSet b -> Ordering
liftCompare a -> b -> Ordering
c (HashSet HashMap a ()
a) (HashSet HashMap b ()
b) = forall (f :: * -> * -> *) a b c d.
Ord2 f =>
(a -> b -> Ordering)
-> (c -> d -> Ordering) -> f a c -> f b d -> Ordering
liftCompare2 a -> b -> Ordering
c forall a. Ord a => a -> a -> Ordering
compare HashMap a ()
a HashMap b ()
b
instance Foldable.Foldable HashSet where
foldMap :: forall m a. Monoid m => (a -> m) -> HashSet a -> m
foldMap a -> m
f = forall m k v. Monoid m => (k -> v -> m) -> HashMap k v -> m
foldMapWithKey (\a
a ()
_ -> a -> m
f a
a) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
foldr :: forall a b. (a -> b -> b) -> b -> HashSet a -> b
foldr = forall a b. (a -> b -> b) -> b -> HashSet a -> b
foldr
{-# INLINE foldr #-}
foldl :: forall b a. (b -> a -> b) -> b -> HashSet a -> b
foldl = forall b a. (b -> a -> b) -> b -> HashSet a -> b
foldl
{-# INLINE foldl #-}
foldl' :: forall b a. (b -> a -> b) -> b -> HashSet a -> b
foldl' = forall b a. (b -> a -> b) -> b -> HashSet a -> b
foldl'
{-# INLINE foldl' #-}
foldr' :: forall a b. (a -> b -> b) -> b -> HashSet a -> b
foldr' = forall a b. (a -> b -> b) -> b -> HashSet a -> b
foldr'
{-# INLINE foldr' #-}
toList :: forall a. HashSet a -> [a]
toList = forall a. HashSet a -> [a]
toList
{-# INLINE toList #-}
null :: forall a. HashSet a -> Bool
null = forall a. HashSet a -> Bool
null
{-# INLINE null #-}
length :: forall a. HashSet a -> Int
length = forall a. HashSet a -> Int
size
{-# INLINE length #-}
instance (Hashable a, Eq a) => Semigroup (HashSet a) where
<> :: HashSet a -> HashSet a -> HashSet a
(<>) = forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
union
{-# INLINE (<>) #-}
stimes :: forall b. Integral b => b -> HashSet a -> HashSet a
stimes = forall b a. (Integral b, Monoid a) => b -> a -> a
stimesIdempotentMonoid
{-# INLINE stimes #-}
instance (Hashable a, Eq a) => Monoid (HashSet a) where
mempty :: HashSet a
mempty = forall a. HashSet a
empty
{-# INLINE mempty #-}
mappend :: HashSet a -> HashSet a -> HashSet a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE mappend #-}
instance (Eq a, Hashable a, Read a) => Read (HashSet a) where
readPrec :: ReadPrec (HashSet a)
readPrec = forall a. ReadPrec a -> ReadPrec a
parens forall a b. (a -> b) -> a -> b
$ forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 forall a b. (a -> b) -> a -> b
$ do
Ident String
"fromList" <- ReadPrec Lexeme
lexP
forall a. (Eq a, Hashable a) => [a] -> HashSet a
fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Read a => ReadPrec a
readPrec
readListPrec :: ReadPrec [HashSet a]
readListPrec = forall a. Read a => ReadPrec [a]
readListPrecDefault
instance Show1 HashSet where
liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> HashSet a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d HashSet a
m =
forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith (forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl) String
"fromList" Int
d (forall a. HashSet a -> [a]
toList HashSet a
m)
instance (Show a) => Show (HashSet a) where
showsPrec :: Int -> HashSet a -> ShowS
showsPrec Int
d HashSet a
m = Bool -> ShowS -> ShowS
showParen (Int
d forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"fromList " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> ShowS
shows (forall a. HashSet a -> [a]
toList HashSet a
m)
instance (Data a, Eq a, Hashable a) => Data (HashSet a) where
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> HashSet a -> c (HashSet a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z HashSet a
m = forall g. g -> c g
z forall a. (Eq a, Hashable a) => [a] -> HashSet a
fromList forall d b. Data d => c (d -> b) -> d -> c b
`f` forall a. HashSet a -> [a]
toList HashSet a
m
toConstr :: HashSet a -> Constr
toConstr HashSet a
_ = Constr
fromListConstr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HashSet a)
gunfold forall b r. Data b => c (b -> r) -> c r
k forall r. r -> c r
z Constr
c = case Constr -> Int
Data.constrIndex Constr
c of
Int
1 -> forall b r. Data b => c (b -> r) -> c r
k (forall r. r -> c r
z forall a. (Eq a, Hashable a) => [a] -> HashSet a
fromList)
Int
_ -> forall a. HasCallStack => String -> a
error String
"gunfold"
dataTypeOf :: HashSet a -> DataType
dataTypeOf HashSet a
_ = DataType
hashSetDataType
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (HashSet a))
dataCast1 forall d. Data d => c (t d)
f = forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
(a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
Data.gcast1 forall d. Data d => c (t d)
f
instance Hashable1 HashSet where
liftHashWithSalt :: forall a. (Int -> a -> Int) -> Int -> HashSet a -> Int
liftHashWithSalt Int -> a -> Int
h Int
s = forall (t :: * -> * -> *) a b.
Hashable2 t =>
(Int -> a -> Int) -> (Int -> b -> Int) -> Int -> t a b -> Int
liftHashWithSalt2 Int -> a -> Int
h forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
s forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
instance (Hashable a) => Hashable (HashSet a) where
hashWithSalt :: Int -> HashSet a -> Int
hashWithSalt Int
salt = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
fromListConstr :: Constr
fromListConstr :: Constr
fromListConstr = DataType -> String -> [String] -> Fixity -> Constr
Data.mkConstr DataType
hashSetDataType String
"fromList" [] Fixity
Data.Prefix
hashSetDataType :: DataType
hashSetDataType :: DataType
hashSetDataType = String -> [Constr] -> DataType
Data.mkDataType String
"Data.HashSet.Internal.HashSet" [Constr
fromListConstr]
empty :: HashSet a
empty :: forall a. HashSet a
empty = forall a. HashMap a () -> HashSet a
HashSet forall k v. HashMap k v
H.empty
singleton :: Hashable a => a -> HashSet a
singleton :: forall a. Hashable a => a -> HashSet a
singleton a
a = forall a. HashMap a () -> HashSet a
HashSet (forall k v. Hashable k => k -> v -> HashMap k v
H.singleton a
a ())
{-# INLINABLE singleton #-}
toMap :: HashSet a -> HashMap a ()
toMap :: forall a. HashSet a -> HashMap a ()
toMap = forall a. HashSet a -> HashMap a ()
asMap
fromMap :: HashMap a () -> HashSet a
fromMap :: forall a. HashMap a () -> HashSet a
fromMap = forall a. HashMap a () -> HashSet a
HashSet
keysSet :: HashMap k a -> HashSet k
keysSet :: forall k a. HashMap k a -> HashSet k
keysSet HashMap k a
m = forall a. HashMap a () -> HashSet a
fromMap (() forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ HashMap k a
m)
isSubsetOf :: (Eq a, Hashable a) => HashSet a -> HashSet a -> Bool
isSubsetOf :: forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> Bool
isSubsetOf HashSet a
s1 HashSet a
s2 = forall k v1 v2.
(Eq k, Hashable k) =>
(v1 -> v2 -> Bool) -> HashMap k v1 -> HashMap k v2 -> Bool
H.isSubmapOfBy (\()
_ ()
_ -> Bool
True) (forall a. HashSet a -> HashMap a ()
asMap HashSet a
s1) (forall a. HashSet a -> HashMap a ()
asMap HashSet a
s2)
union :: (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
union :: forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
union HashSet a
s1 HashSet a
s2 = forall a. HashMap a () -> HashSet a
HashSet forall a b. (a -> b) -> a -> b
$ forall k v.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k v -> HashMap k v
H.union (forall a. HashSet a -> HashMap a ()
asMap HashSet a
s1) (forall a. HashSet a -> HashMap a ()
asMap HashSet a
s2)
{-# INLINE union #-}
unions :: (Eq a, Hashable a) => [HashSet a] -> HashSet a
unions :: forall a. (Eq a, Hashable a) => [HashSet a] -> HashSet a
unions = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
union forall a. HashSet a
empty
{-# INLINE unions #-}
null :: HashSet a -> Bool
null :: forall a. HashSet a -> Bool
null = forall k v. HashMap k v -> Bool
H.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
{-# INLINE null #-}
size :: HashSet a -> Int
size :: forall a. HashSet a -> Int
size = forall k v. HashMap k v -> Int
H.size forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
{-# INLINE size #-}
member :: (Eq a, Hashable a) => a -> HashSet a -> Bool
member :: forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
member a
a HashSet a
s = case forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
H.lookup a
a (forall a. HashSet a -> HashMap a ()
asMap HashSet a
s) of
Just ()
_ -> Bool
True
Maybe ()
_ -> Bool
False
{-# INLINABLE member #-}
insert :: (Eq a, Hashable a) => a -> HashSet a -> HashSet a
insert :: forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
insert a
a = forall a. HashMap a () -> HashSet a
HashSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
H.insert a
a () forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
{-# INLINABLE insert #-}
delete :: (Eq a, Hashable a) => a -> HashSet a -> HashSet a
delete :: forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
delete a
a = forall a. HashMap a () -> HashSet a
HashSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v
H.delete a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
{-# INLINABLE delete #-}
map :: (Hashable b, Eq b) => (a -> b) -> HashSet a -> HashSet b
map :: forall b a.
(Hashable b, Eq b) =>
(a -> b) -> HashSet a -> HashSet b
map a -> b
f = forall a. (Eq a, Hashable a) => [a] -> HashSet a
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
List.map a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> [a]
toList
{-# INLINE map #-}
difference :: (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
difference :: forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
difference (HashSet HashMap a ()
a) (HashSet HashMap a ()
b) = forall a. HashMap a () -> HashSet a
HashSet (forall k v w.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k w -> HashMap k v
H.difference HashMap a ()
a HashMap a ()
b)
{-# INLINABLE difference #-}
intersection :: (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
intersection :: forall a. (Eq a, Hashable a) => HashSet a -> HashSet a -> HashSet a
intersection (HashSet HashMap a ()
a) (HashSet HashMap a ()
b) = forall a. HashMap a () -> HashSet a
HashSet (forall k v w.
(Eq k, Hashable k) =>
HashMap k v -> HashMap k w -> HashMap k v
H.intersection HashMap a ()
a HashMap a ()
b)
{-# INLINABLE intersection #-}
foldl' :: (a -> b -> a) -> a -> HashSet b -> a
foldl' :: forall b a. (b -> a -> b) -> b -> HashSet a -> b
foldl' a -> b -> a
f a
z0 = forall a k v. (a -> k -> v -> a) -> a -> HashMap k v -> a
H.foldlWithKey' a -> b -> () -> a
g a
z0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
where g :: a -> b -> () -> a
g a
z b
k ()
_ = a -> b -> a
f a
z b
k
{-# INLINE foldl' #-}
foldr' :: (b -> a -> a) -> a -> HashSet b -> a
foldr' :: forall a b. (a -> b -> b) -> b -> HashSet a -> b
foldr' b -> a -> a
f a
z0 = forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
H.foldrWithKey' b -> () -> a -> a
g a
z0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
where g :: b -> () -> a -> a
g b
k ()
_ a
z = b -> a -> a
f b
k a
z
{-# INLINE foldr' #-}
foldr :: (b -> a -> a) -> a -> HashSet b -> a
foldr :: forall a b. (a -> b -> b) -> b -> HashSet a -> b
foldr b -> a -> a
f a
z0 = forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
foldrWithKey b -> () -> a -> a
g a
z0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
where g :: b -> () -> a -> a
g b
k ()
_ a
z = b -> a -> a
f b
k a
z
{-# INLINE foldr #-}
foldl :: (a -> b -> a) -> a -> HashSet b -> a
foldl :: forall b a. (b -> a -> b) -> b -> HashSet a -> b
foldl a -> b -> a
f a
z0 = forall a k v. (a -> k -> v -> a) -> a -> HashMap k v -> a
foldlWithKey a -> b -> () -> a
g a
z0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
where g :: a -> b -> () -> a
g a
z b
k ()
_ = a -> b -> a
f a
z b
k
{-# INLINE foldl #-}
filter :: (a -> Bool) -> HashSet a -> HashSet a
filter :: forall a. (a -> Bool) -> HashSet a -> HashSet a
filter a -> Bool
p = forall a. HashMap a () -> HashSet a
HashSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. (k -> v -> Bool) -> HashMap k v -> HashMap k v
H.filterWithKey a -> () -> Bool
q forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HashSet a -> HashMap a ()
asMap
where q :: a -> () -> Bool
q a
k ()
_ = a -> Bool
p a
k
{-# INLINE filter #-}
toList :: HashSet a -> [a]
toList :: forall a. HashSet a -> [a]
toList HashSet a
t = forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
Exts.build (\ a -> b -> b
c b
z -> forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
foldrWithKey (forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> b
c) b
z (forall a. HashSet a -> HashMap a ()
asMap HashSet a
t))
{-# INLINE toList #-}
fromList :: (Eq a, Hashable a) => [a] -> HashSet a
fromList :: forall a. (Eq a, Hashable a) => [a] -> HashSet a
fromList = forall a. HashMap a () -> HashSet a
HashSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' (\ HashMap a ()
m a
k -> forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
H.insert a
k () HashMap a ()
m) forall k v. HashMap k v
H.empty
{-# INLINE fromList #-}
instance (Eq a, Hashable a) => Exts.IsList (HashSet a) where
type Item (HashSet a) = a
fromList :: [Item (HashSet a)] -> HashSet a
fromList = forall a. (Eq a, Hashable a) => [a] -> HashSet a
fromList
toList :: HashSet a -> [Item (HashSet a)]
toList = forall a. HashSet a -> [a]
toList