Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Definition of BlockUnitRange #373

Open
putianyi889 opened this issue Mar 31, 2024 · 3 comments
Open

Definition of BlockUnitRange #373

putianyi889 opened this issue Mar 31, 2024 · 3 comments

Comments

@putianyi889
Copy link
Contributor

putianyi889 commented Mar 31, 2024

A single firsts field should work, where the last index of the range is firsts[end]-1. The advantages are:

  • The codes will become cleaner and the performance can be better. There are currently many conditional statements on the first block. The proposed definition eliminates them.
  • The type stability can be improved. Now blocklengths(::BlockUnitRange{<:AbstractUnitRange}) is clearly a Fill and blocklengths(::BlockUnitRange{<:AbstractRange}) is a Vcat(::Int,::Fill). reference:
    function blockfirsts(a::BlockedUnitRange{Base.OneTo{Int}})
    first(a) == 1 || error("Offset axes not supported")
    Base.OneTo{Int}(length(blocklasts(a)))
    end
    function blocklengths(a::BlockedUnitRange{Base.OneTo{Int}})
    first(a) == 1 || error("Offset axes not supported")
    Ones{Int}(length(blocklasts(a)))
    end
    function blockfirsts(a::BlockedUnitRange{<:AbstractRange})
    st = step(blocklasts(a))
    first(a) == 1 || error("Offset axes not supported")
    @assert first(blocklasts(a))-first(a)+1 == st
    range(1; step=st, length=length(blocklasts(a)))
    end
    function blocklengths(a::BlockedUnitRange{<:AbstractRange})
    st = step(blocklasts(a))
    first(a) == 1 || error("Offset axes not supported")
    @assert first(blocklasts(a))-first(a)+1 == st
    Fill(st,length(blocklasts(a)))
    end
@jishnub
Copy link
Member

jishnub commented Apr 2, 2024

See #348, which generalizes a BlockedUnitRange to allow a first value that's not 1, and adds a BlockedOneTo type that enforces the first value to be 1.

julia> b = blockedrange(Ones(Int,3))
3-blocked 3-element BlockedOneTo{Base.OneTo{Int64}}:
 123

julia> blocklasts(b)
Base.OneTo(3)

julia> blockfirsts(b)
Base.OneTo(3)

julia> blocklengths(b)
3-element Ones{Int64}

julia> b = blockedrange(Fill(2,3))
3-blocked 6-element BlockedOneTo{StepRangeLen{Int64, Int64, Int64, Int64}}:
 1
 23
 45
 6

julia> blocklengths(b)
3-element Fill{Int64}, with entries equal to 2

julia> blockfirsts(b)
1:2:5

julia> blocklasts(b)
2:2:6

@putianyi889
Copy link
Contributor Author

This proposal is not about the first entry of the range. It's rather about the first block being separately processed.

@jishnub
Copy link
Member

jishnub commented Apr 9, 2024

Could you create a PR with this change to BlockedUnitRange to the release-1.0 branch? It would be good to test out the implications.

@mtfishman mtfishman mentioned this issue May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants