Skip to content

Commit f9b16dc

Browse files
committed
Tests pass
1 parent af97a8c commit f9b16dc

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ jobs:
5252
- uses: julia-actions/setup-julia@latest
5353
with:
5454
version: '1.2'
55+
- run: julia --color=yes -e 'using Pkg; VERSION >= v"1.5-" && !isdir(joinpath(DEPOT_PATH[1], "registries", "General")) && Pkg.Registry.add("General")'
56+
shell: bash
57+
env:
58+
JULIA_PKG_SERVER: ""
5559
- run: julia --project=docs -e '
5660
using Pkg;
5761
Pkg.develop(PackageSpec(; path=pwd()));

src/ArrayInterface.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ known_length(::Type{<:Tuple{Vararg{Any,N}}}) where {N} = N
9393
known_length(::Type{<:Number}) = 1
9494
known_length(::Type{<:AbstractCartesianIndex{N}}) where {N} = N
9595
known_length(::Type{T}) where {T} = _maybe_known_length(Base.IteratorSize(T), T)
96-
_maybe_known_length(::Base.HasShape, ::Type{T}) where {T} = prod(Static.maybe_static(known_size, size, T))
96+
@inline _prod_or_nothing(x, ::Tuple{}) = x
97+
@inline _prod_or_nothing(_, ::Tuple{Nothing,Vararg}) = nothing
98+
@inline _prod_or_nothing(x, y::Tuple{I,Vararg}) where {I} = _prod_or_nothing(x*first(y), Base.tail(y))
99+
_maybe_known_length(::Base.HasShape, ::Type{T}) where {T} = _prod_or_nothing(1, known_size(T))
97100
_maybe_known_length(::Base.IteratorSize, ::Type) = nothing
98101
function known_length(::Type{<:Iterators.Flatten{I}}) where {I}
99-
known_length(I) * known_length(eltype(I))
102+
_prod_or_nothing(1, (known_length(I),known_length(eltype(I))))
100103
end
101104

102105
"""

src/dimensions.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,37 +146,39 @@ function to_parent_dims(::Type{T}, ::StaticInt{dim}) where {T,dim}
146146
throw_dim_error(T, dim)
147147
end
148148
end
149-
150-
_nunderscore(::Val{N}) where {N} = ntuple(Compat.Returns(:_), Val(N))
149+
struct Returns{T}; x::T; end; @inline (r::Returns)(_::Vararg{Any,K}) where {K} = r.x
150+
_nunderscore(::Val{N}) where {N} = ntuple(Returns(nothing), Val(N))
151151

152152
"""
153153
has_dimnames(::Type{T}) -> StaticBool
154154
155155
Returns `static(true)` if `x` has on or more named dimensions. If all dimensions correspond
156-
to `static(:_)`, then `static(false)` is returned.
156+
to `nothing`, then `static(false)` is returned.
157157
"""
158158
Compat.@constprop :aggressive has_dimnames(x) = static(_is_named(known_dimnames(x)))
159-
_is_named(x::NTuple{N,Symbol}) where {N} = x !== _nunderscore(Val(N))
160-
_is_named(::Any) = true
159+
_is_named(x::Tuple{Nothing,Vararg}) = _is_named(Base.tail(x))
160+
_is_named(x::Tuple{Nothing}) = false
161+
_is_named(x::Tuple{Symbol,Vararg}) = true
162+
_is_named(x::Tuple{}) = true
161163

162164
"""
163165
known_dimnames(::Type{T}) -> Tuple{Vararg{Union{Symbol,Nothing}}}
164166
known_dimnames(::Type{T}, dim::Union{Int,StaticInt}) -> Union{Symbol,Nothing}
165167
166-
Return the names of the dimensions for `x`. `:_` is used to indicate a dimension does not
168+
Return the names of the dimensions for `x`. `nothing` is used to indicate a dimension does not
167169
have a name.
168170
"""
169171
@inline known_dimnames(x, dim::Integer) = _known_dimname(known_dimnames(x), canonicalize(dim))
170172
known_dimnames(x) = known_dimnames(typeof(x))
171173
known_dimnames(::Type{T}) where {T} = _known_dimnames(T, parent_type(T))
172174
_known_dimnames(::Type{T}, ::Type{T}) where {T} = _unknown_dimnames(Base.IteratorSize(T))
173175
_unknown_dimnames(::Base.HasShape{N}) where {N} = _nunderscore(Val(N))
174-
_unknown_dimnames(::Any) = (:_,)
176+
_unknown_dimnames(::Any) = (nothing,)
175177
function _known_dimnames(::Type{C}, ::Type{P}) where {C,P}
176178
eachop(_inbounds_known_dimname, to_parent_dims(C), known_dimnames(P))
177179
end
178180
@inline function _known_dimname(x::Tuple{Vararg{Any,N}}, dim::CanonicalInt) where {N}
179-
@boundscheck (dim > N || dim < 1) && return :_
181+
(dim > N || dim < 1) && return nothing
180182
return @inbounds(x[dim])
181183
end
182184
@inline _inbounds_known_dimname(x, dim) = @inbounds(_known_dimname(x, dim))
@@ -185,17 +187,17 @@ end
185187
dimnames(x) -> Tuple{Vararg{Union{Symbol,StaticSymbol}}}
186188
dimnames(x, dim::Union{Int,StaticInt}) -> Union{Symbol,StaticSymbol}
187189
188-
Return the names of the dimensions for `x`. `:_` is used to indicate a dimension does not
190+
Return the names of the dimensions for `x`. `nothing` is used to indicate a dimension does not
189191
have a name.
190192
"""
191193
@inline dimnames(x, dim::Integer) = _dimname(dimnames(x), canonicalize(dim))
192194
@inline dimnames(x) = _dimnames(has_parent(x), x)
193195
@inline function _dimnames(::True, x)
194196
eachop(_inbounds_dimname, to_parent_dims(x), dimnames(parent(x)))
195197
end
196-
_dimnames(::False, x) = ntuple(_->static(:_), Val(ndims(x)))
198+
_dimnames(::False, x) = ntuple(_->nothing, Val(ndims(x)))
197199
@inline function _dimname(x::Tuple{Vararg{Any,N}}, dim::CanonicalInt) where {N}
198-
@boundscheck (dim > N || dim < 1) && return static(:_)
200+
@boundscheck (dim > N || dim < 1) && return nothing
199201
return @inbounds(x[dim])
200202
end
201203
@inline _inbounds_dimname(x, dim) = @inbounds(_dimname(x, dim))

