Skip to content

Commit

Permalink
Fix resizing methods by using parent_type
Browse files Browse the repository at this point in the history
  • Loading branch information
Tokazama committed Oct 17, 2020
1 parent 6d9c642 commit db2d04e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StaticRanges"
uuid = "d8176aec-3168-11e9-3c98-e3954798be3a"
authors = ["zchristensen "]
version = "0.8.2"
version = "0.8.3"

[deps]
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Expand Down
70 changes: 61 additions & 9 deletions src/resize.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


function nprev_type(x::T, n) where {T}
return [x = prev_type(x) for _ in 1:n]::Vector{T}
end
Expand Down Expand Up @@ -101,7 +100,13 @@ UnitMRange(1:12)
i = last(x)
return vcat(x, nnext_type(i, n))
end
grow_last(x::AbstractRange, n::Integer) = set_last(x, last(x) + step(x) * n)
function grow_last(x::AbstractRange, n::Integer)
if parent_type(x) <: typeof(x)
return set_last(x, last(x) + step(x) * n)
else
return unsafe_reconstruct(x, grow_last(parent(x), n))
end
end
grow_last(x::AbstractRange, n::AbstractUnitRange) = unsafe_reconstruct(x, n)

"""
Expand All @@ -126,7 +131,14 @@ function grow_last!(x::AbstractVector, n::Integer)
i = last(x)
return append!(x, nnext_type(i, n))
end
grow_last!(x::AbstractRange, n::Integer) = set_last!(x, last(x) + step(x) * n)
function grow_last!(x::AbstractRange, n::Integer)
if parent_type(x) <: typeof(x)
set_last!(x, last(x) + step(x) * n)
else
grow_last!(parent(x), n)
end
return x
end

"""
grow_first(x, n)
Expand All @@ -148,7 +160,13 @@ function grow_first(x::AbstractVector, n::Integer)
i = first(x)
return vcat(reverse!(nprev_type(i, n)), x)
end
grow_first(x::AbstractRange, n::Integer) = set_first(x, first(x) - step(x) * n)
function grow_first(x::AbstractRange, n::Integer)
if parent_type(x) <: typeof(x)
return set_first(x, first(x) - step(x) * n)
else
return unsafe_reconstruct(x, grow_first(parent(x), n))
end
end
grow_first(x::AbstractRange, n::AbstractUnitRange) = unsafe_reconstruct(x, n)

"""
Expand All @@ -173,7 +191,14 @@ function grow_first!(x::AbstractVector, n::Integer)
i = first(x)
return prepend!(x, reverse!(nprev_type(i, n)))
end
grow_first!(x::AbstractRange, n::Integer) = set_first!(x, first(x) - step(x) * n)
function grow_first!(x::AbstractRange, n::Integer)
if parent_type(x) <: typeof(x)
set_first!(x, first(x) - step(x) * n)
else
grow_first!(parent(x), n)
end
return x
end

"""
shrink_last!(x, n)
Expand All @@ -199,7 +224,15 @@ function shrink_last!(x::AbstractVector, n::Integer)
end
return x
end
shrink_last!(x::AbstractRange, n::Integer) = set_last!(x, last(x) - step(x) * n)
function shrink_last!(x::AbstractRange, n::Integer)
if parent_type(x) <: typeof(x)
set_last!(x, last(x) - step(x) * n)
else
shrink_last!(parent(x), n)
end
return x
end


"""
shrink_last(x, n)
Expand All @@ -218,7 +251,13 @@ UnitMRange(1:8)
```
"""
@propagate_inbounds shrink_last(x::AbstractVector, n::Integer) = x[firstindex(x):end - n]
shrink_last(x::AbstractRange, n::Integer) = set_last(x, last(x) - step(x) * n)
function shrink_last(x::AbstractRange, n::Integer)
if parent_type(x) <: typeof(x)
return set_last(x, last(x) - step(x) * n)
else
return unsafe_reconstruct(x, shrink_last(parent(x), n))
end
end
shrink_last(x::AbstractRange, n::AbstractUnitRange) = unsafe_reconstruct(x, n)

"""
Expand All @@ -238,11 +277,17 @@ UnitMRange(3:10)
```
"""
@propagate_inbounds shrink_first(x::AbstractVector, n::Integer) = x[(firstindex(x) + n):end]
shrink_first(x::AbstractRange, n::Integer) = set_first(x, first(x) + step(x) * n)
shrink_first(x::OneTo{T}, n::Integer) where {T} = UnitRange{T}(1 + n, last(x))
shrink_first(x::OneToMRange{T}, n::Integer) where {T} = UnitMRange{T}(1 + n, last(x))
shrink_first(x::OneToSRange{T}, n::Integer) where {T} = UnitSRange{T}(1 + n, last(x))
shrink_first(x::AbstractRange, n::AbstractUnitRange) = unsafe_reconstruct(x, n)
function shrink_first(x::AbstractRange, n::Integer)
if parent_type(x) <: typeof(x)
return set_first(x, first(x) + (step(x) * n))
else
return unsafe_reconstruct(x, shrink_first(parent(x), n))
end
end

"""
shrink_first!(x, n)
Expand All @@ -255,7 +300,14 @@ function shrink_first!(x::AbstractVector, n::Integer)
end
return x
end
shrink_first!(x::AbstractRange, n::Integer) = set_first!(x, first(x) + step(x) * n)
function shrink_first!(x::AbstractRange, n::Integer)
if parent_type(x) <: typeof(x)
set_first!(x, first(x) + step(x) * n)
else
shrink_first!(parent(x), n)
end
return x
end

"""
resize_last(x, n::Integer)
Expand Down

2 comments on commit db2d04e

@Tokazama
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/23136

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.3 -m "<description of version>" db2d04e1749d2a8e2c3c0b5211229a3823e19b26
git push origin v0.8.3

Please sign in to comment.