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
8 changes: 6 additions & 2 deletions src/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ end

function Base.setindex!(c::CleverDict{K, V}, value::V, key::K)::V where {K, V}
h = c.hash(key)::Int64
if h > c.last_index
c.last_index = ifelse(h == c.last_index + 1, h, -1)
if c.last_index != -1
if h == c.last_index + 1
c.last_index = h
elseif h <= 0 || h > c.last_index
c.last_index = -1
end
end
if 1 <= h <= length(c.vector) && _is_dense(c)
c.vector[h] = value
Expand Down
11 changes: 11 additions & 0 deletions test/Utilities/CleverDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,15 @@ CleverDicts.index_to_key(::Type{MyKey}, index::Int64) = MyKey(index)
@test d[4] == 0.25
@test d[6] == 0.75
end

@testset "Negative index" begin
d = CleverDicts.CleverDict{MathOptInterface.VariableIndex, String}()
d[MathOptInterface.VariableIndex(-3)] = "a"
@test d[MathOptInterface.VariableIndex(-3)] == "a"
@test_throws ErrorException CleverDicts.add_item(d, "b")
d[MathOptInterface.VariableIndex(0)] = "b"
@test d[MathOptInterface.VariableIndex(-3)] == "a"
@test d[MathOptInterface.VariableIndex(0)] == "b"
@test_throws ErrorException CleverDicts.add_item(d, "c")
end
end