Skip to content

Commit 8286e57

Browse files
committed
Add FrozenVector to wrap neighbors
1 parent cc3052f commit 8286e57

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

src/Graphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ and tutorials are available at the
452452
Graphs
453453
include("interface.jl")
454454
include("utils.jl")
455+
include("frozenvector.jl")
455456
include("deprecations.jl")
456457
include("core.jl")
457458

src/SimpleGraphs/SimpleGraphs.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Graphs:
1515
AbstractGraph,
1616
AbstractEdge,
1717
AbstractEdgeIter,
18+
FrozenVector,
1819
src,
1920
dst,
2021
edgetype,
@@ -152,8 +153,8 @@ add_edge!(g::AbstractSimpleGraph, x) = add_edge!(g, edgetype(g)(x))
152153
has_edge(g::AbstractSimpleGraph, x, y) = has_edge(g, edgetype(g)(x, y))
153154
add_edge!(g::AbstractSimpleGraph, x, y) = add_edge!(g, edgetype(g)(x, y))
154155

155-
inneighbors(g::AbstractSimpleGraph, v::Integer) = badj(g, v)
156-
outneighbors(g::AbstractSimpleGraph, v::Integer) = fadj(g, v)
156+
inneighbors(g::AbstractSimpleGraph, v::Integer) = FrozenVector(badj(g, v))
157+
outneighbors(g::AbstractSimpleGraph, v::Integer) = FrozenVector(fadj(g, v))
157158

158159
function issubset(g::T, h::T) where {T<:AbstractSimpleGraph}
159160
nv(g) <= nv(h) || return false

src/core.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,10 @@ For directed graphs, the default is equivalent to [`outneighbors`](@ref);
221221
use [`all_neighbors`](@ref) to list inbound and outbound neighbors.
222222
223223
### Implementation Notes
224-
Returns a reference to the current graph's internal structures, not a copy.
225-
Do not modify result. If the graph is modified, the behavior is undefined:
224+
In some cases might return a reference to the current graph's internal structures,
225+
not a copy. Do not modify result. If the graph is modified, the behavior is undefined:
226226
the array behind this reference may be modified too, but this is not guaranteed.
227+
If you need to modify the result use `collect` or `copy` to create a copy.
227228
228229
# Examples
229230
```jldoctest
@@ -236,14 +237,14 @@ julia> add_edge!(g, 2, 3);
236237
julia> add_edge!(g, 3, 1);
237238
238239
julia> neighbors(g, 1)
239-
Int64[]
240+
0-element Graphs.FrozenVector{Int64}
240241
241242
julia> neighbors(g, 2)
242-
1-element Vector{Int64}:
243+
1-element Graphs.FrozenVector{Int64}:
243244
3
244245
245246
julia> neighbors(g, 3)
246-
1-element Vector{Int64}:
247+
1-element Graphs.FrozenVector{Int64}:
247248
1
248249
```
249250
"""
@@ -257,9 +258,10 @@ For undirected graphs, this is equivalent to both [`outneighbors`](@ref)
257258
and [`inneighbors`](@ref).
258259
259260
### Implementation Notes
260-
Returns a reference to the current graph's internal structures, not a copy.
261-
Do not modify result. If the graph is modified, the behavior is undefined:
261+
In some cases might return a reference to the current graph's internal structures,
262+
not a copy. Do not modify result. If the graph is modified, the behavior is undefined:
262263
the array behind this reference may be modified too, but this is not guaranteed.
264+
If you need to modify the result use `collect` or `copy` to create a copy.
263265
264266
# Examples
265267
```jldoctest

src/frozenvector.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
FrozenVector(v::Vector) <: AbstractVector
3+
4+
A data structure that wraps a `Vector` but does not allow modifications.
5+
"""
6+
struct FrozenVector{T} <: AbstractVector{T}
7+
wrapped::Vector{T}
8+
end
9+
10+
Base.size(v::FrozenVector) = Base.size(v.wrapped)
11+
12+
Base.@propagate_inbounds Base.getindex(v::FrozenVector, i::Int) = Base.getindex(v.wrapped, i)
13+
14+
Base.IndexStyle(v::Type{FrozenVector{T}}) where {T} = Base.IndexStyle(Vector{T})
15+
16+
Base.iterate(v::FrozenVector) = Base.iterate(v.wrapped)
17+
Base.iterate(v::FrozenVector, state) = Base.iterate(v.wrapped, state)
18+
19+
Base.similar(v::FrozenVector) = Base.similar(v.wrapped)
20+
Base.similar(v::FrozenVector, T::Type) = Base.similar(v.wrapped, T)
21+
Base.similar(v::FrozenVector, T::Type, dims::Base.Dims) = Base.similar(v.wrapped, T, dims)
22+
23+
Base.copy(v::FrozenVector) = Base.copy(v.wrapped)

src/interface.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,10 @@ has_edge(g, e) = has_edge(g, src(e), dst(e))
281281
Return a list of all neighbors connected to vertex `v` by an incoming edge.
282282
283283
### Implementation Notes
284-
Returns a reference to the current graph's internal structures, not a copy.
285-
Do not modify result. If the graph is modified, the behavior is undefined:
284+
In some cases might return a reference to the current graph's internal structures,
285+
not a copy. Do not modify result. If the graph is modified, the behavior is undefined:
286286
the array behind this reference may be modified too, but this is not guaranteed.
287+
If you need to modify the result use `collect` or `copy` to create a copy.
287288
288289
# Examples
289290
```jldoctest
@@ -292,7 +293,7 @@ julia> using Graphs
292293
julia> g = SimpleDiGraph([0 1 0 0 0; 0 0 1 0 0; 1 0 0 1 0; 0 0 0 0 1; 0 0 0 1 0]);
293294
294295
julia> inneighbors(g, 4)
295-
2-element Vector{Int64}:
296+
2-element Graphs.FrozenVector{Int64}:
296297
3
297298
5
298299
```
@@ -305,9 +306,10 @@ inneighbors(x, v) = _NI("inneighbors")
305306
Return a list of all neighbors connected to vertex `v` by an outgoing edge.
306307
307308
# Implementation Notes
308-
Returns a reference to the current graph's internal structures, not a copy.
309-
Do not modify result. If the graph is modified, the behavior is undefined:
309+
In some cases might return a reference to the current graph's internal structures,
310+
not a copy. Do not modify result. If the graph is modified, the behavior is undefined:
310311
the array behind this reference may be modified too, but this is not guaranteed.
312+
If you need to modify the result use `collect` or `copy` to create a copy.
311313
312314
# Examples
313315
```jldoctest
@@ -316,7 +318,7 @@ julia> using Graphs
316318
julia> g = SimpleDiGraph([0 1 0 0 0; 0 0 1 0 0; 1 0 0 1 0; 0 0 0 0 1; 0 0 0 1 0]);
317319
318320
julia> outneighbors(g, 4)
319-
1-element Vector{Int64}:
321+
1-element Graphs.FrozenVector{Int64}:
320322
5
321323
```
322324
"""

0 commit comments

Comments
 (0)