Skip to content

Commit 1fd424e

Browse files
committed
Improve consistency of graph and dataview indexing with Indices; fix some bugs
1 parent 14257ac commit 1fd424e

File tree

3 files changed

+55
-45
lines changed

3 files changed

+55
-45
lines changed

src/dataview.jl

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function Base.setindex!(view::VertexOrEdgeDataView{K, V}, data::V, key::K) where
9393
setindex!(view.graph, data, key)
9494
return view
9595
end
96-
function Base.setindex!(view::EdgeDataView{<:Any, V}, data::V, key::Pair{V, V}) where {V}
96+
function Base.setindex!(view::EdgeDataView{<:Any, V}, data::V, key::Pair) where {V}
9797
setindex!(view, data, to_graph_index(view.graph, key))
9898
return view
9999
end
@@ -125,31 +125,38 @@ end
125125

126126
Base.keys(dvs::DataViewSlice) = dvs.inds
127127

128-
Base.getindex(dvs::DataViewSlice, key) = dvs.view[key]
129-
Base.getindex(view::DataViewSlice{K}, key::K) where {K} = view.view[key]
128+
Base.getindex(view::DataViewSlice, key) = getindex_dataview(view, key)
129+
Base.getindex(view::DataViewSlice{K}, key::K) where {K} = getindex_dataview(view, key)
130+
function getindex_dataview(dvs::DataViewSlice, key)
131+
isassigned(dvs, key) || throw(IndexError("Dictionary does not contain index: $key"))
132+
return dvs.view[key]
133+
end
130134

131-
Base.isassigned(view::DataViewSlice, key) = key in keys(view)
132135
Base.isassigned(view::DataViewSlice{K}, key::K) where {K} = key in keys(view)
136+
Base.isassigned(view::DataViewSlice, key::Pair) = isassigned(view, to_graph_index(view.view.graph, key))
133137

134138
Base.getindex(view::VertexOrEdgeDataView, keys::Indices) = DataViewSlice(view, keys)
135139
function Base.getindex(view::EdgeDataView, keys::Indices{<:Pair})
136140
return DataViewSlice(view, Indices(map(k -> to_graph_index(view.graph, k), collect(keys))))
137141
end
138142

143+
# For method ambiguity
139144
function Base.setindex!(view::DataViewSlice{K, V}, data::V, key::K) where {K, V}
140-
setindex!(view.view, data, key)
141-
return view
145+
return setindex!_dataview(view, data, key)
142146
end
143-
function Base.setindex!(view::DataViewSlice{<:Any, V}, data::V, key::Pair{V, V}) where {V}
144-
setindex!(view, data, to_graph_index(view.view.graph, key))
145-
return view
147+
function Base.setindex!(view::DataViewSlice{<:Any, V}, data::V, key::Pair) where {V}
148+
return setindex!_dataview(view, data, key)
146149
end
147150

148-
Base.axes(view::DataViewSlice) = (Base.OneTo(length(keys(view))),)
151+
function setindex!_dataview(view::DataViewSlice, data, key)
152+
isassigned(view, key) || throw(IndexError("Dictionary does not contain index: $key"))
153+
setindex!(view.view, data, key)
154+
return view
155+
end
149156

150-
function Base.copyto!(dest::DataViewSlice, bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}})
151-
for (i, key) in enumerate(keys(dest))
152-
@inbounds dest[key] = bc[i]
157+
function Base.copyto!(dest::DataViewSlice, bc::Dictionaries.BroadcastedDictionary)
158+
for (key, val) in pairs(bc)
159+
dest[key] = val
153160
end
154161
return dest
155162
end

src/indexing.jl

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,9 @@ using NamedGraphs:
66
to_edges,
77
AbstractGraphIndices
88
using NamedGraphs.GraphsExtensions: subgraph
9-
using Dictionaries: AbstractIndices
9+
using Dictionaries: AbstractIndices, getindices
1010

11-
struct Keys{I, Indices} <: AbstractIndices{I}
12-
parent::Indices
13-
Keys(parent::GI) where {GI} = new{eltype(GI), GI}(parent)
14-
end
15-
16-
Base.iterate(keys::Keys, state...) = iterate(keys.parent, state...)
17-
Base.length(keys::Keys) = length(keys.parent)
18-
Base.in(i, keys::Keys) = in(i, keys.parent)
19-
20-
# ====================================== getindex! ======================================= #
11+
# ====================================== getindex ======================================= #
2112

