Skip to content

Commit

Permalink
Generalize IndexedVector. Closes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Sep 21, 2013
1 parent d8947fa commit 027b672
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ end
IndexedVector(T::Type,n::Integer) = IndexedVector(zeros(T,n),zeros(Int,n),0)

function addelt{T}(v::IndexedVector{T},i::Integer,val::T)
if v.elts[i] == 0 # new index
if v.elts[i] == zero(T) # new index
v.elts[i] = val
v.nzidx[v.nnz += 1] = i
else
v.elts[i] += val
if v.elts[i] == 0
# set to tiny value
v.elts[i] = 1e-50
if v.elts[i] == zero(T)
# set to tiny value.

# if two instances of T can sum to zero(T),
# type must also implement one().
# if not, code will never be reached
v.elts[i] = 1e-50*one(T)
end
end
end
Expand Down

0 comments on commit 027b672

Please sign in to comment.