Skip to content

Commit a056588

Browse files
authored
Reuse similar for undef initialization (#115)
and also remove unnecessary legacy methods
1 parent 7294f3d commit a056588

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/OffsetArrays.jl

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ function Base.similar(::Type{T}, shape::Tuple{OffsetAxis,Vararg{OffsetAxis}}) wh
126126
end
127127

128128
Base.fill(v, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
129-
fill!(OffsetArray(Array{typeof(v), N}(undef, map(indexlength, inds)), map(indexoffset, inds)), v)
129+
fill!(similar(Array{typeof(v)}, inds), v)
130130
Base.zeros(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =
131-
fill!(OffsetArray(Array{T, N}(undef, map(indexlength, inds)), map(indexoffset, inds)), zero(T))
131+
fill!(similar(Array{T}, inds), zero(T))
132132
Base.ones(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =
133-
fill!(OffsetArray(Array{T, N}(undef, map(indexlength, inds)), map(indexoffset, inds)), one(T))
133+
fill!(similar(Array{T}, inds), one(T))
134134
Base.trues(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
135-
fill!(OffsetArray(BitArray{N}(undef, map(indexlength, inds)), map(indexoffset, inds)), true)
135+
fill!(similar(BitArray, inds), true)
136136
Base.falses(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
137-
fill!(OffsetArray(BitArray{N}(undef, map(indexlength, inds)), map(indexoffset, inds)), false)
137+
fill!(similar(BitArray, inds), false)
138138

139139
## Indexing
140140

@@ -207,12 +207,6 @@ function Base.show(io::IO, r::OffsetRange)
207207
end
208208
Base.show(io::IO, ::MIME"text/plain", r::OffsetRange) = show(io, r)
209209

210-
### Convenience functions ###
211-
212-
Base.fill(x, inds::Tuple{UnitRange,Vararg{UnitRange}}) =
213-
fill!(OffsetArray{typeof(x)}(undef, inds), x)
214-
@inline Base.fill(x, ind1::UnitRange, inds::UnitRange...) = fill(x, (ind1, inds...))
215-
216210

217211
### Some mutating functions defined only for OffsetVector ###
218212

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ end
9292
@test axes(a) == ()
9393
@test ndims(a) == 0
9494
@test a[] == 3
95+
@test a == OffsetArray(a, ())
9596
end
9697

9798
@testset "OffsetVector constructors" begin
@@ -393,6 +394,9 @@ end
393394
@test similar(Array{Int}, (0:0, 0:0)) isa OffsetArray{Int, 2}
394395
@test similar(Array{Int}, (1, 1)) isa Matrix{Int}
395396
@test similar(Array{Int}, (Base.OneTo(1), Base.OneTo(1))) isa Matrix{Int}
397+
B = similar(Array{Int}, (0:0, 3))
398+
@test isa(B, OffsetArray{Int, 2})
399+
@test axes(B) == (0:0, 1:3)
396400
end
397401

398402
@testset "reshape" begin
@@ -612,6 +616,14 @@ end
612616
B = fill(5, 1:3, -1:1)
613617
@test axes(B) == (1:3,-1:1)
614618
@test all(B.==5)
619+
620+
B = fill(5, (1:3, -1:1))
621+
@test axes(B) == (1:3,-1:1)
622+
@test all(B.==5)
623+
624+
B = fill(5, 3, -1:1)
625+
@test axes(B) == (1:3,-1:1)
626+
@test all(B.==5)
615627
end
616628

617629
@testset "broadcasting" begin

0 commit comments

Comments
 (0)