Skip to content

Commit 0326baf

Browse files
author
Pietro Vertechi
authored
General indexstyle clean up (#82)
* refactor bestindex and indexstyle * switch to index_type * more clean up
1 parent 0b6be8d commit 0326baf

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

src/StructArrays.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module StructArrays
22

3+
using Base: tuple_type_cons, tuple_type_head, tuple_type_tail, tail
4+
35
import Requires
46
using PooledArrays: PooledArray
57

src/lazy.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ Base.size(v::LazyRows) = size(parent(v))
4444
Base.getindex(v::LazyRows{<:Any, <:Any, <:Any, Int}, i::Int) = LazyRow(parent(v), i)
4545
Base.getindex(v::LazyRows{<:Any, <:Any, <:Any, CartesianIndex{N}}, i::Vararg{Int, N}) where {N} = LazyRow(parent(v), CartesianIndex(i))
4646

47-
_best_index(::Type{LazyRows{T, N, C, I}}) where {T, N, C, I} = I
48-
Base.IndexStyle(::Type{L}) where {L<:LazyRows} = _indexstyle(_best_index(L))
47+
index_type(::Type{LazyRows{T, N, C, I}}) where {T, N, C, I} = I
48+
function Base.IndexStyle(::Type{L}) where {L<:LazyRows}
49+
index_type(L) === Int ? IndexLinear() : IndexCartesian()
50+
end
4951

5052
function Base.showarg(io::IO, s::LazyRows{T}, toplevel) where T
5153
print(io, "LazyRows")

src/structarray.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,23 @@ struct StructArray{T, N, C<:Tup, I} <: AbstractArray{T, N}
1414
axes(c[i]) == ax || error("all field arrays must have same shape")
1515
end
1616
end
17-
new{T, N, C, _best_index(c...)}(c)
17+
new{T, N, C, index_type(C)}(c)
1818
end
1919
end
2020

21-
_best_index() = Int
22-
_best_index(col::AbstractArray, cols::AbstractArray...) = _best_index(IndexStyle(col, cols...), col)
23-
_best_index(::IndexLinear, ::AbstractArray) = Int
24-
_best_index(::IndexCartesian, ::AbstractArray{T, N}) where {T, N} = CartesianIndex{N}
25-
_best_index(::Type{StructArray{T, N, C, I}}) where {T, N, C, I} = I
26-
_indexstyle(::Type{Int}) = IndexLinear()
27-
_indexstyle(::Type{CartesianIndex{N}}) where {N} = IndexCartesian()
21+
index_type(::Type{NamedTuple{names, types}}) where {names, types} = index_type(types)
22+
index_type(::Type{Tuple{}}) = Int
23+
function index_type(::Type{T}) where {T<:Tuple}
24+
S, U = tuple_type_head(T), tuple_type_tail(T)
25+
IndexStyle(S) isa IndexCartesian ? CartesianIndex{ndims(S)} : index_type(U)
26+
end
2827

29-
_dims(c::Tup) = length(axes(c[1]))
30-
_dims(c::EmptyTup) = 1
28+
index_type(::Type{StructArray{T, N, C, I}}) where {T, N, C, I} = I
3129

3230
function StructArray{T}(c::C) where {T, C<:Tup}
3331
cols = strip_params(staticschema(T))(c)
34-
StructArray{T, _dims(cols), typeof(cols)}(cols)
32+
N = isempty(cols) ? 1 : ndims(cols[1])
33+
StructArray{T, N, typeof(cols)}(cols)
3534
end
3635

3736
StructArray(c::C) where {C<:NamedTuple} = StructArray{eltypes(C)}(c)
@@ -49,7 +48,9 @@ const StructVector{T, C<:Tup, I} = StructArray{T, 1, C, I}
4948
StructVector{T}(args...; kwargs...) where {T} = StructArray{T}(args...; kwargs...)
5049
StructVector(args...; kwargs...) = StructArray(args...; kwargs...)
5150

52-
Base.IndexStyle(::Type{S}) where {S<:StructArray} = _indexstyle(_best_index(S))
51+
function Base.IndexStyle(::Type{S}) where {S<:StructArray}
52+
index_type(S) === Int ? IndexLinear() : IndexCartesian()
53+
end
5354

5455
function _undef_array(::Type{T}, sz; unwrap = t -> false) where {T}
5556
if unwrap(T)

src/utils.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Base: tuple_type_cons, tuple_type_head, tuple_type_tail, tail
2-
31
eltypes(::Type{T}) where {T} = map_params(eltype, T)
42

53
map_params(f, ::Type{Tuple{}}) = Tuple{}

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ end
4646
s = StructArray(a=rand(10,10), b=view(rand(100,100), 1:10, 1:10))
4747
T = typeof(s)
4848
@test IndexStyle(T) === IndexCartesian()
49-
@test StructArrays._best_index(T) == CartesianIndex{2}
49+
@test StructArrays.index_type(T) == CartesianIndex{2}
5050
@test s[100] == s[10, 10] == (a=s.a[10,10], b=s.b[10,10])
5151
s[100] = (a=1, b=1)
5252
@test s[100] == s[10, 10] == (a=1, b=1)
@@ -56,7 +56,7 @@ end
5656
s = StructArray(a=rand(10,10), b=rand(10,10))
5757
T = typeof(s)
5858
@test IndexStyle(T) === IndexLinear()
59-
@test StructArrays._best_index(T) == Int
59+
@test StructArrays.index_type(T) == Int
6060
@test s[100] == s[10, 10] == (a=s.a[10,10], b=s.b[10,10])
6161
s[100] = (a=1, b=1)
6262
@test s[100] == s[10, 10] == (a=1, b=1)

0 commit comments

Comments
 (0)