1
1
using OffsetArrays
2
- using OffsetArrays: IdentityUnitRange, no_offset_view
2
+ using OffsetArrays: IdentityUnitRange, no_offset_view, IIUR
3
+ using Base: Slice
3
4
using OffsetArrays: IdOffsetRange
4
5
using Test, Aqua, Documenter
5
6
using LinearAlgebra
@@ -11,6 +12,8 @@ using StaticArrays
11
12
using FillArrays
12
13
using DistributedArrays
13
14
15
+ const SliceIntUR = Slice{<: AbstractUnitRange{<:Integer} }
16
+
14
17
DocMeta. setdocmeta! (OffsetArrays, :DocTestSetup , :(using OffsetArrays); recursive= true )
15
18
16
19
# https://github.com/JuliaLang/julia/pull/29440
@@ -26,58 +29,7 @@ struct TupleOfRanges{N}
26
29
x :: NTuple{N, UnitRange{Int}}
27
30
end
28
31
29
- # Useful for testing indexing
30
- struct ZeroBasedRange{T,A<: AbstractRange{T} } <: AbstractRange{T}
31
- a :: A
32
- function ZeroBasedRange (a:: AbstractRange{T} ) where {T}
33
- @assert ! Base. has_offset_axes (a)
34
- new {T, typeof(a)} (a)
35
- end
36
- end
37
-
38
- struct ZeroBasedUnitRange{T,A<: AbstractUnitRange{T} } <: AbstractUnitRange{T}
39
- a :: A
40
- function ZeroBasedUnitRange (a:: AbstractUnitRange{T} ) where {T}
41
- @assert ! Base. has_offset_axes (a)
42
- new {T, typeof(a)} (a)
43
- end
44
- end
45
-
46
- for Z in [:ZeroBasedRange , :ZeroBasedUnitRange ]
47
- @eval Base. parent (A:: $Z ) = A. a
48
- @eval Base. first (A:: $Z ) = first (A. a)
49
- @eval Base. length (A:: $Z ) = length (A. a)
50
- @eval Base. last (A:: $Z ) = last (A. a)
51
- @eval Base. size (A:: $Z ) = size (A. a)
52
- @eval Base. axes (A:: $Z ) = map (x -> IdentityUnitRange (0 : x- 1 ), size (A. a))
53
- @eval Base. getindex (A:: $Z , i:: Int ) = A. a[i + 1 ]
54
- @eval Base. firstindex (A:: $Z ) = 0
55
- @eval Base. axes (A:: $Z ) = map (x -> IdentityUnitRange (0 : x- 1 ), size (A. a))
56
- @eval Base. getindex (A:: $Z , i:: Integer ) = A. a[i + 1 ]
57
- @eval Base. step (A:: $Z ) = step (A. a)
58
- @eval OffsetArrays. no_offset_view (A:: $Z ) = A. a
59
- @eval function Base. show (io:: IO , A:: $Z )
60
- show (io, A. a)
61
- print (io, " with indices $(axes (A,1 )) " )
62
- end
63
-
64
- for R in [:AbstractRange , :AbstractUnitRange , :StepRange ]
65
- @eval @inline function Base. getindex (A:: $Z , r:: $R{<:Integer} )
66
- @boundscheck checkbounds (A, r)
67
- OffsetArrays. _indexedby (A. a[r .+ 1 ], axes (r))
68
- end
69
- end
70
- for R in [:UnitRange , :StepRange , :StepRangeLen , :LinRange ]
71
- @eval @inline function Base. getindex (A:: $R , r:: $Z )
72
- @boundscheck checkbounds (A, r)
73
- OffsetArrays. _indexedby (A[r. a], axes (r))
74
- end
75
- end
76
- @eval @inline function Base. getindex (A:: StepRangeLen{<:Any,<:Base.TwicePrecision,<:Base.TwicePrecision} , r:: $Z )
77
- @boundscheck checkbounds (A, r)
78
- OffsetArrays. _indexedby (A[r. a], axes (r))
79
- end
80
- end
32
+ include (" customranges.jl" )
81
33
82
34
function same_value (r1, r2)
83
35
length (r1) == length (r2) || return false
@@ -1128,6 +1080,10 @@ end
1128
1080
OffsetArray (IdOffsetRange (IdOffsetRange (10 : 1000 , - 1 ), 1 ), 3 ), # offset index
1129
1081
1130
1082
# AbstractRanges
1083
+ Base. OneTo (1000 ),
1084
+ CustomRange (Base. OneTo (1000 )),
1085
+ Slice (Base. OneTo (1000 )),
1086
+ SOneTo (1000 ),
1131
1087
1 : 1000 ,
1132
1088
UnitRange (1.0 , 1000.0 ),
1133
1089
1 : 3 : 1000 ,
@@ -1144,6 +1100,7 @@ end
1144
1100
ZeroBasedUnitRange (1 : 1000 ), # offset range
1145
1101
ZeroBasedRange (1 : 1000 ), # offset range
1146
1102
ZeroBasedRange (1 : 1 : 1000 ), # offset range
1103
+ CustomRange (ZeroBasedRange (1 : 1 : 1000 )), # offset range
1147
1104
]
1148
1105
1149
1106
# AbstractArrays with 1-based indices
@@ -1158,7 +1115,7 @@ end
1158
1115
test_indexing_axes_and_vals (r1, r2)
1159
1116
test_indexing_axes_and_vals (r1, collect (r2))
1160
1117
1161
- if r1 isa AbstractRange && axes (r2, 1 ) isa Base. OneTo
1118
+ if r1 isa AbstractRange && ! (r1 isa CustomRange) && axes (r2, 1 ) isa Base. OneTo
1162
1119
@test r1[r2] isa AbstractRange
1163
1120
end
1164
1121
end
@@ -1269,13 +1226,18 @@ end
1269
1226
OffsetArray (IdOffsetRange (IdOffsetRange (10 : 1000 , - 1 ), 1 ), 3 ), # offset index
1270
1227
1271
1228
# AbstractRanges
1229
+ Base. OneTo (1000 ),
1230
+ Slice (Base. OneTo (1000 )),
1231
+ SOneTo (1000 ),
1232
+ CustomRange (Base. OneTo (1000 )),
1272
1233
1 : 1000 ,
1273
1234
UnitRange (1.0 , 1000.0 ),
1274
1235
1 : 2 : 2000 ,
1275
1236
2000 : - 1 : 1 ,
1276
1237
1.0 : 2.0 : 2000.0 ,
1277
1238
StepRangeLen (Float64 (1 ), Float64 (1000 ), 1000 ),
1278
1239
LinRange (1.0 , 2000.0 , 2000 ),
1240
+ Base. Slice (Base. OneTo (1000 )), # 1-based index
1279
1241
IdOffsetRange (Base. OneTo (1000 )), # 1-based index
1280
1242
IdOffsetRange (1 : 1000 , 0 ), # 1-based index
1281
1243
IdOffsetRange (Base. OneTo (1000 ), 4 ), # offset index
@@ -1288,6 +1250,7 @@ end
1288
1250
ZeroBasedRange (1 : 1000 ), # offset index
1289
1251
ZeroBasedRange (1 : 1 : 1000 ), # offset index
1290
1252
ZeroBasedUnitRange (IdentityUnitRange (1 : 1000 )), # offset index
1253
+ CustomRange (ZeroBasedUnitRange (IdentityUnitRange (1 : 1000 ))), # offset index
1291
1254
]
1292
1255
1293
1256
# AbstractArrays with offset axes
0 commit comments