Skip to content

Commit

Permalink
broadcast: replace broadcast_axes with axes
Browse files Browse the repository at this point in the history
This was a crutch as we transitioned to relying purely on axes.
  • Loading branch information
vtjnash committed Nov 1, 2018
1 parent 9e98386 commit a2feccf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
37 changes: 15 additions & 22 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ module Broadcast
using .Base.Cartesian
using .Base: Indices, OneTo, tail, to_shape, isoperator, promote_typejoin,
_msk_end, unsafe_bitgetindex, bitcache_chunks, bitcache_size, dumpbitcache, unalias
import .Base: copy, copyto!
import .Base: copy, copyto!, axes
export broadcast, broadcast!, BroadcastStyle, broadcast_axes, broadcastable, dotview, @__dot__

## Computing the result's axes: deprecated name
const broadcast_axes = axes

### Objects with customized broadcasting behavior should declare a BroadcastStyle

"""
Expand Down Expand Up @@ -200,16 +203,6 @@ Base.similar(bc::Broadcasted{ArrayConflict}, ::Type{ElType}) where ElType =
Base.similar(bc::Broadcasted{ArrayConflict}, ::Type{Bool}) =
similar(BitArray, axes(bc))

## Computing the result's axes. Most types probably won't need to specialize this.
"""
Base.broadcast_axes(A)
Compute the axes for `A`.
This should only be specialized for objects that do not define [`axes`](@ref) but want to participate in broadcasting.
"""
@inline broadcast_axes(A) = axes(A)

@inline Base.axes(bc::Broadcasted) = _axes(bc, bc.axes)
_axes(::Broadcasted, axes::Tuple) = axes
@inline _axes(bc::Broadcasted, ::Nothing) = combine_axes(bc.args...)
Expand Down Expand Up @@ -425,8 +418,8 @@ One of these should be undefined (and thus return Broadcast.Unknown).""")
end

# Indices utilities
@inline combine_axes(A, B...) = broadcast_shape(broadcast_axes(A), combine_axes(B...))
combine_axes(A) = broadcast_axes(A)
@inline combine_axes(A, B...) = broadcast_shape(axes(A), combine_axes(B...))
combine_axes(A) = axes(A)

