Skip to content

Commit 559dd24

Browse files
committed
revamp range computation
1 parent 23a8020 commit 559dd24

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/blockmap.jl

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ BlockMap{T}(maps::As, rows::Rs) where {T, As<:LinearMapTuple, Rs} =
2424

2525
MulStyle(A::BlockMap) = MulStyle(A.maps...)
2626

27+
@inline function _getranges(maps, dim, inds::NTuple{N,Int}=ntuple(identity, Val(length(maps)))) where {N}
28+
sizes = ntuple(i -> (@inbounds size(maps[inds[i]], dim)), Val(length(inds)))
29+
ends = cumsum(sizes)
30+
starts = (1, (1 .+ Base.front(ends))...)
31+
return UnitRange.(starts, ends)
32+
end
33+
2734
"""
2835
rowcolranges(maps, rows)
2936
@@ -32,24 +39,19 @@ map in `maps`, according to its position in a virtual matrix representation of t
3239
block linear map obtained from `hvcat(rows, maps...)`.
3340
"""
3441
function rowcolranges(maps, rows)
35-
rowranges::NTuple{length(rows),UnitRange{Int}} = ntuple(n->1:0, Val(length(rows)))
36-
colranges::NTuple{length(maps),UnitRange{Int}} = ntuple(n->1:0, Val(length(maps)))
37-
mapind = 0
38-
rowstart = 1
39-
for (i, row) in enumerate(rows)
40-
mapind += 1
41-
rowend = rowstart + Int(size(maps[mapind], 1))::Int - 1
42-
rowranges = Base.setindex(rowranges, rowstart:rowend, i)
43-
colstart = 1
44-
colend = Int(size(maps[mapind], 2))::Int
45-
colranges = Base.setindex(colranges, colstart:colend, mapind)
46-
for colind in 2:row
47-
mapind += 1
48-
colstart = colend + 1
49-
colend += Int(size(maps[mapind], 2))::Int
50-
colranges = Base.setindex(colranges, colstart:colend, mapind)
51-
end
52-
rowstart = rowend + 1
42+
# find indices of the row-wise first maps
43+
firstmapinds = cumsum((1, Base.front(rows)...))
44+
# compute rowranges from size(map, 1) of the row-wise first maps
45+
rowranges = _getranges(maps, 1, firstmapinds)
46+
47+
# compute ranges from size(map, 1) as if all in one row
48+
temp = _getranges(maps, 2)
49+
# introduce "line breaks"
50+
colranges = ntuple(Val(length(maps))) do i
51+
# for each map find the index of the respective row-wise first map
52+
firstmapind = firstmapinds[something(findlast(<=(i), firstmapinds), 1)]
53+
# shift ranges by the first col-index of the row-wise first map
54+
return temp[i] .- first(temp[firstmapind]) .+ 1
5355
end
5456
return rowranges, colranges
5557
end

0 commit comments

Comments
 (0)