-
Notifications
You must be signed in to change notification settings - Fork 94
Use DenseDict for copy_to #1122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
struct DenseDict{K, V, F, I} <: AbstractDict{K, V} | ||
hash::F | ||
inverse_hash::I | ||
set::BitSet | ||
map::Vector{V} | ||
end | ||
|
||
Same as `Dict{K, V}` but `hash(key)` is assumed to belong to `eachindex(map)`. | ||
""" | ||
struct DenseDict{K, V, F, I} <: AbstractDict{K, V} | ||
hash::F | ||
inverse_hash::I | ||
set::BitSet | ||
map::Vector{V} | ||
function DenseDict{K, V}(hash, inverse_hash, n) where {K, V} | ||
set = BitSet() | ||
sizehint!(set, n) | ||
return new{K, V, typeof(hash), typeof(inverse_hash)}(hash, inverse_hash, set, Vector{K}(undef, n)) | ||
end | ||
end | ||
|
||
# Implementation of the `AbstractDict` API. | ||
# Base.empty(::DenseDict, ::Type{K}, ::Type{V}) not implemented | ||
function Base.iterate(d::DenseDict, args...) | ||
itr = iterate(d.set, args...) | ||
if itr === nothing | ||
return nothing | ||
else | ||
el, i = itr | ||
return d.inverse_hash(el) => d.map[el], i | ||
end | ||
end | ||
Base.length(d::DenseDict) = length(d.set) | ||
Base.haskey(dict::DenseDict, key) = dict.hash(key) in dict.set | ||
Base.getindex(dict::DenseDict, key) = dict.map[dict.hash(key)] | ||
function Base.setindex!(dict::DenseDict, value, key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens if one goes beyond the limit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently it's an error, we can revisit once we have a use case for this |
||
h = dict.hash(key) | ||
push!(dict.set, h) | ||
dict.map[h] = value | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Test | ||
import MathOptInterface | ||
const MOI = MathOptInterface | ||
|
||
mul2(x) = x * 2 | ||
div2(x) = div(x, 2) | ||
d = MOI.Utilities.DenseDict{Int, Float64}(div2, mul2, 3) | ||
|
||
d[4] = 0.25 | ||
@test !haskey(d, 2) | ||
@test haskey(d, 4) | ||
@test !haskey(d, 6) | ||
@test length(d) == 1 | ||
@test collect(d) == [4 => 0.25] | ||
@test d[4] == 0.25 | ||
|
||
d[2] = 1.5 | ||
@test haskey(d, 2) | ||
@test haskey(d, 4) | ||
@test !haskey(d, 6) | ||
@test length(d) == 2 | ||
@test collect(d) == [2 => 1.5, 4 => 0.25] | ||
@test d[2] == 1.5 | ||
@test d[4] == 0.25 | ||
|
||
d[6] = 0.75 | ||
@test haskey(d, 2) | ||
@test haskey(d, 4) | ||
@test haskey(d, 6) | ||
@test length(d) == 3 | ||
@test collect(d) == [2 => 1.5, 4 => 0.25, 6 => 0.75] | ||
@test d[2] == 1.5 | ||
@test d[4] == 0.25 | ||
@test d[6] == 0.75 |
Uh oh!
There was an error while loading. Please reload this page.