Skip to content

Commit 12c2bd9

Browse files
asmeurerkgryte
andauthored
Clarify the exact bounds of what is required for slices (#138)
* Clarify the exact bounds of what is required for slices * Apply suggestions from code review Co-authored-by: Athan <[email protected]> * Fix some markdown Co-authored-by: Athan <[email protected]>
1 parent b0b23be commit 12c2bd9

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

spec/API_specification/indexing.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,19 @@ Using a slice to index a single array axis must adhere to the following rules. L
114114

115115
```{note}
116116
117-
This specification does not require "clipping" out-of-bounds indices (i.e., requiring the starting and stopping indices `i` and `j` be bound by `0` and `n`, respectively).
118-
119-
_Rationale: this is consistent with bounds checking for integer indexing; the behavior of out-of-bounds indices is left unspecified. Implementations may choose to clip, raise an exception, return junk values, or some other behavior depending on device requirements and performance considerations._
117+
This specification does not require "clipping" out-of-bounds slice indices. This is in contrast to Python slice semantics where `0:100` and `0:10` are equivalent on a list of length `10`.
120118
```
121119

122-
```{note}
120+
The following ranges for the start and stop values of a slice must be supported. Let `n` be the axis (dimension) size being sliced. For a slice `i:j:k`, the behavior specified above should be implemented for the following:
121+
122+
- `i` or `j` omitted (`None`).
123+
- `-n <= i <= max(0, n - 1)`.
124+
- For `k > 0` or `k` omitted (`None`), `-n <= j <= n`.
125+
- For `k < 0`, `-n - 1 <= j <= max(0, n - 1)`.
123126

124-
This specification leaves unspecified the behavior of indexing a single array axis with an out-of-bounds slice (i.e., a slice which does not select any array axis elements).
127+
The behavior outside of these bounds is unspecified.
125128

126-
_Rationale: this is consistent with bounds checking for integer indexing; the behavior of out-of-bounds indices is left unspecified. Implementations may choose to return an empty array (whose axis (dimension) size along the indexed axis is `0`), raise an exception, or some other behavior depending on device requirements and performance considerations._
129+
_Rationale: this is consistent with bounds checking for integer indexing; the behavior of out-of-bounds indices is left unspecified. Implementations may choose to clip (consistent with Python `list` slicing semantics), raise an exception, return junk values, or some other behavior depending on device requirements and performance considerations._
127130
```
128131
129132
## Multi-axis Indexing

0 commit comments

Comments
 (0)