From 1025b370c0ce15a6c00d4a84a40dc0fb30189112 Mon Sep 17 00:00:00 2001 From: Lars Hellemo Date: Mon, 29 May 2023 14:09:59 +0200 Subject: [PATCH 1/2] Parametrize safe/unsafe inserts --- src/SparseVariables.jl | 1 + src/indexedarray.jl | 15 +++++++++++---- test/runtests.jl | 3 +++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/SparseVariables.jl b/src/SparseVariables.jl index 9eb187a..96b2e63 100644 --- a/src/SparseVariables.jl +++ b/src/SparseVariables.jl @@ -14,6 +14,7 @@ export SparseArray export IndexedVarArray export insertvar! export unsafe_insertvar! +export SafeInsert, UnsafeInsert @setup_workload begin # Putting some things in `setup` can reduce the size of the diff --git a/src/indexedarray.jl b/src/indexedarray.jl index cb351af..09eaf85 100644 --- a/src/indexedarray.jl +++ b/src/indexedarray.jl @@ -10,6 +10,10 @@ struct IndexedVarArray{V<:AbstractVariableRef,N,T} <: AbstractSparseArray{V,N} index_cache::Vector{Dictionary} end + +struct SafeInsert end +struct UnsafeInsert end + _data(sa::IndexedVarArray) = sa.data already_defined(var, index) = haskey(_data(var), index) @@ -36,16 +40,19 @@ end Insert a new variable with the given index only after checking if keys are valid and not already defined. """ -function insertvar!(var::IndexedVarArray{V,N,T}, index...) where {V,N,T} +insertvar!(var::IndexedVarArray{V,N,T}, index...) where{V,N,T} = insertvar!(var, SafeInsert(), index...) +function insertvar!(var::IndexedVarArray{V,N,T},::SafeInsert=SafeInsert(), index...) where {V,N,T} !valid_index(var, index) && throw(BoundsError(var, index))# "Not a valid index for $(var.name): $index"g already_defined(var, index) && error("$index already defined for array") - var[index] = var.f(index...) - clear_cache!(var) return var[index] end +function insertvar!(var::IndexedVarArray{V,N,T},::UnsafeInsert, index..., ) where {V,N,T} + return var[index] = var.f(index...) +end + """ unsafe_insertvar!(var::indexedVarArray{V,N,T}, index...) @@ -53,7 +60,7 @@ Insert a new variable with the given index withouth checking if the index is val already assigned. """ function unsafe_insertvar!(var::IndexedVarArray{V,N,T}, index...) where {V,N,T} - return var[index] = var.f(index...) + insertvar!(var, UnsafeInsert(), index...) end joinex(ex1, ex2) = :($ex1..., $ex2...) diff --git a/test/runtests.jl b/test/runtests.jl index 1a8a4cf..9b346a4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -273,6 +273,9 @@ end unsafe_insertvar!(x, 2, 102) @test length(x) == 2 + insertvar!(x, UnsafeInsert(), 2, 103) + @test length(x) == 3 + # When no names are provided @variable(m, y[1:3, 100:102] >= 0, container = IndexedVarArray) @test length(y) == 0 From 59476819561e8b13893c62374b41ae2d09881744 Mon Sep 17 00:00:00 2001 From: Lars Hellemo Date: Mon, 5 Jun 2023 21:17:17 +0200 Subject: [PATCH 2/2] Fix formatting --- src/indexedarray.jl | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/indexedarray.jl b/src/indexedarray.jl index 09eaf85..9f52440 100644 --- a/src/indexedarray.jl +++ b/src/indexedarray.jl @@ -10,7 +10,6 @@ struct IndexedVarArray{V<:AbstractVariableRef,N,T} <: AbstractSparseArray{V,N} index_cache::Vector{Dictionary} end - struct SafeInsert end struct UnsafeInsert end @@ -40,8 +39,14 @@ end Insert a new variable with the given index only after checking if keys are valid and not already defined. """ -insertvar!(var::IndexedVarArray{V,N,T}, index...) where{V,N,T} = insertvar!(var, SafeInsert(), index...) -function insertvar!(var::IndexedVarArray{V,N,T},::SafeInsert=SafeInsert(), index...) where {V,N,T} +function insertvar!(var::IndexedVarArray{V,N,T}, index...) where {V,N,T} + return insertvar!(var, SafeInsert(), index...) +end +function insertvar!( + var::IndexedVarArray{V,N,T}, + ::SafeInsert = SafeInsert(), + index..., +) where {V,N,T} !valid_index(var, index) && throw(BoundsError(var, index))# "Not a valid index for $(var.name): $index"g already_defined(var, index) && error("$index already defined for array") var[index] = var.f(index...) @@ -49,7 +54,11 @@ function insertvar!(var::IndexedVarArray{V,N,T},::SafeInsert=SafeInsert(), index return var[index] end -function insertvar!(var::IndexedVarArray{V,N,T},::UnsafeInsert, index..., ) where {V,N,T} +function insertvar!( + var::IndexedVarArray{V,N,T}, + ::UnsafeInsert, + index..., +) where {V,N,T} return var[index] = var.f(index...) end @@ -60,7 +69,7 @@ Insert a new variable with the given index withouth checking if the index is val already assigned. """ function unsafe_insertvar!(var::IndexedVarArray{V,N,T}, index...) where {V,N,T} - insertvar!(var, UnsafeInsert(), index...) + return insertvar!(var, UnsafeInsert(), index...) end joinex(ex1, ex2) = :($ex1..., $ex2...)