-- GENERATED by C->Haskell Compiler, version 0.28.7 Switcheroo, 25 November 2017 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "lib/CPython/Types/Set.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}

-- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

-- | Any functionality not listed below is best accessed using the either
-- the 'Object' protocol (including 'callMethod', 'richCompare', 'hash',
-- 'repr', 'isTrue', and 'getIter') or the 'Number' protocol (including 'and',
-- 'subtract', 'or', 'xor', 'inPlaceAnd', 'inPlaceSubtract', 'inPlaceOr',
-- and 'inPlaceXor').
module CPython.Types.Set
  ( AnySet
  , Set
  , FrozenSet
  , setType
  , frozenSetType
  , toSet
  , toFrozenSet
  , iterableToSet
  , iterableToFrozenSet
  , fromSet
  , size
  , contains
  , add
  , discard
  , pop
  , clear
  ) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified System.IO.Unsafe as C2HSImp





import           CPython.Internal
import           CPython.Types.Tuple (toTuple, iterableToTuple, fromTuple)

class Object a => AnySet a

newtype Set = Set (ForeignPtr Set)

instance Object Set where
  toObject :: Set -> SomeObject
toObject (Set x :: ForeignPtr Set
x) = ForeignPtr Set -> SomeObject
forall a. Object a => ForeignPtr a -> SomeObject
SomeObject ForeignPtr Set
x
  fromForeignPtr :: ForeignPtr Set -> Set
fromForeignPtr = ForeignPtr Set -> Set
Set

instance Concrete Set where
  concreteType :: Set -> Type
concreteType _ = Type
setType

newtype FrozenSet = FrozenSet (ForeignPtr FrozenSet)

instance Object FrozenSet where
  toObject :: FrozenSet -> SomeObject
toObject (FrozenSet x :: ForeignPtr FrozenSet
x) = ForeignPtr FrozenSet -> SomeObject
forall a. Object a => ForeignPtr a -> SomeObject
SomeObject ForeignPtr FrozenSet
x
  fromForeignPtr :: ForeignPtr FrozenSet -> FrozenSet
fromForeignPtr = ForeignPtr FrozenSet -> FrozenSet
FrozenSet

instance Concrete FrozenSet where
  concreteType :: FrozenSet -> Type
concreteType _ = Type
frozenSetType

instance AnySet Set
instance AnySet FrozenSet

setType :: (Type)
setType =
  C2HSImp.unsafePerformIO $
  setType'_ >>= \res ->
  peekStaticObject res >>= \res' :: Type
res' ->
  Type -> IO Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
res')

