Skip to content

Commit 41ccf23

Browse files
authored
Remove allocation in add_vertex! (#163)
1 parent db8acc9 commit 41ccf23

File tree

3 files changed

+55
-48
lines changed

3 files changed

+55
-48
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Improved the error message for an incorrect orientation. See [#144](https://github.com/JuliaGeometry/DelaunayTriangulation.jl/pull/144).
1717
- Added a CONTRIBUTING.md file and issue templates. See [#160](https://github.com/JuliaGeometry/DelaunayTriangulation.jl/pull/160).
1818
- Added `is_point2` and `is_point3` to detect if a given input is a point. This allows vector coordinates to be passed to `convert_boundary_points_to_indices`. See [#161](https://github.com/JuliaGeometry/DelaunayTriangulation.jl/pull/161).
19+
- Removed an allocation from `add_vertex!`. See [#163](https://github.com/JuliaGeometry/DelaunayTriangulation.jl/pull/163).
1920

2021
## v1.0.5
2122

src/data_structures/triangulation/adjacent2vertex.jl

+38-38
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ The map taking `w` to the set of all `(u, v)` such that `(u, v, w)` is a positiv
1313
Adjacent2Vertex(adj2v::Dict{IntegerType, EdgesType})
1414
"""
1515
struct Adjacent2Vertex{I,Es}
16-
adjacent2vertex::Dict{I,Es}
17-
Adjacent2Vertex(adj2v::Dict{I,Es}) where {I,Es} = new{I,Es}(adj2v)
16+
adjacent2vertex::Dict{I,Es}
17+
Adjacent2Vertex(adj2v::Dict{I,Es}) where {I,Es} = new{I,Es}(adj2v)
1818
end
1919
Adjacent2Vertex{I,Es}() where {I,Es} = Adjacent2Vertex(Dict{I,Es}())
2020
Base.:(==)(adj2v::Adjacent2Vertex, adj2v2::Adjacent2Vertex) = get_adjacent2vertex(adj2v) == get_adjacent2vertex(adj2v2)
2121
function Base.show(io::IO, m::MIME"text/plain", adj2v::Adjacent2Vertex{I,Es}) where {I,Es}
22-
println(io, "Adjacent2Vertex{", I, ", ", Es, "} with map:")
23-
show(io, m, get_adjacent2vertex(adj2v))
22+
println(io, "Adjacent2Vertex{", I, ", ", Es, "} with map:")
23+
show(io, m, get_adjacent2vertex(adj2v))
2424
end
2525
Base.sizehint!(adj2v::Adjacent2Vertex, n) = Base.sizehint!(get_adjacent2vertex(adj2v), n)
2626

@@ -86,8 +86,8 @@ Set{Tuple{Int64, Int64}} with 3 elements:
8686
```
8787
"""
8888
function get_adjacent2vertex(adj2v::Adjacent2Vertex, w)
89-
dict = get_adjacent2vertex(adj2v)
90-
return dict[w]
89+
dict = get_adjacent2vertex(adj2v)
90+
return dict[w]
9191
end
9292

9393
"""
@@ -123,15 +123,15 @@ Dict{Int64, Set{Tuple{Int64, Int64}}} with 2 entries:
123123
```
124124
"""
125125
function add_adjacent2vertex!(adj2v::Adjacent2Vertex{I,Es}, w, uv) where {I,Es}
126-
dict = get_adjacent2vertex(adj2v)
127-
existing_edges = get!(Es, dict, w)
128-
add_edge!(existing_edges, uv)
129-
return adj2v
126+
dict = get_adjacent2vertex(adj2v)
127+
existing_edges = get!(Es, dict, w)
128+
add_edge!(existing_edges, uv)
129+
return adj2v
130130
end
131131
function add_adjacent2vertex!(adj2v::Adjacent2Vertex{I,Es}, w, u, v) where {I,Es}
132-
E = edge_type(Es)
133-
uv = construct_edge(E, u, v)
134-
return add_adjacent2vertex!(adj2v, w, uv)
132+
E = edge_type(Es)
133+
uv = construct_edge(E, u, v)
134+
return add_adjacent2vertex!(adj2v, w, uv)
135135
end
136136

137137
"""
@@ -165,14 +165,14 @@ Dict{Int64, Set{Tuple{Int64, Int64}}} with 2 entries:
165165
```
166166
"""
167167
function delete_adjacent2vertex!(adj2v::Adjacent2Vertex, w, uv)
168-
existing_edges = get_adjacent2vertex(adj2v, w)
169-
delete_edge!(existing_edges, uv)
170-
return adj2v
168+
existing_edges = get_adjacent2vertex(adj2v, w)
169+
delete_edge!(existing_edges, uv)
170+
return adj2v
171171
end
172172
function delete_adjacent2vertex!(adj2v::Adjacent2Vertex{I,Es}, w, u, v) where {I,Es}
173-
E = edge_type(Es)
174-
uv = construct_edge(E, u, v)
175-
return delete_adjacent2vertex!(adj2v, w, uv)
173+
E = edge_type(Es)
174+
uv = construct_edge(E, u, v)
175+
return delete_adjacent2vertex!(adj2v, w, uv)
176176
end
177177

178178
"""
@@ -201,9 +201,9 @@ Dict{Int64, Set{Tuple{Int64, Int64}}}()
201201
```
202202
"""
203203
function delete_adjacent2vertex!(adj2v::Adjacent2Vertex, w)
204-
dict = get_adjacent2vertex(adj2v)
205-
delete!(dict, w)
206-
return adj2v
204+
dict = get_adjacent2vertex(adj2v)
205+
delete!(dict, w)
206+
return adj2v
207207
end
208208

209209
"""
@@ -238,10 +238,10 @@ Dict{Int32, Set{Tuple{Int32, Int32}}} with 5 entries:
238238
```
239239
"""
240240
function add_triangle!(adj2v::Adjacent2Vertex, u::Integer, v::Integer, w::Integer)
241-
add_adjacent2vertex!(adj2v, u, v, w)
242-
add_adjacent2vertex!(adj2v, v, w, u)
243-
add_adjacent2vertex!(adj2v, w, u, v)
244-
return adj2v
241+
add_adjacent2vertex!(adj2v, u, v, w)
242+
add_adjacent2vertex!(adj2v, v, w, u)
243+
add_adjacent2vertex!(adj2v, w, u, v)
244+
return adj2v
245245
end
246246
add_triangle!(adj2v::Adjacent2Vertex, T) = add_triangle!(adj2v, geti(T), getj(T), getk(T))
247247

@@ -295,10 +295,10 @@ Dict{Int32, Set{Tuple{Int32, Int32}}} with 5 entries:
295295
```
296296
"""
297297
function delete_triangle!(adj2v::Adjacent2Vertex, u::Integer, v::Integer, w::Integer)
298-
delete_adjacent2vertex!(adj2v, u, v, w)
299-
delete_adjacent2vertex!(adj2v, v, w, u)
300-
delete_adjacent2vertex!(adj2v, w, u, v)
301-
return adj2v
298+
delete_adjacent2vertex!(adj2v, u, v, w)
299+
delete_adjacent2vertex!(adj2v, v, w, u)
300+
delete_adjacent2vertex!(adj2v, w, u, v)
301+
return adj2v
302302
end
303303
delete_triangle!(adj2v::Adjacent2Vertex, T) = delete_triangle!(adj2v, geti(T), getj(T), getk(T))
304304

@@ -335,15 +335,15 @@ Dict{Int64, Set{Tuple{Int64, Int64}}}()
335335
```
336336
"""
337337
function clear_empty_keys!(adj2v::Adjacent2Vertex)
338-
dict = get_adjacent2vertex(adj2v)
339-
for (w, S) in dict
340-
isempty(S) && delete_adjacent2vertex!(adj2v, w)
341-
end
342-
return adj2v
338+
dict = get_adjacent2vertex(adj2v)
339+
foreach(dict) do (w, S)
340+
isempty(S) && delete_adjacent2vertex!(adj2v, w)
341+
end
342+
return adj2v
343343
end
344344

345345
function Base.empty!(adj2v::Adjacent2Vertex)
346-
dict = get_adjacent2vertex(adj2v)
347-
empty!(dict)
348-
return adj2v
346+
dict = get_adjacent2vertex(adj2v)
347+
empty!(dict)
348+
return adj2v
349349
end

src/data_structures/triangulation/graph.jl

+16-10
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,19 @@ has_vertex(G::Graph, u) = u ∈ get_vertices(G)
116116
117117
Adds the vertices `u...` to `G`.
118118
"""
119-
function add_vertex!(G::Graph{I}, u...) where {I}
119+
function add_vertex!(G::Graph{I}, v) where {I}
120+
has_vertex(G, v) && return G
120121
V = get_vertices(G)
122+
push!(V, v)
121123
N = get_neighbours(G)
122-
for v in u
123-
has_vertex(G, v) && continue
124-
push!(V, v)
125-
!haskey(N, v) && (N[v] = Set{I}())
124+
get!(N, v) do
125+
Set{I}() # in case N is empty, let's add it here
126+
end
127+
return G
128+
end
129+
function add_vertex!(G::Graph{I}, u...) where {I}
130+
foreach(u) do v
131+
add_vertex!(G, v)
126132
end
127133
return G
128134
end
@@ -167,7 +173,7 @@ function add_neighbour!(G::Graph{I}, u, v) where {I}
167173
return G
168174
end
169175
function add_neighbour!(G::Graph{I}, u, v...) where {I}
170-
for w in v
176+
foreach(v) do w
171177
add_neighbour!(G, u, w)
172178
end
173179
return G
@@ -188,7 +194,7 @@ function delete_neighbour!(G::Graph{I}, u, v) where {I}
188194
return G
189195
end
190196
function delete_neighbour!(G::Graph{I}, u, v...) where {I}
191-
for w in v
197+
foreach(v) do w
192198
delete_neighbour!(G, u, w)
193199
end
194200
return G
@@ -229,7 +235,7 @@ Deletes the vertices `u...` from `G`.
229235
function delete_vertex!(G::Graph{I}, u) where {I}
230236
N = get_neighbours(G)
231237
u_N = get!(Set{I}, N, u)
232-
for v in u_N
238+
foreach(u_N) do v
233239
v_N = get!(Set{I}, N, v)
234240
delete!(v_N, u) # don't use delete_neighbour! because it will delete u_N[v] as well
235241
end
@@ -239,7 +245,7 @@ function delete_vertex!(G::Graph{I}, u) where {I}
239245
return G
240246
end
241247
function delete_vertex!(G::Graph, u...)
242-
for v in u
248+
foreach(u) do v
243249
delete_vertex!(G, v)
244250
end
245251
return G
@@ -276,7 +282,7 @@ Deletes all empty vertices from `G`.
276282
"""
277283
function clear_empty_vertices!(G::Graph)
278284
V = get_vertices(G)
279-
for i in V
285+
foreach(V) do i
280286
n = num_neighbours(G, i)
281287
iszero(n) && delete_vertex!(G, i)
282288
end

0 commit comments

Comments
 (0)