Skip to content

Commit 2b92199

Browse files
committed
fix sparse getindex regression
ref JuliaLang#7131, JuliaLang#7047
1 parent 35cc52a commit 2b92199

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

base/sparse/sparsematrix.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,8 @@ prod{T}(A::SparseMatrixCSC{T}, region) = reducedim(*,A,region,one(T))
715715
function binarysearch(haystack::AbstractVector, needle, lo::Int, hi::Int)
716716
# Finds the first occurrence of needle in haystack[lo:hi]
717717
lo = lo-1
718-
hi2 = hi
719718
hi = hi+1
719+
hi2 = hi
720720
@inbounds while lo < hi-1
721721
m = (lo+hi)>>>1
722722
if haystack[m] < needle
@@ -725,7 +725,7 @@ function binarysearch(haystack::AbstractVector, needle, lo::Int, hi::Int)
725725
hi = m
726726
end
727727
end
728-
(hi==hi2+1 || haystack[hi]!=needle) ? -1 : hi
728+
(hi==hi2 || haystack[hi]!=needle) ? -1 : hi
729729
end
730730
function rangesearch(haystack::Range, needle)
731731
(i,rem) = divrem(needle - first(haystack), step(haystack))
@@ -737,7 +737,7 @@ getindex(A::SparseMatrixCSC, I::(Integer,Integer)) = getindex(A, I[1], I[2])
737737

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

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

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

test/sparse.jl

+8
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ for (aa116, ss116) in [(a116, s116), (ad116, sd116)]
289289
@test full(ss116[li,lj]) == aa116[li,lj]
290290
end
291291

292+
let S = SparseMatrixCSC(3, 3, Uint8[1,1,1,1], Uint8[], Int64[])
293+
S[1,1] = 1
294+
S[5] = 2
295+
S[end] = 3
296+
@test S[end] == (S[1] + S[2,2])
297+
@test 6 == sum(diag(S))
298+
end
299+
292300

293301
# setindex tests
294302
let a = spzeros(Int, 10, 10)

0 commit comments

Comments
 (0)