Skip to content

Commit dfb8540

Browse files
authored
Forward length to parent (#304)
* forward length to parent * version bump to v1.12.6
1 parent 9b018f0 commit dfb8540

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OffsetArrays"
22
uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3-
version = "1.12.5"
3+
version = "1.12.6"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/OffsetArrays.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ Base.parent(A::OffsetArray) = A.parent
287287
# Base.Broadcast.BroadcastStyle(::Type{<:OffsetArray{<:Any, <:Any, AA}}) where AA = Base.Broadcast.BroadcastStyle(AA)
288288

289289
@inline Base.size(A::OffsetArray) = size(parent(A))
290+
# specializing length isn't necessary, as length(A) = prod(size(A)),
291+
# but specializing length enables constant-propagation for statically sized arrays
292+
# see https://github.com/JuliaArrays/OffsetArrays.jl/pull/304
293+
@inline Base.length(A::OffsetArray) = length(parent(A))
290294

291295
@inline Base.axes(A::OffsetArray) = map(IdOffsetRange, axes(parent(A)), A.offsets)
292296
@inline Base.axes(A::OffsetArray, d) = d <= ndims(A) ? IdOffsetRange(axes(parent(A), d), A.offsets[d]) : IdOffsetRange(axes(parent(A), d))

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,18 @@ Base.Int(a::WeirdInteger) = a
907907
@test axes(y) == (r, )
908908
end
909909
end
910+
911+
@testset "size/length" begin
912+
for p in Any[SA[1,2,3,4], 1:4, [1:4;]]
913+
for A in Any[OffsetArray(p, 4),
914+
OffsetArray(reshape(p, 2, 2), 3, 4),
915+
OffsetArray(reshape(p, 2, 1, 2), 3, 0, 4),
916+
OffsetArray(reshape(p, Val(1)), 2)]
917+
@test size(A) == size(parent(A))
918+
@test length(A) == length(parent(A))
919+
end
920+
end
921+
end
910922
end
911923

912924
@testset "Axes supplied to constructor correspond to final result" begin

0 commit comments

Comments
 (0)