Skip to content

Commit bb79e83

Browse files
nalimilanmbauman
authored andcommitted
Add docstring for keys(::AbstractArray) (JuliaLang#36073)
This is helpful for users, and it matters because some Base functions may rely on these guarantees for their correct behavior (e.g. `hash`). Co-authored-by: Matt Bauman <[email protected]>
1 parent 293d1f2 commit bb79e83

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

base/abstractarray.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ axes1(iter) = oneto(length(iter))
119119
unsafe_indices(A) = axes(A)
120120
unsafe_indices(r::AbstractRange) = (oneto(unsafe_length(r)),) # Ranges use checked_sub for size
121121

122+
"""
123+
keys(a::AbstractArray)
124+
125+
Return an efficient array describing all valid indices for `a` arranged in the shape of `a` itself.
126+
127+
They keys of 1-dimensional arrays (vectors) are integers, whereas all other N-dimensional
128+
arrays use [`CartesianIndex`](@ref) to describe their locations. Often the special array
129+
types [`LinearIndices`](@ref) and [`CartesianIndices`](@ref) are used to efficiently
130+
represent these arrays of integers and `CartesianIndex`es, respectively.
131+
132+
Note that the `keys` of an array might not be the most efficient index type; for maximum
133+
performance use [`eachindex`](@ref) instead.
134+
"""
122135
keys(a::AbstractArray) = CartesianIndices(axes(a))
123136
keys(a::AbstractVector) = LinearIndices(a)
124137

doc/src/base/arrays.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Base.size
5252
Base.axes(::Any)
5353
Base.axes(::AbstractArray, ::Any)
5454
Base.length(::AbstractArray)
55+
Base.keys(::AbstractArray)
5556
Base.eachindex
5657
Base.IndexStyle
5758
Base.IndexLinear

0 commit comments

Comments
 (0)