Skip to content

Commit

Permalink
Use Infinities
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Nov 30, 2023
1 parent 1b6ac70 commit f4938d1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 35 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ LazyArraysStaticArraysExt = "StaticArrays"
Aqua = "0.6"
ArrayLayouts = "1.4.1"
FillArrays = "1.0"
Infinities = "0.1"
MacroTools = "0.5"
MatrixFactorizations = "1.0, 2.0"
StaticArrays = "1.0"
Expand All @@ -29,9 +30,10 @@ julia = "1.9"
[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Infinities = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"

[targets]
test = ["Aqua", "Base64", "StaticArrays", "Tracker", "Test"]
test = ["Aqua", "Base64", "StaticArrays", "Tracker", "Test", "Infinities"]
4 changes: 3 additions & 1 deletion test/cachetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import LazyArrays: CachedArray, CachedMatrix, CachedVector, PaddedLayout, Cached
CachedAbstractArray, CachedAbstractVector, CachedAbstractMatrix, AbstractCachedArray, AbstractCachedMatrix

include("infinitearrays.jl")
using .InfiniteArrays
using Infinities

@testset "Cache" begin
@testset "basics" begin
Expand Down Expand Up @@ -175,7 +177,7 @@ include("infinitearrays.jl")
@test c[2:3] isa LazyArrays.CachedVector{Int,Vector{Int},Fill{Int,1,Tuple{Base.OneTo{Int}}}}
@test c[[2,4,6]] isa LazyArrays.CachedVector{Int,Vector{Int},Fill{Int,1,Tuple{Base.OneTo{Int}}}}

F = Fill(2, InfiniteArrays.Infinity())
F = Fill(2, ℵ₀)
C = cumsum(cache(F))
@test axes(C) == (InfiniteArrays.OneToInf(),)
end
Expand Down
38 changes: 5 additions & 33 deletions test/infinitearrays.jl
Original file line number Diff line number Diff line change
@@ -1,38 +1,12 @@
# Infinite Arrays implementation from
# https://github.com/JuliaLang/julia/blob/master/test/testhelpers/InfiniteArrays.jl
module InfiniteArrays
export OneToInf, Infinity

"""
Infinity()
Represents infinite cardinality. Note that `Infinity <: Integer` to support
being treated as an index.
"""
struct Infinity <: Integer end

Base.:(==)(::Infinity, ::Int) = false
Base.:(==)(::Int, ::Infinity) = false
Base.:(<)(::Int, ::Infinity) = true
Base.:(<)(::Infinity, ::Int) = false
Base.:()(::Int, ::Infinity) = true
Base.:()(::Infinity, ::Int) = false
Base.:()(::Infinity, ::Infinity) = true
Base.:(-)(::Infinity, ::Int) = Infinity()
Base.:(+)(::Infinity, ::Int) = Infinity()
Base.:(:)(::Infinity, ::Infinity) = 1:0

Base.:(+)(::Integer, ::Infinity) = Infinity()
Base.:(+)(::Infinity, ::Integer) = Infinity()
Base.:(*)(::Integer, ::Infinity) = Infinity()
Base.:(*)(::Infinity, ::Integer) = Infinity()

Base.isinf(::Infinity) = true
using Infinities

abstract type AbstractInfUnitRange{T<:Real} <: AbstractUnitRange{T} end
Base.length(r::AbstractInfUnitRange) = Infinity()
Base.size(r::AbstractInfUnitRange) = (Infinity(),)
Base.unitrange(r::AbstractInfUnitRange) = InfUnitRange(r)
Base.last(r::AbstractInfUnitRange) = Infinity()
Base.length(r::AbstractInfUnitRange) = ℵ₀
Base.size(r::AbstractInfUnitRange) = (ℵ₀,)
Base.last(r::AbstractInfUnitRange) = ℵ₀
Base.axes(r::AbstractInfUnitRange) = (OneToInf(),)

Base.IteratorSize(::Type{<:AbstractInfUnitRange}) = Base.IsInfinite()
Expand All @@ -49,7 +23,6 @@ module InfiniteArrays

Base.axes(r::OneToInf) = (r,)
Base.first(r::OneToInf{T}) where {T} = oneunit(T)
Base.oneto(::Infinity) = OneToInf()

struct InfUnitRange{T<:Real} <: AbstractInfUnitRange{T}
start::T
Expand All @@ -58,8 +31,7 @@ module InfiniteArrays
InfUnitRange(a::InfUnitRange) = a
InfUnitRange{T}(a::AbstractInfUnitRange) where T<:Real = InfUnitRange{T}(first(a))
InfUnitRange(a::AbstractInfUnitRange{T}) where T<:Real = InfUnitRange{T}(first(a))
unitrange(a::AbstractInfUnitRange) = InfUnitRange(a)
Base.:(:)(start::T, stop::Infinity) where {T<:Integer} = InfUnitRange{T}(start)
Base.:(:)(start::T, stop::InfiniteCardinal{0}) where {T<:Integer} = InfUnitRange{T}(start)
function getindex(v::InfUnitRange{T}, i::Integer) where T
@boundscheck i > 0 || Base.throw_boundserror(v, i)
convert(T, first(v) + i - 1)
Expand Down

0 comments on commit f4938d1

Please sign in to comment.