-
Notifications
You must be signed in to change notification settings - Fork 152
WIP Add size to abstract StaticArray type #134
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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 |
---|---|---|
@@ -1,72 +1,70 @@ | ||
""" | ||
SArray{Size, T, L}(x::NTuple{L, T}) | ||
SArray{Size, T, L}(x1, x2, x3, ...) | ||
SArray{S, T, L}(x::NTuple{L, T}) | ||
SArray{S, T, L}(x1, x2, x3, ...) | ||
|
||
Construct a statically-sized array `SArray`. Since this type is immutable, | ||
the data must be provided upon construction and cannot be mutated later. The | ||
`Size` parameter is a Tuple specifying the dimensions of the array. The | ||
`L` parameter is the `length` of the array and is always equal to `prod(S)`. | ||
Constructors may drop the `L` and `T` parameters if they are inferrable | ||
from the input (e.g. `L` is always inferrable from `Size`). | ||
Construct a statically-sized array `SArray`. Since this type is immutable, the data must be | ||
provided upon construction and cannot be mutated later. The `S` parameter is a Tuple-type | ||
specifying the dimensions, or size, of the array - such as `Tuple{3,4,5}` for a 3×4×5-sized | ||
array. The `L` parameter is the `length` of the array and is always equal to `prod(S)`. | ||
Constructors may drop the `L` and `T` parameters if they are inferrable from the input | ||
(e.g. `L` is always inferrable from `S`). | ||
|
||
SArray{Size}(a::Array) | ||
SArray{S}(a::Array) | ||
|
||
Construct a statically-sized array of dimensions `Size` using the data from | ||
`a`. The `Size` parameter is mandatory since the size of `a` is unknown to the | ||
Construct a statically-sized array of dimensions `S` (expressed as a `Tuple{...}`) using | ||
the data from `a`. The `S` parameter is mandatory since the size of `a` is unknown to the | ||
compiler (the element type may optionally also be specified). | ||
""" | ||
immutable SArray{Size, T, N, L} <: StaticArray{T, N} | ||
immutable SArray{S <: Tuple, T, N, L} <: StaticArray{S, T, N} | ||
data::NTuple{L,T} | ||
|
||
function (::Type{SArray{Size,T,N,L}}){Size,T,N,L}(x::NTuple{L,T}) | ||
check_array_parameters(Size, T, Val{N}, Val{L}) | ||
new{Size,T,N,L}(x) | ||
function (::Type{SArray{S, T, N, L}}){S, T, N, L}(x::NTuple{L,T}) | ||
check_array_parameters(S, T, Val{N}, Val{L}) | ||
new{S, T, N, L}(x) | ||
end | ||
|
||
function (::Type{SArray{Size,T,N,L}}){Size,T,N,L}(x::NTuple{L,Any}) | ||
check_array_parameters(Size, T, Val{N}, Val{L}) | ||
new{Size,T,N,L}(convert_ntuple(T, x)) | ||
function (::Type{SArray{S, T, N, L}}){S, T, N, L}(x::NTuple{L,Any}) | ||
check_array_parameters(S, T, Val{N}, Val{L}) | ||
new{S, T, N, L}(convert_ntuple(T, x)) | ||
end | ||
end | ||
|
||
@generated function (::Type{SArray{Size,T,N}}){Size <: Tuple,T,N}(x::Tuple) | ||
@generated function (::Type{SArray{S, T, N}}){S <: Tuple, T, N}(x::Tuple) | ||
return quote | ||
$(Expr(:meta, :inline)) | ||
SArray{Size,T,N,$(tuple_prod(Size))}(x) | ||
@_inline_meta | ||
SArray{S, T, N, $(tuple_prod(S))}(x) | ||
end | ||
end | ||
|
||
@generated function (::Type{SArray{Size,T}}){Size <: Tuple,T}(x::Tuple) | ||
@generated function (::Type{SArray{S, T}}){S <: Tuple, T}(x::Tuple) | ||
return quote | ||
$(Expr(:meta, :inline)) | ||
SArray{Size,T,$(tuple_length(Size)),$(tuple_prod(Size))}(x) | ||
@_inline_meta | ||
SArray{S, T, $(tuple_length(S)), $(tuple_prod(S))}(x) | ||
end | ||
end | ||
|
||
@generated function (::Type{SArray{Size}}){Size <: Tuple, T <: Tuple}(x::T) | ||
@generated function (::Type{SArray{S}}){S <: Tuple, T <: Tuple}(x::T) | ||
return quote | ||
$(Expr(:meta, :inline)) | ||
SArray{Size,$(promote_tuple_eltype(T)),$(tuple_length(Size)),$(tuple_prod(Size))}(x) | ||
@_inline_meta | ||
SArray{S, $(promote_tuple_eltype(T)), $(tuple_length(S)), $(tuple_prod(S))}(x) | ||
end | ||
end | ||
|
||
@inline SArray(a::StaticArray) = SArray{size_tuple(a)}(Tuple(a)) | ||
@inline SArray(a::StaticArray) = SArray{size_tuple(a)}(Tuple(a)) # TODO fixme | ||
|
||
# Simplified show for the type | ||
show(io::IO, ::Type{SArray{S, T, N}}) where {S, T, N} = print(io, "SArray{$S,$T,$N}") | ||
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. Nice. |
||
|
||
# Some more advanced constructor-like functions | ||
@inline one(::Type{SArray{S}}) where {S} = one(SArray{S,Float64,tuple_length(S)}) | ||
@inline eye(::Type{SArray{S}}) where {S} = eye(SArray{S,Float64,tuple_length(S)}) | ||
@inline one(::Type{SArray{S,T}}) where {S,T} = one(SArray{S,T,tuple_length(S)}) | ||
@inline eye(::Type{SArray{S,T}}) where {S,T} = eye(SArray{S,T,tuple_length(S)}) | ||
@inline one(::Type{SArray{S}}) where {S} = one(SArray{S, Float64, tuple_length(S)}) | ||
@inline eye(::Type{SArray{S}}) where {S} = eye(SArray{S, Float64, tuple_length(S)}) | ||
@inline one(::Type{SArray{S, T}}) where {S, T} = one(SArray{S, T, tuple_length(S)}) | ||
@inline eye(::Type{SArray{S, T}}) where {S, T} = eye(SArray{S, T, tuple_length(S)}) | ||
|
||
#################### | ||
## SArray methods ## | ||
#################### | ||
|
||
@pure Size{S}(::Type{SArray{S}}) = Size(S) | ||
@pure Size{S,T}(::Type{SArray{S,T}}) = Size(S) | ||
@pure Size{S,T,N}(::Type{SArray{S,T,N}}) = Size(S) | ||
@pure Size{S,T,N,L}(::Type{SArray{S,T,N,L}}) = Size(S) | ||
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. Nice′ |
||
|
||
function getindex(v::SArray, i::Int) | ||
Base.@_inline_meta | ||
v.data[i] | ||
|
Oops, something went wrong.
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.
Perhaps drop the
L
from the constructor usage docs here (the docs below go on to suggest that this is the right thing). Is there any reason people should ever need to specifyL
explicitly?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.
This I always find a bit weird. In some sense, we are documenting the type and the constructors. The one without
T
andL
is documented below.The
L
is necessary to know about whenever you want to specify it as an element type of an array or a member of a struct, so this was my effort to get the user to find out about it's existence.Though now I notice that I forgot the
N
!Uh oh!
There was an error while loading. Please reload this page.
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.
Ah, righto. Yes, I find the constructor/type dichotomy odd too. I often wish the doc system would provide more help here, in terms of autodocumenting the type signature docs, in a way which is systematic but not intrusive. Oh ho, look at this trick:
Kinda close?
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.
Still, even if this was neater, it relies on package authors being systematic and good at writing docs...