Skip to content

Commit db2d04e

Browse files
committed
Fix resizing methods by using parent_type
1 parent 6d9c642 commit db2d04e

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StaticRanges"
22
uuid = "d8176aec-3168-11e9-3c98-e3954798be3a"
33
authors = ["zchristensen "]
4-
version = "0.8.2"
4+
version = "0.8.3"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/resize.jl

+61-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
function nprev_type(x::T, n) where {T}
43
return [x = prev_type(x) for _ in 1:n]::Vector{T}
54
end
@@ -101,7 +100,13 @@ UnitMRange(1:12)
101100
i = last(x)
102101
return vcat(x, nnext_type(i, n))
103102
end
104-
grow_last(x::AbstractRange, n::Integer) = set_last(x, last(x) + step(x) * n)
103+
function grow_last(x::AbstractRange, n::Integer)
104+
if parent_type(x) <: typeof(x)
105+
return set_last(x, last(x) + step(x) * n)
106+
else
107+
return unsafe_reconstruct(x, grow_last(parent(x), n))
108+
end
109+
end
105110
grow_last(x::AbstractRange, n::AbstractUnitRange) = unsafe_reconstruct(x, n)
106111

107112
"""
@@ -126,7 +131,14 @@ function grow_last!(x::AbstractVector, n::Integer)
126131
i = last(x)
127132
return append!(x, nnext_type(i, n))
128133
end
129-
grow_last!(x::AbstractRange, n::Integer) = set_last!(x, last(x) + step(x) * n)
134+
function grow_last!(x::AbstractRange, n::Integer)
135+
if parent_type(x) <: typeof(x)
136+
set_last!(x, last(x) + step(x) * n)
137+
else
138+
grow_last!(parent(x), n)
139+
end
140+
return x
141+
end
130142

131143
"""
132144
grow_first(x, n)
@@ -148,7 +160,13 @@ function grow_first(x::AbstractVector, n::Integer)
148160
i = first(x)
149161
return vcat(reverse!(nprev_type(i, n)), x)
150162
end
151-
grow_first(x::AbstractRange, n::Integer) = set_first(x, first(x) - step(x) * n)
163+
function grow_first(x::AbstractRange, n::Integer)
164+
if parent_type(x) <: typeof(x)
165+
return set_first(x, first(x) - step(x) * n)
166+
else
167+
return unsafe_reconstruct(x, grow_first(parent(x), n))
168+
end
169+
end
152170
grow_first(x::AbstractRange, n::AbstractUnitRange) = unsafe_reconstruct(x, n)
153171

154172
"""
@@ -173,7 +191,14 @@ function grow_first!(x::AbstractVector, n::Integer)
173191
i = first(x)
174192
return prepend!(x, reverse!(nprev_type(i, n)))
175193
end
176-
grow_first!(x::AbstractRange, n::Integer) = set_first!(x, first(x) - step(x) * n)
194+
function grow_first!(x::AbstractRange, n::Integer)
195+
if parent_type(x) <: typeof(x)
196+
set_first!(x, first(x) - step(x) * n)
197+
else
198+
grow_first!(parent(x), n)
199+
end
200+
return x
201+
end
177202

178203
"""
179204
shrink_last!(x, n)
@@ -199,7 +224,15 @@ function shrink_last!(x::AbstractVector, n::Integer)
199224
end
200225
return x
201226
end
202-
shrink_last!(x::AbstractRange, n::Integer) = set_last!(x, last(x) - step(x) * n)
227+
function shrink_last!(x::AbstractRange, n::Integer)
228+
if parent_type(x) <: typeof(x)
229+
set_last!(x, last(x) - step(x) * n)
230+
else
231+
shrink_last!(parent(x), n)
232+
end
233+
return x
234+
end
235+
203236

204237
"""
205238
shrink_last(x, n)
@@ -218,7 +251,13 @@ UnitMRange(1:8)
218251
```
219252
"""
220253
@propagate_inbounds shrink_last(x::AbstractVector, n::Integer) = x[firstindex(x):end - n]
221-
shrink_last(x::AbstractRange, n::Integer) = set_last(x, last(x) - step(x) * n)
254+
function shrink_last(x::AbstractRange, n::Integer)
255+
if parent_type(x) <: typeof(x)
256+
return set_last(x, last(x) - step(x) * n)
257+
else
258+
return unsafe_reconstruct(x, shrink_last(parent(x), n))
259+
end
260+
end
222261
shrink_last(x::AbstractRange, n::AbstractUnitRange) = unsafe_reconstruct(x, n)
223262

224263
"""
@@ -238,11 +277,17 @@ UnitMRange(3:10)
238277
```
239278
"""
240279
@propagate_inbounds shrink_first(x::AbstractVector, n::Integer) = x[(firstindex(x) + n):end]
241-
shrink_first(x::AbstractRange, n::Integer) = set_first(x, first(x) + step(x) * n)
242280
shrink_first(x::OneTo{T}, n::Integer) where {T} = UnitRange{T}(1 + n, last(x))
243281
shrink_first(x::OneToMRange{T}, n::Integer) where {T} = UnitMRange{T}(1 + n, last(x))
244282
shrink_first(x::OneToSRange{T}, n::Integer) where {T} = UnitSRange{T}(1 + n, last(x))
245283
shrink_first(x::AbstractRange, n::AbstractUnitRange) = unsafe_reconstruct(x, n)
284+
function shrink_first(x::AbstractRange, n::Integer)
285+
if parent_type(x) <: typeof(x)
286+
return set_first(x, first(x) + (step(x) * n))
287+
else
288+
return unsafe_reconstruct(x, shrink_first(parent(x), n))
289+
end
290+
end
246291

247292
"""
248293
shrink_first!(x, n)
@@ -255,7 +300,14 @@ function shrink_first!(x::AbstractVector, n::Integer)
255300
end
256301
return x
257302
end
258-
shrink_first!(x::AbstractRange, n::Integer) = set_first!(x, first(x) + step(x) * n)
303+
function shrink_first!(x::AbstractRange, n::Integer)
304+
if parent_type(x) <: typeof(x)
305+
set_first!(x, first(x) + step(x) * n)
306+
else
307+
shrink_first!(parent(x), n)
308+
end
309+
return x
310+
end
259311

260312
"""
261313
resize_last(x, n::Integer)

0 commit comments

Comments
 (0)