Skip to content

Commit

Permalink
Vector indexing for BlockRange (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Jun 21, 2023
1 parent f1826ef commit 881e81e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,19 @@ broadcasted(::DefaultArrayStyle{0}, ::Type{Int}, block::Block{1}) = Int(block)

# AbstractArray implementation
axes(iter::BlockRange{N,R}) where {N,R} = map(axes1, iter.indices)
@inline function Base.getindex(iter::BlockRange{N,<:NTuple{N,Base.OneTo}}, I::Vararg{Integer, N}) where {N}
@inline function Base.getindex(iter::BlockRange{N,<:NTuple{N,Base.OneTo}}, I::Vararg{Int, N}) where {N}
@boundscheck checkbounds(iter, I...)
Block(I)
end
@inline function Base.getindex(iter::BlockRange{N,R}, I::Vararg{Integer, N}) where {N,R}
@propagate_inbounds function Base.getindex(iter::BlockRange{N}, I::Vararg{Int, N}) where {N}
@boundscheck checkbounds(iter, I...)
Block(I .- first.(axes1.(iter.indices)) .+ first.(iter.indices))
Block(getindex.(iter.indices, I))
end

@propagate_inbounds function Base.getindex(iter::BlockRange{N}, I::Vararg{AbstractUnitRange{<:Integer}, N}) where {N}
@boundscheck checkbounds(iter, I...)
BlockRange(getindex.(iter.indices, I))
end

@inline function iterate(iter::BlockRange)
iterfirst, iterlast = first(iter), last(iter)
Expand Down
7 changes: 7 additions & 0 deletions test/test_blockindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ end
@test b[Block(100_000)] == 299_998:300_000
@test_throws BlockBoundsError b[Block(0)]
@test_throws BlockBoundsError b[Block(1_000_001)]

b = BlockRange((2:4, 3:4))
@test b[2,2] === Block(3,4)
@test b[axes(b)...] === b

b = BlockRange(OffsetArrays.IdOffsetRange.((2:4, 3:5), 2))
@test b[axes(b)...] === b
end

@testset "firsts/lasts/lengths" begin
Expand Down

2 comments on commit 881e81e

@jishnub
Copy link
Member Author

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

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 v0.16.32 -m "<description of version>" 881e81ee7d0ff177bdb03735eca13664a93580c1
git push origin v0.16.32

Please sign in to comment.