Skip to content

Commit a2feccf

Browse files
committed
broadcast: replace broadcast_axes with axes
This was a crutch as we transitioned to relying purely on axes.
1 parent 9e98386 commit a2feccf

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

base/broadcast.jl

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ module Broadcast
1010
using .Base.Cartesian
1111
using .Base: Indices, OneTo, tail, to_shape, isoperator, promote_typejoin,
1212
_msk_end, unsafe_bitgetindex, bitcache_chunks, bitcache_size, dumpbitcache, unalias
13-
import .Base: copy, copyto!
13+
import .Base: copy, copyto!, axes
1414
export broadcast, broadcast!, BroadcastStyle, broadcast_axes, broadcastable, dotview, @__dot__
1515

16+
## Computing the result's axes: deprecated name
17+
const broadcast_axes = axes
18+
1619
### Objects with customized broadcasting behavior should declare a BroadcastStyle
1720

1821
"""
@@ -200,16 +203,6 @@ Base.similar(bc::Broadcasted{ArrayConflict}, ::Type{ElType}) where ElType =
200203
Base.similar(bc::Broadcasted{ArrayConflict}, ::Type{Bool}) =
201204
similar(BitArray, axes(bc))
202205

203-
## Computing the result's axes. Most types probably won't need to specialize this.
204-
"""
205-
Base.broadcast_axes(A)
206-
207-
Compute the axes for `A`.
208-
209-
This should only be specialized for objects that do not define [`axes`](@ref) but want to participate in broadcasting.
210-
"""
211-
@inline broadcast_axes(A) = axes(A)
212-
213206
@inline Base.axes(bc::Broadcasted) = _axes(bc, bc.axes)
214207
_axes(::Broadcasted, axes::Tuple) = axes
215208
@inline _axes(bc::Broadcasted, ::Nothing) = combine_axes(bc.args...)
@@ -425,8 +418,8 @@ One of these should be undefined (and thus return Broadcast.Unknown).""")
425418
end
426419

427420
# Indices utilities
428-
@inline combine_axes(A, B...) = broadcast_shape(broadcast_axes(A), combine_axes(B...))
429-
combine_axes(A) = broadcast_axes(A)
421+
@inline combine_axes(A, B...) = broadcast_shape(axes(A), combine_axes(B...))
422+
combine_axes(A) = axes(A)
430423

431424
# shape (i.e., tuple-of-indices) inputs
432425
broadcast_shape(shape::Tuple) = shape
@@ -458,7 +451,7 @@ function check_broadcast_shape(shp, Ashp::Tuple)
458451
_bcsm(shp[1], Ashp[1]) || throw(DimensionMismatch("array could not be broadcast to match destination"))
459452
check_broadcast_shape(tail(shp), tail(Ashp))
460453
end
461-
check_broadcast_axes(shp, A) = check_broadcast_shape(shp, broadcast_axes(A))
454+
check_broadcast_axes(shp, A) = check_broadcast_shape(shp, axes(A))
462455
# comparing many inputs
463456
@inline function check_broadcast_axes(shp, A, As...)
464457
check_broadcast_axes(shp, A)
@@ -482,8 +475,8 @@ an `Int`.
482475
Any remaining indices in `I` beyond the length of the `keep` tuple are truncated. The `keep` and `default`
483476
tuples may be created by `newindexer(argument)`.
484477
"""
485-
Base.@propagate_inbounds newindex(arg, I::CartesianIndex) = CartesianIndex(_newindex(broadcast_axes(arg), I.I))
486-
Base.@propagate_inbounds newindex(arg, I::Integer) = CartesianIndex(_newindex(broadcast_axes(arg), (I,)))
478+
Base.@propagate_inbounds newindex(arg, I::CartesianIndex) = CartesianIndex(_newindex(axes(arg), I.I))
479+
Base.@propagate_inbounds newindex(arg, I::Integer) = CartesianIndex(_newindex(axes(arg), (I,)))
487480
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))...)
488481
Base.@propagate_inbounds _newindex(ax::Tuple{}, I::Tuple) = ()
489482
Base.@propagate_inbounds _newindex(ax::Tuple, I::Tuple{}) = (ax[1][1], _newindex(tail(ax), ())...)
@@ -499,7 +492,7 @@ Base.@propagate_inbounds _newindex(ax::Tuple{}, I::Tuple{}) = ()
499492

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

950943
@inline function copy(bc::Broadcasted{Style{Tuple}})
951-
axes = broadcast_axes(bc)
952-
length(axes) == 1 || throw(DimensionMismatch("tuple only supports one dimension"))
953-
N = Val(length(axes[1]))
954-
return ntuple(k -> @inbounds(_broadcast_getindex(bc, k)), N)
944+
dim = axes(bc)
945+
length(dim) == 1 || throw(DimensionMismatch("tuple only supports one dimension"))
946+
N = length(dim[1])
947+
return ntuple(k -> @inbounds(_broadcast_getindex(bc, k)), Val(N))
955948
end
956949

957950
## scalar-range broadcast operations ##

doc/src/base/arrays.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ Base.@__dot__
7676
For specializing broadcast on custom types, see
7777
```@docs
7878
Base.BroadcastStyle
79-
Base.broadcast_axes
8079
Base.Broadcast.AbstractArrayStyle
8180
Base.Broadcast.ArrayStyle
8281
Base.Broadcast.DefaultArrayStyle

doc/src/manual/interfaces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ V = view(A, [1,2,4], :) # is not strided, as the spacing between rows is not f
440440
| `Base.similar(bc::Broadcasted{DestStyle}, ::Type{ElType})` | Allocation of output container |
441441
| **Optional methods** | | |
442442
| `Base.BroadcastStyle(::Style1, ::Style2) = Style12()` | Precedence rules for mixing styles |
443-
| `Base.broadcast_axes(x)` | Declaration of the indices of `x` for broadcasting purposes (defaults to [`axes(x)`](@ref)) |
443+
| `Base.axes(x)` | Declaration of the indices of `x`, as per [`axes(x)`](@ref). |
444444
| `Base.broadcastable(x)` | Convert `x` to an object that has `axes` and supports indexing |
445445
| **Bypassing default machinery** | |
446446
| `Base.copy(bc::Broadcasted{DestStyle})` | Custom implementation of `broadcast` |

test/broadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ struct T22053
709709
t
710710
end
711711
Broadcast.BroadcastStyle(::Type{T22053}) = Broadcast.Style{T22053}()
712-
Broadcast.broadcast_axes(::T22053) = ()
712+
Broadcast.axes(::T22053) = ()
713713
Broadcast.broadcastable(t::T22053) = t
714714
function Base.copy(bc::Broadcast.Broadcasted{Broadcast.Style{T22053}})
715715
all(x->isa(x, T22053), bc.args) && return 1

0 commit comments

Comments
 (0)