Skip to content

Commit d15b091

Browse files
authored
Doc IndexLinear and IndexCartesian (#28476)
* Doc IndexLinear and IndexCartesian
1 parent 0846eb5 commit d15b091

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

base/cartesian.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ would generate:
3232
end
3333
end
3434
35-
If you want just a post-expression, supply `nothing` for the pre-expression. Using
35+
If you want just a post-expression, supply [`nothing`](@ref) for the pre-expression. Using
3636
parentheses and semicolons, you can supply multi-statement expressions.
3737
"""
3838
macro nloops(N, itersym, rangeexpr, args...)

base/indices.jl

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,47 @@ Indices{N} = NTuple{N,AbstractUnitRange}
1313
## Traits for array types ##
1414

1515
abstract type IndexStyle end
16+
"""
17+
IndexLinear()
18+
19+
Subtype of [`IndexStyle`](@ref) used to describe arrays which
20+
are optimally indexed by one linear index.
21+
22+
A linear indexing style uses one integer to describe the position in the array
23+
(even if it's a multidimensional array) and column-major
24+
ordering is used to access the elements. For example,
25+
if `A` were a `(2, 3)` custom matrix type with linear indexing,
26+
and we referenced `A[5]` (using linear style), this would
27+
be equivalent to referencing `A[1, 3]` (since `2*1 + 3 = 5`).
28+
See also [`IndexCartesian`](@ref).
29+
"""
1630
struct IndexLinear <: IndexStyle end
31+
"""
32+
IndexCartesian()
33+
34+
Subtype of [`IndexStyle`](@ref) used to describe arrays which
35+
are optimally indexed by a Cartesian index.
36+
37+
A cartesian indexing style uses multiple integers/indices to describe the position in the array.
38+
For example, if `A` were a `(2, 3, 4)` custom matrix type with cartesian indexing,
39+
we could reference `A[2, 1, 3]` and Julia would automatically convert this into the
40+
correct location in the underlying memory. See also [`IndexLinear`](@ref).
41+
"""
1742
struct IndexCartesian <: IndexStyle end
1843

1944
"""
2045
IndexStyle(A)
2146
IndexStyle(typeof(A))
2247
2348
`IndexStyle` specifies the "native indexing style" for array `A`. When
24-
you define a new `AbstractArray` type, you can choose to implement
25-
either linear indexing or cartesian indexing. If you decide to
26-
implement linear indexing, then you must set this trait for your array
49+
you define a new [`AbstractArray`](@ref) type, you can choose to implement
50+
either linear indexing (with [`IndexLinear`](@ref)) or cartesian indexing.
51+
If you decide to implement linear indexing, then you must set this trait for your array
2752
type:
2853
2954
Base.IndexStyle(::Type{<:MyArray}) = IndexLinear()
3055
31-
The default is `IndexCartesian()`.
56+
The default is [`IndexCartesian()`](@ref).
3257
3358
Julia's internal indexing machinery will automatically (and invisibly)
3459
convert all indexing operations into the preferred style. This allows users

base/iterators.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ Also similar to `enumerate(A)`, except `i` will be a valid index
181181
for `A`, while `enumerate` always counts from 1 regardless of the indices
182182
of `A`.
183183
184-
Specifying `IndexLinear()` ensures that `i` will be an integer;
185-
specifying `IndexCartesian()` ensures that `i` will be a
186-
`CartesianIndex`; specifying `IndexStyle(A)` chooses whichever has
184+
Specifying [`IndexLinear()`](@ref) ensures that `i` will be an integer;
185+
specifying [`IndexCartesian()`](@ref) ensures that `i` will be a
186+
[`CartesianIndex`](@ref); specifying `IndexStyle(A)` chooses whichever has
187187
been defined as the native indexing style for array `A`.
188188
189189
Mutation of the bounds of the underlying array will invalidate this iterator.

doc/src/base/arrays.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Base.axes(::AbstractArray, ::Any)
5353
Base.length(::AbstractArray)
5454
Base.eachindex
5555
Base.IndexStyle
56+
Base.IndexLinear
57+
Base.IndexCartesian
5658
Base.conj!
5759
Base.stride
5860
Base.strides

0 commit comments

Comments
 (0)