Skip to content

Commit c457caf

Browse files
authored
Support setindex!/fill! when value is consistent (#69)
* Support setindex!/fill! when value is consistent * v0.6.4 * Add fill! test
1 parent dd89fed commit c457caf

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FillArrays"
22
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
3-
version = "0.6.3"
3+
version = "0.6.4"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/FillArrays.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ using LinearAlgebra, SparseArrays
44
import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
55
+, -, *, /, \, diff, sum, cumsum, maximum, minimum, sort, sort!,
66
any, all, axes, isone, iterate, unique, allunique, permutedims, inv,
7-
copy, vec
7+
copy, vec, setindex!
88

9-
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint
9+
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!
1010

1111
import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
1212

@@ -27,6 +27,23 @@ end
2727
getindex_value(F)
2828
end
2929

30+
@inline function setindex!(F::AbstractFill, v, k::Integer)
31+
@boundscheck checkbounds(F, k)
32+
v == getindex_value(F) || throw(ArgumentError("Cannot setindex! to $v for an AbstractFill with value $(getindex_value(F))."))
33+
F
34+
end
35+
36+
@inline function setindex!(F::AbstractFill{T, N}, v, kj::Vararg{<:Integer, N}) where {T, N}
37+
@boundscheck checkbounds(F, kj...)
38+
v == getindex_value(F) || throw(ArgumentError("Cannot setindex! to $v for an AbstractFill with value $(getindex_value(F))."))
39+
F
40+
end
41+
42+
@inline function fill!(F::AbstractFill, v)
43+
v == getindex_value(F) || throw(ArgumentError("Cannot fill! with $v an AbstractFill with value $(getindex_value(F))."))
44+
F
45+
end
46+
3047
rank(F::AbstractFill) = iszero(getindex_value(F)) ? 0 : 1
3148
IndexStyle(::Type{<:AbstractFill{<:Any,N,<:NTuple{N,Base.OneTo{Int}}}}) where N = IndexLinear()
3249

test/runtests.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,3 +722,25 @@ end
722722
@test Fill([1+im 2; 3 4; 5 6], 2,3)' == Fill([1+im 2; 3 4; 5 6]', 3,2)
723723
@test transpose(Fill([1+im 2; 3 4; 5 6], 2,3)) == Fill(transpose([1+im 2; 3 4; 5 6]), 3,2)
724724
end
725+
726+
@testset "setindex!/fill!" begin
727+
F = Fill(1,10)
728+
@test (F[1] = 1) == 1
729+
@test_throws BoundsError (F[11] = 1)
730+
@test_throws ArgumentError (F[10] = 2)
731+
732+
733+
F = Fill(1,10,5)
734+
@test (F[1] = 1) == 1
735+
@test (F[3,3] = 1) == 1
736+
@test_throws BoundsError (F[51] = 1)
737+
@test_throws BoundsError (F[1,6] = 1)
738+
@test_throws ArgumentError (F[10] = 2)
739+
@test_throws ArgumentError (F[10,1] = 2)
740+
741+
@test (F[:,1] .= 1) == fill(1,10)
742+
@test_throws ArgumentError (F[:,1] .= 2)
743+
744+
@test fill!(F,1) == F
745+
@test_throws ArgumentError fill!(F,2)
746+
end

0 commit comments

Comments
 (0)