From 63cf027af67e733c4549a610622f3a5a7b4661fc Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 26 Aug 2021 12:11:25 +1200 Subject: [PATCH] [breaking] remove size argument for CleverDict --- src/MathOptInterface.jl | 5 +---- src/Utilities/CleverDicts.jl | 19 ++++++++---------- src/Utilities/copy.jl | 2 +- src/Utilities/copy/index_map.jl | 34 ++++----------------------------- src/deprecate.jl | 10 ++++++++++ test/deprecate.jl | 16 ++++++++++++++++ 6 files changed, 40 insertions(+), 46 deletions(-) diff --git a/src/MathOptInterface.jl b/src/MathOptInterface.jl index e3a8702748..ac6b80cef6 100644 --- a/src/MathOptInterface.jl +++ b/src/MathOptInterface.jl @@ -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 diff --git a/src/Utilities/CleverDicts.jl b/src/Utilities/CleverDicts.jl index 0ab32237e6..955cbac8f1 100644 --- a/src/Utilities/CleverDicts.jl +++ b/src/Utilities/CleverDicts.jl @@ -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} diff --git a/src/Utilities/copy.jl b/src/Utilities/copy.jl index 8b2cbbee4b..3a5c9cf258 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -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. diff --git a/src/Utilities/copy/index_map.jl b/src/Utilities/copy/index_map.jl index c1a6a57ca2..b8084ccec4 100644 --- a/src/Utilities/copy/index_map.jl +++ b/src/Utilities/copy/index_map.jl @@ -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}, @@ -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 diff --git a/src/deprecate.jl b/src/deprecate.jl index 6cfa53ad4a..e609596919 100644 --- a/src/deprecate.jl +++ b/src/deprecate.jl @@ -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 diff --git a/test/deprecate.jl b/test/deprecate.jl index fc70cfd9dd..68d0d9729a 100644 --- a/test/deprecate.jl +++ b/test/deprecate.jl @@ -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_")