Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/MathOptInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,10 @@ include("deprecate.jl")
include("DeprecatedTest/DeprecatedTest.jl")

"""
IndexMap(number_of_variables::Int = 0)
IndexMap()

The dictionary-like object returned by [`copy_to`](@ref).

If known in advance, pass `number_of_variables` to preallocate the necessary
space for the variables.

## IndexMap

Implementations of [`copy_to`](@ref) must return an [`IndexMap`](@ref). For
Expand Down
19 changes: 8 additions & 11 deletions src/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,24 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V}
vector::Vector{V}
dict::OrderedCollections.OrderedDict{K,V}
function CleverDict{K,V}(
hash::F,
inverse_hash::I,
n::Integer = 0,
) where {K,V,F,I}
vec = K[]
sizehint!(vec, n)
hash::F = key_to_index,
inverse_hash::I = index_to_key,
n = nothing,
) where {K,V,F<:Function,I<:Function}
if n !== nothing
@warn("The `n` argument to `CleverDict` has been removed.")
end
return new{K,V,F,I}(
0,
hash,
inverse_hash,
true,
vec,
K[],
OrderedCollections.OrderedDict{K,V}(),
)
end
end

function CleverDict{K,V}(n::Integer = 0) where {K,V}
return CleverDict{K,V}(key_to_index, index_to_key, n)
end

_is_dense(c::CleverDict) = c.is_dense

function _inverse_hash(c::CleverDict{K}, index::Integer) where {K}
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function default_copy_to(
end
MOI.empty!(dest)
vis_src = MOI.get(src, MOI.ListOfVariableIndices())
index_map = _index_map_for_variable_indices(vis_src)
index_map = IndexMap()
# The `NLPBlock` assumes that the order of variables does not change (#849)
# Therefore, all SingleVariable and VectorOfVariable constraints are added
# seprately, and no variables constrained-on-creation are added.
Expand Down
34 changes: 4 additions & 30 deletions src/Utilities/copy/index_map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,16 @@ struct IndexMap <: AbstractDict{MOI.Index,MOI.Index}
end

"""
IndexMap(number_of_variables::Int = 0)
IndexMap()

The dictionary-like object returned by [`MathOptInterface.copy_to`](@ref).

If known in advance, pass `number_of_variables` to preallocate the necessary
space for the variables.
"""
function IndexMap(number_of_variables::Int = 0)
var_map = CleverDicts.CleverDict{MOI.VariableIndex,MOI.VariableIndex}(
CleverDicts.key_to_index,
CleverDicts.index_to_key,
number_of_variables,
)
function IndexMap()
var_map = CleverDicts.CleverDict{MOI.VariableIndex,MOI.VariableIndex}()
con_map = DoubleDicts.IndexDoubleDict()
return IndexMap(var_map, con_map)
end

"""
_index_map_for_variable_indices(variables)

This function does not add variables to the IndexMap.
It simply initializes the IndexMap with a proper data struture.
If the variable indices are contiguous and start from 1, then
an optimized data structure with pre allocated memory is initialized.
Otherwise the data structure will start empty and will try to
keep using performant structure for as long as possible.
"""
function _index_map_for_variable_indices(variables)
n = length(variables)
if all(i -> variables[i] == MOI.VariableIndex(i), 1:n)
return IndexMap(n)
else
return IndexMap()
end
end

function _identity_constraints_map(
model,
map::MOIU.DoubleDicts.IndexDoubleDictInner{F,S},
Expand All @@ -71,7 +45,7 @@ Return an [`IndexMap`](@ref) that maps all variable and constraint indices of
"""
function identity_index_map(model::MOI.ModelLike)
variables = MOI.get(model, MOI.ListOfVariableIndices())
map = _index_map_for_variable_indices(variables)
map = IndexMap()
for x in variables
map[x] = x
end
Expand Down
10 changes: 10 additions & 0 deletions src/deprecate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ function VectorQuadraticFunction(
@warn("Fields of VectorQuadraticFunction have been re-ordered.", maxlog = 1)
return VectorQuadraticFunction(quadratic_terms, affine_terms, constant)
end

function Utilities.CleverDicts.CleverDict{K,V}(::Base.Integer) where {K,V}
@warn("The `n` argument to `CleverDict` has been removed.")
return Utilities.CleverDicts.CleverDict{K,V}()
end

function Utilities.IndexMap(::Int)
@warn("The number_of_variables argument to `IndexMap` has been removed.")
return Utilities.IndexMap()
end
16 changes: 16 additions & 0 deletions test/deprecate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ function test_copy_to_copy_names()
return
end

function test_IndexMap()
@test_logs (:warn,) MOI.IndexMap(1)
return
end

function test_CleverDicts()
K, V = MOI.VariableIndex, MOI.VariableIndex
@test_logs (:warn,) MOI.Utilities.CleverDicts.CleverDict{K,V}(1)
@test_logs (:warn,) MOI.Utilities.CleverDicts.CleverDict{K,V}(
MOI.Utilities.CleverDicts.key_to_index,
MOI.Utilities.CleverDicts.index_to_key,
1,
)
return
end

function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
Expand Down