Skip to content

Commit abbdc6a

Browse files
authored
General Improvements to indexing (#137)
* Improved docs for indices * consolidated the flatten_* code within to_indices * removed superfluous keyword arguments from several functions * Added support for IndexCartesian style to_index * Multidim -> linear indexing
1 parent 903e6ef commit abbdc6a

File tree

8 files changed

+310
-399
lines changed

8 files changed

+310
-399
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "3.1.6"
3+
version = "3.1.7"
44

55
[deps]
66
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"

src/ArrayInterface.jl

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ using Static: Zero, One, nstatic, _get_tuple, eq, ne, gt, ge, lt, le, eachop, ea
1010
using Base.Cartesian
1111

1212
using Base: @propagate_inbounds, tail, OneTo, LogicalIndex, Slice, ReinterpretArray,
13-
ReshapedArray
13+
ReshapedArray, AbstractCartesianIndex
1414

1515

16+
## utilites for internal use only ##
17+
_int_or_static_int(::Nothing) = Int
18+
_int_or_static_int(x::Int) = StaticInt{x}
19+
_int(i::Integer) = Int(i)
20+
_int(i::StaticInt) = i
21+
1622
@static if VERSION >= v"1.7.0-DEV.421"
1723
using Base: @aggressive_constprop
1824
else
@@ -21,6 +27,8 @@ else
2127
end
2228
end
2329

30+
static_ndims(x) = static(ndims(x))
31+
2432
if VERSION v"1.6.0-DEV.1581"
2533
_is_reshaped(::Type{ReinterpretArray{T,N,S,A,true}}) where {T,N,S,A} = true
2634
_is_reshaped(::Type{ReinterpretArray{T,N,S,A,false}}) where {T,N,S,A} = false
@@ -34,6 +42,8 @@ parameterless_type(x::Type) = __parameterless_type(x)
3442

3543
const VecAdjTrans{T,V<:AbstractVector{T}} = Union{Transpose{T,V},Adjoint{T,V}}
3644
const MatAdjTrans{T,M<:AbstractMatrix{T}} = Union{Transpose{T,M},Adjoint{T,M}}
45+
const UpTri{T,M} = Union{UpperTriangular{T,M},UnitUpperTriangular{T,M}}
46+
const LoTri{T,M} = Union{LowerTriangular{T,M},UnitLowerTriangular{T,M}}
3747

3848
@inline static_length(a::UnitRange{T}) where {T} = last(a) - first(a) + oneunit(T)
3949
@inline static_length(x) = Static.maybe_static(known_length, length, x)
@@ -57,12 +67,15 @@ parent_type(::Type{<:PermutedDimsArray{T,N,I1,I2,A}}) where {T,N,I1,I2,A} = A
5767
parent_type(::Type{Slice{T}}) where {T} = T
5868
parent_type(::Type{T}) where {T} = T
5969
parent_type(::Type{R}) where {S,T,A,N,R<:Base.ReinterpretArray{T,N,S,A}} = A
60-
70+
parent_type(::Type{LoTri{T,M}}) where {T,M} = M
71+
parent_type(::Type{UpTri{T,M}}) where {T,M} = M
72+
parent_type(::Type{Diagonal{T,V}}) where {T,V} = V
6173
"""
6274
has_parent(::Type{T}) -> StaticBool
6375
6476
Returns `True` if `parent_type(T)` a type unique to `T`.
6577
"""
78+
has_parent(x) = has_parent(typeof(x))
6679
has_parent(::Type{T}) where {T} = _has_parent(parent_type(T), T)
6780
_has_parent(::Type{T}, ::Type{T}) where {T} = False()
6881
_has_parent(::Type{T1}, ::Type{T2}) where {T1,T2} = True()
@@ -786,15 +799,6 @@ end
786799
end
787800
end
788801

789-
include("ranges.jl")
790-
include("indexing.jl")
791-
include("dimensions.jl")
792-
include("axes.jl")
793-
include("size.jl")
794-
include("stridelayout.jl")
795-
include("broadcast.jl")
796-
797-
798802
abstract type AbstractArray2{T,N} <: AbstractArray{T,N} end
799803

800804
Base.size(A::AbstractArray2) = map(Int, ArrayInterface.size(A))
@@ -836,6 +840,14 @@ end
836840
return setindex!(A, val; kwargs...)
837841
end
838842

843+
include("ranges.jl")
844+
include("indexing.jl")
845+
include("dimensions.jl")
846+
include("axes.jl")
847+
include("size.jl")
848+
include("stridelayout.jl")
849+
include("broadcast.jl")
850+
839851
function __init__()
840852

841853
@require SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" begin

src/axes.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ function axes_types(::Type{T}) where {N,T<:Base.ReshapedArray{<:Any,N}}
5757
return Tuple{Vararg{OptionallyStaticUnitRange{One,Int},N}}
5858
end
5959

60-
_int_or_static_int(::Nothing) = Int
61-
_int_or_static_int(x::Int) = StaticInt{x}
62-
6360
@inline function axes_types(::Type{T}) where {N,P,I,T<:SubArray{<:Any,N,P,I}}
6461
return eachop_tuple(_sub_axis_type, to_parent_dims(T), T)
6562
end
@@ -126,6 +123,8 @@ similar_type(::Type{OptionallyStaticUnitRange{One,StaticInt{N1}}}, ::Type{Int},
126123
Return a valid range that maps to each index along dimension `d` of `A`.
127124
"""
128125
axes(a, dim) = axes(a, to_dims(a, dim))
126+
axes(a, dims::Tuple) = (axes(a, first(dims)), axes(a, tail(dims))...)
127+
axes(a, ::Tuple{}) = ()
129128
function axes(a::A, dim::Integer) where {A}
130129
if parent_type(A) <: A
131130
return Base.axes(a, Int(dim))
@@ -157,4 +156,6 @@ end
157156
axes(A::SubArray) = Base.axes(A) # TODO implement ArrayInterface version
158157
axes(A::ReinterpretArray) = Base.axes(A) # TODO implement ArrayInterface version
159158
axes(A::Base.ReshapedArray) = Base.axes(A) # TODO implement ArrayInterface version
159+
axes(A::CartesianIndices) = A.indices
160+
axes(A::LinearIndices) = A.indices
160161

0 commit comments

Comments
 (0)