1 | use crate::object::*; |
2 | #[cfg (not(any(Py_LIMITED_API, PyPy)))] |
3 | use crate::pyport::Py_hash_t; |
4 | use crate::pyport::Py_ssize_t; |
5 | use std::os::raw::c_int; |
6 | use std::ptr::addr_of_mut; |
7 | |
8 | pub const PySet_MINSIZE: usize = 8; |
9 | |
10 | #[cfg (not(any(Py_LIMITED_API, PyPy)))] |
11 | #[repr (C)] |
12 | #[derive (Debug)] |
13 | pub struct setentry { |
14 | pub key: *mut PyObject, |
15 | pub hash: Py_hash_t, |
16 | } |
17 | |
18 | #[cfg (not(any(Py_LIMITED_API, PyPy)))] |
19 | #[repr (C)] |
20 | #[derive (Debug)] |
21 | pub struct PySetObject { |
22 | pub ob_base: PyObject, |
23 | pub fill: Py_ssize_t, |
24 | pub used: Py_ssize_t, |
25 | pub mask: Py_ssize_t, |
26 | pub table: *mut setentry, |
27 | pub hash: Py_hash_t, |
28 | pub finger: Py_ssize_t, |
29 | pub smalltable: [setentry; PySet_MINSIZE], |
30 | pub weakreflist: *mut PyObject, |
31 | } |
32 | |
33 | // skipped |
34 | #[inline ] |
35 | #[cfg (all(not(PyPy), not(Py_LIMITED_API)))] |
36 | pub unsafe fn PySet_GET_SIZE(so: *mut PyObject) -> Py_ssize_t { |
37 | debug_assert_eq!(PyAnySet_Check(so), 1); |
38 | let so: *mut PySetObject = so.cast::<PySetObject>(); |
39 | (*so).used |
40 | } |
41 | |
42 | #[cfg (not(Py_LIMITED_API))] |
43 | #[cfg_attr (windows, link(name = "pythonXY" ))] |
44 | extern "C" { |
45 | pub static mut _PySet_Dummy: *mut PyObject; |
46 | } |
47 | |
48 | extern "C" { |
49 | #[cfg (not(Py_LIMITED_API))] |
50 | #[cfg_attr (PyPy, link_name = "_PyPySet_NextEntry" )] |
51 | pub fn _PySet_NextEntry( |
52 | set: *mut PyObject, |
53 | pos: *mut Py_ssize_t, |
54 | key: *mut *mut PyObject, |
55 | hash: *mut super::Py_hash_t, |
56 | ) -> c_int; |
57 | |
58 | // skipped non-limited _PySet_Update |
59 | } |
60 | |
61 | #[cfg_attr (windows, link(name = "pythonXY" ))] |
62 | extern "C" { |
63 | #[cfg_attr (PyPy, link_name = "PyPySet_Type" )] |
64 | pub static mut PySet_Type: PyTypeObject; |
65 | #[cfg_attr (PyPy, link_name = "PyPyFrozenSet_Type" )] |
66 | pub static mut PyFrozenSet_Type: PyTypeObject; |
67 | pub static mut PySetIter_Type: PyTypeObject; |
68 | } |
69 | |
70 | extern "C" { |
71 | #[cfg_attr (PyPy, link_name = "PyPySet_New" )] |
72 | pub fn PySet_New(arg1: *mut PyObject) -> *mut PyObject; |
73 | #[cfg_attr (PyPy, link_name = "PyPyFrozenSet_New" )] |
74 | pub fn PyFrozenSet_New(arg1: *mut PyObject) -> *mut PyObject; |
75 | |
76 | #[cfg_attr (PyPy, link_name = "PyPySet_Add" )] |
77 | pub fn PySet_Add(set: *mut PyObject, key: *mut PyObject) -> c_int; |
78 | #[cfg_attr (PyPy, link_name = "PyPySet_Clear" )] |
79 | pub fn PySet_Clear(set: *mut PyObject) -> c_int; |
80 | #[cfg_attr (PyPy, link_name = "PyPySet_Contains" )] |
81 | pub fn PySet_Contains(anyset: *mut PyObject, key: *mut PyObject) -> c_int; |
82 | #[cfg_attr (PyPy, link_name = "PyPySet_Discard" )] |
83 | pub fn PySet_Discard(set: *mut PyObject, key: *mut PyObject) -> c_int; |
84 | #[cfg_attr (PyPy, link_name = "PyPySet_Pop" )] |
85 | pub fn PySet_Pop(set: *mut PyObject) -> *mut PyObject; |
86 | #[cfg_attr (PyPy, link_name = "PyPySet_Size" )] |
87 | pub fn PySet_Size(anyset: *mut PyObject) -> Py_ssize_t; |
88 | |
89 | #[cfg (PyPy)] |
90 | #[link_name = "PyPyFrozenSet_CheckExact" ] |
91 | pub fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int; |
92 | } |
93 | |
94 | #[inline ] |
95 | #[cfg (not(PyPy))] |
96 | pub unsafe fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int { |
97 | (Py_TYPE(ob) == addr_of_mut!(PyFrozenSet_Type)) as c_int |
98 | } |
99 | |
100 | extern "C" { |
101 | #[cfg (PyPy)] |
102 | #[link_name = "PyPyFrozenSet_Check" ] |
103 | pub fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int; |
104 | } |
105 | |
106 | #[inline ] |
107 | #[cfg (not(PyPy))] |
108 | pub unsafe fn PyFrozenSet_Check(ob: *mut PyObject) -> c_int { |
109 | (Py_TYPE(ob) == addr_of_mut!(PyFrozenSet_Type) |
110 | || PyType_IsSubtype(a:Py_TYPE(ob), b:addr_of_mut!(PyFrozenSet_Type)) != 0) as c_int |
111 | } |
112 | |
113 | extern "C" { |
114 | #[cfg (PyPy)] |
115 | #[link_name = "PyPyAnySet_CheckExact" ] |
116 | pub fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int; |
117 | } |
118 | |
119 | #[inline ] |
120 | #[cfg (not(PyPy))] |
121 | pub unsafe fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int { |
122 | (Py_TYPE(ob) == addr_of_mut!(PySet_Type) || Py_TYPE(ob) == addr_of_mut!(PyFrozenSet_Type)) |
123 | as c_int |
124 | } |
125 | |
126 | #[inline ] |
127 | pub unsafe fn PyAnySet_Check(ob: *mut PyObject) -> c_int { |
128 | (PyAnySet_CheckExact(ob) != 0 |
129 | || PyType_IsSubtype(a:Py_TYPE(ob), b:addr_of_mut!(PySet_Type)) != 0 |
130 | || PyType_IsSubtype(a:Py_TYPE(ob), b:addr_of_mut!(PyFrozenSet_Type)) != 0) as c_int |
131 | } |
132 | |
133 | #[inline ] |
134 | #[cfg (Py_3_10)] |
135 | pub unsafe fn PySet_CheckExact(op: *mut PyObject) -> c_int { |
136 | crate::Py_IS_TYPE(ob:op, tp:addr_of_mut!(PySet_Type)) |
137 | } |
138 | |
139 | extern "C" { |
140 | #[cfg (PyPy)] |
141 | #[link_name = "PyPySet_Check" ] |
142 | pub fn PySet_Check(ob: *mut PyObject) -> c_int; |
143 | } |
144 | |
145 | #[inline ] |
146 | #[cfg (not(PyPy))] |
147 | pub unsafe fn PySet_Check(ob: *mut PyObject) -> c_int { |
148 | (Py_TYPE(ob) == addr_of_mut!(PySet_Type) |
149 | || PyType_IsSubtype(a:Py_TYPE(ob), b:addr_of_mut!(PySet_Type)) != 0) as c_int |
150 | } |
151 | |