Skip to content

Commit 9f5b3d3

Browse files
author
Christopher Doris
committed
implements jlwrap set
1 parent c267ea6 commit 9f5b3d3

File tree

3 files changed

+108
-298
lines changed

3 files changed

+108
-298
lines changed

src/jlwrap/any.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function pyjlany_delitem(self, k_::Py)
6969
end
7070
pyjl_handle_error_type(::typeof(pyjlany_delitem), self, exc) = exc isa BoundsError ? pybuiltins.IndexError : exc isa KeyError ? pybuiltins.KeyError : PyNULL
7171

72-
pyjlany_contains(self, v::Py) = Py(pyconvert(Any, v) in self)
72+
pyjlany_contains(self, v::Py) = Py(@pyconvert(eltype(self), v, return Py(false)) in self)
7373
pyjl_handle_error_type(::typeof(pyjlany_contains), self, exc) = exc isa MethodError && exc.f === in ? pybuiltins.TypeError : PyNULL
7474

7575
struct pyjlany_op{OP}

src/jlwrap/set.jl

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,119 @@
11
const pyjlsettype = pynew()
22

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+
376
function init_jlwrap_set()
477
jl = pyjuliacallmodule
578
filename = "$(@__FILE__):$(1+@__LINE__)"
679
pybuiltins.exec(pybuiltins.compile("""
780
class SetValue(AnyValue):
881
__slots__ = ()
982
__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)
10117
""", filename, "exec"), jl.__dict__)
11118
pycopy!(pyjlsettype, jl.SetValue)
12119
end

0 commit comments

Comments
 (0)