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
36 changes: 30 additions & 6 deletions src/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V}
end
end
function CleverDict{K,V}(n::Integer = 0) where {K,V}
return CleverDict{K,V}(key_to_index, Base.Fix1(index_to_key, K), n)
return CleverDict{K,V}(key_to_index, index_to_key, n)
end

"""
Expand All @@ -106,6 +106,13 @@ function key_to_index end

_is_dense(c::CleverDict) = c.is_dense

function _inverse_hash(c::CleverDict{K}, index::Integer) where {K}
return c.inverse_hash(Int64(index))::K
end
function _inverse_hash(c::CleverDict{K,V,F,typeof(index_to_key)}, index::Integer) where {K,V,F<:Function}
return index_to_key(K, Int64(index))::K
end

"""
add_item(c::CleverDict{K, V}, val::Val)::K where {K, V}

Expand All @@ -116,7 +123,7 @@ function add_item(c::CleverDict{K,V}, val::V)::K where {K,V}
error("Keys were added out of order. `add_item` requires that keys are always added in order.")
end
# adding a key in order
key = c.inverse_hash(Int64(c.last_index + 1))::K
key = _inverse_hash(c, c.last_index + 1)
c[key] = val
return key
end
Expand All @@ -142,9 +149,9 @@ function Base.haskey(c::CleverDict{K}, key::K) where {K}
return _is_dense(c) ? c.hash(key)::Int64 in c.set : haskey(c.dict, key)
end

function Base.keys(c::CleverDict{K}) where {K}
function Base.keys(c::CleverDict)
return if _is_dense(c)
[c.inverse_hash(Int64(index))::K for index in c.set]
[_inverse_hash(c, index) for index in c.set]
else
collect(keys(c.dict))
end
Expand Down Expand Up @@ -284,7 +291,7 @@ function Base.iterate(
return nothing
else
el, i = itr
new_el = c.inverse_hash(Int64(el))::K => c.vector[el]::V
new_el = _inverse_hash(c, el) => c.vector[el]::V
@static if VERSION >= v"1.4.0"
return new_el, State(i[2], i[1])
else
Expand Down Expand Up @@ -318,7 +325,7 @@ function Base.iterate(
return nothing
else
el, i = itr
new_el = c.inverse_hash(Int64(el))::K => c.vector[el]::V
new_el = _inverse_hash(c, el) => c.vector[el]::V
@static if VERSION >= v"1.4.0"
return new_el, State(i[2], i[1])
else
Expand Down Expand Up @@ -377,4 +384,21 @@ function map_values!(f::Function, d::CleverDict)
return
end

function Base.convert(
::Type{CleverDict{K,V,typeof(key_to_index),typeof(index_to_key)}},
d::CleverDict{K,V,typeof(key_to_index),typeof(index_to_key)},
) where {K,V}
return d
end
function Base.convert(
::Type{CleverDict{K,V,typeof(key_to_index),typeof(index_to_key)}},
src::AbstractDict{K,V},
) where {K,V}
dest = CleverDict{K,V}()
for key in sort(collect(keys(src)), by=dest.hash)
dest[key] = src[key]
end
return dest
end

end
11 changes: 3 additions & 8 deletions src/Utilities/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ _index_to_variable(i) = MOI.VariableIndex(i)
const DenseVariableDict{V} = CleverDicts.CleverDict{
MOI.VariableIndex,
V,
typeof(MOI.index_value),
typeof(_index_to_variable),
typeof(CleverDicts.key_to_index),
typeof(CleverDicts.index_to_key),
}
function dense_variable_dict(::Type{V}, n) where {V}
return CleverDicts.CleverDict{MOI.VariableIndex,V}(
Expand All @@ -99,12 +99,7 @@ function dense_variable_dict(::Type{V}, n) where {V}
end

struct IndexMap <: AbstractDict{MOI.Index,MOI.Index}
# just keeping the union to make it more backward compatible
# we should remove as soon as possible.
varmap::Union{
DenseVariableDict{MOI.VariableIndex},
Dict{MOI.VariableIndex,MOI.VariableIndex},
}
varmap::DenseVariableDict{MOI.VariableIndex}
conmap::DoubleDicts.MainIndexDoubleDict
end

Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/vector_of_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct VectorOfConstraints{
MOI.ConstraintIndex{F,S},
Tuple{F,S},
typeof(CleverDicts.key_to_index),
Base.Fix1{typeof(CleverDicts.index_to_key),DataType}
typeof(CleverDicts.index_to_key),
}

function VectorOfConstraints{F,S}() where {F,S}
Expand Down
12 changes: 12 additions & 0 deletions test/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,16 @@ CleverDicts.index_to_key(::Type{MyKey}, index::Int64) = MyKey(index)
@test d[MathOptInterface.VariableIndex(0)] == "b"
@test_throws ErrorException CleverDicts.add_item(d, "c")
end

@testset "convert" begin
vals = [MathOptInterface.VariableIndex(-i) for i in 1:10]
d = Dict(MathOptInterface.VariableIndex(i) => vals[i] for i in 1:10)
T = MathOptInterface.Utilities.DenseVariableDict{MathOptInterface.VariableIndex}
c = convert(T, d)
@test c isa T
@test c.is_dense
@test c.last_index == 10
@test c.vector == vals
@test c === convert(T, c)
end
end