Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 26, 2024
1 parent ebeffc6 commit 32301d6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 46 deletions.
7 changes: 7 additions & 0 deletions src/abstractnamedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ function Graphs.a_star(
return namedgraph_a_star(graph, source, destination, args...)
end

# Fix ambiguity error with `AbstractGraph` version
function Graphs.a_star(
graph::AbstractNamedGraph, source::Integer, destination::Integer, args...
)
return namedgraph_a_star(graph, source, destination, args...)
end

function Graphs.spfa_shortest_paths(
graph::AbstractNamedGraph, vertex, distmx=weights(graph)
)
Expand Down
33 changes: 12 additions & 21 deletions src/lib/OrderedDictionaries/src/ordereddictionary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function OrderedDictionary(indices, values)
return OrderedDictionary(OrderedIndices(indices), Vector(values))
end

_values(dict::OrderedDictionary) = getfield(dict, :values)
Base.values(dict::OrderedDictionary) = getfield(dict, :values)

# https://github.com/andyferris/Dictionaries.jl/tree/master?tab=readme-ov-file#abstractdictionary
Base.keys(dict::OrderedDictionary) = getfield(dict, :indices)
Expand All @@ -27,33 +27,19 @@ ordered_indices(dict::OrderedDictionary) = ordered_indices(keys(dict))
# https://github.com/andyferris/Dictionaries.jl/tree/master?tab=readme-ov-file#implementing-the-token-interface-for-abstractdictionary
Dictionaries.istokenizable(dict::OrderedDictionary) = Dictionaries.istokenizable(keys(dict))
Base.@propagate_inbounds function Dictionaries.gettokenvalue(
dict::OrderedDictionary, (_slot, index)
dict::OrderedDictionary, token::Int
)
return _values(dict)[index]
return values(dict)[token]
end
Base.@propagate_inbounds function Dictionaries.gettokenvalue(
dict::OrderedDictionary, index::Int
)
return _values(dict)[index]
end
function Dictionaries.istokenassigned(dict::OrderedDictionary, (_slot, index))
return isassigned(_values(dict), index)
end
function Dictionaries.istokenassigned(dict::OrderedDictionary, index::Int)
return isassigned(_values(dict), index)
function Dictionaries.istokenassigned(dict::OrderedDictionary, token::Int)
return isassigned(values(dict), token)
end

Dictionaries.issettable(dict::OrderedDictionary) = true
Base.@propagate_inbounds function Dictionaries.settokenvalue!(
dict::OrderedDictionary{<:Any,T}, (_slot, index), value::T
) where {T}
_values(dict)[index] = value
return dict
end
Base.@propagate_inbounds function Dictionaries.settokenvalue!(
dict::OrderedDictionary{<:Any,T}, index::Int, value::T
dict::OrderedDictionary{<:Any,T}, token::Int, value::T
) where {T}
_values(dict)[index] = value
values(dict)[token] = value
return dict
end

Expand All @@ -64,3 +50,8 @@ Dictionaries.deletetoken!(dict::OrderedDictionary, token) = error()
function Base.similar(indices::OrderedIndices, type::Type)
return OrderedDictionary(indices, Vector{type}(undef, length(indices)))
end

# Circumvents https://github.com/andyferris/Dictionaries.jl/pull/140
function Base.map(f, dict::OrderedDictionary)
return OrderedDictionary(keys(dict), map(f, values(dict)))
end
48 changes: 32 additions & 16 deletions src/lib/OrderedDictionaries/src/orderedindices.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
using Dictionaries: Dictionaries, AbstractIndices, Dictionary

# Represents a [set](https://en.wikipedia.org/wiki/Set_(mathematics)) of indices
# `I` whose elements/members are ordered in a sequence such that each element can be
# associated with a position which is a positive integer
# (1-based [natural numbers](https://en.wikipedia.org/wiki/Natural_number))
# which can be accessed through ordinal indexing (`I[4th]`).
# Related to an (indexed family)[https://en.wikipedia.org/wiki/Indexed_family],
# [index set](https://en.wikipedia.org/wiki/Index_set), or
# [sequence](https://en.wikipedia.org/wiki/Sequence).
# In other words, it is a [bijection](https://en.wikipedia.org/wiki/Bijection)
# from a finite subset of 1-based natural numbers to a set of corresponding
# elements/members.
struct OrderedIndices{I} <: AbstractIndices{I}
ordered_indices::Vector{I}
index_ordinals::Dictionary{I,Int}
index_positions::Dictionary{I,Int}
function OrderedIndices{I}(indices) where {I}
ordered_indices = collect(indices)
index_ordinals = Dictionary{I,Int}(copy(ordered_indices), undef)
index_positions = Dictionary{I,Int}(copy(ordered_indices), undef)
for i in eachindex(ordered_indices)
index_ordinals[ordered_indices[i]] = i
index_positions[ordered_indices[i]] = i
end
return new{I}(ordered_indices, index_ordinals)
return new{I}(ordered_indices, index_positions)
end
end
OrderedIndices(indices) = OrderedIndices{eltype(indices)}(indices)
Expand All @@ -18,9 +29,9 @@ OrderedIndices{I}(indices::OrderedIndices{I}) where {I} = copy(indices)

ordered_indices(indices::OrderedIndices) = getfield(indices, :ordered_indices)
# TODO: Better name for this?
index_ordinals(indices::OrderedIndices) = getfield(indices, :index_ordinals)
index_positions(indices::OrderedIndices) = getfield(indices, :index_positions)
# TODO: Better name for this?
parent_indices(indices::OrderedIndices) = keys(index_ordinals(indices))
parent_indices(indices::OrderedIndices) = keys(index_positions(indices))

# https://github.com/andyferris/Dictionaries.jl/tree/master?tab=readme-ov-file#abstractindices
function Dictionaries.iterate(indices::OrderedIndices, state...)
Expand All @@ -41,10 +52,10 @@ function Dictionaries.iteratetoken_reverse(indices::OrderedIndices, state...)
return iterate(reverse(Base.OneTo(length(indices))), state...)
end
function Dictionaries.gettoken(indices::OrderedIndices, key)
if !haskey(index_ordinals(indices), key)
if !haskey(index_positions(indices), key)
return (false, 0)
end
return (true, index_ordinals(indices)[key])
return (true, index_positions(indices)[key])
end
function Dictionaries.gettokenvalue(indices::OrderedIndices, token)
return ordered_indices(indices)[token]
Expand All @@ -58,21 +69,26 @@ function Dictionaries.gettoken!(indices::OrderedIndices{I}, key::I) where {I}
end
push!(ordered_indices(indices), key)
token = length(ordered_indices(indices))
insert!(index_ordinals(indices), key, token)
insert!(index_positions(indices), key, token)
return (false, token)
end
function Dictionaries.deletetoken!(indices::OrderedIndices, token)
len = length(indices)
ordinal = token
index = ordered_indices(indices)[ordinal]
position = token
index = ordered_indices(indices)[position]
# Move the last vertex to the position of the deleted one.
if ordinal < len
ordered_indices(indices)[ordinal] = last(ordered_indices(indices))
if position < len
ordered_indices(indices)[position] = last(ordered_indices(indices))
end
last_index = pop!(ordered_indices(indices))
delete!(index_ordinals(indices), index)
if ordinal < len
index_ordinals(indices)[last_index] = ordinal
delete!(index_positions(indices), index)
if position < len
index_positions(indices)[last_index] = position
end
return indices
end

# Circumvents https://github.com/andyferris/Dictionaries.jl/pull/140
function Base.map(f, indices::OrderedIndices)
return OrderedDictionary(indices, map(f, ordered_indices(indices)))
end
13 changes: 8 additions & 5 deletions src/lib/OrderedDictionaries/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@eval module $(gensym())
using Dictionaries: Dictionary
using NamedGraphs.OrderedDictionaries: OrderedDictionary, OrderedIndices, each_ordinal_index
using NamedGraphs.OrderedDictionaries:
OrderedDictionaries, OrderedDictionary, OrderedIndices, each_ordinal_index
using NamedGraphs.OrdinalIndexing: th
using Test: @test, @testset
@testset "OrderedDictionaries" begin
Expand Down Expand Up @@ -37,8 +38,9 @@ using Test: @test, @testset
@test i[1th] == "x1"
@test i[2th] == "x4"
@test i[3th] == "x3"
@test i.ordered_indices == ["x1", "x4", "x3"]
@test i.index_ordinals == Dictionary(["x1", "x3", "x4"], [1, 3, 2])
@test OrderedDictionaries.ordered_indices(i) == ["x1", "x4", "x3"]
@test OrderedDictionaries.index_positions(i) ==
Dictionary(["x1", "x3", "x4"], [1, 3, 2])

# Test for deleting the last index, this is a special
# case in the code.
Expand All @@ -56,8 +58,9 @@ using Test: @test, @testset
@test i[1th] == "x1"
@test i[2th] == "x2"
@test i[3th] == "x3"
@test i.ordered_indices == ["x1", "x2", "x3"]
@test i.index_ordinals == Dictionary(["x1", "x2", "x3"], [1, 2, 3])
@test OrderedDictionaries.ordered_indices(i) == ["x1", "x2", "x3"]
@test OrderedDictionaries.index_positions(i) ==
Dictionary(["x1", "x2", "x3"], [1, 2, 3])

i = OrderedIndices(["x1", "x2", "x3", "x4"])
d = Dictionary(["x1", "x2", "x3", "x4"], [:x1, :x2, :x3, :x4])
Expand Down
13 changes: 9 additions & 4 deletions src/namedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using Graphs:
using Graphs.SimpleGraphs: AbstractSimpleGraph, SimpleDiGraph, SimpleGraph
using .GraphsExtensions:
GraphsExtensions, vertextype, directed_graph_type, undirected_graph_type
using .OrderedDictionaries: OrderedIndices
using .OrderedDictionaries: OrderedDictionaries, OrderedIndices
using .OrdinalIndexing: th

struct GenericNamedGraph{V,G<:AbstractSimpleGraph{Int}} <: AbstractNamedGraph{V}
Expand All @@ -30,10 +30,13 @@ function one_based_graph_type(graph_type::Type{<:GenericNamedGraph})
return fieldtype(graph_type, :one_based_graph)
end
one_based_graph(graph::GenericNamedGraph) = getfield(graph, :one_based_graph)

# TODO: Rename `vertex_positions(graph::GenericNamedGraph)`.
function vertex_to_one_based_vertex(graph::GenericNamedGraph, vertex)
# TODO: Define a function `index_ordinal(ordinal_indices, index)`.
return vertices(graph).index_ordinals[vertex]
return OrderedDictionaries.index_positions(vertices(graph))[vertex]
end

# TODO: Rename `ordered_vertices(graph::GenericNamedGraph)`.
function one_based_vertex_to_vertex(graph::GenericNamedGraph, one_based_vertex::Integer)
return vertices(graph)[one_based_vertex * th]
end
Expand Down Expand Up @@ -61,7 +64,9 @@ function Graphs.rem_vertex!(graph::GenericNamedGraph, vertex)
end

function GraphsExtensions.rename_vertices(f::Function, graph::GenericNamedGraph)
return GenericNamedGraph(one_based_graph(graph), f.(vertices(graph)))
# TODO: Fix broadcasting of `OrderedIndices`.
# return GenericNamedGraph(one_based_graph(graph), f.(vertices(graph)))
return GenericNamedGraph(one_based_graph(graph), map(f, vertices(graph)))
end

function GraphsExtensions.rename_vertices(f::Function, g::AbstractSimpleGraph)
Expand Down

0 comments on commit 32301d6

Please sign in to comment.