Skip to content

Commit e0f8b05

Browse files
blegatodow
andauthored
Refactor Model and UniversalFallback (jump-dev#1245)
* Refactor Model and UniversalFallback * Fixes * Fixes * Simplify * Don't do breaking changes to CleverDicts * Refactor UniversalFallback * Fixes * Rebase and formatting fixes * Fix docs Co-authored-by: odow <o.dowson@gmail.com>
1 parent 2bd73ab commit e0f8b05

File tree

9 files changed

+653
-543
lines changed

9 files changed

+653
-543
lines changed

docs/src/manual/basic_usage.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)
291291
292292
# output
293293
294-
MAX_SENSE::OptimizationSense = 1
295294
```
296295

297296
We add the knapsack constraint and integrality constraints:

src/Utilities/CleverDicts.jl

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ function index_to_key(::Type{MathOptInterface.VariableIndex}, index::Int64)
1010
return MathOptInterface.VariableIndex(index)
1111
end
1212

13-
key_to_index(key::MathOptInterface.VariableIndex) = key.value
13+
function index_to_key(::Type{MathOptInterface.ConstraintIndex{F,S}}, index::Int64) where {F,S}
14+
return MathOptInterface.ConstraintIndex{F,S}(index)
15+
end
16+
17+
key_to_index(key::MathOptInterface.Index) = key.value
1418

1519
# Now, on with `CleverDicts`.
1620

@@ -62,22 +66,6 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V}
6266
set::BitSet
6367
vector::Vector{V}
6468
dict::OrderedCollections.OrderedDict{K,V}
65-
function CleverDict{K,V}(n::Integer = 0) where {K,V}
66-
set = BitSet()
67-
sizehint!(set, n)
68-
vec = Vector{K}(undef, n)
69-
inverse_hash = x -> index_to_key(K, x)
70-
hash = key_to_index
71-
return new{K,V,typeof(hash),typeof(inverse_hash)}(
72-
0,
73-
hash,
74-
inverse_hash,
75-
true,
76-
set,
77-
vec,
78-
OrderedCollections.OrderedDict{K,V}(),
79-
)
80-
end
8169
function CleverDict{K,V}(
8270
hash::F,
8371
inverse_hash::I,
@@ -97,6 +85,9 @@ mutable struct CleverDict{K,V,F<:Function,I<:Function} <: AbstractDict{K,V}
9785
)
9886
end
9987
end
88+
function CleverDict{K,V}(n::Integer = 0) where {K,V}
89+
return CleverDict{K,V}(key_to_index, Base.Fix1(index_to_key, K), n)
90+
end
10091

10192
"""
10293
index_to_key(::Type{K}, index::Int)
@@ -151,6 +142,14 @@ function Base.haskey(c::CleverDict{K}, key::K) where {K}
151142
return _is_dense(c) ? c.hash(key)::Int64 in c.set : haskey(c.dict, key)
152143
end
153144

145+
function Base.keys(c::CleverDict{K}) where {K}
146+
return if _is_dense(c)
147+
[c.inverse_hash(Int64(index))::K for index in c.set]
148+
else
149+
collect(keys(c.dict))
150+
end
151+
end
152+
154153
function Base.get(c::CleverDict, key, default)
155154
if _is_dense(c)
156155
if !haskey(c, key)
@@ -363,4 +362,19 @@ function Base.resize!(c::CleverDict{K,V}, n) where {K,V}
363362
return
364363
end
365364

365+
Base.values(d::CleverDict) = _is_dense(d) ? d.vector : values(d.dict)
366+
367+
# TODO `map!(f, values(dict::AbstractDict))` requires Julia 1.2 or later,
368+
# use `map_values` once we drop Julia 1.1 and earlier.
369+
function map_values!(f::Function, d::CleverDict)
370+
if _is_dense(d)
371+
map!(f, d.vector, d.vector)
372+
else
373+
for (k, v) in d.dict
374+
d.dict[k] = f(v)
375+
end
376+
end
377+
return
378+
end
379+
366380
end

src/Utilities/DoubleDicts.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Works as a `AbstractDict{CI, V}` with minimal differences.
1919
Note that `CI` is not a concrete type, opposed to `CI{MOI.SingleVariable, MOI.Integers}`,
2020
which is a concrete type.
2121
22-
When optimal performance or type stability is required its possible to obtain a
22+
When optimal performance or type stability is required it is possible to obtain a
2323
fully type stable dictionary with values of type `V` and keys of type
2424
`CI{MOI.SingleVariable, MOI.Integers}` from the dictionary `dict`, for instance:
2525

src/Utilities/Utilities.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ include("copy.jl")
5757
include("results.jl")
5858
include("variables.jl")
5959

60+
include("vector_of_constraints.jl")
6061
include("model.jl")
6162
include("parser.jl")
6263
include("mockoptimizer.jl")

0 commit comments

Comments
 (0)