diff --git a/docs/src/changelog.md b/docs/src/changelog.md index 4aa49558..c93e1a69 100644 --- a/docs/src/changelog.md +++ b/docs/src/changelog.md @@ -11,12 +11,12 @@ and edges used internally. terminology. The overloads for implementing a new `AbstractNamedGraph` are `encoded_graph(g)` (replaces `position_graph`, with a generic `EncodedGraphView` fallback so a graph type does not need to store an - integer graph), `encode_vertex(g, v)` (replaces `vertex_positions`), and - `decode_vertex(g, c)` (replaces `ordered_vertices`). Codes are not stable + integer graph), `encoded_vertex(g, v)` (replaces `vertex_positions`), and + `decoded_vertex(g, c)` (replaces `ordered_vertices`). Codes are not stable across mutation ([#178](https://github.com/ITensor/NamedGraphs.jl/pull/178)). - The `OrdinalIndexing` submodule is removed, so `vertices(g)[4th]` becomes - `decode_vertex(g, 4)` + `decoded_vertex(g, 4)` ([#178](https://github.com/ITensor/NamedGraphs.jl/pull/178)). - `vertices(g::Named[Di]Graph)` outputs a `Dictionaries.Indices` instead of the internal `OrderedIndices` type, which @@ -25,7 +25,7 @@ and edges used internally. Vertices iterate in insertion order, which is stable under removals and therefore no longer matches the integer codes after removals, so code that aligns `vertices(g)` with results computed on the integer graph should - translate through `decode_vertex` + translate through `decoded_vertex` ([#178](https://github.com/ITensor/NamedGraphs.jl/pull/178)). - `edges(g)` outputs a lazy iterator instead of a `Vector`, like `edges(::SimpleGraph)` in Graphs.jl. Membership (`in`) matches `has_edge`, diff --git a/docs/src/dev_interface.md b/docs/src/dev_interface.md index 01464c35..50d531f3 100644 --- a/docs/src/dev_interface.md +++ b/docs/src/dev_interface.md @@ -11,21 +11,21 @@ Subtype [`AbstractNamedGraph`](@ref) and overload the minimal interface below, the graph on integer vertex codes and the translation between names and codes. Everything else in the Graphs.jl interface has generic fallbacks in terms of these. Graph types that do not store an integer graph get a generic -`EncodedGraphView` fallback for `encoded_graph` and only need `encode_vertex` -and `decode_vertex`. +`EncodedGraphView` fallback for `encoded_graph` and only need `encoded_vertex` +and `decoded_vertex`. Vertex codes are not stable across mutation: adding or removing vertices may reassign the codes of other vertices. These names are `public` rather than exported, so reach them with -`using NamedGraphs: encode_vertex` or by qualifying. +`using NamedGraphs: encoded_vertex` or by qualifying. ```@docs; canonical=false encoded_graph -encode_vertex -decode_vertex -encode_edge -decode_edge +encoded_vertex +decoded_vertex +encoded_edge +decoded_edge ``` ## Graphs.jl interface extensions diff --git a/ext/NamedGraphsGraphsFlowsExt/NamedGraphsGraphsFlowsExt.jl b/ext/NamedGraphsGraphsFlowsExt/NamedGraphsGraphsFlowsExt.jl index e373f3a1..54a20ce0 100644 --- a/ext/NamedGraphsGraphsFlowsExt/NamedGraphsGraphsFlowsExt.jl +++ b/ext/NamedGraphsGraphsFlowsExt/NamedGraphsGraphsFlowsExt.jl @@ -2,7 +2,7 @@ module NamedGraphsGraphsFlowsExt using Graphs: AbstractGraph, IsDirected using GraphsFlows: GraphsFlows using NamedGraphs: NamedGraphs, AbstractNamedGraph, DefaultNamedCapacity, _symmetrize, - decode_vertex, directed_graph, encode_dist_matrix, encode_vertex, encoded_graph + decoded_vertex, directed_graph, encode_dist_matrix, encoded_graph, encoded_vertex using SimpleTraits: SimpleTraits, @traitfn @traitfn function NamedGraphs.encode_dist_matrix( @@ -20,13 +20,13 @@ end ) encoded_part1, encoded_part2, flow = GraphsFlows.mincut( encoded_graph(graph), - encode_vertex(graph, source), - encode_vertex(graph, target), + encoded_vertex(graph, source), + encoded_vertex(graph, target), encode_dist_matrix(graph, capacity_matrix), algorithm ) (part1, part2) = map((encoded_part1, encoded_part2)) do encoded_part - return map(c -> decode_vertex(graph, c), encoded_part) + return map(c -> decoded_vertex(graph, c), encoded_part) end return (part1, part2, flow) end diff --git a/ext/NamedGraphsITensorVisualizationBaseExt/NamedGraphsITensorVisualizationBaseExt.jl b/ext/NamedGraphsITensorVisualizationBaseExt/NamedGraphsITensorVisualizationBaseExt.jl index a9d8562a..18f549df 100644 --- a/ext/NamedGraphsITensorVisualizationBaseExt/NamedGraphsITensorVisualizationBaseExt.jl +++ b/ext/NamedGraphsITensorVisualizationBaseExt/NamedGraphsITensorVisualizationBaseExt.jl @@ -2,7 +2,7 @@ module NamedGraphsITensorVisualizationBaseExt using Graphs: vertices using ITensorVisualizationBase: ITensorVisualizationBase -using NamedGraphs: AbstractNamedGraph, decode_vertex, encoded_graph +using NamedGraphs: AbstractNamedGraph, decoded_vertex, encoded_graph function ITensorVisualizationBase.visualize( graph::AbstractNamedGraph, @@ -14,7 +14,7 @@ function ITensorVisualizationBase.visualize( if !isnothing(vertex_labels_prefix) # In code order, to align with the vertices of `encoded_graph(graph)`. vertex_labels = [ - vertex_labels_prefix * string(decode_vertex(graph, c)) for + vertex_labels_prefix * string(decoded_vertex(graph, c)) for c in vertices(encoded_graph(graph)) ] end diff --git a/ext/NamedGraphsSimpleGraphAlgorithmsExt/NamedGraphsSimpleGraphAlgorithmsExt.jl b/ext/NamedGraphsSimpleGraphAlgorithmsExt/NamedGraphsSimpleGraphAlgorithmsExt.jl index 9e718798..b6f72dbd 100644 --- a/ext/NamedGraphsSimpleGraphAlgorithmsExt/NamedGraphsSimpleGraphAlgorithmsExt.jl +++ b/ext/NamedGraphsSimpleGraphAlgorithmsExt/NamedGraphsSimpleGraphAlgorithmsExt.jl @@ -1,6 +1,6 @@ module NamedGraphsSimpleGraphAlgorithmsExt using Graphs: AbstractGraph, dst, edges, edgetype, src, vertices -using NamedGraphs: AbstractNamedGraph, decode_vertex, encoded_graph +using NamedGraphs: AbstractNamedGraph, decoded_vertex, encoded_graph using SimpleGraphAlgorithms: SimpleGraphAlgorithms using SimpleGraphs: UndirectedGraph, add! @@ -23,7 +23,7 @@ function SimpleGraphAlgorithms.edge_color(g::AbstractNamedGraph, k::Int64) return [ [ edgetype(g)( - decode_vertex(g, first(first(e))), decode_vertex(g, last(first(e))) + decoded_vertex(g, first(first(e))), decoded_vertex(g, last(first(e))) ) for e in ec_dict if last(e) == i ] diff --git a/src/NamedGraphs.jl b/src/NamedGraphs.jl index e7bd5bb7..cd26b5c6 100644 --- a/src/NamedGraphs.jl +++ b/src/NamedGraphs.jl @@ -40,7 +40,7 @@ if VERSION >= v"1.11.0-DEV.469" # overload, so it is public rather than exported. eval( Meta.parse( - "public decode_edge, decode_vertex, encode_edge, encode_vertex, encoded_graph, to_graph_index" + "public decoded_edge, decoded_vertex, encoded_edge, encoded_graph, encoded_vertex, to_graph_index" ) ) end diff --git a/src/PartitionedGraphs/abstractpartitionedgraph.jl b/src/PartitionedGraphs/abstractpartitionedgraph.jl index 080c80ba..22037e5c 100644 --- a/src/PartitionedGraphs/abstractpartitionedgraph.jl +++ b/src/PartitionedGraphs/abstractpartitionedgraph.jl @@ -126,10 +126,7 @@ end The [`QuotientEdge`](@ref)s of `graph` that connect the given quotient vertices to the quotient vertices outside of them, i.e. the boundary edges of -`quotientvertices` in the quotient graph of `graph`. Returns an `AbstractVector` -of [`QuotientEdge`](@ref)s; the concrete type is not part of the interface, and -the result may be a view into `graph`, so do not modify it or use it after -mutating `graph`. +`quotientvertices` in the quotient graph of `graph`. Keyword arguments are forwarded to `boundary_edges`, in particular `dir`, which selects the edge direction to consider in a directed @@ -283,11 +280,11 @@ Graphs.edges(pg::AbstractPartitionedGraph) = edges(unpartitioned_graph(pg)) function NamedGraphs.encoded_graph(pg::AbstractPartitionedGraph) return NamedGraphs.encoded_graph(unpartitioned_graph(pg)) end -function NamedGraphs.encode_vertex(pg::AbstractPartitionedGraph, vertex) - return NamedGraphs.encode_vertex(unpartitioned_graph(pg), vertex) +function NamedGraphs.encoded_vertex(pg::AbstractPartitionedGraph, vertex) + return NamedGraphs.encoded_vertex(unpartitioned_graph(pg), vertex) end -function NamedGraphs.decode_vertex(pg::AbstractPartitionedGraph, code::Integer) - return NamedGraphs.decode_vertex(unpartitioned_graph(pg), code) +function NamedGraphs.decoded_vertex(pg::AbstractPartitionedGraph, code::Integer) + return NamedGraphs.decoded_vertex(unpartitioned_graph(pg), code) end Graphs.edgetype(pg::AbstractPartitionedGraph) = edgetype(unpartitioned_graph(pg)) diff --git a/src/PartitionedGraphs/quotientedge.jl b/src/PartitionedGraphs/quotientedge.jl index a0490056..eaf1ada7 100644 --- a/src/PartitionedGraphs/quotientedge.jl +++ b/src/PartitionedGraphs/quotientedge.jl @@ -99,10 +99,6 @@ end Return the set of edges in the graph `g` that correspond to a single quotient edge or a list of quotient edges. - -As with `vertices(g, ::QuotientVertex)`, the result can alias the -partitioning stored in `g`: treat it as read-only, discard it when `g` changes, -and rely only on it being an `AbstractVector` of edges. """ function Graphs.edges(pg::AbstractGraph, quotientedge::QuotientEdge) pes = partitioned_edges(pg) diff --git a/src/PartitionedGraphs/quotientvertex.jl b/src/PartitionedGraphs/quotientvertex.jl index 513e3034..a1639355 100644 --- a/src/PartitionedGraphs/quotientvertex.jl +++ b/src/PartitionedGraphs/quotientvertex.jl @@ -71,14 +71,12 @@ end """ vertices(g::AbstractGraph, quotientvertex::QuotientVertex) - vertices(g::AbstractGraph, quotientvertices::Vector{QuotientVertex}) + vertices(g::AbstractGraph, quotientvertices::QuotientVertices) Return the set of vertices in the graph `g` associated with the quotient vertex `quotientvertex` or set of quotient vertices `quotientvertices`. -The result can alias the partitioning stored in `g`, so do not modify it and do -not use it after mutating `g`. Rely only on it being an `AbstractVector` of -vertices. +The result can alias the partitioning stored in `g`, so do not modify it. """ function Graphs.vertices(g::AbstractGraph, quotientvertex::QuotientVertex) qv = parent(quotientvertex) diff --git a/src/PartitionedGraphs/quotientview.jl b/src/PartitionedGraphs/quotientview.jl index 30a3506b..1f77e4ae 100644 --- a/src/PartitionedGraphs/quotientview.jl +++ b/src/PartitionedGraphs/quotientview.jl @@ -1,5 +1,5 @@ using ..NamedGraphs: NamedGraph, directed_graph_type, encoded_graph_type, - induced_subgraph_from_vertices, similar_type, undirected_graph_type + induced_subgraph_from_vertices, parent_graph_type, similar_type, undirected_graph_type using Graphs: AbstractGraph, edges, has_edge, rem_edge!, rem_vertex!, vertices """ @@ -54,8 +54,7 @@ struct QuotientView{V, G <: AbstractGraph} <: AbstractNamedGraph{V} end Base.parent(qg::QuotientView) = qg.graph -parent_graph_type(g::AbstractGraph) = parent_graph_type(typeof(g)) -parent_graph_type(::Type{<:QuotientView{V, G}}) where {V, G} = G +NamedGraphs.parent_graph_type(::Type{<:QuotientView{V, G}}) where {V, G} = G Base.copy(qv::QuotientView) = copy(quotient_graph(parent(qv))) @@ -90,11 +89,11 @@ function Graphs.rem_edge!(qg::QuotientView, e) end NamedGraphs.encoded_graph(g::QuotientView) = NamedGraphs.encoded_graph(copy(g)) -function NamedGraphs.encode_vertex(g::QuotientView, vertex) - return NamedGraphs.encode_vertex(copy(g), vertex) +function NamedGraphs.encoded_vertex(g::QuotientView, vertex) + return NamedGraphs.encoded_vertex(copy(g), vertex) end -function NamedGraphs.decode_vertex(g::QuotientView, code::Integer) - return NamedGraphs.decode_vertex(copy(g), code) +function NamedGraphs.decoded_vertex(g::QuotientView, code::Integer) + return NamedGraphs.decoded_vertex(copy(g), code) end function NamedGraphs.similar_type(type::Type{<:QuotientView}) diff --git a/src/abstractnamedgraph.jl b/src/abstractnamedgraph.jl index dbe4748c..53a2a96f 100644 --- a/src/abstractnamedgraph.jl +++ b/src/abstractnamedgraph.jl @@ -12,7 +12,7 @@ using SimpleTraits: SimpleTraits, @traitfn, Not Abstract type for graphs whose vertices are names of type `V` rather than contiguous integers. Subtypes implement the Graphs.jl interface in terms of a graph on integer vertex codes through the minimal interface -[`encoded_graph`](@ref), [`encode_vertex`](@ref), and [`decode_vertex`](@ref). +[`encoded_graph`](@ref), [`encoded_vertex`](@ref), and [`decoded_vertex`](@ref). """ abstract type AbstractNamedGraph{V} <: AbstractGraph{V} end @@ -21,11 +21,11 @@ abstract type AbstractNamedGraph{V} <: AbstractGraph{V} end # """ - encode_vertex(graph::AbstractNamedGraph, vertex) -> Int + encoded_vertex(graph::AbstractNamedGraph, vertex) -> Int -The code of `vertex` in `graph`, i.e. the corresponding vertex of -[`encoded_graph(graph)`](@ref encoded_graph). `encode_vertex(graph, ·)` and -[`decode_vertex(graph, ·)`](@ref decode_vertex) are inverse bijections between +The vertex of [`encoded_graph(graph)`](@ref encoded_graph) corresponding to +`vertex`, i.e. its code in `graph`. `encoded_vertex(graph, ·)` and +[`decoded_vertex(graph, ·)`](@ref decoded_vertex) are inverse bijections between `vertices(graph)` and `Base.OneTo(nv(graph))`. Codes are not stable across mutation: adding or removing vertices may reassign @@ -36,51 +36,51 @@ the codes of other vertices. ```jldoctest julia> using Graphs: path_graph -julia> using NamedGraphs: NamedGraph, decode_vertex, encode_vertex +julia> using NamedGraphs: NamedGraph, decoded_vertex, encoded_vertex julia> g = NamedGraph(path_graph(3), ["a", "b", "c"]); -julia> encode_vertex(g, "b") +julia> encoded_vertex(g, "b") 2 -julia> decode_vertex(g, 2) +julia> decoded_vertex(g, 2) "b" ``` """ -encode_vertex(graph::AbstractNamedGraph, vertex) = not_implemented() -encode_vertex(graph::AbstractSimpleGraph, vertex) = vertex +encoded_vertex(graph::AbstractNamedGraph, vertex) = not_implemented() +encoded_vertex(graph::AbstractSimpleGraph, vertex) = vertex """ - decode_vertex(graph::AbstractNamedGraph, code::Integer) + decoded_vertex(graph::AbstractNamedGraph, code::Integer) The vertex of `graph` whose code is `code`, i.e. the vertex corresponding to the vertex `code` of [`encoded_graph(graph)`](@ref encoded_graph). Inverse of -[`encode_vertex`](@ref). +[`encoded_vertex`](@ref). # Examples ```jldoctest julia> using Graphs: nv, path_graph -julia> using NamedGraphs: NamedGraph, decode_vertex +julia> using NamedGraphs: NamedGraph, decoded_vertex julia> g = NamedGraph(path_graph(3), ["a", "b", "c"]); -julia> [decode_vertex(g, c) for c in 1:nv(g)] +julia> [decoded_vertex(g, c) for c in 1:nv(g)] 3-element Vector{String}: "a" "b" "c" ``` """ -decode_vertex(graph::AbstractNamedGraph, code::Integer) = not_implemented() -decode_vertex(graph::AbstractSimpleGraph, code::Integer) = code +decoded_vertex(graph::AbstractNamedGraph, code::Integer) = not_implemented() +decoded_vertex(graph::AbstractSimpleGraph, code::Integer) = code Graphs.rem_vertex!(graph::AbstractNamedGraph, vertex) = not_implemented() Graphs.add_vertex!(graph::AbstractNamedGraph, vertex) = not_implemented() function rename_vertices(f::Function, graph::AbstractNamedGraph) - new_vertices = map(c -> f(decode_vertex(graph, c)), vertices(encoded_graph(graph))) + new_vertices = map(c -> f(decoded_vertex(graph, c)), vertices(encoded_graph(graph))) return namedgraph(copy(encoded_graph(graph)), new_vertices) end @@ -93,7 +93,7 @@ end The graph in coded form: a graph with the same topology as `graph` whose vertices are the codes `1:nv(graph)` of the vertices of `graph`, i.e. it has -the edge `encode_vertex(graph, u) => encode_vertex(graph, v)` if and only if +the edge `encoded_vertex(graph, u) => encoded_vertex(graph, v)` if and only if `graph` has the edge `u => v`. May be a stored field or a view of `graph`; mutate the graph only through @@ -104,7 +104,7 @@ May be a stored field or a view of `graph`; mutate the graph only through ```jldoctest julia> using Graphs: edges, has_edge, path_graph, vertices -julia> using NamedGraphs: NamedGraph, encode_vertex, encoded_graph +julia> using NamedGraphs: NamedGraph, encoded_graph, encoded_vertex julia> g = NamedGraph(path_graph(3), ["a", "b", "c"]); @@ -119,7 +119,7 @@ julia> collect(edges(cg)) Edge 1 => 2 Edge 2 => 3 -julia> encode_vertex(g, "a"), encode_vertex(g, "b") +julia> encoded_vertex(g, "a"), encoded_vertex(g, "b") (1, 2) julia> has_edge(cg, 1, 2) @@ -141,8 +141,8 @@ The vertices iterate in insertion order: they appear in the order they were added to the graph, removing a vertex does not reorder the rest, and added vertices appear at the end. The iteration order therefore does not in general match the vertex codes after removals, since codes are reassigned. Use -`decode_vertex` for the vertices in code order, for example -`map(c -> decode_vertex(graph, c), 1:nv(graph))`. +`decoded_vertex` for the vertices in code order, for example +`map(c -> decoded_vertex(graph, c), 1:nv(graph))`. The output is a live read-only view of the graph: do not mutate it directly, and do not rely on it (or containers sharing its state) across mutations of @@ -156,7 +156,7 @@ two orders come apart: ```jldoctest julia> using Graphs: nv, path_graph, rem_vertex!, vertices -julia> using NamedGraphs: NamedGraph, decode_vertex +julia> using NamedGraphs: NamedGraph, decoded_vertex julia> g = NamedGraph(path_graph(4), ["v1", "v2", "v3", "v4"]); @@ -168,7 +168,7 @@ julia> collect(vertices(g)) "v3" "v4" -julia> [decode_vertex(g, c) for c in 1:nv(g)] +julia> [decoded_vertex(g, c) for c in 1:nv(g)] 3-element Vector{String}: "v1" "v4" @@ -179,7 +179,7 @@ Graphs.vertices(graph::AbstractNamedGraph) = NamedVerticesView(graph) # TODO: Is this a good definition? Maybe make it generic to any graph? function permute_vertices(graph::AbstractNamedGraph, permutation) - return subgraph(graph, map(c -> decode_vertex(graph, c), permutation)) + return subgraph(graph, map(c -> decoded_vertex(graph, c), permutation)) end Graphs.edgetype(graph::AbstractNamedGraph) = edgetype(typeof(graph)) @@ -192,7 +192,7 @@ end convert_vertextype(::Type{V}, g::AbstractNamedGraph{V}) where {V} = g function convert_vertextype(vertextype::Type, graph::AbstractNamedGraph) - new_vertices = map(c -> decode_vertex(graph, c), vertices(encoded_graph(graph))) + new_vertices = map(c -> decoded_vertex(graph, c), vertices(encoded_graph(graph))) return namedgraph(copy(encoded_graph(graph)), convert(Vector{vertextype}, new_vertices)) end @@ -224,44 +224,44 @@ Base.zero(G::Type{<:AbstractNamedGraph}) = G() Base.eltype(graph::AbstractNamedGraph) = eltype(vertices(graph)) """ - encode_edge(graph::AbstractNamedGraph, edge) -> AbstractEdge{Int} + encoded_edge(graph::AbstractNamedGraph, edge) -> AbstractEdge{Int} The edge of [`encoded_graph(graph)`](@ref encoded_graph) corresponding to `edge`, i.e. the edge between the codes of the vertices of `edge`. -Inverse of [`decode_edge`](@ref). +Inverse of [`decoded_edge`](@ref). # Examples ```jldoctest julia> using Graphs: path_graph -julia> using NamedGraphs: NamedEdge, NamedGraph, decode_edge, encode_edge +julia> using NamedGraphs: NamedEdge, NamedGraph, decoded_edge, encoded_edge julia> g = NamedGraph(path_graph(3), ["a", "b", "c"]); -julia> ce = encode_edge(g, NamedEdge("a" => "b")) +julia> ce = encoded_edge(g, NamedEdge("a" => "b")) Edge 1 => 2 -julia> decode_edge(g, ce) +julia> decoded_edge(g, ce) "a" => "b" ``` """ -function encode_edge(graph::AbstractNamedGraph, edge::AbstractEdge) +function encoded_edge(graph::AbstractNamedGraph, edge::AbstractEdge) return edgetype(encoded_graph(graph))( - encode_vertex(graph, src(edge)), encode_vertex(graph, dst(edge)) + encoded_vertex(graph, src(edge)), encoded_vertex(graph, dst(edge)) ) end """ - decode_edge(graph::AbstractNamedGraph, encoded_edge) + decoded_edge(graph::AbstractNamedGraph, encoded_edge) The edge of `graph` corresponding to the edge `encoded_edge` of -[`encoded_graph(graph)`](@ref encoded_graph). Inverse of [`encode_edge`](@ref). +[`encoded_graph(graph)`](@ref encoded_graph). Inverse of [`encoded_edge`](@ref). """ -function decode_edge(graph::AbstractNamedGraph, encoded_edge::AbstractEdge) +function decoded_edge(graph::AbstractNamedGraph, encoded_edge::AbstractEdge) return edgetype(graph)( - decode_vertex(graph, src(encoded_edge)), - decode_vertex(graph, dst(encoded_edge)) + decoded_vertex(graph, src(encoded_edge)), + decoded_vertex(graph, dst(encoded_edge)) ) end @@ -307,8 +307,8 @@ for f in [:outneighbors, :inneighbors, :all_neighbors, :neighbors] @eval begin function $f_namedgraph(graph::AbstractNamedGraph, vertex) encoded_neighbors = - Graphs.$f(encoded_graph(graph), encode_vertex(graph, vertex)) - return map(c -> decode_vertex(graph, c), encoded_neighbors) + Graphs.$f(encoded_graph(graph), encoded_vertex(graph, vertex)) + return map(c -> decoded_vertex(graph, c), encoded_neighbors) end Graphs.$f(graph::AbstractNamedGraph, vertex) = $f_namedgraph(graph, vertex) @@ -326,10 +326,6 @@ are the out-neighbors, following the Graphs.jl convention. `inneighbors`, `outneighbors`, and `all_neighbors` select the other directions and behave the same way in every other respect, including the caveat below. -The output may be a view into the graph's own data, so treat it as read-only and -discard it before mutating `graph`. Only its `AbstractVector` interface is -promised; the concrete type may change. - # Examples ```jldoctest @@ -412,9 +408,9 @@ function neighborhood_namedgraph( ) encoded_distmx = encode_dist_matrix(graph, distmx) encoded_neighborhood = neighborhood( - encoded_graph(graph), encode_vertex(graph, vertex), d, encoded_distmx; dir + encoded_graph(graph), encoded_vertex(graph, vertex), d, encoded_distmx; dir ) - return [decode_vertex(graph, c) for c in encoded_neighborhood] + return [decoded_vertex(graph, c) for c in encoded_neighborhood] end function Graphs.neighborhood( @@ -441,10 +437,10 @@ end function neighborhood_dists_namedgraph(graph::AbstractNamedGraph, vertex, d, distmx; dir) encoded_distmx = encode_dist_matrix(graph, distmx) encoded_vertices_and_dists = neighborhood_dists( - encoded_graph(graph), encode_vertex(graph, vertex), d, encoded_distmx; dir + encoded_graph(graph), encoded_vertex(graph, vertex), d, encoded_distmx; dir ) return [ - (decode_vertex(graph, c), dist) for (c, dist) in encoded_vertices_and_dists + (decoded_vertex(graph, c), dist) for (c, dist) in encoded_vertices_and_dists ] end @@ -472,7 +468,7 @@ end function mincut_namedgraph(graph::AbstractNamedGraph, distmx) encoded_distmx = encode_dist_matrix(graph, distmx) encoded_parity, bestcut = Graphs.mincut(encoded_graph(graph), encoded_distmx) - graph_vertices = map(c -> decode_vertex(graph, c), vertices(encoded_graph(graph))) + graph_vertices = map(c -> decoded_vertex(graph, c), vertices(encoded_graph(graph))) return Dictionary(graph_vertices, encoded_parity), bestcut end @@ -500,7 +496,7 @@ function partition_vertices( # of the vertices in each partition). # return Dictionary(vertices(g), partitions) return map(vertex_partitions) do vertex_partition - return map(c -> decode_vertex(graph, c), vertex_partition) + return map(c -> decoded_vertex(graph, c), vertex_partition) end end @@ -514,8 +510,8 @@ function a_star_namedgraph( ) encoded_shortest_path = a_star( encoded_graph(graph), - encode_vertex(graph, source), - encode_vertex(graph, destination), + encoded_vertex(graph, source), + encoded_vertex(graph, destination), encode_dist_matrix(graph, distmx), heuristic, # The encoded graph has integer vertices, so the inner search returns @@ -523,7 +519,7 @@ function a_star_namedgraph( SimpleEdge ) return map(encoded_shortest_path) do encoded_edge - edge = decode_edge(graph, encoded_edge) + edge = decoded_edge(graph, encoded_edge) # Build from the endpoints rather than converting, so edge types other # than `edgetype(graph)` do not need a constructor taking a `NamedEdge`. return edgetype_to_return(src(edge), dst(edge)) @@ -562,9 +558,9 @@ end function spfa_shortest_paths_namedgraph(graph::AbstractNamedGraph, vertex, distmx) encoded_distmx = encode_dist_matrix(graph, distmx) encoded_shortest_paths = spfa_shortest_paths( - encoded_graph(graph), encode_vertex(graph, vertex), encoded_distmx + encoded_graph(graph), encoded_vertex(graph, vertex), encoded_distmx ) - graph_vertices = map(c -> decode_vertex(graph, c), vertices(encoded_graph(graph))) + graph_vertices = map(c -> decoded_vertex(graph, c), vertices(encoded_graph(graph))) return Dictionary(graph_vertices, encoded_shortest_paths) end @@ -588,26 +584,26 @@ function Graphs.boruvka_mst( g::AbstractNamedGraph, distmx::AbstractMatrix{<:Real} = weights(g); minimize = true ) encoded_mst, weights = boruvka_mst(encoded_graph(g), distmx; minimize) - return map(e -> decode_edge(g, e), encoded_mst), weights + return map(e -> decoded_edge(g, e), encoded_mst), weights end function Graphs.kruskal_mst( g::AbstractNamedGraph, distmx::AbstractMatrix{<:Real} = weights(g); minimize = true ) encoded_mst = kruskal_mst(encoded_graph(g), distmx; minimize) - return map(e -> decode_edge(g, e), encoded_mst) + return map(e -> decoded_edge(g, e), encoded_mst) end function Graphs.prim_mst(g::AbstractNamedGraph, distmx::AbstractMatrix{<:Real} = weights(g)) encoded_mst = prim_mst(encoded_graph(g), distmx) - return map(e -> decode_edge(g, e), encoded_mst) + return map(e -> decoded_edge(g, e), encoded_mst) end function Graphs.add_edge!(graph::AbstractNamedGraph, edge) e = edgetype(graph)(edge) has_vertex(graph, src(e)) || return false has_vertex(graph, dst(e)) || return false - return add_edge!(encoded_graph(graph), encode_edge(graph, e)) + return add_edge!(encoded_graph(graph), encoded_edge(graph, e)) end Graphs.add_edge!(g::AbstractNamedGraph, src, dst) = add_edge!(g, edgetype(g)(src, dst)) @@ -922,7 +918,7 @@ function Graphs.rem_edge!(graph::AbstractNamedGraph, edge) e = edgetype(graph)(edge) has_vertex(graph, src(e)) || return false has_vertex(graph, dst(e)) || return false - return rem_edge!(encoded_graph(graph), encode_edge(graph, e)) + return rem_edge!(encoded_graph(graph), encoded_edge(graph, e)) end """ @@ -1094,8 +1090,8 @@ function Graphs.has_edge(graph::AbstractNamedGraph, edge::AbstractNamedEdge) has_vertex(graph, src(edge)) || return false has_vertex(graph, dst(edge)) || return false return has_edge( - encoded_graph(graph), encode_vertex(graph, src(edge)), - encode_vertex(graph, dst(edge)) + encoded_graph(graph), encoded_vertex(graph, src(edge)), + encoded_vertex(graph, dst(edge)) ) end @@ -1111,9 +1107,9 @@ function has_path_namedgraph( ) return has_path( encoded_graph(graph), - encode_vertex(graph, source), - encode_vertex(graph, destination); - exclude_vertices = map(v -> encode_vertex(graph, v), exclude_vertices) + encoded_vertex(graph, source), + encoded_vertex(graph, destination); + exclude_vertices = map(v -> encoded_vertex(graph, v), exclude_vertices) ) end @@ -1167,7 +1163,7 @@ Graphs.is_connected(graph::AbstractNamedGraph) = is_connected(encoded_graph(grap Graphs.is_cyclic(graph::AbstractNamedGraph) = is_cyclic(encoded_graph(graph)) function Base.reverse(graph::AbstractNamedGraph) - new_vertices = map(c -> decode_vertex(graph, c), vertices(encoded_graph(graph))) + new_vertices = map(c -> decoded_vertex(graph, c), vertices(encoded_graph(graph))) return namedgraph(reverse(encoded_graph(graph)), new_vertices) end @@ -1179,8 +1175,8 @@ end function Graphs.blockdiag(graph1::AbstractNamedGraph, graph2::AbstractNamedGraph) new_encoded_graph = blockdiag(encoded_graph(graph1), encoded_graph(graph2)) new_vertices = vcat( - map(c -> decode_vertex(graph1, c), vertices(encoded_graph(graph1))), - map(c -> decode_vertex(graph2, c), vertices(encoded_graph(graph2))) + map(c -> decoded_vertex(graph1, c), vertices(encoded_graph(graph1))), + map(c -> decoded_vertex(graph2, c), vertices(encoded_graph(graph2))) ) @assert allunique(new_vertices) return namedgraph(new_encoded_graph, new_vertices) @@ -1195,7 +1191,7 @@ end function Graphs.connected_components(graph::AbstractNamedGraph) encoded_connected_components = connected_components(encoded_graph(graph)) return map(encoded_connected_components) do encoded_connected_component - return map(c -> decode_vertex(graph, c), encoded_connected_component) + return map(c -> decoded_vertex(graph, c), encoded_connected_component) end end @@ -1252,14 +1248,14 @@ end # vertex in the traversal/spanning tree. function bfs_parents_namedgraph(graph::AbstractNamedGraph, vertex; kwargs...) encoded_bfs_parents = bfs_parents( - encoded_graph(graph), encode_vertex(graph, vertex); kwargs... + encoded_graph(graph), encoded_vertex(graph, vertex); kwargs... ) - graph_vertices = map(c -> decode_vertex(graph, c), vertices(encoded_graph(graph))) + graph_vertices = map(c -> decoded_vertex(graph, c), vertices(encoded_graph(graph))) # Unreachable vertices have parent code 0; map them to themselves like # `dijkstra_shortest_paths` does. parents = map(eachindex(encoded_bfs_parents)) do c p = encoded_bfs_parents[c] - return decode_vertex(graph, iszero(p) ? c : p) + return decoded_vertex(graph, iszero(p) ? c : p) end return Dictionary(graph_vertices, parents) end diff --git a/src/dfs.jl b/src/dfs.jl index bcd3d6c4..e2c06139 100644 --- a/src/dfs.jl +++ b/src/dfs.jl @@ -3,7 +3,7 @@ using Graphs: Graphs, dfs_parents, dfs_tree, topological_sort_by_dfs using SimpleTraits: SimpleTraits, @traitfn, Not @traitfn function Graphs.topological_sort_by_dfs(g::AbstractNamedGraph::IsDirected) - return map(c -> decode_vertex(g, c), topological_sort_by_dfs(encoded_graph(g))) + return map(c -> decoded_vertex(g, c), topological_sort_by_dfs(encoded_graph(g))) end function dfs_tree_namedgraph(graph::AbstractNamedGraph, vertex; kwargs...) @@ -20,14 +20,14 @@ end # vertex in the traversal/spanning tree. function dfs_parents_namedgraph(graph::AbstractNamedGraph, vertex; kwargs...) encoded_dfs_parents = dfs_parents( - encoded_graph(graph), encode_vertex(graph, vertex); kwargs... + encoded_graph(graph), encoded_vertex(graph, vertex); kwargs... ) - graph_vertices = map(c -> decode_vertex(graph, c), vertices(encoded_graph(graph))) + graph_vertices = map(c -> decoded_vertex(graph, c), vertices(encoded_graph(graph))) # Unreachable vertices have parent code 0; map them to themselves like # `dijkstra_shortest_paths` does. parents = map(eachindex(encoded_dfs_parents)) do c p = encoded_dfs_parents[c] - return decode_vertex(graph, iszero(p) ? c : p) + return decoded_vertex(graph, iszero(p) ? c : p) end return Dictionary(graph_vertices, parents) end diff --git a/src/distances_and_capacities.jl b/src/distances_and_capacities.jl index ab91ca09..bcac2dca 100644 --- a/src/distances_and_capacities.jl +++ b/src/distances_and_capacities.jl @@ -35,7 +35,7 @@ function encode_dist_matrix_namedgraph( ) encoded_dist_matrix = spzeros(valtype(dist_matrix), nv(graph), nv(graph)) for e in edges(graph) - encoded_e = encode_edge(graph, e) + encoded_e = encoded_edge(graph, e) encoded_dist_matrix[src(encoded_e), dst(encoded_e)] = getindex_dist_matrix( dist_matrix, src(e), dst(e) ) diff --git a/src/encodedgraphview.jl b/src/encodedgraphview.jl index 10ae457a..59996119 100644 --- a/src/encodedgraphview.jl +++ b/src/encodedgraphview.jl @@ -6,7 +6,7 @@ using Graphs: Graphs, Edge, add_edge!, add_vertex!, edges, has_edge, has_vertex, # Default output of `encoded_graph(graph::AbstractNamedGraph)` for graph types # that are implemented directly, as opposed to as a wrapper around a stored # integer graph, such as NamedGridGraph. -# Assumes `encode_vertex(g.graph, v)` and `decode_vertex(g.graph, c)` are +# Assumes `encoded_vertex(g.graph, v)` and `decoded_vertex(g.graph, c)` are # implemented for the wrapped graph. struct EncodedGraphView{G <: AbstractGraph} <: AbstractGraph{Int} graph::G @@ -17,33 +17,33 @@ Graphs.ne(g::EncodedGraphView) = ne(g.graph) Graphs.vertices(g::EncodedGraphView) = Base.OneTo(nv(g)) Graphs.has_vertex(g::EncodedGraphView, v::Int) = v ∈ vertices(g) function Graphs.add_vertex!(g::EncodedGraphView, v::Int) - return add_vertex!(g.graph, decode_vertex(g.graph, v)) + return add_vertex!(g.graph, decoded_vertex(g.graph, v)) end function Graphs.rem_vertex!(g::EncodedGraphView, v::Int) - return rem_vertex!(g.graph, decode_vertex(g.graph, v)) + return rem_vertex!(g.graph, decoded_vertex(g.graph, v)) end function Graphs.has_edge(g::EncodedGraphView, e::Edge) - return has_edge(g.graph, decode_edge(g.graph, e)) + return has_edge(g.graph, decoded_edge(g.graph, e)) end function Graphs.add_edge!(g::EncodedGraphView, e::Edge) - return add_edge!(g.graph, decode_edge(g.graph, e)) + return add_edge!(g.graph, decoded_edge(g.graph, e)) end function Graphs.rem_edge!(g::EncodedGraphView, e::Edge) - return rem_edge!(g.graph, decode_edge(g.graph, e)) + return rem_edge!(g.graph, decoded_edge(g.graph, e)) end Graphs.edgetype(g::EncodedGraphView) = Edge{Int} function Graphs.edges(g::EncodedGraphView) return Iterators.map(edges(g.graph)) do e - return encode_edge(g.graph, e) + return encoded_edge(g.graph, e) end end function Graphs.outneighbors(g::EncodedGraphView, v::Int) - return map(outneighbors(g.graph, decode_vertex(g.graph, v))) do v′ - return encode_vertex(g.graph, v′) + return map(outneighbors(g.graph, decoded_vertex(g.graph, v))) do v′ + return encoded_vertex(g.graph, v′) end end function Graphs.inneighbors(g::EncodedGraphView, v::Int) - return map(inneighbors(g.graph, decode_vertex(g.graph, v))) do v′ - return encode_vertex(g.graph, v′) + return map(inneighbors(g.graph, decoded_vertex(g.graph, v))) do v′ + return encoded_vertex(g.graph, v′) end end diff --git a/src/graphsextensions/abstractgraph.jl b/src/graphsextensions/abstractgraph.jl index 135867c3..e28e9296 100644 --- a/src/graphsextensions/abstractgraph.jl +++ b/src/graphsextensions/abstractgraph.jl @@ -274,9 +274,7 @@ end in_incident_edges(graph::AbstractGraph, vertex) Edges of `graph` pointing into `vertex`, one for each in-neighbor. Equivalent to -[`incident_edges`](@ref) with `dir = :in`, and carrying the same caveats: the -output may be a read-only view into `graph`, is invalidated by mutating `graph`, -and is only promised to be an `AbstractVector`. +[`incident_edges`](@ref) with `dir = :in`. """ function in_incident_edges(graph::AbstractGraph, vertex) return [ @@ -302,10 +300,6 @@ Edges incident to the vertex `vertex`. For undirected graphs, returns all incident edges. -The output may be a view into the graph's own data, so treat it as read-only and -discard it before mutating `graph`. Only its `AbstractVector` interface is -promised; the concrete type may change. - # Examples ```jldoctest diff --git a/src/graphsextensions/abstracttrees.jl b/src/graphsextensions/abstracttrees.jl index 7d93adc1..a72e8f01 100644 --- a/src/graphsextensions/abstracttrees.jl +++ b/src/graphsextensions/abstracttrees.jl @@ -1,3 +1,5 @@ +parent_graph_type(graph::AbstractGraph) = parent_graph_type(typeof(graph)) + # AbstractTreeGraph # Tree view of a graph. abstract type AbstractTreeGraph{V} <: AbstractGraph{V} end diff --git a/src/graphsextensions/boundary.jl b/src/graphsextensions/boundary.jl index bc08ec30..a6262f17 100644 --- a/src/graphsextensions/boundary.jl +++ b/src/graphsextensions/boundary.jl @@ -8,10 +8,6 @@ The edges of `graph` with one endpoint in `subgraph_vertices` and the other outside of it. `dir` orients the returned edges as in [`incident_edges`](@ref), so by default each one points from the vertex inside to the vertex outside. -The result may be a view into `graph` rather than freshly allocated storage: -do not modify it, and do not use it after mutating `graph`. Its concrete -`AbstractVector` type is not part of the interface. - # Examples ```jldoctest diff --git a/src/indicesviews.jl b/src/indicesviews.jl index 7a9a62b6..4c7b0e70 100644 --- a/src/indicesviews.jl +++ b/src/indicesviews.jl @@ -19,7 +19,7 @@ function Dictionaries.iterate(vs::NamedVerticesView, state...) next = iterate(Base.OneTo(nv(vs.graph)), state...) isnothing(next) && return nothing code, new_state = next - return decode_vertex(vs.graph, code), new_state + return decoded_vertex(vs.graph, code), new_state end Base.in(vertex::V, vs::NamedVerticesView{V}) where {V} = has_vertex(vs.graph, vertex) @@ -34,9 +34,9 @@ function Dictionaries.iteratetoken_reverse(vs::NamedVerticesView, state...) end function Dictionaries.gettoken(vs::NamedVerticesView, vertex) has_vertex(vs.graph, vertex) || return (false, 0) - return (true, encode_vertex(vs.graph, vertex)) + return (true, encoded_vertex(vs.graph, vertex)) end -Dictionaries.gettokenvalue(vs::NamedVerticesView, token) = decode_vertex(vs.graph, token) +Dictionaries.gettokenvalue(vs::NamedVerticesView, token) = decoded_vertex(vs.graph, token) # Lazy iterator over the edges of a named graph, in the style of # `Graphs.SimpleGraphs.SimpleEdgeIter`: iterates by decoding the edges of @@ -55,7 +55,7 @@ function Base.iterate(es::NamedEdgeIter, state...) next = iterate(edges(encoded_graph(es.graph)), state...) isnothing(next) && return nothing encoded_edge, new_state = next - return decode_edge(es.graph, encoded_edge), new_state + return decoded_edge(es.graph, encoded_edge), new_state end Base.in(edge, es::NamedEdgeIter) = has_edge(es.graph, edge) diff --git a/src/namedgraph.jl b/src/namedgraph.jl index 2469282b..3db84746 100644 --- a/src/namedgraph.jl +++ b/src/namedgraph.jl @@ -38,7 +38,7 @@ end for T in (:NamedGraph, :NamedDiGraph) @eval begin encoded_graph(graph::$T) = graph.encoded_graph - function encode_vertex(graph::$T, vertex) + function encoded_vertex(graph::$T, vertex) # A single lookup, so that checking membership costs nothing when the # vertex is there. Indexing directly would report a missing vertex as a # `convert` error from inside Dictionaries.jl. @@ -47,7 +47,7 @@ for T in (:NamedGraph, :NamedDiGraph) throw(ArgumentError("$(repr(vertex)) is not a vertex of the graph.")) return code end - decode_vertex(graph::$T, code::Integer) = graph.decoded_vertices[code] + decoded_vertex(graph::$T, code::Integer) = graph.decoded_vertices[code] Graphs.vertices(graph::$T) = keys(graph.encoded_vertices) @@ -67,7 +67,7 @@ for T in (:NamedGraph, :NamedDiGraph) if vertex ∉ vertices(graph) return false end - code = encode_vertex(graph, vertex) + code = encoded_vertex(graph, vertex) # `rem_vertex!` on an `AbstractSimpleGraph` moves the last vertex into # the removed slot, so mirror that reassignment in the vertex-code maps. rem_vertex!(encoded_graph(graph), code) diff --git a/src/namedgridgraph.jl b/src/namedgridgraph.jl index 08685684..0d708137 100644 --- a/src/namedgridgraph.jl +++ b/src/namedgridgraph.jl @@ -115,14 +115,14 @@ function NamedGridGraph(grid_size::NTuple{N, Int}, ishypertorus::Bool = false) w end # Minimal interface functions # `encoded_graph` uses the generic `EncodedGraphView` fallback. -function encode_vertex(g::NamedGridGraph, vertex) +function encoded_vertex(g::NamedGridGraph, vertex) # Membership is just a bounds check here, unlike the dictionary lookup in # `NamedGraph`, so checking it up front costs nothing worth avoiding. has_vertex(g, vertex) || throw(ArgumentError("$(repr(vertex)) is not a vertex of the graph.")) return LinearIndices(grid_size(g))[CartesianIndex(vertex)] end -function decode_vertex(g::NamedGridGraph, code::Integer) +function decoded_vertex(g::NamedGridGraph, code::Integer) return Tuple(CartesianIndices(grid_size(g))[code]) end ishypertorus(g::NamedGridGraph{<:Any, istorus}) where {istorus} = istorus diff --git a/src/shortestpaths.jl b/src/shortestpaths.jl index 8877978d..033b9032 100644 --- a/src/shortestpaths.jl +++ b/src/shortestpaths.jl @@ -29,7 +29,7 @@ function encoded_path_state_to_path_state( pᵢ = encoded_path_state.parents[i] return iszero(pᵢ) ? i : pᵢ end - decode(c) = decode_vertex(graph, c) + decode(c) = decoded_vertex(graph, c) # Keys in code order, to align with the code-indexed path state. graph_vertices = map(decode, vertices(encoded_graph(graph))) return NamedDijkstraState( @@ -52,7 +52,7 @@ function dijkstra_shortest_paths_namedgraph( ) encoded_path_state = dijkstra_shortest_paths( encoded_graph(graph), - map(v -> encode_vertex(graph, v), srcs), + map(v -> encoded_vertex(graph, v), srcs), encode_dist_matrix(graph, distmx); allpaths, trackvertices, diff --git a/src/steiner_tree.jl b/src/steiner_tree.jl index 86b5451d..9e5fa907 100644 --- a/src/steiner_tree.jl +++ b/src/steiner_tree.jl @@ -6,7 +6,7 @@ function steiner_tree_namedgraph( ) encoded_tree = steiner_tree( encoded_graph(g), - map(v -> encode_vertex(g, v), term_vert), + map(v -> encoded_vertex(g, v), term_vert), encode_dist_matrix(g, distmx) ) @@ -16,7 +16,7 @@ function steiner_tree_namedgraph( # Get only those vertices that appear in an edge for edge in edges(encoded_tree) - tree_edge = decode_edge(g, edge) + tree_edge = decoded_edge(g, edge) push!(tree_edges, tree_edge) push!(featured_vertices, src(tree_edge)) push!(featured_vertices, dst(tree_edge)) diff --git a/test/test_codes.jl b/test/test_codes.jl index fd103fb0..b3c7ef21 100644 --- a/test/test_codes.jl +++ b/test/test_codes.jl @@ -1,64 +1,64 @@ using Dictionaries: AbstractDictionary, AbstractIndices, Dictionary using Graphs: AbstractEdgeIter, add_edge!, add_vertex!, eccentricity, edges, has_edge, has_vertex, ne, neighbors, nv, path_graph, rem_vertex!, vertices, weights -using NamedGraphs: NamedGraphs, NamedEdge, NamedGraph, NamedGridGraph, decode_edge, - decode_vertex, encode_edge, encode_vertex, encoded_graph +using NamedGraphs: NamedGraphs, NamedEdge, NamedGraph, NamedGridGraph, decoded_edge, + decoded_vertex, encoded_edge, encoded_graph, encoded_vertex using Test: @test, @test_throws, @testset @testset "Vertex codes" begin - @testset "encode_vertex and decode_vertex are inverse bijections" begin + @testset "encoded_vertex and decoded_vertex are inverse bijections" begin g = NamedGraph(path_graph(4), ["a", "b", "c", "d"]) - @test all(c -> encode_vertex(g, decode_vertex(g, c)) == c, 1:nv(g)) - @test all(v -> decode_vertex(g, encode_vertex(g, v)) == v, vertices(g)) - @test sort(map(v -> encode_vertex(g, v), collect(vertices(g)))) == 1:nv(g) + @test all(c -> encoded_vertex(g, decoded_vertex(g, c)) == c, 1:nv(g)) + @test all(v -> decoded_vertex(g, encoded_vertex(g, v)) == v, vertices(g)) + @test sort(map(v -> encoded_vertex(g, v), collect(vertices(g)))) == 1:nv(g) end - @testset "encode_vertex reports values that aren't vertices" begin + @testset "encoded_vertex reports values that aren't vertices" begin g = NamedGraph(path_graph(3), ["a", "b", "c"]) - @test_throws ArgumentError encode_vertex(g, "z") - @test_throws "\"z\" is not a vertex of the graph" encode_vertex(g, "z") + @test_throws ArgumentError encoded_vertex(g, "z") + @test_throws "\"z\" is not a vertex of the graph" encoded_vertex(g, "z") # A value of the wrong type entirely, which is the case that reaches - # `encode_vertex` when an argument Graphs.jl would reinterpret by type stays + # `encoded_vertex` when an argument Graphs.jl would reinterpret by type stays # a vertex here. gi = NamedGraph(path_graph(3)) - @test_throws "(1, 1) is not a vertex of the graph" encode_vertex(gi, (1, 1)) - @test_throws "\"z\" is not a vertex of the graph" encode_vertex(gi, "z") - @test_throws ArgumentError encode_vertex(gi, weights(gi)) + @test_throws "(1, 1) is not a vertex of the graph" encoded_vertex(gi, (1, 1)) + @test_throws "\"z\" is not a vertex of the graph" encoded_vertex(gi, "z") + @test_throws ArgumentError encoded_vertex(gi, weights(gi)) # `weights(g)` sits in the vertex position of `eccentricity`, so this has to # name the offending value rather than fail converting it to a vertex. @test_throws "is not a vertex of the graph" eccentricity(gi, weights(gi)) # The same report from a graph type that computes codes rather than storing # them, including for a coordinate that is outside the grid. gg = NamedGridGraph((2, 2)) - @test_throws ArgumentError encode_vertex(gg, "z") - @test_throws "\"z\" is not a vertex of the graph" encode_vertex(gg, "z") - @test_throws ArgumentError encode_vertex(gg, (3, 1)) - @test_throws "(3, 1) is not a vertex of the graph" encode_vertex(gg, (3, 1)) + @test_throws ArgumentError encoded_vertex(gg, "z") + @test_throws "\"z\" is not a vertex of the graph" encoded_vertex(gg, "z") + @test_throws ArgumentError encoded_vertex(gg, (3, 1)) + @test_throws "(3, 1) is not a vertex of the graph" encoded_vertex(gg, (3, 1)) end @testset "Edge codes" begin g = NamedGraph(path_graph(3), ["a", "b", "c"]) e = NamedEdge("a" => "b") - ce = encode_edge(g, e) + ce = encoded_edge(g, e) @test ce == first(edges(encoded_graph(g))) - @test decode_edge(g, ce) == e + @test decoded_edge(g, ce) == e end @testset "encoded_graph matches the graph topology" begin g = NamedGraph(path_graph(4), ["a", "b", "c", "d"]) cg = encoded_graph(g) @test nv(cg) == nv(g) @test ne(cg) == ne(g) - @test all(e -> has_edge(g, decode_edge(g, e)), edges(cg)) + @test all(e -> has_edge(g, decoded_edge(g, e)), edges(cg)) end @testset "Codes are reassigned by rem_vertex!" begin g = NamedGraph(path_graph(4), ["a", "b", "c", "d"]) rem_vertex!(g, "b") @test !has_vertex(g, "b") @test nv(g) == 3 - @test all(c -> encode_vertex(g, decode_vertex(g, c)) == c, 1:nv(g)) + @test all(c -> encoded_vertex(g, decoded_vertex(g, c)) == c, 1:nv(g)) @test issetequal(neighbors(g, "c"), ["d"]) @test isempty(neighbors(g, "a")) add_vertex!(g, "e") - @test encode_vertex(g, "e") == 4 - @test decode_vertex(g, 4) == "e" + @test encoded_vertex(g, "e") == 4 + @test decoded_vertex(g, 4) == "e" add_edge!(g, "a" => "e") @test has_edge(g, NamedEdge("a" => "e")) end @@ -66,7 +66,7 @@ using Test: @test, @test_throws, @testset g = NamedGraph(path_graph(4), ["v1", "v2", "v3", "v4"]) rem_vertex!(g, "v2") @test collect(vertices(g)) == ["v1", "v3", "v4"] - @test [decode_vertex(g, c) for c in 1:nv(g)] == ["v1", "v4", "v3"] + @test [decoded_vertex(g, c) for c in 1:nv(g)] == ["v1", "v4", "v3"] add_vertex!(g, "v5") @test collect(vertices(g)) == ["v1", "v3", "v4", "v5"] end @@ -98,8 +98,8 @@ using Test: @test, @test_throws, @testset @testset "Graph types without a stored coded graph" begin g = NamedGridGraph((2, 2)) @test encoded_graph(g) isa NamedGraphs.EncodedGraphView - @test encode_vertex(g, (2, 1)) == 2 - @test decode_vertex(g, 3) == (1, 2) + @test encoded_vertex(g, (2, 1)) == 2 + @test decoded_vertex(g, 3) == (1, 2) @test (2, 2) ∈ vertices(g) @test (3, 1) ∉ vertices(g) @test issetequal( diff --git a/test/test_exports.jl b/test/test_exports.jl index 3272bb80..3b9cdca4 100644 --- a/test/test_exports.jl +++ b/test/test_exports.jl @@ -56,11 +56,11 @@ using Test: @test, @testset public_names = if VERSION >= v"1.11.0-DEV.469" [ :PartitionedGraphs, - :decode_edge, - :decode_vertex, - :encode_edge, - :encode_vertex, + :decoded_edge, + :decoded_vertex, + :encoded_edge, :encoded_graph, + :encoded_vertex, :to_graph_index, ] else diff --git a/test/test_namedgraphgenerators.jl b/test/test_namedgraphgenerators.jl index a68311f5..64639ffd 100644 --- a/test/test_namedgraphgenerators.jl +++ b/test/test_namedgraphgenerators.jl @@ -1,6 +1,6 @@ using Graphs: a_star, add_edge!, add_vertex!, degree, dst, edges, edgetype, has_edge, has_vertex, is_directed, ne, neighbors, nv, rem_edge!, rem_vertex!, src, vertices -using NamedGraphs: NamedEdge, NamedGridGraph, decode_vertex, encode_vertex, grid_ndims, +using NamedGraphs: NamedEdge, NamedGridGraph, decoded_vertex, encoded_vertex, grid_ndims, grid_size, is_cycle_graph, is_directed_grid, ishypertorus, named_binary_tree, named_cycle_graph, named_grid, named_hexagonal_lattice_graph, named_triangular_lattice_graph, vertextype @@ -107,11 +107,11 @@ end @test !has_vertex(g, 1) @test !has_vertex(g, nothing) - @test all(v -> decode_vertex(g, encode_vertex(g, v)) == v, vertices(g)) - @test all(c -> encode_vertex(g, decode_vertex(g, c)) == c, 1:nv(g)) - @test encode_vertex(g, (Int32(2), Int32(3))) == encode_vertex(g, (2, 3)) - @test_throws ArgumentError encode_vertex(g, "abc") - @test_throws ArgumentError encode_vertex(g, (5, 2)) + @test all(v -> decoded_vertex(g, encoded_vertex(g, v)) == v, vertices(g)) + @test all(c -> encoded_vertex(g, decoded_vertex(g, c)) == c, 1:nv(g)) + @test encoded_vertex(g, (Int32(2), Int32(3))) == encoded_vertex(g, (2, 3)) + @test_throws ArgumentError encoded_vertex(g, "abc") + @test_throws ArgumentError encoded_vertex(g, (5, 2)) @test_throws ArgumentError neighbors(g, "abc") @test_throws ArgumentError neighbors(g, (5, 2))