|
1 | 1 | const pyjlsettype = pynew()
|
2 | 2 |
|
| 3 | +pyjlset_add(x::AbstractSet, v::Py) = (push!(x, pyconvertarg(eltype(x), v, "value")); Py(nothing)) |
| 4 | + |
| 5 | +function pyjlset_discard(x::AbstractSet, v_::Py) |
| 6 | + v = @pyconvert(eltype(x), v_, return Py(nothing)) |
| 7 | + delete!(x, v) |
| 8 | + Py(nothing) |
| 9 | +end |
| 10 | + |
| 11 | +pyjlset_clear(x::AbstractSet) = (empty!(x); Py(nothing)) |
| 12 | + |
| 13 | +function pyjlset_pop(x::AbstractSet) |
| 14 | + if isempty(x) |
| 15 | + errset(pybuiltins.KeyError, "pop from an empty set") |
| 16 | + pynew() |
| 17 | + else |
| 18 | + Py(pop!(x)) |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +function pyjlset_remove(x::AbstractSet, v_::Py) |
| 23 | + v = @pyconvert eltype(x) v_ begin |
| 24 | + errset(pybuiltins.KeyError, v_) |
| 25 | + return pynew() |
| 26 | + end |
| 27 | + if v in x |
| 28 | + delete!(x, v) |
| 29 | + return Py(nothing) |
| 30 | + else |
| 31 | + errset(pybuiltins.KeyError, v_) |
| 32 | + return pynew() |
| 33 | + end |
| 34 | +end |
| 35 | + |
| 36 | +function pyjlset_update(x::AbstractSet, vs_::Py) |
| 37 | + for v_ in vs_ |
| 38 | + v = pyconvert(eltype(x), v_) |
| 39 | + push!(x, v) |
| 40 | + pydel!(v_) |
| 41 | + end |
| 42 | + Py(nothing) |
| 43 | +end |
| 44 | + |
| 45 | +function pyjlset_difference_update(x::AbstractSet, vs_::Py) |
| 46 | + for v_ in vs_ |
| 47 | + v = @pyconvert(eltype(x), v_, continue) |
| 48 | + delete!(x, v) |
| 49 | + pydel!(v_) |
| 50 | + end |
| 51 | + Py(nothing) |
| 52 | +end |
| 53 | + |
| 54 | +function pyjlset_intersection_update(x::AbstractSet, vs_::Py) |
| 55 | + vs = Set{eltype(x)}() |
| 56 | + for v_ in vs_ |
| 57 | + v = @pyconvert(eltype(x), v_, continue) |
| 58 | + push!(vs, v) |
| 59 | + pydel!(v_) |
| 60 | + end |
| 61 | + intersect!(x, vs) |
| 62 | + Py(nothing) |
| 63 | +end |
| 64 | + |
| 65 | +function pyjlset_symmetric_difference_update(x::AbstractSet, vs_::Py) |
| 66 | + vs = Set{eltype(x)}() |
| 67 | + for v_ in vs_ |
| 68 | + v = pyconvert(eltype(x), v_) |
| 69 | + push!(vs, v) |
| 70 | + pydel!(v_) |
| 71 | + end |
| 72 | + symdiff!(x, vs) |
| 73 | + Py(nothing) |
| 74 | +end |
| 75 | + |
3 | 76 | function init_jlwrap_set()
|
4 | 77 | jl = pyjuliacallmodule
|
5 | 78 | filename = "$(@__FILE__):$(1+@__LINE__)"
|
6 | 79 | pybuiltins.exec(pybuiltins.compile("""
|
7 | 80 | class SetValue(AnyValue):
|
8 | 81 | __slots__ = ()
|
9 | 82 | __module__ = "juliacall"
|
| 83 | + def add(self, value): |
| 84 | + return self._jl_callmethod($(pyjl_methodnum(pyjlset_add)), value) |
| 85 | + def discard(self, value): |
| 86 | + return self._jl_callmethod($(pyjl_methodnum(pyjlset_discard)), value) |
| 87 | + def clear(self): |
| 88 | + return self._jl_callmethod($(pyjl_methodnum(pyjlset_clear))) |
| 89 | + def copy(self): |
| 90 | + return self._jl_callmethod($(pyjl_methodnum(Py ∘ copy))) |
| 91 | + def pop(self): |
| 92 | + return self._jl_callmethod($(pyjl_methodnum(pyjlset_pop))) |
| 93 | + def remove(self, value): |
| 94 | + return self._jl_callmethod($(pyjl_methodnum(pyjlset_remove)), value) |
| 95 | + def difference(self, other): |
| 96 | + return set(self).difference(other) |
| 97 | + def intersection(self, other): |
| 98 | + return set(self).intersection(other) |
| 99 | + def symmetric_difference(self, other): |
| 100 | + return set(self).symmetric_difference(other) |
| 101 | + def union(self, other): |
| 102 | + return set(self).union(other) |
| 103 | + def isdisjoint(self, other): |
| 104 | + return set(self).isdisjoint(other) |
| 105 | + def issubset(self, other): |
| 106 | + return set(self).issubset(other) |
| 107 | + def issuperset(self, other): |
| 108 | + return set(self).issuperset(other) |
| 109 | + def difference_update(self, other): |
| 110 | + return self._jl_callmethod($(pyjl_methodnum(pyjlset_difference_update)), other) |
| 111 | + def intersection_update(self, other): |
| 112 | + return self._jl_callmethod($(pyjl_methodnum(pyjlset_intersection_update)), other) |
| 113 | + def symmetric_difference_update(self, other): |
| 114 | + return self._jl_callmethod($(pyjl_methodnum(pyjlset_symmetric_difference_update)), other) |
| 115 | + def update(self, other): |
| 116 | + return self._jl_callmethod($(pyjl_methodnum(pyjlset_update)), other) |
10 | 117 | """, filename, "exec"), jl.__dict__)
|
11 | 118 | pycopy!(pyjlsettype, jl.SetValue)
|
12 | 119 | end
|
|
0 commit comments