From 32301d6e8c3e2ae38db32127b245e28ec74dbb62 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Fri, 26 Apr 2024 10:21:12 -0400 Subject: [PATCH] Fix tests --- src/abstractnamedgraph.jl | 7 +++ .../src/ordereddictionary.jl | 33 +++++-------- .../OrderedDictionaries/src/orderedindices.jl | 48 ++++++++++++------- src/lib/OrderedDictionaries/test/runtests.jl | 13 +++-- src/namedgraph.jl | 13 +++-- 5 files changed, 68 insertions(+), 46 deletions(-) diff --git a/src/abstractnamedgraph.jl b/src/abstractnamedgraph.jl index 682a01d..a875d64 100644 --- a/src/abstractnamedgraph.jl +++ b/src/abstractnamedgraph.jl @@ -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) ) diff --git a/src/lib/OrderedDictionaries/src/ordereddictionary.jl b/src/lib/OrderedDictionaries/src/ordereddictionary.jl index 4b544fa..96ed50a 100644 --- a/src/lib/OrderedDictionaries/src/ordereddictionary.jl +++ b/src/lib/OrderedDictionaries/src/ordereddictionary.jl @@ -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) @@ -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 @@ -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 diff --git a/src/lib/OrderedDictionaries/src/orderedindices.jl b/src/lib/OrderedDictionaries/src/orderedindices.jl index 8efd79e..066d054 100644 --- a/src/lib/OrderedDictionaries/src/orderedindices.jl +++ b/src/lib/OrderedDictionaries/src/orderedindices.jl @@ -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) @@ -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...) @@ -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] @@ -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 diff --git a/src/lib/OrderedDictionaries/test/runtests.jl b/src/lib/OrderedDictionaries/test/runtests.jl index 44e591d..de5a078 100644 --- a/src/lib/OrderedDictionaries/test/runtests.jl +++ b/src/lib/OrderedDictionaries/test/runtests.jl @@ -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 @@ -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. @@ -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]) diff --git a/src/namedgraph.jl b/src/namedgraph.jl index 7cafa67..85534a2 100644 --- a/src/namedgraph.jl +++ b/src/namedgraph.jl @@ -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} @@ -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 @@ -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)