{-# LINE 71 "lib/CPython/Types/Set.chs" #-}


frozenSetType :: (Type)
frozenSetType =
  C2HSImp.unsafePerformIO $
  frozenSetType'_ >>= \res ->
  peekStaticObject res >>= \res' ->
  return (res')

{-# LINE 74 "lib/CPython/Types/Set.chs" #-}


toSet :: [SomeObject] -> IO Set
toSet xs = toTuple xs >>= iterableToSet

toFrozenSet :: [SomeObject] -> IO FrozenSet
toFrozenSet xs = toTuple xs >>= iterableToFrozenSet

-- | Return a new 'Set' from the contents of an iterable 'Object'. The object
-- may be 'Nothing' to create an empty set. Throws a @TypeError@ if the object
-- is not iterable.
iterableToSet :: Object obj => (obj) -> IO ((Set))
iterableToSet :: obj -> IO Set
iterableToSet a1 :: obj
a1 =
  obj -> (Ptr () -> IO Set) -> IO Set
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject obj
a1 ((Ptr () -> IO Set) -> IO Set) -> (Ptr () -> IO Set) -> IO Set
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  Ptr () -> IO (Ptr ())
iterableToSet'_ Ptr ()
a1' IO (Ptr ()) -> (Ptr () -> IO Set) -> IO Set
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO Set
forall obj a. Object obj => Ptr a -> IO obj
stealObject Ptr ()
res IO Set -> (Set -> IO Set) -> IO Set
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Set
res' ->
  Set -> IO Set
forall (m :: * -> *) a. Monad m => a -> m a
return (Set
res')

{-# LINE 88 "lib/CPython/Types/Set.chs" #-}


-- | Return a new 'FrozenSet' from the contents of an iterable 'Object'. The
-- object may be 'Nothing' to create an empty frozen set. Throws a @TypeError@
-- if the object is not iterable.
iterableToFrozenSet :: Object obj => (obj) -> IO ((FrozenSet))
iterableToFrozenSet :: obj -> IO FrozenSet
iterableToFrozenSet a1 :: obj
a1 =
  obj -> (Ptr () -> IO FrozenSet) -> IO FrozenSet
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject obj
a1 ((Ptr () -> IO FrozenSet) -> IO FrozenSet)
-> (Ptr () -> IO FrozenSet) -> IO FrozenSet
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  Ptr () -> IO (Ptr ())
iterableToFrozenSet'_ Ptr ()
a1' IO (Ptr ()) -> (Ptr () -> IO FrozenSet) -> IO FrozenSet
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO FrozenSet
forall obj a. Object obj => Ptr a -> IO obj
stealObject Ptr ()
res IO FrozenSet -> (FrozenSet -> IO FrozenSet) -> IO FrozenSet
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: FrozenSet
res' ->
  FrozenSet -> IO FrozenSet
forall (m :: * -> *) a. Monad m => a -> m a
return (FrozenSet
res')

{-# LINE 96 "lib/CPython/Types/Set.chs" #-}


fromSet :: AnySet set => set -> IO [SomeObject]
fromSet set = iterableToTuple set >>= fromTuple

-- | Return the size of a 'Set' or 'FrozenSet'.
size :: AnySet set => (set) -> IO ((Integer))
size :: set -> IO Integer
size a1 :: set
a1 =
  set -> (Ptr () -> IO Integer) -> IO Integer
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject set
a1 ((Ptr () -> IO Integer) -> IO Integer)
-> (Ptr () -> IO Integer) -> IO Integer
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  Ptr () -> IO CLong
size'_ Ptr ()
a1' IO CLong -> (CLong -> IO Integer) -> IO Integer
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: CLong
res ->
  CLong -> IO Integer
forall a. Integral a => a -> IO Integer
checkIntReturn CLong
res IO Integer -> (Integer -> IO Integer) -> IO Integer
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Integer
res' ->
  Integer -> IO Integer
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer
res')

{-# LINE 105 "lib/CPython/Types/Set.chs" #-}


-- | Return 'True' if found, 'False' if not found. Unlike the Python
-- @__contains__()@ method, this computation does not automatically convert
-- unhashable 'Set's into temporary 'FrozenSet's. Throws a @TypeError@ if the
-- key is unhashable.
contains :: (AnySet set, Object key) => (set) -> (key) -> IO ((Bool))
contains :: set -> key -> IO Bool
contains a1 :: set
a1 a2 :: key
a2 =
  set -> (Ptr () -> IO Bool) -> IO Bool
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject set
a1 ((Ptr () -> IO Bool) -> IO Bool) -> (Ptr () -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  key -> (Ptr () -> IO Bool) -> IO Bool
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject key
a2 ((Ptr () -> IO Bool) -> IO Bool) -> (Ptr () -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \a2' :: Ptr ()
a2' -> 
  Ptr () -> Ptr () -> IO CInt
contains'_ Ptr ()
a1' Ptr ()
a2' IO CInt -> (CInt -> IO Bool) -> IO Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: CInt
res ->
  CInt -> IO Bool
checkBoolReturn CInt
res IO Bool -> (Bool -> IO Bool) -> IO Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Bool
res' ->
  Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
res')

{-# LINE 115 "lib/CPython/Types/Set.chs" #-}


-- | Add /key/ to a 'Set'. Also works with 'FrozenSet' (like
-- 'CPython.Types.Tuple.setItem' it can be used to fill-in the values of
-- brand new 'FrozenSet's before they are exposed to other code). Throws a
-- @TypeError@ if the key is unhashable. Throws a @MemoryError@ if there is
-- no room to grow.
add :: (AnySet set, Object key) => set -> key -> IO ()
add :: set -> key -> IO ()
add = set -> key -> IO ()
forall set key. (AnySet set, Object key) => set -> key -> IO ()
c_add

-- c2hs won't accept functions named "add" any more, so have it generate
-- c_add and then wrap that manually.
c_add :: (AnySet set, Object key) => (set) -> (key) -> IO ((()))
c_add :: set -> key -> IO ()
c_add a1 :: set
a1 a2 :: key
a2 =
  set -> (Ptr () -> IO ()) -> IO ()
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject set
a1 ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  key -> (Ptr () -> IO ()) -> IO ()
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject key
a2 ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \a2' :: Ptr ()
a2' -> 
  Ptr () -> Ptr () -> IO CInt
c_add'_ Ptr ()
a1' Ptr ()
a2' IO CInt -> (CInt -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: CInt
res ->
  CInt -> IO ()
checkStatusCode CInt
res IO () -> (() -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: ()
res' ->
  () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return (()
res')

{-# LINE 131 "lib/CPython/Types/Set.chs" #-}


-- | Return 'True' if found and removed, 'False' if not found (no action
-- taken). Does not throw @KeyError@ for missing keys. Throws a @TypeError@
-- if /key/ is unhashable. Unlike the Python @discard()@ method, this
-- computation does not automatically convert unhashable sets into temporary
-- 'FrozenSet's.
discard :: Object key => (Set) -> (key) -> IO ((Bool))
discard :: Set -> key -> IO Bool
discard a1 :: Set
a1 a2 :: key
a2 =
  Set -> (Ptr () -> IO Bool) -> IO Bool
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Set
a1 ((Ptr () -> IO Bool) -> IO Bool) -> (Ptr () -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  key -> (Ptr () -> IO Bool) -> IO Bool
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject key
a2 ((Ptr () -> IO Bool) -> IO Bool) -> (Ptr () -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \a2' :: Ptr ()
a2' -> 
  Ptr () -> Ptr () -> IO CInt
discard'_ Ptr ()
a1' Ptr ()
a2' IO CInt -> (CInt -> IO Bool) -> IO Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: CInt
res ->
  CInt -> IO Bool
checkBoolReturn CInt
res IO Bool -> (Bool -> IO Bool) -> IO Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Bool
res' ->
  Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
res')

{-# LINE 142 "lib/CPython/Types/Set.chs" #-}


-- | Return an arbitrary object in the set, and removes the object from the
-- set. Throws @KeyError@ if the set is empty.
pop :: (Set) -> IO ((SomeObject))
pop :: Set -> IO SomeObject
pop a1 :: Set
a1 =
  Set -> (Ptr () -> IO SomeObject) -> IO SomeObject
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject Set
a1 ((Ptr () -> IO SomeObject) -> IO SomeObject)
-> (Ptr () -> IO SomeObject) -> IO SomeObject
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  Ptr () -> IO (Ptr ())
pop'_ Ptr ()
a1' IO (Ptr ()) -> (Ptr () -> IO SomeObject) -> IO SomeObject
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO SomeObject
forall obj a. Object obj => Ptr a -> IO obj
stealObject Ptr ()
res IO SomeObject -> (SomeObject -> IO SomeObject) -> IO SomeObject
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: SomeObject
res' ->
  SomeObject -> IO SomeObject
forall (m :: * -> *) a. Monad m => a -> m a
return (SomeObject
res')

{-# LINE 148 "lib/CPython/Types/Set.chs" #-}


-- | Remove all elements from a set.
clear :: (Set) -> IO ((()))
clear a1 =
  withObject a1 $ \a1' -> 
  clear'_ a1' >>= \res ->
  checkStatusCode res >>= \res' ->
  return (res')

{-# LINE 153 "lib/CPython/Types/Set.chs" #-}


foreign import ccall unsafe "CPython/Types/Set.chs.h hscpython_PySet_Type"
  setType'_ :: (IO (C2HSImp.Ptr ()))

foreign import ccall unsafe "CPython/Types/Set.chs.h hscpython_PyFrozenSet_Type"
  frozenSetType'_ :: (IO (C2HSImp.Ptr ()))

foreign import ccall safe "CPython/Types/Set.chs.h PySet_New"
  iterableToSet'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Set.chs.h PyFrozenSet_New"
  iterableToFrozenSet'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Set.chs.h PySet_Size"
  size'_ :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CLong))

foreign import ccall safe "CPython/Types/Set.chs.h PySet_Contains"
  contains'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))

foreign import ccall safe "CPython/Types/Set.chs.h PySet_Add"
  c_add'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))

foreign import ccall safe "CPython/Types/Set.chs.h PySet_Discard"
  discard'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt)))

foreign import ccall safe "CPython/Types/Set.chs.h PySet_Pop"
  pop'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/Set.chs.h PySet_Clear"
  clear'_ :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CInt))