Skip to content

Commit d8c7717

Browse files
authored
Specialize axes for rangecumsum (#191)
1 parent 8f5ed36 commit d8c7717

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ArrayLayouts"
22
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
33
authors = ["Sheehan Olver <[email protected]>"]
4-
version = "1.4.4"
4+
version = "1.4.5"
55

66
[deps]
77
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
@@ -17,6 +17,7 @@ ArrayLayoutsSparseArraysExt = "SparseArrays"
1717
[compat]
1818
Aqua = "0.8"
1919
FillArrays = "1.2.1"
20+
Infinities = "0.1"
2021
LinearAlgebra = "1.6"
2122
Quaternions = "0.7"
2223
Random = "1.6"
@@ -27,11 +28,12 @@ julia = "1.6"
2728

2829
[extras]
2930
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
31+
Infinities = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
3032
Quaternions = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0"
3133
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
3234
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
3335
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
3436
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3537

3638
[targets]
37-
test = ["Aqua", "Random", "StableRNGs", "SparseArrays", "Test", "Quaternions"]
39+
test = ["Aqua", "Infinities", "Quaternions", "Random", "StableRNGs", "SparseArrays", "Test"]

src/cumsum.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct RangeCumsum{T, RR<:AbstractRange{T}} <: LayoutVector{T}
88
end
99

1010
size(c::RangeCumsum) = size(c.range)
11+
axes(c::RangeCumsum) = axes(c.range)
1112

1213
==(a::RangeCumsum, b::RangeCumsum) = a.range == b.range
1314
BroadcastStyle(::Type{<:RangeCumsum{<:Any,RR}}) where RR = BroadcastStyle(RR)

test/infinitearrays.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Infinite Arrays implementation from
2+
# https://github.com/JuliaLang/julia/blob/master/test/testhelpers/InfiniteArrays.jl
3+
module InfiniteArrays
4+
using Infinities
5+
export OneToInf
6+
7+
abstract type AbstractInfUnitRange{T<:Real} <: AbstractUnitRange{T} end
8+
Base.length(r::AbstractInfUnitRange) = ℵ₀
9+
Base.size(r::AbstractInfUnitRange) = (ℵ₀,)
10+
Base.last(r::AbstractInfUnitRange) = ℵ₀
11+
Base.axes(r::AbstractInfUnitRange) = (OneToInf(),)
12+
13+
Base.IteratorSize(::Type{<:AbstractInfUnitRange}) = Base.IsInfinite()
14+
15+
"""
16+
OneToInf(n)
17+
Define an `AbstractInfUnitRange` that behaves like `1:∞`, with the added
18+
distinction that the limits are guaranteed (by the type system) to
19+
be 1 and ∞.
20+
"""
21+
struct OneToInf{T<:Integer} <: AbstractInfUnitRange{T} end
22+
23+
OneToInf() = OneToInf{Int}()
24+
25+
Base.axes(r::OneToInf) = (r,)
26+
Base.first(r::OneToInf{T}) where {T} = oneunit(T)
27+
Base.oneto(::InfiniteCardinal{0}) = OneToInf()
28+
29+
struct InfUnitRange{T<:Real} <: AbstractInfUnitRange{T}
30+
start::T
31+
end
32+
Base.first(r::InfUnitRange) = r.start
33+
InfUnitRange(a::InfUnitRange) = a
34+
InfUnitRange{T}(a::AbstractInfUnitRange) where T<:Real = InfUnitRange{T}(first(a))
35+
InfUnitRange(a::AbstractInfUnitRange{T}) where T<:Real = InfUnitRange{T}(first(a))
36+
Base.:(:)(start::T, stop::InfiniteCardinal{0}) where {T<:Integer} = InfUnitRange{T}(start)
37+
function getindex(v::InfUnitRange{T}, i::Integer) where T
38+
@boundscheck i > 0 || Base.throw_boundserror(v, i)
39+
convert(T, first(v) + i - 1)
40+
end
41+
end

test/test_cumsum.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using ArrayLayouts, Test
22

3+
include("infinitearrays.jl")
4+
35
@testset "RangeCumsum" begin
46
for r in (RangeCumsum(Base.OneTo(5)), RangeCumsum(2:5), RangeCumsum(2:2:6), RangeCumsum(6:-2:1))
57
@test r == cumsum(r.range)
@@ -20,4 +22,7 @@ using ArrayLayouts, Test
2022
a = RangeCumsum(Base.OneTo(3))
2123
b = RangeCumsum(1:3)
2224
@test oftype(a, b) === a
25+
26+
r = RangeCumsum(InfiniteArrays.OneToInf())
27+
@test axes(r, 1) == InfiniteArrays.OneToInf()
2328
end

0 commit comments

Comments
 (0)