Skip to content

Commit b19d33b

Browse files
committed
Use _shouldforwardindex in setindex!
1 parent 763a99d commit b19d33b

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/triangular.jl

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -289,57 +289,65 @@ end
289289
end
290290

291291
@propagate_inbounds function setindex!(A::UpperTriangular, x, i::Integer, j::Integer)
292-
if i > j
292+
if _shouldforwardindex(A, i, j)
293+
A.data[i,j] = x
294+
else
293295
@boundscheck checkbounds(A, i, j)
294296
# the value must be convertible to the eltype for setindex! to be meaningful
295-
xT = convert(eltype(A), x)
296-
iszero(xT) || throw_nonzeroerror(:UpperTriangular, x, i, j)
297-
else
298-
A.data[i,j] = x
297+
# however, the converted value is unused, and the compiler is free to remove
298+
# the conversion if the call is guaranteed to succeed
299+
convert(eltype(A), x)
300+
iszero(x) || throw_nonzeroerror(nameof(typeof(A)), x, i, j)
299301
end
300302
return A
301303
end
302304

303305
@propagate_inbounds function setindex!(A::UnitUpperTriangular, x, i::Integer, j::Integer)
304-
if i >= j
306+
if _shouldforwardindex(A, i, j)
307+
A.data[i,j] = x
308+
else
305309
@boundscheck checkbounds(A, i, j)
306310
# the value must be convertible to the eltype for setindex! to be meaningful
307-
xT = convert(eltype(A), x)
311+
# however, the converted value is unused, and the compiler is free to remove
312+
# the conversion if the call is guaranteed to succeed
313+
convert(eltype(A), x)
308314
if i > j
309-
iszero(xT) || throw_nonzeroerror(:UnitUpperTriangular, x, i, j)
315+
iszero(x) || throw_nonzeroerror(nameof(typeof(A)), x, i, j)
310316
else
311-
xT == oneunit(eltype(A)) || throw_nonuniterror(:UnitUpperTriangular, x, i, j)
317+
x == oneunit(eltype(A)) || throw_nonuniterror(nameof(typeof(A)), x, i, j)
312318
end
313-
else
314-
A.data[i,j] = x
315319
end
316320
return A
317321
end
318322

319323
@propagate_inbounds function setindex!(A::LowerTriangular, x, i::Integer, j::Integer)
320-
if i < j
324+
if _shouldforwardindex(A, i, j)
325+
A.data[i,j] = x
326+
else
321327
@boundscheck checkbounds(A, i, j)
322328
# the value must be convertible to the eltype for setindex! to be meaningful
323-
xT = convert(eltype(A), x)
324-
iszero(xT) || throw_nonzeroerror(:LowerTriangular, x, i, j)
325-
else
326-
A.data[i,j] = x
329+
# however, the converted value is unused, and the compiler is free to remove
330+
# the conversion if the call is guaranteed to succeed
331+
convert(eltype(A), x)
332+
iszero(x) || throw_nonzeroerror(nameof(typeof(A)), x, i, j)
327333
end
328334
return A
329335
end
330336

331337
@propagate_inbounds function setindex!(A::UnitLowerTriangular, x, i::Integer, j::Integer)
332-
if i <= j
338+
if _shouldforwardindex(A, i, j)
339+
A.data[i,j] = x
340+
else
333341
@boundscheck checkbounds(A, i, j)
334342
# the value must be convertible to the eltype for setindex! to be meaningful
335-
xT = convert(eltype(A), x)
343+
# however, the converted value is unused, and the compiler is free to remove
344+
# the conversion if the call is guaranteed to succeed
345+
convert(eltype(A), x)
336346
if i < j
337-
iszero(xT) || throw_nonzeroerror(:UnitLowerTriangular, x, i, j)
347+
iszero(x) || throw_nonzeroerror(nameof(typeof(A)), x, i, j)
338348
else
339-
xT == oneunit(eltype(A)) || throw_nonuniterror(:UnitLowerTriangular, x, i, j)
349+
x == oneunit(eltype(A)) || throw_nonuniterror(nameof(typeof(A)), x, i, j)
340350
end
341-
else
342-
A.data[i,j] = x
343351
end
344352
return A
345353
end

0 commit comments

Comments
 (0)