|
| 1 | +const PyJuliaSetValue_Type__ref = Ref(PyPtr()) |
| 2 | +PyJuliaSetValue_Type() = begin |
| 3 | + ptr = PyJuliaSetValue_Type__ref[] |
| 4 | + if isnull(ptr) |
| 5 | + c = [] |
| 6 | + base = PyJuliaAnyValue_Type() |
| 7 | + isnull(base) && return PyPtr() |
| 8 | + t = fill(PyType_Create(c, |
| 9 | + name = "julia.SetValue", |
| 10 | + base = base, |
| 11 | + methods = [ |
| 12 | + (name="add", flags=Py_METH_O, meth=pyjlset_add), |
| 13 | + (name="clear", flags=Py_METH_NOARGS, meth=pyjlset_clear), |
| 14 | + (name="copy", flags=Py_METH_NOARGS, meth=pyjlset_copy), |
| 15 | + (name="difference", flags=Py_METH_O, meth=pyjlset_difference), |
| 16 | + (name="difference_update", flags=Py_METH_O, meth=pyjlset_difference_update), |
| 17 | + (name="discard", flags=Py_METH_O, meth=pyjlset_discard), |
| 18 | + (name="intersection", flags=Py_METH_O, meth=pyjlset_intersection), |
| 19 | + (name="intersection_update", flags=Py_METH_O, meth=pyjlset_intersection_update), |
| 20 | + # (name="isdisjoint", flags=Py_METH_O, meth=pyjlset_isdisjoint), |
| 21 | + # (name="issubset", flags=Py_METH_O, meth=pyjlset_issubset), |
| 22 | + # (name="issuperset", flags=Py_METH_O, meth=pyjlset_issuperset), |
| 23 | + (name="pop", flags=Py_METH_NOARGS, meth=pyjlset_pop), |
| 24 | + (name="remove", flags=Py_METH_O, meth=pyjlset_remove), |
| 25 | + (name="symmetric_difference", flags=Py_METH_O, meth=pyjlset_symmetric_difference), |
| 26 | + (name="symmetric_difference_update", flags=Py_METH_O, meth=pyjlset_symmetric_difference_update), |
| 27 | + (name="union", flags=Py_METH_O, meth=pyjlset_union), |
| 28 | + (name="update", flags=Py_METH_O, meth=pyjlset_update), |
| 29 | + ], |
| 30 | + as_number = ( |
| 31 | + or = pyjlset_or, |
| 32 | + and = pyjlset_and, |
| 33 | + xor = pyjlset_xor, |
| 34 | + subtract = pyjlset_sub, |
| 35 | + inplace_or = pyjlset_ior, |
| 36 | + inplace_and = pyjlset_iand, |
| 37 | + inplace_xor = pyjlset_ixor, |
| 38 | + inplace_subtract = pyjlset_isub, |
| 39 | + ), |
| 40 | + )) |
| 41 | + ptr = PyPtr(pointer(t)) |
| 42 | + err = PyType_Ready(ptr) |
| 43 | + ism1(err) && return PyPtr() |
| 44 | + abc = PyMutableSetABC_Type() |
| 45 | + isnull(abc) && return PyPtr() |
| 46 | + ism1(PyABC_Register(ptr, abc)) && return PyPtr() |
| 47 | + PYJLGCCACHE[ptr] = push!(c, t) |
| 48 | + PyJuliaSetValue_Type__ref[] = ptr |
| 49 | + end |
| 50 | + ptr |
| 51 | +end |
| 52 | + |
| 53 | +PyJuliaSetValue_New(x::AbstractSet) = PyJuliaValue_New(PyJuliaSetValue_Type(), x) |
| 54 | +PyJuliaValue_From(x::AbstractSet) = PyJuliaSetValue_New(x) |
| 55 | + |
| 56 | +pyjlset_add(xo::PyPtr, vo::PyPtr) = begin |
| 57 | + x = PyJuliaValue_GetValue(xo)::AbstractSet |
| 58 | + ism1(PyObject_Convert(vo, eltype(x))) && return PyPtr() |
| 59 | + v = takeresult(eltype(x)) |
| 60 | + try |
| 61 | + push!(x, v) |
| 62 | + PyNone_New() |
| 63 | + catch err |
| 64 | + PyErr_SetJuliaError(err) |
| 65 | + PyPtr() |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +pyjlset_discard(xo::PyPtr, vo::PyPtr) = begin |
| 70 | + x = PyJuliaValue_GetValue(xo)::AbstractSet |
| 71 | + r = PyObject_TryConvert(vo, eltype(x)) |
| 72 | + r == -1 && return PyPtr() |
| 73 | + r == 0 && return PyNone_New() |
| 74 | + v = takeresult(eltype(x)) |
| 75 | + try |
| 76 | + delete!(x, v) |
| 77 | + PyNone_New() |
| 78 | + catch err |
| 79 | + PyErr_SetJuliaError(err) |
| 80 | + PyPtr() |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +pyjlset_clear(xo::PyPtr, _::PyPtr) = begin |
| 85 | + x = PyJuliaValue_GetValue(xo)::AbstractSet |
| 86 | + try |
| 87 | + empty!(x) |
| 88 | + PyNone_New() |
| 89 | + catch err |
| 90 | + PyErr_SetJuliaError(err) |
| 91 | + PyPtr() |
| 92 | + end |
| 93 | +end |
| 94 | + |
| 95 | +pyjlset_copy(xo::PyPtr, _::PyPtr) = begin |
| 96 | + x = PyJuliaValue_GetValue(xo)::AbstractSet |
| 97 | + try |
| 98 | + PyObject_From(copy(x)) |
| 99 | + catch err |
| 100 | + PyErr_SetJuliaError(err) |
| 101 | + PyPtr() |
| 102 | + end |
| 103 | +end |
| 104 | + |
| 105 | +pyjlset_pop(xo::PyPtr, _::PyPtr) = begin |
| 106 | + x = PyJuliaValue_GetValue(xo)::AbstractSet |
| 107 | + try |
| 108 | + if isempty(x) |
| 109 | + PyErr_SetString(PyExc_KeyError(), "pop from an empty set") |
| 110 | + PyPtr() |
| 111 | + else |
| 112 | + PyObject_From(pop!(x)) |
| 113 | + end |
| 114 | + catch err |
| 115 | + PyErr_SetJuliaError(err) |
| 116 | + PyPtr() |
| 117 | + end |
| 118 | +end |
| 119 | + |
| 120 | +pyjlset_remove(xo::PyPtr, vo::PyPtr) = begin |
| 121 | + x = PyJuliaValue_GetValue(xo)::AbstractSet |
| 122 | + r = PyObject_TryConvert(vo, eltype(x)) |
| 123 | + if r == -1 |
| 124 | + return PyPtr() |
| 125 | + elseif r == 0 |
| 126 | + PyErr_SetObject(PyExc_KeyError(), vo) |
| 127 | + return PyPtr() |
| 128 | + end |
| 129 | + v = takeresult(eltype(x)) |
| 130 | + try |
| 131 | + if v in x |
| 132 | + delete!(x, v) |
| 133 | + PyNone_New() |
| 134 | + else |
| 135 | + PyErr_SetObject(PyExc_KeyError(), vo) |
| 136 | + PyPtr() |
| 137 | + end |
| 138 | + catch err |
| 139 | + PyErr_SetJuliaError(err) |
| 140 | + PyPtr() |
| 141 | + end |
| 142 | +end |
| 143 | + |
| 144 | +pyjlset_ibinop_named(xo, yo, op, skip) = begin |
| 145 | + x = PyJuliaValue_GetValue(xo)::AbstractSet |
| 146 | + y = PyIterable_Collect(yo, eltype(x), skip) |
| 147 | + isempty(y) && PyErr_IsSet() && return PyPtr() |
| 148 | + try |
| 149 | + op(x, y) |
| 150 | + PyNone_New() |
| 151 | + catch err |
| 152 | + PyErr_SetJuliaError(err) |
| 153 | + PyPtr() |
| 154 | + end |
| 155 | +end |
| 156 | +pyjlset_update(xo, yo) = pyjlset_ibinop_named(xo, yo, union!, false) |
| 157 | +pyjlset_difference_update(xo, yo) = pyjlset_ibinop_named(xo, yo, setdiff!, true) |
| 158 | +pyjlset_symmetric_difference_update(xo, yo) = pyjlset_ibinop_named(xo, yo, symdiff!, false) |
| 159 | +pyjlset_intersection_update(xo, yo) = pyjlset_ibinop_named(xo, yo, intersect!, true) |
| 160 | + |
| 161 | +pyjlset_ibinop_operator(xo, yo, op, skip) = begin |
| 162 | + r = PySetABC_Check(yo) |
| 163 | + r == -1 && return PyPtr() |
| 164 | + r == 0 && return PyNotImplemented_New() |
| 165 | + x = PyJuliaValue_GetValue(xo)::AbstractSet |
| 166 | + y = PyIterable_Collect(yo, eltype(x), skip) |
| 167 | + isempty(y) && PyErr_IsSet() && return PyPtr() |
| 168 | + try |
| 169 | + PyObject_From(op(x, y)) |
| 170 | + catch err |
| 171 | + PyErr_SetJuliaError(err) |
| 172 | + PyPtr() |
| 173 | + end |
| 174 | +end |
| 175 | +pyjlset_ior(xo, yo) = pyjlset_ibinop_operator(xo, yo, union!, false) |
| 176 | +pyjlset_isub(xo, yo) = pyjlset_ibinop_operator(xo, yo, setdiff!, true) |
| 177 | +pyjlset_ixor(xo, yo) = pyjlset_ibinop_operator(xo, yo, symdiff!, false) |
| 178 | +pyjlset_iand(xo, yo) = pyjlset_ibinop_operator(xo, yo, intersect!, true) |
| 179 | + |
| 180 | +pyjlset_binop_generic(xo::PyPtr, yo::PyPtr, op) = begin |
| 181 | + xo2 = PySet_New(xo) |
| 182 | + isnull(xo2) && return PyPtr() |
| 183 | + yo2 = PySet_New(yo) |
| 184 | + isnull(yo2) && (Py_DecRef(xo2); return PyPtr()) |
| 185 | + ro = op(xo2, yo2) |
| 186 | + Py_DecRef(xo2) |
| 187 | + Py_DecRef(yo2) |
| 188 | + ro |
| 189 | +end |
| 190 | +pyjlset_or_generic(xo, yo) = pyjlset_binop_generic(xo, yo, PyNumber_InPlaceOr) |
| 191 | +pyjlset_xor_generic(xo, yo) = pyjlset_binop_generic(xo, yo, PyNumber_InPlaceXor) |
| 192 | +pyjlset_and_generic(xo, yo) = pyjlset_binop_generic(xo, yo, PyNumber_InPlaceAnd) |
| 193 | +pyjlset_sub_generic(xo, yo) = pyjlset_binop_generic(xo, yo, PyNumber_InPlaceSubtract) |
| 194 | + |
| 195 | +pyjlset_binop_special(xo::PyPtr, yo::PyPtr, op) = begin |
| 196 | + x = PyJuliaValue_GetValue(xo)::AbstractSet |
| 197 | + y = PyJuliaValue_GetValue(yo)::AbstractSet |
| 198 | + try |
| 199 | + PyObject_From(op(x, y)) |
| 200 | + catch err |
| 201 | + PyErr_SetJuliaError(err) |
| 202 | + PyPtr() |
| 203 | + end |
| 204 | +end |
| 205 | +pyjlset_or_special(xo, yo) = pyjlset_binop_special(xo, yo, union) |
| 206 | +pyjlset_xor_special(xo, yo) = pyjlset_binop_special(xo, yo, symdiff) |
| 207 | +pyjlset_and_special(xo, yo) = pyjlset_binop_special(xo, yo, intersect) |
| 208 | +pyjlset_sub_special(xo, yo) = pyjlset_binop_special(xo, yo, setdiff) |
| 209 | + |
| 210 | +pyjlset_binop_operator(xo::PyPtr, yo::PyPtr, gop, sop) = begin |
| 211 | + t = PyJuliaSetValue_Type() |
| 212 | + isnull(t) && return PyPtr() |
| 213 | + if (r=PyObject_IsInstance(xo, t); ism1(r) && return PyPtr(); r!=0) |
| 214 | + if (r=PyObject_IsInstance(yo, t); ism1(r) && return PyPtr(); r!=0) |
| 215 | + sop(xo, yo) |
| 216 | + elseif (r=PySetABC_Check(yo); ism1(r) && return PyPtr(); r!=0) |
| 217 | + gop(xo, yo) |
| 218 | + else |
| 219 | + PyNotImplemented_New() |
| 220 | + end |
| 221 | + elseif (r=PyObject_IsInstance(yo, t); ism1(r) && return PyPtr(); r!=0) |
| 222 | + if (r=PySetABC_Check(xo); ism1(r) && return PyPtr(); r!=0) |
| 223 | + gop(xo, yo) |
| 224 | + else |
| 225 | + PyNotImplemented_New() |
| 226 | + end |
| 227 | + else |
| 228 | + PyNotImplemented_New() |
| 229 | + end |
| 230 | +end |
| 231 | +pyjlset_or(xo, yo) = pyjlset_binop_operator(xo, yo, pyjlset_or_generic, pyjlset_or_special) |
| 232 | +pyjlset_xor(xo, yo) = pyjlset_binop_operator(xo, yo, pyjlset_xor_generic, pyjlset_xor_special) |
| 233 | +pyjlset_and(xo, yo) = pyjlset_binop_operator(xo, yo, pyjlset_and_generic, pyjlset_and_special) |
| 234 | +pyjlset_sub(xo, yo) = pyjlset_binop_operator(xo, yo, pyjlset_sub_generic, pyjlset_sub_special) |
| 235 | + |
| 236 | +pyjlset_binop_named(xo, yo, gop, sop) = begin |
| 237 | + t = PyJuliaSetValue_Type() |
| 238 | + isnull(t) && return PyPtr() |
| 239 | + if (r=PyObject_IsInstance(yo, t); ism1(r) && return PyPtr(); r!=0) |
| 240 | + sop(xo, yo) |
| 241 | + elseif (r=PySetABC_Check(yo); ism1(r) && return PyPtr(); r!=0) |
| 242 | + gop(xo, yo) |
| 243 | + else |
| 244 | + PyNotImplemented_New() |
| 245 | + end |
| 246 | +end |
| 247 | +pyjlset_union(xo, yo) = pyjlset_binop_named(xo, yo, pyjlset_or_generic, pyjlset_or_special) |
| 248 | +pyjlset_symmetric_difference(xo, yo) = pyjlset_binop_named(xo, yo, pyjlset_xor_generic, pyjlset_xor_special) |
| 249 | +pyjlset_intersection(xo, yo) = pyjlset_binop_named(xo, yo, pyjlset_and_generic, pyjlset_and_special) |
| 250 | +pyjlset_difference(xo, yo) = pyjlset_binop_named(xo, yo, pyjlset_sub_generic, pyjlset_sub_special) |
0 commit comments