Skip to content

Commit

Permalink
fix sparse getindex regression
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmaykm committed Jun 9, 2014
1 parent cb9fca4 commit 1003f70
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 6 additions & 6 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,8 @@ prod{T}(A::SparseMatrixCSC{T}, region) = reducedim(*,A,region,one(T))
function binarysearch(haystack::AbstractVector, needle, lo::Int, hi::Int)
# Finds the first occurrence of needle in haystack[lo:hi]
lo = lo-1
hi2 = hi
hi = hi+1
hi2 = hi
@inbounds while lo < hi-1
m = (lo+hi)>>>1
if haystack[m] < needle
Expand All @@ -725,7 +725,7 @@ function binarysearch(haystack::AbstractVector, needle, lo::Int, hi::Int)
hi = m
end
end
(hi==hi2+1 || haystack[hi]!=needle) ? -1 : hi
(hi==hi2 || haystack[hi]!=needle) ? -1 : hi
end
function rangesearch(haystack::Range, needle)
(i,rem) = divrem(needle - first(haystack), step(haystack))
Expand All @@ -737,7 +737,7 @@ getindex(A::SparseMatrixCSC, I::(Integer,Integer)) = getindex(A, I[1], I[2])

function getindex{T}(A::SparseMatrixCSC{T}, i0::Integer, i1::Integer)
if !(1 <= i0 <= A.m && 1 <= i1 <= A.n); throw(BoundsError()); end
ind = binarysearch(A.rowval, i0, A.colptr[i1], A.colptr[i1+1]-1)
ind = binarysearch(A.rowval, i0, int(A.colptr[i1]), A.colptr[i1+1]-1)
ind > -1 ? A.nzval[ind] : zero(T)
end

Expand Down Expand Up @@ -1073,8 +1073,8 @@ function setindex!{T,Ti}(A::SparseMatrixCSC{T,Ti}, v, i0::Integer, i1::Integer)
i1 = convert(Ti, i1)
if !(1 <= i0 <= A.m && 1 <= i1 <= A.n); throw(BoundsError()); end
v = convert(T, v)
r1 = A.colptr[i1]
r2 = A.colptr[i1+1]-1
r1 = int(A.colptr[i1])
r2 = int(A.colptr[i1+1]-1)
if v == 0 #either do nothing or delete entry if it exists
loc = binarysearch(A.rowval, i0, r1, r2)
if loc != -1
Expand Down Expand Up @@ -1851,7 +1851,7 @@ done(d::SpDiagIterator, j) = j > d.n

function next{Tv}(d::SpDiagIterator{Tv}, j)
A = d.A
idx = binarysearch(A.rowval, j, A.colptr[j], A.colptr[j+1]-1)
idx = binarysearch(A.rowval, j, int(A.colptr[j]), A.colptr[j+1]-1)
((idx == -1) ? zero(Tv) : A.nzval[idx], j+1)
end

Expand Down
8 changes: 8 additions & 0 deletions test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ for (aa116, ss116) in [(a116, s116), (ad116, sd116)]
@test full(ss116[li,lj]) == aa116[li,lj]
end

let S = SparseMatrixCSC(3, 3, Uint8[1,1,1,1], Uint8[], Int64[])
S[1,1] = 1
S[5] = 2
S[end] = 3
@test S[end] == (S[1] + S[2,2])
@test 6 == sum(diag(S))
end


# setindex tests
let a = spzeros(Int, 10, 10)
Expand Down

0 comments on commit 1003f70

Please sign in to comment.