11using OffsetArrays
2- using OffsetArrays: IdentityUnitRange, no_offset_view
2+ using OffsetArrays: IdentityUnitRange, no_offset_view, IIUR
3+ using Base: Slice
34using OffsetArrays: IdOffsetRange
45using Test, Aqua, Documenter
56using LinearAlgebra
@@ -11,6 +12,8 @@ using StaticArrays
1112using FillArrays
1213using DistributedArrays
1314
15+ const SliceIntUR = Slice{<: AbstractUnitRange{<:Integer} }
16+
1417DocMeta. setdocmeta! (OffsetArrays, :DocTestSetup , :(using OffsetArrays); recursive= true )
1518
1619# https://github.com/JuliaLang/julia/pull/29440
@@ -26,58 +29,7 @@ struct TupleOfRanges{N}
2629 x :: NTuple{N, UnitRange{Int}}
2730end
2831
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" )
8133
8234function same_value (r1, r2)
8335 length (r1) == length (r2) || return false
@@ -1128,6 +1080,10 @@ end
11281080 OffsetArray (IdOffsetRange (IdOffsetRange (10 : 1000 , - 1 ), 1 ), 3 ), # offset index
11291081
11301082 # AbstractRanges
1083+ Base. OneTo (1000 ),
1084+ CustomRange (Base. OneTo (1000 )),
1085+ Slice (Base. OneTo (1000 )),
1086+ SOneTo (1000 ),
11311087 1 : 1000 ,
11321088 UnitRange (1.0 , 1000.0 ),
11331089 1 : 3 : 1000 ,
@@ -1144,6 +1100,7 @@ end
11441100 ZeroBasedUnitRange (1 : 1000 ), # offset range
11451101 ZeroBasedRange (1 : 1000 ), # offset range
11461102 ZeroBasedRange (1 : 1 : 1000 ), # offset range
1103+ CustomRange (ZeroBasedRange (1 : 1 : 1000 )), # offset range
11471104 ]
11481105
11491106 # AbstractArrays with 1-based indices
@@ -1158,7 +1115,7 @@ end
11581115 test_indexing_axes_and_vals (r1, r2)
11591116 test_indexing_axes_and_vals (r1, collect (r2))
11601117
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
11621119 @test r1[r2] isa AbstractRange
11631120 end
11641121 end
@@ -1269,13 +1226,18 @@ end
12691226 OffsetArray (IdOffsetRange (IdOffsetRange (10 : 1000 , - 1 ), 1 ), 3 ), # offset index
12701227
12711228 # AbstractRanges
1229+ Base. OneTo (1000 ),
1230+ Slice (Base. OneTo (1000 )),
1231+ SOneTo (1000 ),
1232+ CustomRange (Base. OneTo (1000 )),
12721233 1 : 1000 ,
12731234 UnitRange (1.0 , 1000.0 ),
12741235 1 : 2 : 2000 ,
12751236 2000 : - 1 : 1 ,
12761237 1.0 : 2.0 : 2000.0 ,
12771238 StepRangeLen (Float64 (1 ), Float64 (1000 ), 1000 ),
12781239 LinRange (1.0 , 2000.0 , 2000 ),
1240+ Base. Slice (Base. OneTo (1000 )), # 1-based index
12791241 IdOffsetRange (Base. OneTo (1000 )), # 1-based index
12801242 IdOffsetRange (1 : 1000 , 0 ), # 1-based index
12811243 IdOffsetRange (Base. OneTo (1000 ), 4 ), # offset index
@@ -1288,6 +1250,7 @@ end
12881250 ZeroBasedRange (1 : 1000 ), # offset index
12891251 ZeroBasedRange (1 : 1 : 1000 ), # offset index
12901252 ZeroBasedUnitRange (IdentityUnitRange (1 : 1000 )), # offset index
1253+ CustomRange (ZeroBasedUnitRange (IdentityUnitRange (1 : 1000 ))), # offset index
12911254 ]
12921255
12931256 # AbstractArrays with offset axes
0 commit comments