-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename BlockZero to GetUnstoredBlock
- Loading branch information
Showing
3 changed files
with
26 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,25 @@ | ||
using BlockArrays: Block, blockedrange | ||
using ArrayLayouts: zero! | ||
using BlockArrays: Block | ||
|
||
# Extensions to BlockArrays.jl | ||
blocktuple(b::Block) = Block.(b.n) | ||
inttuple(b::Block) = b.n | ||
|
||
# The size of a block | ||
function block_size(axes::Tuple{Vararg{AbstractUnitRange}}, block::Block) | ||
return length.(getindex.(axes, blocktuple(block))) | ||
end | ||
|
||
# The size of a block | ||
function block_size(blockinds::Tuple{Vararg{AbstractVector}}, block::Block) | ||
return block_size(blockedrange.(blockinds), block) | ||
end | ||
|
||
struct BlockZero{Axes} | ||
struct GetUnstoredBlock{Axes} | ||
axes::Axes | ||
end | ||
|
||
function (f::BlockZero)(a::AbstractArray, I...) | ||
return f(eltype(a), I...) | ||
end | ||
|
||
function (f::BlockZero)(arraytype::Type{<:SubArray{<:Any,<:Any,P}}, I...) where {P} | ||
return f(P, I...) | ||
end | ||
|
||
function (f::BlockZero)(arraytype::Type{<:AbstractArray}, I::CartesianIndex) | ||
return f(arraytype, Tuple(I)...) | ||
end | ||
|
||
function (f::BlockZero)(arraytype::Type{<:AbstractArray}, I::Int...) | ||
@inline function (f::GetUnstoredBlock)( | ||
a::AbstractArray{<:Any,N}, I::Vararg{Int,N} | ||
) where {N} | ||
# TODO: Make sure this works for sparse or block sparse blocks, immutable | ||
# blocks, diagonal blocks, etc.! | ||
blck_size = block_size(f.axes, Block(I)) | ||
blck_type = similartype(arraytype, blck_size) | ||
return fill!(blck_type(undef, blck_size), false) | ||
end | ||
|
||
# Fallback so that `SparseArray` with scalar elements works. | ||
function (f::BlockZero)(blocktype::Type{<:Number}, I...) | ||
return zero(blocktype) | ||
end | ||
|
||
# Fallback to Array if it is abstract | ||
function (f::BlockZero)(arraytype::Type{AbstractArray{T,N}}, I...) where {T,N} | ||
return f(Array{T,N}, I...) | ||
b_size = ntuple(ndims(a)) do d | ||
return length(f.axes[d][Block(I[d])]) | ||
end | ||
b = similar(eltype(a), b_size) | ||
zero!(b) | ||
return b | ||
end | ||
# TODO: Use `Base.to_indices`. | ||
@inline function (f::GetUnstoredBlock)( | ||
a::AbstractArray{<:Any,N}, I::CartesianIndex{N} | ||
) where {N} | ||
return f(a, Tuple(I)...) | ||
end |