You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/src/devdocs/offset-arrays.md
+21-63Lines changed: 21 additions & 63 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,34 +1,40 @@
1
1
# [Arrays with custom indices](@id man-custom-indices)
2
2
3
-
Julia 0.5 adds experimental support for arrays with arbitrary indices. Conventionally, Julia's
3
+
Conventionally, Julia's
4
4
arrays are indexed starting at 1, whereas some other languages start numbering at 0, and yet others
5
5
(e.g., Fortran) allow you to specify arbitrary starting indices. While there is much merit in
6
6
picking a standard (i.e., 1 for Julia), there are some algorithms which simplify considerably
7
-
if you can index outside the range `1:size(A,d)` (and not just `0:size(A,d)-1`, either). Such
8
-
array types are expected to be supplied through packages.
7
+
if you can index outside the range `1:size(A,d)` (and not just `0:size(A,d)-1`, either). To facilitate such computations, Julia supports array with arbitrary indices.
9
8
10
9
The purpose of this page is to address the question, "what do I have to do to support such arrays
11
10
in my own code?" First, let's address the simplest case: if you know that your code will never
12
11
need to handle arrays with unconventional indexing, hopefully the answer is "nothing." Old code,
13
12
on conventional arrays, should function essentially without alteration as long as it was using
14
13
the exported interfaces of Julia.
14
+
If you find it more convenient to just force your users to supply traditional arrays where indexing starts at one, you can add
15
+
16
+
```julia
17
+
@assertis_one_indexed(arrays...)
18
+
```
19
+
20
+
where `arrays...` is a list of the array objects that you wish to check for 1-based indexing.
15
21
16
22
## Generalizing existing code
17
23
18
24
As an overview, the steps are:
19
25
20
26
* replace many uses of `size` with `axes`
21
27
* replace `1:length(A)` with `eachindex(A)`, or in some cases `LinearIndices(A)`
22
-
* replace `length(A)` with `length(LinearIndices(A))`
23
28
* replace explicit allocations like `Array{Int}(size(B))` with `similar(Array{Int}, axes(B))`
24
29
25
30
These are described in more detail below.
26
31
27
-
### Background
32
+
### Things to watch out for
28
33
29
-
Because unconventional indexing breaks deeply-held assumptions throughout the Julia ecosystem,
30
-
early adopters running code that has not been updated are likely to experience errors. The most
31
-
frustrating bugs would be incorrect results or segfaults (total crashes of Julia). For example,
34
+
Because unconventional indexing breaks many people's assumptions that all arrays start indexing with 1, there is always the chance that using such arrays will trigger errors.
35
+
The most
36
+
frustrating bugs would be incorrect results or segfaults (total crashes of Julia).
37
+
For example,
32
38
consider the following function:
33
39
34
40
```julia
@@ -42,16 +48,10 @@ function mycopy!(dest::AbstractVector, src::AbstractVector)
42
48
end
43
49
```
44
50
45
-
This code implicitly assumes that vectors are indexed from 1. Previously that was a safe assumption,
46
-
so this code was fine, but (depending on what types the user passes to this function) it may no
47
-
longer be safe. If this code continued to work when passed a vector with non-1 indices, it would
48
-
either produce an incorrect answer or it would segfault. (If you do get segfaults, to help locate
51
+
This code implicitly assumes that vectors are indexed from 1; if `dest` starts at a different index than `src`, there is a chance that this code would trigger a segfault.
52
+
(If you do get segfaults, to help locate
49
53
the cause try running julia with the option `--check-bounds=yes`.)
50
54
51
-
To ensure that such errors are caught, in Julia 0.5 both `length` and `size`**should** throw an
52
-
error when passed an array with non-1 indexing. This is designed to force users of such arrays
53
-
to check the code, and inspect it for whether it needs to be generalized.
54
-
55
55
### Using `axes` for bounds checks and loop iteration
56
56
57
57
`axes(A)` (reminiscent of `size(A)`) returns a tuple of `AbstractUnitRange` objects, specifying
@@ -62,14 +62,7 @@ is `axes(A, d)`.
62
62
Base implements a custom range type, `OneTo`, where `OneTo(n)` means the same thing as `1:n` but
63
63
in a form that guarantees (via the type system) that the lower index is 1. For any new [`AbstractArray`](@ref)
64
64
type, this is the default returned by `axes`, and it indicates that this array type uses "conventional"
65
-
1-based indexing. Note that if you don't want to be bothered supporting arrays with non-1 indexing,
66
-
you can add the following line:
67
-
68
-
```julia
69
-
@assertall(x->isa(x, Base.OneTo), axes(A))
70
-
```
71
-
72
-
at the top of any function.
65
+
1-based indexing.
73
66
74
67
For bounds checking, note that there are dedicated functions `checkbounds` and `checkindex` which
75
68
can sometimes simplify such tests.
@@ -115,40 +108,11 @@ then "wrap" it in a type that shifts the indices.)
115
108
Note also that `similar(Array{Int}, (axes(A, 2),))` would allocate an `AbstractVector{Int}`
116
109
(i.e., 1-dimensional array) that matches the indices of the columns of `A`.
117
110
118
-
### Deprecations
119
-
120
-
In generalizing Julia's code base, at least one deprecation was unavoidable: earlier versions
121
-
of Julia defined `first(::Colon) = 1`, meaning that the first index along a dimension indexed
122
-
by `:` is 1. This definition can no longer be justified, so it was deprecated. There is no provided
123
-
replacement, because the proper replacement depends on what you are doing and might need to know
124
-
more about the array. However, it appears that many uses of `first(::Colon)` are really about
125
-
computing an index offset; when that is the case, a candidate replacement is:
126
-
127
-
```julia
128
-
indexoffset(r::AbstractVector) =first(r) -1
129
-
indexoffset(::Colon) =0
130
-
```
131
-
132
-
In other words, while `first(:)` does not itself make sense, in general you can say that the offset
133
-
associated with a colon-index is zero.
134
-
135
111
## Writing custom array types with non-1 indexing
136
112
137
113
Most of the methods you'll need to define are standard for any `AbstractArray` type, see [Abstract Arrays](@ref man-interface-array).
138
114
This page focuses on the steps needed to define unconventional indexing.
139
115
140
-
### Do **not** implement `size` or `length`
141
-
142
-
Perhaps the majority of pre-existing code that uses `size` will not work properly for arrays with
143
-
non-1 indices. For that reason, it is much better to avoid implementing these methods, and use
144
-
the resulting `MethodError` to identify code that needs to be audited and perhaps generalized.
145
-
146
-
### Do **not** annotate bounds checks
147
-
148
-
Julia 0.5 includes `@boundscheck` to annotate code that can be removed for callers that exploit
149
-
`@inbounds`. Initially, it seems far preferable to run with bounds checking always enabled (i.e.,
150
-
omit the `@boundscheck` annotation so the check always runs).
151
-
152
116
### Custom `AbstractUnitRange` types
153
117
154
118
If you're writing a non-1 indexed array type, you will want to specialize `axes` so it returns
and you can `reshape` an array so that the result has custom indices.
225
189
226
-
##Summary
190
+
### Catching errors
227
191
228
-
Writing code that doesn't make assumptions about indexing requires a few extra abstractions, but
229
-
hopefully the necessary changes are relatively straightforward.
192
+
If your new array type triggers errors in other code, one helpful debugging step can be to comment out `@boundscheck` in your `getindex` and `setindex!` implementation.
193
+
This will ensure that every element access checks bounds. Or, restart julia with `--check-bounds=yes`.
230
194
231
-
As a reminder, this support is still experimental. While much of Julia's base code has been updated
232
-
to support unconventional indexing, without a doubt there are many omissions that will be discovered
233
-
only through usage. Moreover, at the time of this writing, most packages do not support unconventional
234
-
indexing. As a consequence, early adopters should be prepared to identify and/or fix bugs. On
235
-
the other hand, only through practical usage will it become clear whether this experimental feature
236
-
should be retained in future versions of Julia; consequently, interested parties are encouraged
237
-
to accept some ownership for putting it through its paces.
195
+
In some cases it may also be helpful to temporarily disable `size` and `length` for your new array type, since sometimes bounds checks are performed incorrectly using these functions.
0 commit comments