Skip to content

Commit 0749d26

Browse files
authored
Update the docstring for OffsetArray (#176)
Rename fill to fill1d in benchmarks
1 parent cd9b075 commit 0749d26

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

benchmark/benchmarks.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ y = OffsetArray{Float64}(undef, -dim + 1 : dim)
88
x2d = Array{Float64}(undef, 2*dim, 2*dim)
99
y2d = OffsetArray{Float64}(undef, -dim + 1 : dim, -dim + 1 : dim)
1010

11-
fill(x) = for i in axes(x,1); x[i] = i; end
11+
fill1d(x) = for i in axes(x,1); x[i] = i; end
1212
fill2d(x) = for j in axes(x,2); for i in axes(x,1); x[i,j] = i + j; end; end
1313
update(x) = for i in axes(x,1); x[i] = x[i] + i; end
1414
update2d(x) = for j in axes(x,2); for i in axes(x,1); x[i,j] = x[i,j] + i + j; end; end
@@ -20,8 +20,8 @@ unsafe_update(x) = @inbounds(for i in axes(x,1); x[i] = x[i] + i; end)
2020
unsafe_update2d(x) = @inbounds(for j in axes(x,2); for i in axes(x,1); x[i,j] = x[i,j] + i + j; end; end)
2121
unsafe_update_eachindex(x) = @inbounds(for i in eachindex(x); x[i] = x[i] + i; end)
2222

23-
@show @benchmark fill(x)
24-
@show @benchmark fill(y)
23+
@show @benchmark fill1d(x)
24+
@show @benchmark fill1d(y)
2525
@show @benchmark fill2d(x2d)
2626
@show @benchmark fill2d(y2d)
2727
@show @benchmark update(x)

src/OffsetArrays.jl

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@ const ArrayInitializer = Union{UndefInitializer, Missing, Nothing}
2323
"""
2424
OffsetArray(A, indices...)
2525
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.
2837
2938
# Example: offsets
3039
@@ -42,27 +51,33 @@ julia> A[0, 1]
4251
5
4352
```
4453
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.
4758
4859
```jldoctest; setup=:(using OffsetArrays)
4960
julia> OffsetArray(reshape(1:6, 2, 3), 0:1, -1:1)
5061
2×3 OffsetArray(reshape(::UnitRange{$Int}, 2, 3), 0:1, -1:1) with eltype $Int with indices 0:1×-1:1:
5162
1 3 5
5263
2 4 6
5364
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
5566
2×3 OffsetArray(reshape(::UnitRange{$Int}, 2, 3), 1:2, -1:1) with eltype $Int with indices 1:2×-1:1:
5667
1 3 5
5768
2 4 6
69+
```
70+
71+
Use `CartesianIndices` to specify the coordinates of two diagonally opposite corners:
5872
73+
```jldoctest; setup=:(using OffsetArrays)
5974
julia> OffsetArray(reshape(1:6, 2, 3), CartesianIndex(0, -1):CartesianIndex(1, 1))
6075
2×3 OffsetArray(reshape(::UnitRange{$Int}, 2, 3), 0:1, -1:1) with eltype $Int with indices 0:1×-1:1:
6176
1 3 5
6277
2 4 6
6378
```
6479
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:
6681
6782
```julia
6883
julia> OffsetArray(reshape(1:6, 2, 3), 0, -1:1)
@@ -71,7 +86,10 @@ ERROR: [...]
7186
7287
# Example: origin
7388
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.
7593
7694
```jldoctest; setup=:(using OffsetArrays)
7795
julia> a = [1 2; 3 4];
@@ -81,7 +99,7 @@ julia> OffsetArray(a, OffsetArrays.Origin(0, 1))
8199
1 2
82100
3 4
83101
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
85103
2×2 OffsetArray(::$(Array{Int, 2}), 0:1, 0:1) with eltype $Int with indices 0:1×0:1:
86104
1 2
87105
3 4
@@ -279,7 +297,7 @@ end
279297

280298
@inline function Base.setindex!(A::OffsetArray{T,N}, val, I::Vararg{Int,N}) where {T,N}
281299
@boundscheck checkbounds(A, I...)
282-
J = @inbounds map(parentindex, axes(A), I)
300+
J = map(parentindex, axes(A), I)
283301
@inbounds parent(A)[J...] = val
284302
A
285303
end

0 commit comments

Comments
 (0)