Skip to content

Commit b55acb3

Browse files
committed
Fix and update a bunch of docs along with the missing SetIndex! method
1 parent 8d2830f commit b55acb3

File tree

5 files changed

+92
-15
lines changed

5 files changed

+92
-15
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "6.0.20"
3+
version = "6.0.21"
44

55
[deps]
66
ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2"

docs/src/api.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ ArrayInterfaceCore.can_change_size
1010
ArrayInterfaceCore.can_setindex
1111
ArrayInterfaceCore.fast_matrix_colors
1212
ArrayInterfaceCore.fast_scalar_indexing
13+
ArrayInterfaceCore.is_forwarding_wrapper
1314
ArrayInterfaceCore.ismutable
1415
ArrayInterfaceCore.isstructured
1516
ArrayInterfaceCore.has_sparsestruct
17+
ArrayInterfaceCore.ndims_index
18+
ArrayInterfaceCore.ndims_shape
19+
1620
```
1721

1822
### Functions
@@ -36,6 +40,8 @@ ArrayInterfaceCore.zeromatrix
3640

3741
```@docs
3842
ArrayInterfaceCore.ArrayIndex
43+
ArrayInterfaceCore.GetIndex
44+
ArrayInterfaceCore.SetIndex!
3945
```
4046

4147
## ArrayInterface.jl
@@ -63,7 +69,6 @@ ArrayInterface.known_offsets
6369
ArrayInterface.known_size
6470
ArrayInterface.known_step
6571
ArrayInterface.known_strides
66-
ArrayInterface.ndims_index
6772
```
6873

6974
### Functions

lib/ArrayInterfaceCore/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterfaceCore"
22
uuid = "30b0a656-2188-435a-8636-2ec0e6a096e2"
3-
version = "0.1.14"
3+
version = "0.1.15"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

lib/ArrayInterfaceCore/src/ArrayInterfaceCore.jl

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ is equivalent to `buffer[inds...]`. If `check` is `false`, then all indexing arg
137137
considered in-bounds. The default value for `check` is `true`, requiring bounds checking for
138138
each index.
139139
140+
See also [`SetIndex!`](@ref)
141+
140142
!!! Warning
141143
Passing `false` as `check` may result in incorrect results/crashes/corruption for
142144
out-of-bounds indices, similar to inappropriate use of `@inbounds`. The user is
@@ -145,10 +147,10 @@ each index.
145147
# Examples
146148
147149
```julia
148-
julia> ArrayInterfaceCore.GetIndex(1:10)[3]
150+
julia> ArrayInterfaceCore.GetIndex(1:10)(3)
149151
3
150152
151-
julia> ArrayInterfaceCore.GetIndex{false}(1:10)[11] # shouldn't be in-bounds
153+
julia> ArrayInterfaceCore.GetIndex{false}(1:10)(11) # shouldn't be in-bounds
152154
11
153155
154156
```
@@ -163,10 +165,52 @@ struct GetIndex{CB,B} <: Function
163165
GetIndex(b) = GetIndex{true}(b)
164166
end
165167

166-
buffer(g::GetIndex) = getfield(g, :buffer)
168+
"""
169+
SetIndex!(buffer) = SetIndex!{true}(buffer)
170+
SetIndex!{check}(buffer) -> g
171+
172+
Wraps an indexable buffer in a function type that sets a value at an index when called, so
173+
that `g(val, inds..)` is equivalent to `setindex!(buffer, val, inds...)`. If `check` is
174+
`false`, then all indexing arguments are considered in-bounds. The default value for `check`
175+
is `true`, requiring bounds checking for each index.
176+
177+
See also [`GetIndex`](@ref)
178+
179+
!!! Warning
180+
Passing `false` as `check` may result in incorrect results/crashes/corruption for
181+
out-of-bounds indices, similar to inappropriate use of `@inbounds`. The user is
182+
responsible for ensuring this is correctly used.
183+
184+
# Examples
185+
186+
```julia
187+
188+
julia> x = [1, 2, 3, 4];
189+
190+
julia> ArrayInterface.SetIndex!(x)(10, 2);
191+
192+
julia> x[2]
193+
10
194+
195+
```
196+
"""
197+
struct SetIndex!{CB,B} <: Function
198+
buffer::B
199+
200+
SetIndex!{true,B}(b) where {B} = new{true,B}(b)
201+
SetIndex!{false,B}(b) where {B} = new{false,B}(b)
202+
SetIndex!{check}(b::B) where {check,B} = SetIndex!{check,B}(b)
203+
SetIndex!(b) = SetIndex!{true}(b)
204+
end
205+
206+
buffer(x::Union{SetIndex!,GetIndex}) = getfield(x, :buffer)
167207

