Skip to content

Commit

Permalink
Sort RangeCumsum for AbstractUnitRange (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Apr 4, 2024
1 parent abc31ef commit 13d5500
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ArrayLayouts"
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
authors = ["Sheehan Olver <solver@mac.com>"]
version = "1.9.0"
version = "1.9.1"

[deps]
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
Expand Down
10 changes: 10 additions & 0 deletions src/cumsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ union(a::RangeCumsum{<:Any,<:OneTo}, b::RangeCumsum{<:Any,<:OneTo}) =

sort!(a::RangeCumsum{<:Any,<:Base.OneTo}) = a
sort(a::RangeCumsum{<:Any,<:Base.OneTo}) = a
function sort(a::RangeCumsum{<:Any,<:AbstractUnitRange{<:Integer}})
r = parent(a)
#= A RangeCumsum with a range (-a:b) for positive (a,b) may be viewed as a concatenation
of two components: (-a:a) and (a+1:b). The second is strictly increasing.
We only sort the first component, and concatenate the second to the result.
=#
a1 = RangeCumsum(r[firstindex(r):searchsortedlast(r, -first(r))])
a2 = RangeCumsum(r[searchsortedfirst(r, -first(r)+1):end])
vcat(sort!(Vector(a1)), a2)
end
Base.issorted(a::RangeCumsum{<:Any,<:Base.OneTo}) = true
function Base.issorted(a::RangeCumsum{<:Any,<:AbstractUnitRange{<:Integer}})
r = parent(a)
Expand Down
20 changes: 14 additions & 6 deletions test/test_cumsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ cmpop(p) = isinteger(real(first(p))) && isinteger(real(step(p))) ? (==) : (≈)

a,b = RangeCumsum(Base.OneTo(5)), RangeCumsum(Base.OneTo(6))
@test union(a,b) union(b,a) b
@test sort!(copy(a)) == a
@test sort!(a) a
@test sort(a) a == Vector(a)

r = RangeCumsum(-4:4)
@test sort(r) == sort(Vector(r))

a = RangeCumsum(Base.OneTo(3))
b = RangeCumsum(1:3)
Expand Down Expand Up @@ -97,6 +91,20 @@ cmpop(p) = isinteger(real(first(p))) && isinteger(real(step(p))) ? (==) : (≈)
@test issorted(a)
end

@testset "sort" begin
@testset "RangeCumsum($start:$stop)" for start in -15:15, stop in start-1:20
a = RangeCumsum(start:stop)
v = Vector(a)
@test sort(a) == sort(v)
end

a,b = RangeCumsum(Base.OneTo(5)), RangeCumsum(Base.OneTo(6))
@test union(a,b) union(b,a) b
@test sort!(copy(a)) == a
@test sort!(a) a
@test sort(a) a == Vector(a)
end

@testset "minimum/maximum" begin
@testset "RangeCumsum($start:$stop)" for start in -15:15, stop in start:20
a = RangeCumsum(start:stop)
Expand Down

2 comments on commit 13d5500

@jishnub
Copy link
Member Author

@jishnub jishnub commented on 13d5500 Apr 4, 2024

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/104225

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v1.9.1 -m "<description of version>" 13d5500e6f4131ca2f8959f3fd4eb7c14af3c18f
git push origin v1.9.1

Please sign in to comment.