Skip to content

Commit

Permalink
AbstractArray constructor for OneElement (#261)
Browse files Browse the repository at this point in the history
* AbstractArray constructor for OneElement

* reduce duplication in tests

* bump version to v1.2.0
  • Loading branch information
jishnub authored Jun 5, 2023
1 parent 6aa6cf7 commit dab9cdf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FillArrays"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "1.1.1"
version = "1.2.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
2 changes: 2 additions & 0 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function Base.getindex(A::OneElement{T,N}, kj::Vararg{Int,N}) where {T,N}
ifelse(kj == A.ind, A.val, zero(T))
end

Base.AbstractArray{T,N}(A::OneElement{<:Any,N}) where {T,N} = OneElement(T(A.val), A.ind, A.axes)

Base.replace_in_print_matrix(o::OneElementVector, k::Integer, j::Integer, s::AbstractString) =
o.ind == (k,) ? s : Base.replace_with_centered_mark(s)

Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,13 @@ end
@test e₁ == [0,1,0,0,0]
@test_throws BoundsError e₁[6]

f₁ = AbstractArray{Float64}(e₁)
@test f₁ isa OneElement{Float64,1}
@test f₁ == e₁
fv₁ = AbstractVector{Float64}(e₁)
@test fv₁ isa OneElement{Float64,1}
@test fv₁ == e₁

@test stringmime("text/plain", e₁) == "5-element OneElement{$Int, 1, Tuple{$Int}, Tuple{Base.OneTo{$Int}}}:\n\n 1\n\n\n"

e₁ = OneElement{Float64}(2, 5)
Expand All @@ -1693,6 +1700,13 @@ end
V = OneElement(2, (2,3), (3,4))
@test V == [0 0 0 0; 0 0 2 0; 0 0 0 0]

Vf = AbstractArray{Float64}(V)
@test Vf isa OneElement{Float64,2}
@test Vf == V
VMf = AbstractMatrix{Float64}(V)
@test VMf isa OneElement{Float64,2}
@test VMf == V

@test stringmime("text/plain", V) == "3×4 OneElement{$Int, 2, Tuple{$Int, $Int}, Tuple{Base.OneTo{$Int}, Base.OneTo{$Int}}}:\n ⋅ ⋅ ⋅ ⋅\n ⋅ ⋅ 2 ⋅\n ⋅ ⋅ ⋅ ⋅"

@test Base.setindex(Zeros(5), 2, 2) OneElement(2.0, 2, 5)
Expand Down

0 comments on commit dab9cdf

Please sign in to comment.