193
193
copy (S:: SparseMatrixCSC ) =
194
194
SparseMatrixCSC (S. m, S. n, copy (S. colptr), copy (S. rowval), copy (S. nzval))
195
195
196
- function copy! {TvA, TiA, TvB, TiB} (A:: SparseMatrixCSC{TvA,TiA} ,
197
- B:: SparseMatrixCSC{TvB,TiB} )
196
+ function copy! (A:: SparseMatrixCSC , B:: SparseMatrixCSC )
198
197
# If the two matrices have the same length then all the
199
198
# elements in A will be overwritten.
200
199
if length (A) == length (B)
@@ -208,10 +207,37 @@ function copy!{TvA, TiA, TvB, TiB}(A::SparseMatrixCSC{TvA,TiA},
208
207
# This is like a "reshape B into A".
209
208
sparse_compute_reshaped_colptr_and_rowval (A. colptr, A. rowval, A. m, A. n, B. colptr, B. rowval, B. m, B. n)
210
209
end
211
- copy! (A. nzval, B. nzval)
212
210
else
213
- invoke (Base. copy!, Tuple{AbstractMatrix{TvA}, AbstractMatrix{TvB}}, A, B)
211
+ length (A) >= length (B) || throw (BoundsError ())
212
+ lB = length (B)
213
+ nnzA = nnz (A)
214
+ nnzB = nnz (B)
215
+ # Up to which col, row, and ptr in rowval/nzval will A be overwritten?
216
+ lastmodcolA = div (lB - 1 , A. m) + 1
217
+ lastmodrowA = mod (lB - 1 , A. m) + 1
218
+ lastmodptrA = A. colptr[lastmodcolA]
219
+ while lastmodptrA < A. colptr[lastmodcolA+ 1 ] && A. rowval[lastmodptrA] <= lastmodrowA
220
+ lastmodptrA += 1
221
+ end
222
+ lastmodptrA -= 1
223
+ if lastmodptrA >= nnzB
224
+ # A will have fewer non-zero elements; unmodified elements are kept at the end.
225
+ deleteat! (A. rowval, nnzB+ 1 : lastmodptrA)
226
+ deleteat! (A. nzval, nnzB+ 1 : lastmodptrA)
227
+ else
228
+ # A will have more non-zero elements; unmodified elements are kept at the end.
229
+ resize! (A. rowval, nnzB + nnzA - lastmodptrA)
230
+ resize! (A. nzval, nnzB + nnzA - lastmodptrA)
231
+ copy! (A. rowval, nnzB+ 1 , A. rowval, lastmodptrA+ 1 , nnzA- lastmodptrA)
232
+ copy! (A. nzval, nnzB+ 1 , A. nzval, lastmodptrA+ 1 , nnzA- lastmodptrA)
233
+ end
234
+ # Adjust colptr accordingly.
235
+ @inbounds for i in 2 : length (A. colptr)
236
+ A. colptr[i] += nnzB - lastmodptrA
237
+ end
238
+ sparse_compute_reshaped_colptr_and_rowval (A. colptr, A. rowval, A. m, lastmodcolA- 1 , B. colptr, B. rowval, B. m, B. n)
214
239
end
240
+ copy! (A. nzval, B. nzval)
215
241
return A
216
242
end
217
243
0 commit comments