Skip to content

Commit 377fcf1

Browse files
Merge pull request #219 from CarloLucibello/cl/copy
implement copy
2 parents 3309dd3 + 8f48eea commit 377fcf1

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Manifest.toml
88
/docs/build/
99
.vscode
1010
LocalPreferences.toml
11+
.DS_Store

src/GNNGraphs/gnngraph.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,27 @@ function GNNGraph(g::GNNGraph; ndata=g.ndata, edata=g.edata, gdata=g.gdata, grap
200200
ndata, edata, gdata)
201201
end
202202

203+
204+
"""
205+
copy(g::GNNGraph; deep=false)
206+
207+
Create a copy of `g`. If `deep` is `true`, then copy will be a deep copy (equivalent to `deepcopy(g)`),
208+
otherwise it will be a shallow copy with the same underlying graph data.
209+
"""
210+
function Base.copy(g::GNNGraph; deep=false)
211+
if deep
212+
GNNGraph(deepcopy(g.graph),
213+
g.num_nodes, g.num_edges, g.num_graphs,
214+
deepcopy(g.graph_indicator),
215+
deepcopy(g.ndata), deepcopy(g.edata), deepcopy(g.gdata))
216+
else
217+
GNNGraph(g.graph,
218+
g.num_nodes, g.num_edges, g.num_graphs,
219+
g.graph_indicator,
220+
g.ndata, g.edata, g.gdata)
221+
end
222+
end
223+
203224
function Base.show(io::IO, g::GNNGraph)
204225
print(io, "GNNGraph($(g.num_nodes), $(g.num_edges))")
205226
end

test/GNNGraphs/gnngraph.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,17 @@
313313
@test hash(g1) != hash(GNNGraph(g1, ndata=rand(5), graph_type=GRAPH_T))
314314
@test hash(g1) != hash(GNNGraph(g1, edata=rand(6), graph_type=GRAPH_T))
315315
end
316+
317+
318+
@testset "copy" begin
319+
g1 = rand_graph(10, 4, ndata=rand(2,10), graph_type=GRAPH_T)
320+
g2 = copy(g1)
321+
@test g1 === g2 # shallow copies are identical for immutable objects
322+
323+
g2 = copy(g1, deep=true)
324+
@test g1 == g2
325+
@test g1 !== g2
326+
end
316327
end
317328

318329

0 commit comments

Comments
 (0)