Skip to content

Commit

Permalink
Allow indexing with Vector along a dimension (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
meggart authored Mar 9, 2020
1 parent 3e17500 commit 0f73499
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DiskArrays"
uuid = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
authors = ["Fabian Gans <[email protected]>"]
version = "0.2.0"
version = "0.2.1"

[compat]
julia = "1.0"
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,7 @@ Reading at index Base.OneTo(10) Base.OneTo(9) 1:1

## Accessing strided Arrays

One
There are situations where one wants to read every other value along a certain axis or provide arbitrary strides. Some DiskArray backends may want to provide optimized methods to read these strided arrays.
In this case a backend can define `readblock!(a,aout,r::OrdinalRange...)` and the respective `writeblock`
method which will overwrite the fallback behavior that would read the whol block of data and only return
the desired range.
8 changes: 4 additions & 4 deletions src/DiskArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ should be supported as well.
"""
function writeblock!() end

function readblock!(A::AbstractDiskArray, A_ret, r::OrdinalRange...)
function readblock!(A::AbstractDiskArray, A_ret, r::AbstractVector...)
#Implement fallback method if DiskArray does not support strided reading
#Currently this allocates an intermediate array. In the future, this
#function could test how sparse the reading is and maybe be smarter here
Expand All @@ -37,7 +37,7 @@ function readblock!(A::AbstractDiskArray, A_ret, r::OrdinalRange...)
nothing
end

function writeblock!(A::AbstractDiskArray, A_ret, r::OrdinalRange...)
function writeblock!(A::AbstractDiskArray, A_ret, r::AbstractVector...)
#Implement fallback method if DiskArray does not support strided reading
#Currently this allocates an intermediate array. In the future, this
#function could test how sparse the reading is and maybe be smarter here
Expand Down Expand Up @@ -99,7 +99,7 @@ interpret_indices_disk(A, r::Tuple{<:CartesianIndices}) =



function interpret_indices_disk(A, r::NTuple{N, Union{Integer, OrdinalRange, Colon}}) where N
function interpret_indices_disk(A, r::NTuple{N, Union{Integer, AbstractVector, Colon}}) where N
if ndims(A)==N
inds = map(_convert_index,r,size(A))
resh = DimsDropper(findints(r))
Expand Down Expand Up @@ -173,7 +173,7 @@ _findints(c, i, x, rest...) = _findints(c, i+1, rest...)
_findints(c, i) = c
#Normal indexing for a full subset of an array
_convert_index(i::Integer, s::Integer) = i:i
_convert_index(i::OrdinalRange, s::Integer) = i
_convert_index(i::AbstractVector, s::Integer) = i
_convert_index(::Colon, s::Integer) = Base.OneTo(Int(s))

macro implement_getindex(t)
Expand Down
2 changes: 1 addition & 1 deletion src/chunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Base.size(g::GridChunks) = g.chunkgridsize
Base.size(g::GridChunks, dim) = g.chunkgridsize[dim]
Base.IteratorSize(::Type{GridChunks{N}}) where N = Base.HasShape{N}()
Base.eltype(::Type{GridChunks{N}}) where N = CartesianIndices{N,NTuple{N,UnitRange{Int64}}}
Base.length(c) = prod(size(c))
Base.length(c::GridChunks) = prod(size(c))
@inline function _iterate(g,r)
if r === nothing
return nothing
Expand Down
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ function test_getindex(a)
@test a[1:2, 1:2,1,1] == [1 5; 2 6]
@test a[2:2:4,1:2:5] == [2 10 18; 4 12 20]
@test a[end:-1:1,1,1] == [4,3,2,1]
@test a[[1,3,4],[1,3],1] == [1 9; 3 11; 4 12]
# Test bitmask indexing
m = falses(4,5,1)
m[2,:,1] .= true
@test a[m] == [2,6,10,14,18]
#Test that readblock was called exactly onces for every getindex
@test getindex_count(a) == 8
@test getindex_count(a) == 9
end

function test_setindex(a)
Expand All @@ -72,6 +73,9 @@ function test_setindex(a)
a[1:2:4,1:2:5,1] = [1 2 3; 5 6 7]
@test trueparent(a)[1:2:4,1:2:5,1] == [1 2 3; 5 6 7]
@test setindex_count(a) == 7
a[[2,4],1:2,1] = [1 2; 5 6]
@test trueparent(a)[[2,4],1:2,1] == [1 2; 5 6]
@test setindex_count(a) == 8
end

function test_view(a)
Expand Down

4 comments on commit 0f73499

@meggart
Copy link
Collaborator Author

@meggart meggart commented on 0f73499 Mar 9, 2020

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

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 Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.1 -m "<description of version>" 0f73499547f65d6f3a27dfe48a5a03dcec9aefa4
git push origin v0.2.1

@meggart
Copy link
Collaborator Author

@meggart meggart commented on 0f73499 Mar 9, 2020

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 updated: JuliaRegistries/General/10738

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 Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.1 -m "<description of version>" 0f73499547f65d6f3a27dfe48a5a03dcec9aefa4
git push origin v0.2.1

Please sign in to comment.