1use crate::object::*;
2#[cfg(not(Py_LIMITED_API))]
3use crate::pyport::Py_hash_t;
4use crate::pyport::Py_ssize_t;
5use std::ffi::c_int;
6
7pub const PySet_MINSIZE: usize = 8;
8
9#[cfg(not(Py_LIMITED_API))]
10#[repr(C)]
11#[derive(Debug)]
12pub struct setentry {
13 pub key: *mut PyObject,
14 pub hash: Py_hash_t,
15}
16
17#[cfg(not(Py_LIMITED_API))]
18#[repr(C)]
19#[derive(Debug)]
20pub struct PySetObject {
21 pub ob_base: PyObject,
22 pub fill: Py_ssize_t,
23 pub used: Py_ssize_t,
24 pub mask: Py_ssize_t,
25 pub table: *mut setentry,
26 pub hash: Py_hash_t,
27 pub finger: Py_ssize_t,
28 pub smalltable: [setentry; PySet_MINSIZE],
29 pub weakreflist: *mut PyObject,
30}
31
32#[inline]
34#[cfg(not(Py_LIMITED_API))]
35pub unsafe fn PySet_GET_SIZE(so: *mut PyObject) -> Py_ssize_t {
36 debug_assert_eq!(PyAnySet_Check(so), 1);
37 let so = so.cast::<PySetObject>();
38 (*so).used
39}
40
41extern_libpython! {
44 #[cfg(not(Py_LIMITED_API))]
45 pub fn _PySet_NextEntry(
46 set: *mut PyObject,
47 pos: *mut Py_ssize_t,
48 key: *mut *mut PyObject,
49 hash: *mut super::Py_hash_t,
50 ) -> c_int;
51
52 }
54
55extern_libpython! {
56 pub static mut PySet_Type: PyTypeObject;
57 pub static mut PyFrozenSet_Type: PyTypeObject;
58 pub static mut PySetIter_Type: PyTypeObject;
59}
60
61extern_libpython! {
62 pub fn PySet_New(arg1: *mut PyObject) -> *mut PyObject;
63 pub fn PyFrozenSet_New(arg1: *mut PyObject) -> *mut PyObject;
64
65 pub fn PySet_Add(set: *mut PyObject, key: *mut PyObject) -> c_int;
66 pub fn PySet_Clear(set: *mut PyObject) -> c_int;
67 pub fn PySet_Contains(anyset: *mut PyObject, key: *mut PyObject) -> c_int;
68 pub fn PySet_Discard(set: *mut PyObject, key: *mut PyObject) -> c_int;
69 pub fn PySet_Pop(set: *mut PyObject) -> *mut PyObject;
70 pub fn PySet_Size(anyset: *mut PyObject) -> Py_ssize_t;
71}
72
73#[inline]
74pub unsafe fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int {
75 (Py_TYPE(ob) == &raw mut PyFrozenSet_Type) as c_int
76}
77
78#[inline]
79pub unsafe fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int {
80 (Py_TYPE(ob) == &raw mut PyFrozenSet_Type
81 || PyType_IsSubtype(Py_TYPE(ob), &raw mut PyFrozenSet_Type) != 0) as c_int
82}
83
84#[inline]
85pub unsafe fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int {
86 (Py_TYPE(ob) == &raw mut PySet_Type || Py_TYPE(ob) == &raw mut PyFrozenSet_Type) as c_int
87}
88
89#[inline]
90pub unsafe fn PyAnySet_Check(ob: *mut PyObject) -> c_int {
91 (PyAnySet_CheckExact(ob) != 0
92 || PyType_IsSubtype(Py_TYPE(ob), &raw mut PySet_Type) != 0
93 || PyType_IsSubtype(Py_TYPE(ob), &raw mut PyFrozenSet_Type) != 0) as c_int
94}
95
96#[inline]
97#[cfg(Py_3_10)]
98pub unsafe fn PySet_CheckExact(op: *mut PyObject) -> c_int {
99 crate::Py_IS_TYPE(op, &raw mut PySet_Type)
100}
101
102#[inline]
103pub unsafe fn PySet_Check(ob: *mut PyObject) -> c_int {
104 (Py_TYPE(ob) == &raw mut PySet_Type || PyType_IsSubtype(Py_TYPE(ob), &raw mut PySet_Type) != 0)
105 as c_int
106}