test/dimensions.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,18 @@ end
101101
@test @inferred(ArrayInterface.has_dimnames(typeof(view(x, :, 1, :)))) == true
102102
@test @inferred(dimnames(x)) === d
103103
@test @inferred(ArrayInterface.dimnames(z)) === (:x, static(:y))
104-
@test @inferred(dimnames(parent(x))) === (static(:_), static(:_))
104+
@test @inferred(dimnames(parent(x))) === (nothing, nothing)
105105
@test @inferred(dimnames(x')) === reverse(d)
106-
@test @inferred(dimnames(y')) === (static(:_), static(:x))
106+
@test @inferred(dimnames(y')) === (nothing, static(:x))
107107
@test @inferred(dimnames(PermutedDimsArray(x, (2, 1)))) === reverse(d)
108108
@test @inferred(dimnames(PermutedDimsArray(x', (2, 1)))) === d
109109
@test @inferred(dimnames(view(x, :, 1))) === (static(:x),)
110-
@test @inferred(dimnames(view(x, :, :, :))) === (static(:x),static(:y), static(:_))
111-
@test @inferred(dimnames(view(x, :, 1, :))) === (static(:x), static(:_))
110+
@test @inferred(dimnames(view(x, :, :, :))) === (static(:x),static(:y), nothing)
111+
@test @inferred(dimnames(view(x, :, 1, :))) === (static(:x), nothing)
112112
@test @inferred(dimnames(x, ArrayInterface.One())) === static(:x)
113-
@test @inferred(dimnames(parent(x), ArrayInterface.One())) === static(:_)
114-
@test @inferred(ArrayInterface.known_dimnames(Iterators.flatten(1:10))) === (:_,)
115-
@test @inferred(ArrayInterface.known_dimnames(Iterators.flatten(1:10), static(1))) === :_
113+
@test @inferred(dimnames(parent(x), ArrayInterface.One())) === nothing
114+
@test @inferred(ArrayInterface.known_dimnames(Iterators.flatten(1:10))) === (nothing,)
115+
@test @inferred(ArrayInterface.known_dimnames(Iterators.flatten(1:10), static(1))) === nothing
116116
@test @inferred(ArrayInterface.known_dimnames(z)) === (nothing, :y)
117117
end
118118

0 commit comments

Comments
 (0)