Skip to content
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

add allowscalar #200

Merged
merged 7 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 34 additions & 0 deletions src/abstractsparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,37 @@ julia> findnz(A)
function findnz end

widelength(x::AbstractSparseArray) = prod(Int64.(size(x)))


const _restore_scalar_indexing = Expr[]
const _destroy_scalar_indexing = Expr[]
"""
@RCI f

records function `f` to be disabled by calling `allowscalar(false)`.
"""
macro RCI(exp)
# evaluate to not push any broken code in the arrays when developping this package.
# also ensures that restore has the exact same effect.
@eval $exp
if length(exp.args) == 2 && exp.head ∈ (:function, :(=))
push!(_restore_scalar_indexing, exp)
push!(_destroy_scalar_indexing,
Expr(exp.head,
exp.args[1],
:(error("scalar indexing was turned off"))))
else
error("can't parse expression")
end
return
end
allowscalar(f::Bool) = if f
for i in _restore_scalar_indexing
@eval $i
end
else
for i in _destroy_scalar_indexing
@eval $i
end
end

6 changes: 3 additions & 3 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2261,9 +2261,9 @@ function rangesearch(haystack::AbstractRange, needle)
(rem==0 && 1<=i+1<=length(haystack)) ? i+1 : 0
end

getindex(A::AbstractSparseMatrixCSC, I::Tuple{Integer,Integer}) = getindex(A, I[1], I[2])
@RCI getindex(A::AbstractSparseMatrixCSC, I::Tuple{Integer,Integer}) = getindex(A, I[1], I[2])

function getindex(A::AbstractSparseMatrixCSC{T}, i0::Integer, i1::Integer) where T
@RCI function getindex(A::AbstractSparseMatrixCSC{T}, i0::Integer, i1::Integer) where T
@boundscheck checkbounds(A, i0, i1)
r1 = Int(getcolptr(A)[i1])
r2 = Int(getcolptr(A)[i1+1]-1)
Expand Down Expand Up @@ -2709,7 +2709,7 @@ getindex(A::AbstractSparseMatrixCSC, I::AbstractVector{Bool}, J::AbstractVector{
## setindex!

# dispatch helper for #29034
setindex!(A::AbstractSparseMatrixCSC, _v, _i::Integer, _j::Integer) = _setindex_scalar!(A, _v, _i, _j)
@RCI setindex!(A::AbstractSparseMatrixCSC, _v, _i::Integer, _j::Integer) = _setindex_scalar!(A, _v, _i, _j)

function _setindex_scalar!(A::AbstractSparseMatrixCSC{Tv,Ti}, _v, _i::Integer, _j::Integer) where {Tv,Ti<:Integer}
v = convert(Tv, _v)
Expand Down
6 changes: 3 additions & 3 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ end

### Element access

function setindex!(x::SparseVector{Tv,Ti}, v::Tv, i::Ti) where {Tv,Ti<:Integer}
@RCI function setindex!(x::SparseVector{Tv,Ti}, v::Tv, i::Ti) where {Tv,Ti<:Integer}
checkbounds(x, i)
nzind = nonzeroinds(x)
nzval = nonzeros(x)
Expand All @@ -352,7 +352,7 @@ function setindex!(x::SparseVector{Tv,Ti}, v::Tv, i::Ti) where {Tv,Ti<:Integer}
x
end

setindex!(x::SparseVector{Tv,Ti}, v, i::Integer) where {Tv,Ti<:Integer} =
@RCI setindex!(x::SparseVector{Tv,Ti}, v, i::Integer) where {Tv,Ti<:Integer} =
setindex!(x, convert(Tv, v), convert(Ti, i))


Expand Down Expand Up @@ -839,7 +839,7 @@ function _spgetindex(m::Int, nzind::AbstractVector{Ti}, nzval::AbstractVector{Tv
(ii <= m && nzind[ii] == i) ? nzval[ii] : zero(Tv)
end

function getindex(x::AbstractSparseVector, i::Integer)
@RCI function getindex(x::AbstractSparseVector, i::Integer)
checkbounds(x, i)
_spgetindex(nnz(x), nonzeroinds(x), nonzeros(x), i)
end
Expand Down
24 changes: 24 additions & 0 deletions test/allowscalar.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Test, SparseArrays

@testset "allowscalar" begin
A = sprandn(10, 20, 0.9)
A[1, 1] = 2
@test A[1, 1] == 2
SparseArrays.allowscalar(false)
@test_throws Any A[1, 1]
@test_throws Any A[1, 1] = 2
SparseArrays.allowscalar(true)
@test A[1, 1] == 2
A[1, 1] = 3
@test A[1, 1] == 3

B = sprandn(10, 0.9)
B[1] = 2
@test B[1] == 2
SparseArrays.allowscalar(false)
@test_throws Any B[1]
SparseArrays.allowscalar(true)
@test B[1] == 2
B[1] = 3
@test B[1] == 3
end
3 changes: 2 additions & 1 deletion test/testgroups
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
allowscalar
ambiguous
higherorderfns
sparsematrix_ops
Expand All @@ -8,4 +9,4 @@ sparsevector
linalg_solvers
umfpack
cholmod
spqr
spqr