Skip to content

Commit a6d5b6a

Browse files
author
Pietro Vertechi
committed
support reshaping by ranges
1 parent c7e7805 commit a6d5b6a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/structarray.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,9 @@ Base.copy(s::StructArray{T,N,C}) where {T,N,C} = StructArray{T,N,C}(C(copy(x) fo
188188
function Base.reshape(s::StructArray{T}, d::Dims) where {T}
189189
StructArray{T}(map(x -> reshape(x, d), fieldarrays(s)))
190190
end
191+
192+
for typ in [:Integer, :(Base.OneTo), :UnitRange, :(Base.IdentityUnitRange)]
193+
@eval function Base.reshape(s::StructArray{T}, d::Tuple{$typ, Vararg{$typ}}) where {T}
194+
StructArray{T}(map(x -> reshape(x, d), fieldarrays(s)))
195+
end
196+
end

test/REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Tables
22
WeakRefStrings
3+
OffsetArrays

test/runtests.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using StructArrays
2+
using OffsetArrays: OffsetArray
23
import Tables, PooledArrays, WeakRefStrings
34
using Test
45

@@ -459,6 +460,30 @@ end
459460
@test v.b == [j for i in 1:3, j in 1:4]
460461
end
461462

463+
@testset "collectoffset" begin
464+
s = OffsetArray([(a=1,) for i in 1:10], -3)
465+
sa = StructArray(s)
466+
@test sa isa StructArray
467+
@test axes(sa) == (-2:7,)
468+
@test sa.a == fill(1, -2:7)
469+
470+
sa = StructArray(i for i in s)
471+
@test sa isa StructArray
472+
@test axes(sa) == (-2:7,)
473+
@test sa.a == fill(1, -2:7)
474+
end
475+
476+
@testset "reshape" begin
477+
s = StructArray(a=[1,2,3,4], b=["a","b","c","d"])
478+
rs = reshape(s, (2, 2))
479+
@test rs.a == [1 3; 2 4]
480+
@test rs.b == ["a" "c"; "b" "d"]
481+
482+
os = reshape(s, -1:2)
483+
@test os.a == OffsetArray([1,2,3,4], -2)
484+
@test os.b == OffsetArray(["a","b","c","d"], -2)
485+
end
486+
462487
@testset "lazy" begin
463488
s = StructArray(rand(ComplexF64, 10, 10))
464489
rows = LazyRows(s)

0 commit comments

Comments
 (0)