Skip to content

Commit d9791eb

Browse files
jishnubpiever
andauthored
IteratorSize for a StructArray (#290)
* IteratorSize for a StructArray * Whitespace Co-authored-by: Pietro Vertechi <[email protected]> * Alphabetical order of test targets --------- Co-authored-by: Pietro Vertechi <[email protected]>
1 parent 8b665db commit d9791eb

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Adapt = "3.4"
2828
ConstructionBase = "1"
2929
DataAPI = "1"
3030
GPUArraysCore = "0.1.2"
31+
InfiniteArrays = "0.13"
3132
StaticArrays = "1.5.6"
3233
Tables = "1"
3334
julia = "1.6"
@@ -36,6 +37,7 @@ julia = "1.6"
3637
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
3738
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3839
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
40+
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
3941
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
4042
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
4143
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
@@ -47,4 +49,4 @@ TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"
4749
WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
4850

4951
[targets]
50-
test = ["Adapt", "Documenter", "GPUArraysCore", "JLArrays", "LinearAlgebra", "OffsetArrays", "PooledArrays", "SparseArrays", "StaticArrays", "Test", "TypedTables", "WeakRefStrings"]
52+
test = ["Adapt", "Documenter", "GPUArraysCore", "InfiniteArrays", "JLArrays", "LinearAlgebra", "OffsetArrays", "PooledArrays", "SparseArrays", "StaticArrays", "Test", "TypedTables", "WeakRefStrings"]

src/structarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,7 @@ function Broadcast.broadcast_unalias(dest::StructArray, src::AbstractArray)
628628
end
629629

630630
Base.dataids(u::StructArray) = mapreduce(Base.dataids, (a, b) -> (a..., b...), values(components(u)), init=())
631+
632+
# Since all the components have the same axes, we choose the type of the first one to
633+
# define IteratorSize for a StructArray
634+
Base.IteratorSize(::Type{<:StructArray{<:Any,<:Any,C}}) where {C} = Base.IteratorSize(fieldtype(C, 1))

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ using GPUArraysCore: backend
1111
using LinearAlgebra
1212
using Test
1313
using SparseArrays
14+
using InfiniteArrays
1415

1516
using Documenter: doctest
1617
if Base.VERSION >= v"1.6" && Int === Int64
@@ -1500,3 +1501,12 @@ end
15001501
ss = map(x -> PS(x.a, x.b), xs)
15011502
@test ss == [PS(1, 2), PS(3, nothing)]
15021503
end
1504+
1505+
@testset "IteratorSize" begin
1506+
S = StructArray{ComplexF64}((zeros(1), zeros(1)))
1507+
@test Base.IteratorSize(S) == Base.HasShape{1}()
1508+
S = StructArray{ComplexF64}((zeros(1,2), zeros(1,2)))
1509+
@test Base.IteratorSize(S) == Base.HasShape{2}()
1510+
S = StructArray{Complex{Int}}((1:∞, 1:∞))
1511+
@test Base.IteratorSize(S) == Base.IsInfinite()
1512+
end

0 commit comments

Comments
 (0)