168208
Base.@propagate_inbounds @inline (g::GetIndex{true})(inds...) = buffer(g)[inds...]
169209
@inline (g::GetIndex{false})(inds...) = @inbounds(buffer(g)[inds...])
210+
Base.@propagate_inbounds @inline function (s::SetIndex!{true})(v, inds...)
211+
setindex!(buffer(s), v, inds...)
212+
end
213+
@inline (s::SetIndex!{false})(v, inds...) = @inbounds(setindex!(buffer(s), v, inds...))
170214

171215
"""
172216
can_change_size(::Type{T}) -> Bool
@@ -520,6 +564,7 @@ const MatrixIndex = ArrayIndex{2}
520564

521565
const VectorIndex = ArrayIndex{1}
522566

567+
Base.ndims(::ArrayIndex{N}) where {N} = N
523568
Base.ndims(::Type{<:ArrayIndex{N}}) where {N} = N
524569

525570
struct BidiagonalIndex <: MatrixIndex
@@ -628,6 +673,7 @@ known_step(@nospecialize T::Type{<:AbstractUnitRange}) = 1
628673

629674
"""
630675
is_splat_index(::Type{T}) -> Bool
676+
631677
Returns `static(true)` if `T` is a type that splats across multiple dimensions.
632678
"""
633679
is_splat_index(T::Type) = false
@@ -636,9 +682,24 @@ is_splat_index(@nospecialize(x)) = is_splat_index(typeof(x))
636682
"""
637683
ndims_index(::Type{I}) -> Int
638684
639-
Returns the number of dimension that an instance of `I` maps to when indexing. For example,
640-
`CartesianIndex{3}` maps to 3 dimensions. If this method is not explicitly defined, then `1`
641-
is returned.
685+
Returns the number of dimensions that an instance of `I` indexes into. If this method is
686+
not explicitly defined, then `1` is returned.
687+
688+
See also [`ndims_shape`](@ref)
689+
690+
# Examples
691+
692+
```julia
693+
julia> ArrayInterfaceCore.ndims_index(Int)
694+
1
695+
696+
julia> ArrayInterfaceCore.ndims_index(CartesianIndex(1, 2, 3))
697+
3
698+
699+
julia> ArrayInterfaceCore.ndims_index([CartesianIndex(1, 2), CartesianIndex(1, 3)])
700+
2
701+
702+
```
642703
"""
643704
ndims_index(::Type{<:Base.AbstractCartesianIndex{N}}) where {N} = N
644705
# preserve CartesianIndices{0} as they consume a dimension.
@@ -652,8 +713,20 @@ ndims_index(@nospecialize(i)) = ndims_index(typeof(i))
652713
"""
653714
ndims_shape(::Type{I}) -> Union{Int,Tuple{Vararg{Int}}}
654715
655-
Returns the number of dimension that are represented in shape of the returned array when
716+
Returns the number of dimension that are represented in the shape of the returned array when
656717
indexing with an instance of `I`.
718+
719+
See also [`ndims_index`](@ref)
720+
721+
# Examples
722+
723+
```julia
724+
julia> ArrayInterfaceCore.ndims_shape([CartesianIndex(1, 1), CartesianIndex(1, 2)])
725+
1
726+
727+
julia> ndims(CartesianIndices((2,2))[[CartesianIndex(1, 1), CartesianIndex(1, 2)]])
728+
1
729+
657730
"""
658731
ndims_shape(T::DataType) = ndims_index(T)
659732
ndims_shape(::Type{Colon}) = 1
@@ -671,11 +744,10 @@ ndims_shape(x) = ndims_shape(typeof(x))
671744
end
672745

673746
"""
674-
IndicesInfo{N}(T::Type{<:Tuple}) -> IndicesInfo{N,NI,NS}()
747+
IndicesInfo{N}(T::Type{<:Tuple}) -> IndicesInfo{N,pdims,cdims}()
675748
676-
Provides basic trait information for each index type in in the tuple `T`. `NI`, `NS`, and
677-
`IS` are tuples of [`ndims_index`](@ref), [`ndims_shape`](@ref), and
678-
[`is_splat_index`](@ref) (respectively) for each field of `T`.
749+
Provides basic trait information for each index type in in the tuple `T`. `pdims` and
750+
`cdims` are dimension mappings to the parent and child dimensions respectively.
679751
680752
# Examples
681753

src/ArrayInterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ArrayInterfaceCore: allowed_getindex, allowed_setindex!, aos_to_soa, buff
66
issingular, isstructured, matrix_colors, restructure, lu_instance,
77
safevec, zeromatrix, ColoringAlgorithm, fast_scalar_indexing, parameterless_type,
88
ndims_index, ndims_shape, is_splat_index, is_forwarding_wrapper, IndicesInfo,
9-
map_tuple_type, flatten_tuples, GetIndex
9+
map_tuple_type, flatten_tuples, GetIndex, SetIndex!
1010

1111
# ArrayIndex subtypes and methods
1212
import ArrayInterfaceCore: ArrayIndex, MatrixIndex, VectorIndex, BidiagonalIndex, TridiagonalIndex

0 commit comments

Comments
 (0)