@@ -23,8 +23,17 @@ const ArrayInitializer = Union{UndefInitializer, Missing, Nothing}
23
23
"""
24
24
OffsetArray(A, indices...)
25
25
26
- Return an `AbstractArray` that shares element type and size with the first argument, but
27
- used the given `indices`, which are checked for compatible size.
26
+ Return an `AbstractArray` that shares element type and size with the first argument but
27
+ uses the supplied `indices` to infer its axes. If all the indices are `AbstractUnitRange`s then
28
+ these are directly used as the axis span along each dimension. Refer to the examples below for other
29
+ permissible types.
30
+
31
+ Alternatively it's possible to specify the coordinates of one corner of the array
32
+ and have the axes be computed automatically from the size of `A`.
33
+ This constructor makes it convenient to shift to
34
+ an arbitrary starting index along each axis, for example to a zero-based indexing scheme followed by
35
+ arrays in languages such as C and Python.
36
+ See [`Origin`](@ref) and the examples below for this usage.
28
37
29
38
# Example: offsets
30
39
@@ -42,27 +51,33 @@ julia> A[0, 1]
42
51
5
43
52
```
44
53
45
- Examples of range-like types are: `Colon()`(aka `:`), `UnitRange`(e.g, `-1:2`), and
46
- `CartesianIndices`.
54
+ Examples of range-like types are: `UnitRange` (e.g, `-1:2`), `CartesianIndices`,
55
+ and `Colon()` (or concisely `:`). A `UnitRange` specifies the axis span along one particular dimension,
56
+ `CartesianIndices` specify the axis spans along multiple dimensions, and a `Colon` is a placeholder
57
+ that specifies that the `OffsetArray` shares its axis with its parent along that dimension.
47
58
48
59
```jldoctest; setup=:(using OffsetArrays)
49
60
julia> OffsetArray(reshape(1:6, 2, 3), 0:1, -1:1)
50
61
2×3 OffsetArray(reshape(::UnitRange{$Int }, 2, 3), 0:1, -1:1) with eltype $Int with indices 0:1×-1:1:
51
62
1 3 5
52
63
2 4 6
53
64
54
- julia> OffsetArray(reshape(1:6, 2, 3), :, -1:1) # : as a placeholder to indicate that no offset is to be applied to this dimension
65
+ julia> OffsetArray(reshape(1:6, 2, 3), :, -1:1) # : as a placeholder to indicate that no offset is to be applied to the first dimension
55
66
2×3 OffsetArray(reshape(::UnitRange{$Int }, 2, 3), 1:2, -1:1) with eltype $Int with indices 1:2×-1:1:
56
67
1 3 5
57
68
2 4 6
69
+ ```
70
+
71
+ Use `CartesianIndices` to specify the coordinates of two diagonally opposite corners:
58
72
73
+ ```jldoctest; setup=:(using OffsetArrays)
59
74
julia> OffsetArray(reshape(1:6, 2, 3), CartesianIndex(0, -1):CartesianIndex(1, 1))
60
75
2×3 OffsetArray(reshape(::UnitRange{$Int }, 2, 3), 0:1, -1:1) with eltype $Int with indices 0:1×-1:1:
61
76
1 3 5
62
77
2 4 6
63
78
```
64
79
65
- Integers and range-like types can't be used interchangebly :
80
+ Integers and range-like types may not be combined in the same call :
66
81
67
82
```julia
68
83
julia> OffsetArray(reshape(1:6, 2, 3), 0, -1:1)
@@ -71,7 +86,10 @@ ERROR: [...]
71
86
72
87
# Example: origin
73
88
74
- [`OffsetArrays.Origin`](@ref) can be used to directly specify the origin of the output OffsetArray.
89
+ [`OffsetArrays.Origin`](@ref) may be used to specify the origin of the OffsetArray. The term origin here
90
+ refers to the corner with the lowest values of coordinates, such as the left edge for an `AbstractVector`,
91
+ the bottom left corner for an `AbstractMatrix` and so on. The coordinates of the origin sets the starting
92
+ index of the array along each dimension.
75
93
76
94
```jldoctest; setup=:(using OffsetArrays)
77
95
julia> a = [1 2; 3 4];
@@ -81,7 +99,7 @@ julia> OffsetArray(a, OffsetArrays.Origin(0, 1))
81
99
1 2
82
100
3 4
83
101
84
- julia> OffsetArray(a, OffsetArrays.Origin(0)) # short notation for `Origin(0, ..., 0)`
102
+ julia> OffsetArray(a, OffsetArrays.Origin(0)) # set the origin to zero along each dimension
85
103
2×2 OffsetArray(::$(Array{Int, 2 }) , 0:1, 0:1) with eltype $Int with indices 0:1×0:1:
86
104
1 2
87
105
3 4
279
297
280
298
@inline function Base. setindex! (A:: OffsetArray{T,N} , val, I:: Vararg{Int,N} ) where {T,N}
281
299
@boundscheck checkbounds (A, I... )
282
- J = @inbounds map (parentindex, axes (A), I)
300
+ J = map (parentindex, axes (A), I)
283
301
@inbounds parent (A)[J... ] = val
284
302
A
285
303
end
0 commit comments