2213
NamedGraphs.get_graph_index(graph::AbstractDataGraph, index) = get_index_data(graph, index)
2314
# If unknown, treat like single vertex
@@ -34,19 +25,6 @@ function _get_index_data(graph::AbstractGraph, edge::AbstractEdge)
3425
return reverse_data_direction(graph, edge, data)
3526
end
3627

37-
# Can force data retrivial instead of subgraphing by using `Keys`.
38-
function NamedGraphs.getindex_namedgraph(graph::AbstractDataGraph, keys::Keys)
39-
return get_indices_data(graph, to_graph_index(graph, keys.parent))
40-
end
41-
42-
function get_indices_data(graph::AbstractGraph, vertices::AbstractVertices)
43-
return get_vertices_data(graph, vertices)
44-
end
45-
46-
function get_indices_data(graph::AbstractGraph, edges::AbstractEdges)
47-
return get_edges_data(graph, edges)
48-
end
49-
5028
# ====================================== isassigned ====================================== #
5129

5230
function Base.isassigned(graph::AbstractDataGraph, index)
@@ -149,3 +127,13 @@ end
149127
function NamedGraphs.to_graph_index(graph::AbstractGraph, vertex::OrdinalSuffixedInteger)
150128
return to_graph_index(graph, vertices(graph)[vertex])
151129
end
130+
131+
# ====================================== getindices ====================================== #
132+
133+
Dictionaries.getindices(graph::AbstractDataGraph, inds::Indices) = map(ind -> graph[ind], inds)
134+
135+
# ========================================= view ========================================= #
136+
137+
Base.view(graph::AbstractDataGraph, inds::Indices) = vertex_data(graph)[inds]
138+
Base.view(graph::AbstractDataGraph, inds::Indices{<:Pair}) = edge_data(graph)[inds]
139+
Base.view(graph::AbstractDataGraph, inds::Indices{<:AbstractEdge}) = edge_data(graph)[inds]

test/test_basics.jl

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ using Dictionaries:
1515
Dictionary,
1616
Indices,
1717
dictionary,
18-
unset!
18+
unset!,
19+
IndexError
1920
using Graphs:
2021
add_edge!,
2122
a_star,
@@ -542,10 +543,23 @@ using Test: @test, @test_broken, @testset
542543
@test g["b"] == 4
543544
@test g["c"] == 4
544545

545-
vertex_data(g)[Indices(["b", "a"])] .= [5, 6]
546-
@test g["a"] == 6
547-
@test g["b"] == 5
548-
@test g["c"] == 4
546+
vdv = vertex_data(g)[Indices(["a", "b"])]
547+
548+
@test isassigned(vdv, "a")
549+
@test isassigned(vdv, "b")
550+
@test !isassigned(vdv, "c")
551+
@test_throws IndexError vdv["c"]
552+
553+
vertex_data(g)[Indices(["b", "a"])] .= Dictionary(["a", "b"], [1, 2])
554+
555+
@test g["a"] == 1
556+
@test g["b"] == 2
557+
558+
vdv["a"] = 2
559+
vdv["b"] = 1
560+
561+
@test g["a"] == 2
562+
@test g["b"] == 1
549563

550564
unset!(g.edge_data, edgetype(g)("b" => "c"))
551565
@test !isassigned(g, "b" => "c")
@@ -554,9 +568,10 @@ using Test: @test, @test_broken, @testset
554568
@test g["a" => "b"] == 4.0
555569
@test g["b" => "c"] == 4.0
556570

557-
edge_data(g)[Indices(["a" => "b", "b" => "c"])] .= [1.5, 2.5]
558-
@test g["a" => "b"] == 1.5
559-
@test g["b" => "c"] == 2.5
571+
edv = edge_data(g)[Indices(["a" => "b", "b" => "c"])]
560572

573+
@test isassigned(edv, "a" => "b")
574+
edv["a" => "b"] = 5.0
575+
@test g["a" => "b"] == 5.0
561576
end
562577
end

0 commit comments

Comments
 (0)