-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Accept non-AbstractArrays in 2-arg axes/size
#59512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jishnub
wants to merge
5
commits into
master
Choose a base branch
from
jishnub/axes_relax
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ffe7bee
Accept non-`AbstractArray`s in 2-arg `axes`/`size`
jishnub 26c9e3c
length of tuple instead of ndims and restrict to HasShape
jishnub 0a7acd9
Trailing whitespace
jishnub 96f38ab
Update base/abstractarray.jl
jishnub 63a15d5
Update base/abstractarray.jl
jishnub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -18,14 +18,20 @@ convert(::Type{AbstractArray{T}}, a::AbstractArray) where {T} = AbstractArray{T} | |||
| convert(::Type{AbstractArray{T,N}}, a::AbstractArray{<:Any,N}) where {T,N} = AbstractArray{T,N}(a)::AbstractArray{T,N} | ||||
|
|
||||
| """ | ||||
| size(A::AbstractArray, [dim]) | ||||
| size(A, [dim]) | ||||
|
|
||||
| Return a tuple containing the dimensions of `A`. Optionally you can specify a | ||||
| dimension to just get the length of that dimension. | ||||
| Return a tuple containing the dimensions of an `AbstractArray` (or a multidimensional iterator) `A`. | ||||
| Optionally you may specify a dimension to just get the length of that dimension. | ||||
|
|
||||
| Non-`AbstractArray` arguments `A` to the two-argument `size` must satisfy | ||||
| `Base.IteratorSize(A) isa Base.HasShape`. | ||||
|
|
||||
| Note that `size` may not be defined for arrays with non-standard indices, in which case [`axes`](@ref) | ||||
| may be useful. See the manual chapter on [arrays with custom indices](@ref man-custom-indices). | ||||
|
|
||||
| !!! compat "Julia 1.13" | ||||
| `size(A, d)` where `A` is not an `AbstractArray` requires at least Julia 1.13. | ||||
|
|
||||
| See also: [`length`](@ref), [`ndims`](@ref), [`eachindex`](@ref), [`sizeof`](@ref). | ||||
|
|
||||
| # Examples | ||||
|
|
@@ -39,12 +45,24 @@ julia> size(A, 2) | |||
| 3 | ||||
| ``` | ||||
| """ | ||||
| size(t::AbstractArray{T,N}, d) where {T,N} = d::Integer <= N ? size(t)[d] : 1 | ||||
| function size(A, d) | ||||
| @inline | ||||
| _size(IteratorSize(A), size(A), d) | ||||
| end | ||||
| function _size(::HasShape, s::Tuple, d::Integer) | ||||
| @inline | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| d_Int = Int(d)::Int | ||||
| d_Int <= length(s) ? s[d_Int] : 1 | ||||
| end | ||||
|
|
||||
| """ | ||||
| axes(A, d) | ||||
|
|
||||
| Return the valid range of indices for array `A` along dimension `d`. | ||||
| Return the valid range of indices for an `AbstractArray` (or a mutidimensional iterator) | ||||
| `A` along dimension `d`. | ||||
|
|
||||
| Non-`AbstractArray` arguments `A` to the two-argument `axes` must satisfy | ||||
| `Base.IteratorSize(A) isa Base.HasShape`. | ||||
|
|
||||
| See also [`size`](@ref), and the manual chapter on [arrays with custom indices](@ref man-custom-indices). | ||||
|
|
||||
|
|
@@ -66,15 +84,23 @@ Each of the indices has to be an `AbstractUnitRange{<:Integer}`, but at the same | |||
| a type that uses custom indices. So, for example, if you need a subset, use generalized | ||||
| indexing constructs like `begin`/`end` or [`firstindex`](@ref)/[`lastindex`](@ref): | ||||
|
|
||||
| !!! compat "Julia 1.13" | ||||
| `axes(A, d)` where `A` is not an `AbstractArray` requires at least Julia 1.13. | ||||
|
|
||||
| ```julia | ||||
| ix = axes(v, 1) | ||||
| ix[2:end] # will work for eg Vector, but may fail in general | ||||
| ix[(begin+1):end] # works for generalized indexes | ||||
| ``` | ||||
| """ | ||||
| function axes(A::AbstractArray{T,N}, d) where {T,N} | ||||
| function axes(A, d) | ||||
| @inline | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| _axes(IteratorSize(A), axes(A), d) | ||||
| end | ||||
| function _axes(::HasShape, ax::Tuple, d::Integer) | ||||
| @inline | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| d::Integer <= N ? axes(A)[d] : OneTo(1) | ||||
| d_Int = Int(d)::Int | ||||
| d_Int <= length(ax) ? ax[d_Int] : OneTo(1) | ||||
| end | ||||
|
|
||||
| """ | ||||
|
|
||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are this
@inline, and other@inlines, actually necessary? Generally I think it's better to leave the inlining to the compiler in generic methods, except maybe for recursive methods, or as a workaround for issue #53584/#56587.