Skip to content

Commit

Permalink
Define similar_type and StaticArrayStyle in core package.
Browse files Browse the repository at this point in the history
  • Loading branch information
N5N3 committed Aug 23, 2022
1 parent 0f68fdb commit a46c084
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/StaticArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ using StaticArraysCore: StaticArraysCore, StaticArray, StaticScalar, StaticVecto
StaticMatrix, StaticVecOrMat, tuple_length, tuple_prod,
tuple_minimum, size_to_tuple, require_one_based_indexing
using StaticArraysCore: FieldArray, FieldMatrix, FieldVector
using StaticArraysCore: StaticArrayStyle
import StaticArraysCore: SArray, SVector, SMatrix
import StaticArraysCore: MArray, MVector, MMatrix
import StaticArraysCore: SizedArray, SizedVector, SizedMatrix
import StaticArraysCore: check_array_parameters, convert_ntuple
import StaticArraysCore: similar_type

# end of StaticArraysCore imports
# StaticArraysCore exports
Expand Down
22 changes: 0 additions & 22 deletions src/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,6 @@ end
Base.IndexStyle(::Type{T}) where {T<:StaticArray} = IndexLinear()

# Default type search for similar_type
"""
similar_type(static_array)
similar_type(static_array, T)
similar_type(array, ::Size)
similar_type(array, T, ::Size)
Returns a constructor for a statically-sized array similar to the input array
(or type) `static_array`/`array`, optionally with different element type `T` or size
`Size`. If the input `array` is not a `StaticArray` then the `Size` is mandatory.
This differs from `similar()` in that the resulting array type may not be
mutable (or define `setindex!()`), and therefore the returned type may need to
be *constructed* with its data.
Note that the (optional) size *must* be specified as a static `Size` object (so the compiler
can infer the result statically).
New types should define the signature `similar_type(::Type{A},::Type{T},::Size{S}) where {A<:MyType,T,S}`
if they wish to overload the default behavior.
"""
function similar_type end

similar_type(::SA) where {SA<:StaticArrayLike} = similar_type(SA,eltype(SA))
similar_type(::Type{SA}) where {SA<:StaticArrayLike} = similar_type(SA,eltype(SA))

Expand Down
5 changes: 1 addition & 4 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ using Base.Broadcast: broadcast_shape, _broadcast_getindex, combine_axes
import Base.Broadcast: BroadcastStyle, materialize!, instantiate
import Base.Broadcast: _bcs1 # for SOneTo axis information
using Base.Broadcast: _bcsm
# Add a new BroadcastStyle for StaticArrays, derived from AbstractArrayStyle
# A constructor that changes the style parameter N (array dimension) is also required
struct StaticArrayStyle{N} <: AbstractArrayStyle{N} end
StaticArrayStyle{M}(::Val{N}) where {M,N} = StaticArrayStyle{N}()

BroadcastStyle(::Type{<:StaticArray{<:Tuple, <:Any, N}}) where {N} = StaticArrayStyle{N}()
BroadcastStyle(::Type{<:Transpose{<:Any, <:StaticArray}}) = StaticArrayStyle{2}()
BroadcastStyle(::Type{<:Adjoint{<:Any, <:StaticArray}}) = StaticArrayStyle{2}()
Expand Down

0 comments on commit a46c084

Please sign in to comment.