diff --git a/src/Utilities/copy.jl b/src/Utilities/copy.jl index 35d62d1738..824084fdba 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -88,6 +88,9 @@ Base.haskey(idxmap::IndexMap, vi::MOI.VariableIndex) = haskey(idxmap.varmap, vi) Base.keys(idxmap::IndexMap) = Iterators.flatten((keys(idxmap.varmap), keys(idxmap.conmap))) +Base.length(idxmap::IndexMap) = length(idxmap.varmap) + length(idxmap.conmap) +Base.iterate(idxmap::MOIU.IndexMap, args...) = iterate(Base.Iterators.flatten((idxmap.varmap, idxmap.conmap)), args...) + """ pass_attributes(dest::MOI.ModelLike, src::MOI.ModelLike, copy_names::Bool, idxmap::IndexMap, pass_attr::Function=MOI.set) diff --git a/test/Utilities/copy.jl b/test/Utilities/copy.jl index 93fb22ece9..ba68b9ca77 100644 --- a/test/Utilities/copy.jl +++ b/test/Utilities/copy.jl @@ -6,6 +6,26 @@ const MOIU = MOI.Utilities include("../dummy.jl") +remove_moi(x::String) = replace(x, "MathOptInterface." => "") +function compare_without_moi(x::String, y::String) + @test remove_moi(x) == remove_moi(y) +end + +@testset "IndexMap" begin + map = MOIU.IndexMap() + @test length(map) == 0 + compare_without_moi(sprint(show, map), "Utilities.IndexMap()") + x = MOI.VariableIndex(1) + y = MOI.VariableIndex(2) + cx = MOI.ConstraintIndex{MOI.SingleVariable, MOI.Integer}(1) + cy = MOI.ConstraintIndex{MOI.SingleVariable, MOI.Integer}(2) + map = MOIU.IndexMap(Dict(x => y), Dict(cx => cy)) + @test length(map) == 2 + # `x=>y` in Julia <= 1.1 and `x => y` in Julia >= 1.2 + x_y = string(Dict(x => y))[6:end-1] + compare_without_moi(sprint(show, map), "Utilities.IndexMap($x_y,Pair{ConstraintIndex,ConstraintIndex}($cx, $cy))") +end + @testset "AUTOMATIC" begin src = DummyModel() dest = DummyModel()