Skip to content

Commit a6954cd

Browse files
committed
Rewrite array interface doc in terms of Vararg
1 parent 7f2d845 commit a6954cd

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

doc/manual/interfaces.rst

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ Methods to implement
153153
:func:`size(A) <size>` Returns a tuple containing the dimensions of A
154154
:func:`Base.linearindexing(Type) <Base.linearindexing>` Returns either ``Base.LinearFast()`` or ``Base.LinearSlow()``. See the description below.
155155
:func:`getindex(A, i::Int) <getindex>` (if ``LinearFast``) Linear scalar indexing
156-
:func:`getindex(A, i1::Int, ..., iN::Int) <getindex>` (if ``LinearSlow``, where ``N = ndims(A)``) N-dimensional scalar indexing
156+
:func:`getindex(A, I::Vararg{Int, N}) <getindex>` (if ``LinearSlow``, where ``N = ndims(A)``) N-dimensional scalar indexing
157157
:func:`setindex!(A, v, i::Int) <getindex>` (if ``LinearFast``) Scalar indexed assignment
158-
:func:`setindex!(A, v, i1::Int, ..., iN::Int) <getindex>` (if ``LinearSlow``, where ``N = ndims(A)``) N-dimensional scalar indexed assignment
158+
:func:`setindex!(A, v, I::Vararg{Int, N}) <getindex>` (if ``LinearSlow``, where ``N = ndims(A)``) N-dimensional scalar indexed assignment
159159
**Optional methods** **Default definition** **Brief description**
160160
:func:`getindex(A, I...) <getindex>` defined in terms of scalar :func:`getindex` :ref:`Multidimensional and nonscalar indexing <man-array-indexing>`
161161
:func:`setindex!(A, I...) <setindex!>` defined in terms of scalar :func:`setindex!` :ref:`Multidimensional and nonscalar indexed assignment <man-array-indexing>`
@@ -226,15 +226,11 @@ As a more complicated example, let's define our own toy N-dimensional sparse-lik
226226

227227
julia> Base.size(A::SparseArray) = A.dims
228228
Base.similar{T}(A::SparseArray, ::Type{T}, dims::Dims) = SparseArray(T, dims)
229-
# Define scalar indexing and indexed assignment for up to 3 dimensions
230-
Base.getindex{T}(A::SparseArray{T,1}, i1::Int) = get(A.data, (i1,), zero(T))
231-
Base.getindex{T}(A::SparseArray{T,2}, i1::Int, i2::Int) = get(A.data, (i1,i2), zero(T))
232-
Base.getindex{T}(A::SparseArray{T,3}, i1::Int, i2::Int, i3::Int) = get(A.data, (i1,i2,i3), zero(T))
233-
Base.setindex!{T}(A::SparseArray{T,1}, v, i1::Int) = (A.data[(i1,)] = v)
234-
Base.setindex!{T}(A::SparseArray{T,2}, v, i1::Int, i2::Int) = (A.data[(i1,i2)] = v)
235-
Base.setindex!{T}(A::SparseArray{T,3}, v, i1::Int, i2::Int, i3::Int) = (A.data[(i1,i2,i3)] = v);
236-
237-
Notice that this is a ``LinearSlow`` array, so we must manually define :func:`getindex` and :func:`setindex!` for each dimensionality we'd like to support. Unlike the ``SquaresVector``, we are able to define :func:`setindex!`, and so we can mutate the array:
229+
# Define scalar indexing and indexed assignment
230+
Base.getindex{T,N}(A::SparseArray{T,N}, I::Vararg{Int,N}) = get(A.data, I, zero(T))
231+
Base.setindex!{T,N}(A::SparseArray{T,N}, v, I::Vararg{Int,N}) = (A.data[I] = v)
232+
233+
Notice that this is a ``LinearSlow`` array, so we must manually define :func:`getindex` and :func:`setindex!` at the dimensionality of the array. Unlike the ``SquaresVector``, we are able to define :func:`setindex!`, and so we can mutate the array:
238234

239235
.. doctest::
240236

0 commit comments

Comments
 (0)