Skip to content

Commit 294fc9c

Browse files
authored
always alllow scalar indexing on CachedDiskArray (#257)
* alllow scalar indexin on CachedDiskArray * bugfix
1 parent f8ce649 commit 294fc9c

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

src/cached.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ function CachedDiskArray(A::AbstractArray{T,N}; maxsize=1000, mmap=false) where
5050
CachedDiskArray(A, LRU{ChunkIndex{N,OffsetChunks},OffsetArray{T,N,Array{T,N}}}(; by, maxsize),mmap)
5151
end
5252

53+
# Scalar indexing is allowed on CachedDiskArray
54+
checkscalar(::Type{Bool}, a::CachedDiskArray, i::Tuple) = true
55+
checkscalar(::Type{Bool}, a::CachedDiskArray, i::Tuple{}) = true
56+
5357
Base.parent(A::CachedDiskArray) = A.parent
5458
Base.size(A::CachedDiskArray) = size(parent(A))
5559
# TODO we need to invalidate caches when we write

src/indexing.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Internal `getindex` for disk arrays.
2929
Converts indices to ranges and calls `DiskArrays.readblock!`
3030
"""
3131
function getindex_disk(a::AbstractArray, i::Union{Integer,CartesianIndex}...)
32-
checkscalar(i)
32+
checkscalar(a, i)
3333
checkbounds(a, i...)
3434
# Use a 1 x 1 block
3535
outputarray = Array{eltype(a)}(undef, map(_ -> 1, size(a))...)
@@ -44,7 +44,7 @@ function getindex_disk(a::AbstractArray, i::Union{Integer,CartesianIndex}...)
4444
return only(outputarray)
4545
end
4646
function getindex_disk(a::AbstractArray, i::Integer)
47-
checkscalar(i)
47+
checkscalar(a, i)
4848
checkbounds(a, i)
4949
# Use a 1 x 1 block
5050
outputarray = Array{eltype(a)}(undef, map(_ -> 1, size(a))...)
@@ -121,7 +121,7 @@ Internal `setindex!` for disk arrays.
121121
Converts indices to ranges and calls `DiskArrays.writeblock!`
122122
"""
123123
function setindex_disk!(a::AbstractArray{T}, values::T, i...) where {T<:AbstractArray}
124-
checkscalar(i)
124+
checkscalar(a, i)
125125
# If values are not an array, wrap them in a vector
126126
return setindex_disk!(a, [values], i...)
127127
end

src/scalar.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ canscalar() = ALLOWSCALAR[]
2424

2525
# Checks if an index is scalar at all, and then if scalar indexing is allowed.
2626
# Syntax as for `checkbounds`.
27-
checkscalar(::Type{Bool}) = true # Handle 0 dimensional
28-
checkscalar(::Type{Bool}, I::Tuple) = checkscalar(Bool, I...)
29-
checkscalar(::Type{Bool}, I...) = !all(map(i -> i isa Int, I)) || canscalar()
30-
checkscalar(I::Tuple) = checkscalar(I...)
31-
checkscalar(I...) = checkscalar(Bool, I...) || _scalar_error()
27+
checkscalar(::Type{Bool}, A::AbstractArray, ::Tuple{}) = true # Handle 0 dimensional
28+
checkscalar(::Type{Bool}, A::AbstractArray, I::Tuple) = !all(map(i -> i isa Int, I)) || canscalar()
29+
checkscalar(::Type{Bool}, A::AbstractArray, I...) = checkscalar(Bool, A, (I...,))
30+
checkscalar(A::AbstractArray, I::Tuple) = checkscalar(Bool, A, I::Tuple) || _scalar_error()
31+
checkscalar(A::AbstractArray, I...) = checkscalar(A, I)
3232

3333
function _scalar_error()
3434
return error(

test/runtests.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ end
2222
@testset "allowscalar" begin
2323
DiskArrays.allowscalar(false)
2424
@test DiskArrays.canscalar() == false
25-
@test DiskArrays.checkscalar(Bool) == true # Always allowed for zero dimensional
26-
@test DiskArrays.checkscalar(Bool, 1, 2, 3) == false
27-
@test DiskArrays.checkscalar(Bool, 1, 2:5, :) == true
25+
@test DiskArrays.checkscalar(Bool, fill(Int), ()) == true # Always allowed for zero dimensional
26+
@test DiskArrays.checkscalar(Bool, zeros(5, 5, 5), (1, 2, 3)) == false
27+
@test DiskArrays.checkscalar(Bool, zeros(5, 5, 5), (1, 2:5, :)) == true
2828
DiskArrays.allowscalar(true)
2929
@test DiskArrays.canscalar() == true
30-
@test DiskArrays.checkscalar(Bool) == true
31-
@test DiskArrays.checkscalar(Bool, 1, 2, 3) == true
32-
@test DiskArrays.checkscalar(Bool, :, 2:5, 3) == true
30+
@test DiskArrays.checkscalar(Bool, fill(Int), ()) == true
31+
@test DiskArrays.checkscalar(Bool, zeros(5, 5, 5), (1, 2, 3)) == true
32+
@test DiskArrays.checkscalar(Bool, zeros(5, 5, 5), (:, 2:5, 3)) == true
3333
a = AccessCountDiskArray(reshape(1:24, 2, 3, 4), chunksize=(2, 2, 2))
3434
@test a[1, 2, 3] == 15
3535
@test a[1, 2, 3, 1] == 15
@@ -973,6 +973,8 @@ end
973973
@test ca[:, 3, 1] == ch[:, 3, 1]
974974
@test ca[:, 200, 1] == ch[:, 200, 1]
975975
@test ca[200, :, 1] == ch[200, :, 1]
976+
# Test scalar indexing is not checked for CachedDiskArray
977+
@test ca[200, 1, 1] == ch[200, 1:1, 1][1]
976978
end
977979
end
978980

0 commit comments

Comments
 (0)