Skip to content

Commit 0f0407a

Browse files
committed
some bugfixes
1 parent 5d913ff commit 0f0407a

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/cat.jl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ function ConcatDiskArray(arrays::AbstractArray)
4242
error("Arrays don't have the same dimensions")
4343
return error("Should not be reached")
4444
end
45-
extenddims(a::NTuple{N},b::NTuple{M},fillval) where {N,M} = extenddims((a...,fillval), b, fillval)
46-
extenddims(a::NTuple{N},_::NTuple{N},_) where {N} = a
45+
extenddims(a::Tuple{Vararg{<:Any,N}}, b::Tuple{Vararg{<:Any,M}}, fillval) where {N,M} = extenddims((a..., fillval), b, fillval)
46+
extenddims(a::Tuple{Vararg{<:Any,N}}, _::Tuple{Vararg{<:Any,N}}, _) where {N} = a
4747

4848
Base.size(a::ConcatDiskArray) = a.size
4949

5050
function arraysize_and_startinds(arrays1)
5151
sizes = map(i->zeros(Int,i),size(arrays1))
5252
for i in CartesianIndices(arrays1)
5353
ai = arrays1[i]
54-
sizecur = size(ai)
54+
sizecur = extenddims(size(ai), size(arrays1), 1)
5555
foreach(sizecur,i.I,sizes) do si, ind, sizeall
5656
if sizeall[ind] == 0
5757
#init the size
@@ -79,12 +79,14 @@ haschunks(c::ConcatDiskArray) = c.haschunks
7979
function readblock!(a::ConcatDiskArray, aout, inds::AbstractUnitRange...)
8080
# Find affected blocks and indices in blocks
8181
_concat_diskarray_block_io(a, inds...) do outer_range, array_range, I
82-
aout[outer_range...] = a.parents[I][array_range...]
82+
vout = view(aout, outer_range...)
83+
readblock!(a.parents[I], vout, array_range...)
8384
end
8485
end
8586
function writeblock!(a::ConcatDiskArray, aout, inds::AbstractUnitRange...)
8687
_concat_diskarray_block_io(a, inds...) do outer_range, array_range, I
87-
a.parents[I][array_range...] = aout[outer_range...]
88+
data = view(aout, outer_range...)
89+
writeblock!(a.parents[I], data, array_range)
8890
end
8991
end
9092

@@ -99,17 +101,26 @@ function _concat_diskarray_block_io(f, a::ConcatDiskArray, inds...)
99101
end
100102
map(CartesianIndices(blockinds)) do cI
101103
myar = a.parents[cI]
102-
mysize = size(myar)
104+
mysize = extenddims(size(myar), cI.I, 1)
103105
array_range = map(cI.I, a.startinds, mysize, inds) do ii, si, ms, indstoread
104106
max(first(indstoread) - si[ii] + 1, 1):min(last(indstoread) - si[ii] + 1, ms)
105107
end
106108
outer_range = map(cI.I, a.startinds, array_range, inds) do ii, si, ar, indstoread
107109
(first(ar)+si[ii]-first(indstoread)):(last(ar)+si[ii]-first(indstoread))
108110
end
109-
# aout[outer_range...] = a.parents[cI][array_range...]
111+
#Shorten array range to shape of actual array
112+
array_range = map((i, j) -> j, size(myar), array_range)
113+
outer_range = fix_outerrangeshape(outer_range, array_range)
110114
f(outer_range, array_range, cI)
111115
end
112116
end
117+
fix_outerrangeshape(outer_range, array_range) = fix_outerrangeshape((), outer_range, array_range)
118+
fix_outerrangeshape(res, outer_range, array_range) =
119+
fix_outerrangeshape((res..., first(outer_range)), Base.tail(outer_range), Base.tail(array_range))
120+
fix_outerrangeshape(res, outer_range, ::Tuple{}) =
121+
fix_outerrangeshape((res..., only(first(outer_range))), Base.tail(outer_range), ())
122+
fix_outerrangeshape(res, ::Tuple{}, ::Tuple{}) = res
123+
113124

114125
function concat_chunksize(parents)
115126
newchunks = map(s->Vector{Union{RegularChunks, IrregularChunks}}(undef, s) ,size(parents))
@@ -125,6 +136,13 @@ function concat_chunksize(parents)
125136
end
126137
end
127138
newchunks = map(newchunks) do v
139+
#Chunks that have not been set are from additional dimensions in the parent array shape
140+
for i in eachindex(v)
141+
if !isassigned(v, i)
142+
v[i] = RegularChunks(1, 0, 1)
143+
end
144+
end
145+
# Merge the chunks
128146
init = RegularChunks(approx_chunksize(first(v)), 0, 0)
129147
reduce(mergechunks, v; init=init)
130148
end

0 commit comments

Comments
 (0)