Skip to content

Commit 99d9671

Browse files
authored
Fixed infinite indexing with cumsum (#173)
* Fixed infinite indexing with cumsum * Update Project.toml * Update Project.toml
1 parent 635b00f commit 99d9671

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

Project.toml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "InfiniteArrays"
22
uuid = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
3-
version = "0.13.7"
3+
version = "0.13.8"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
@@ -11,18 +11,10 @@ LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
1111
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1212
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1313

14-
[weakdeps]
15-
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
16-
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
17-
18-
[extensions]
19-
InfiniteArraysDSPExt = "DSP"
20-
InfiniteArraysStatisticsExt = "Statistics"
21-
2214
[compat]
2315
Aqua = "0.8"
24-
ArrayLayouts = "1.0"
25-
BandedMatrices = "0.17.18, 1"
16+
ArrayLayouts = "1.8"
17+
BandedMatrices = "0.17.18, 1.0"
2618
Base64 = "1"
2719
DSP = "0.7"
2820
FillArrays = "1.0"
@@ -35,6 +27,10 @@ Statistics = "1"
3527
Test = "1"
3628
julia = "1.6"
3729

30+
[extensions]
31+
InfiniteArraysDSPExt = "DSP"
32+
InfiniteArraysStatisticsExt = "Statistics"
33+
3834
[extras]
3935
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
4036
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
@@ -47,3 +43,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4743

4844
[targets]
4945
test = ["Aqua", "Test", "BandedMatrices", "LazyBandedMatrices", "Statistics", "SparseArrays", "Base64", "DSP"]
46+
47+
[weakdeps]
48+
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
49+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

src/infrange.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,10 @@ cumsum(r::InfRanges) = RangeCumsum(r)
513513
diff(r::InfRanges) = Fill(step(r),∞)
514514
diff(r::AbstractInfUnitRange{T}) where T = Ones{T}(∞)
515515
Base.@propagate_inbounds getindex(c::RangeCumsum, kr::OneToInf) = RangeCumsum(c.range[kr])
516-
getindex(c::RangeCumsum{<:Any,<:OneToInf}, k::Integer) = k * (k+1) ÷ 2
516+
Base.@propagate_inbounds function getindex(c::RangeCumsum{<:Any,<:OneToInf}, k::Integer)
517+
@boundscheck checkbounds(c, k)
518+
k * (k+1) ÷ 2
519+
end
517520
function union(r1::RangeCumsum{T1, OneToInf{T1}}, r2::RangeCumsum{T2, OneToInf{T2}}) where {T1,T2}
518521
T = promote_type(T1, T2)
519522
RangeCumsum(OneToInf{T}())
@@ -522,6 +525,12 @@ Base.issorted(r::RangeCumsum{<:Any,<:OneToInf}) = true
522525
Base.sort(r::RangeCumsum{<:Any,<:OneToInf}) = r
523526
Base.sort!(r::RangeCumsum{<:Any,<:OneToInf}) = r
524527

528+
getindex(c::RangeCumsum{<:Any,<:OneToInf}, ::InfiniteCardinal{0}) = last(c)
529+
Base.@propagate_inbounds function getindex(c::RangeCumsum{<:Any,<:AbstractRange}, k::InfiniteCardinal{0})
530+
@boundscheck checkbounds(c, k)
531+
last(c)
532+
end
533+
Base._unsafe_getindex(::IndexStyle, A::RangeCumsum, I::InfiniteCardinal{0}) = last(A)
525534
# vcat
526535

527536
vcat(a::Number, r::InfRanges) = Vcat(a, r)

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ end
226226
rs = cumsum(r)
227227
@test sort(rs) === sort!(rs) === rs
228228
@test @inferred((rs -> Val(issorted(rs)))(rs)) isa Val{true}
229+
@test rs[end] ℵ₀
229230
end
230231
end
231232
@testset "in" begin
@@ -945,6 +946,11 @@ end
945946
@test exp.(c)[1:20] == exp.(c[1:20])
946947
end
947948

949+
@test cumsum(3:4:∞)[end] cumsum(3:4:∞)[∞] cumsum(3:4:∞)[ℵ₀] RealInfinity()
950+
@test cumsum(2:∞)[end] cumsum(2:∞)[∞] cumsum(2:∞)[ℵ₀] cumsum(oneto(∞))[end] cumsum(oneto(∞))[∞] cumsum(oneto(∞))[ℵ₀] ℵ₀
951+
952+
@test_throws BoundsError cumsum(oneto(∞))[-5]
953+
948954
@test cumsum(1:∞)[2:∞][1:5] == cumsum(1:6)[2:end]
949955

950956
@testset "union of cumsum" begin

0 commit comments

Comments
 (0)