# shape (i.e., tuple-of-indices) inputs
broadcast_shape(shape::Tuple) = shape
Expand Down Expand Up @@ -458,7 +451,7 @@ function check_broadcast_shape(shp, Ashp::Tuple)
_bcsm(shp[1], Ashp[1]) || throw(DimensionMismatch("array could not be broadcast to match destination"))
check_broadcast_shape(tail(shp), tail(Ashp))
end
check_broadcast_axes(shp, A) = check_broadcast_shape(shp, broadcast_axes(A))
check_broadcast_axes(shp, A) = check_broadcast_shape(shp, axes(A))
# comparing many inputs
@inline function check_broadcast_axes(shp, A, As...)
check_broadcast_axes(shp, A)
Expand All @@ -482,8 +475,8 @@ an `Int`.
Any remaining indices in `I` beyond the length of the `keep` tuple are truncated. The `keep` and `default`
tuples may be created by `newindexer(argument)`.
"""
Base.@propagate_inbounds newindex(arg, I::CartesianIndex) = CartesianIndex(_newindex(broadcast_axes(arg), I.I))
Base.@propagate_inbounds newindex(arg, I::Integer) = CartesianIndex(_newindex(broadcast_axes(arg), (I,)))
Base.@propagate_inbounds newindex(arg, I::CartesianIndex) = CartesianIndex(_newindex(axes(arg), I.I))
Base.@propagate_inbounds newindex(arg, I::Integer) = CartesianIndex(_newindex(axes(arg), (I,)))
Base.@propagate_inbounds _newindex(ax::Tuple, I::Tuple) = (ifelse(Base.unsafe_length(ax[1])==1, ax[1][1], I[1]), _newindex(tail(ax), tail(I))...)
Base.@propagate_inbounds _newindex(ax::Tuple{}, I::Tuple) = ()
Base.@propagate_inbounds _newindex(ax::Tuple, I::Tuple{}) = (ax[1][1], _newindex(tail(ax), ())...)
Expand All @@ -499,7 +492,7 @@ Base.@propagate_inbounds _newindex(ax::Tuple{}, I::Tuple{}) = ()

# newindexer(A) generates `keep` and `Idefault` (for use by `newindex` above)
# for a particular array `A`; `shapeindexer` does so for its axes.
@inline newindexer(A) = shapeindexer(broadcast_axes(A))
@inline newindexer(A) = shapeindexer(axes(A))
@inline shapeindexer(ax) = _newindexer(ax)
@inline _newindexer(indsA::Tuple{}) = (), ()
@inline function _newindexer(indsA::Tuple)
Expand Down Expand Up @@ -542,7 +535,7 @@ struct Extruded{T, K, D}
keeps::K # A tuple of booleans, specifying which indices should be passed normally
defaults::D # A tuple of integers, specifying the index to use when keeps[i] is false (as defaults[i])
end
@inline broadcast_axes(b::Extruded) = broadcast_axes(b.x)
@inline axes(b::Extruded) = axes(b.x)
Base.@propagate_inbounds _broadcast_getindex(b::Extruded, i) = b.x[newindex(i, b.keeps, b.defaults)]
extrude(x::AbstractArray) = Extruded(x, newindexer(x)...)
extrude(x) = x
Expand Down Expand Up @@ -948,10 +941,10 @@ end
## Tuple methods

@inline function copy(bc::Broadcasted{Style{Tuple}})
axes = broadcast_axes(bc)
length(axes) == 1 || throw(DimensionMismatch("tuple only supports one dimension"))
N = Val(length(axes[1]))
return ntuple(k -> @inbounds(_broadcast_getindex(bc, k)), N)
dim = axes(bc)
length(dim) == 1 || throw(DimensionMismatch("tuple only supports one dimension"))
N = length(dim[1])
return ntuple(k -> @inbounds(_broadcast_getindex(bc, k)), Val(N))
end

## scalar-range broadcast operations ##
Expand Down
1 change: 0 additions & 1 deletion doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ Base.@__dot__
For specializing broadcast on custom types, see
```@docs
Base.BroadcastStyle
Base.broadcast_axes
Base.Broadcast.AbstractArrayStyle
Base.Broadcast.ArrayStyle
Base.Broadcast.DefaultArrayStyle
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ V = view(A, [1,2,4], :) # is not strided, as the spacing between rows is not f
| `Base.similar(bc::Broadcasted{DestStyle}, ::Type{ElType})` | Allocation of output container |
| **Optional methods** | | |
| `Base.BroadcastStyle(::Style1, ::Style2) = Style12()` | Precedence rules for mixing styles |
| `Base.broadcast_axes(x)` | Declaration of the indices of `x` for broadcasting purposes (defaults to [`axes(x)`](@ref)) |
| `Base.axes(x)` | Declaration of the indices of `x`, as per [`axes(x)`](@ref). |
| `Base.broadcastable(x)` | Convert `x` to an object that has `axes` and supports indexing |
| **Bypassing default machinery** | |
| `Base.copy(bc::Broadcasted{DestStyle})` | Custom implementation of `broadcast` |
Expand Down
2 changes: 1 addition & 1 deletion test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ struct T22053
t
end
Broadcast.BroadcastStyle(::Type{T22053}) = Broadcast.Style{T22053}()
Broadcast.broadcast_axes(::T22053) = ()
Broadcast.axes(::T22053) = ()
Broadcast.broadcastable(t::T22053) = t
function Base.copy(bc::Broadcast.Broadcasted{Broadcast.Style{T22053}})
all(x->isa(x, T22053), bc.args) && return 1
Expand Down

0 comments on commit a2feccf

Please sign in to comment.