@@ -24,6 +24,13 @@ BlockMap{T}(maps::As, rows::Rs) where {T, As<:LinearMapTuple, Rs} =
24
24
25
25
MulStyle (A:: BlockMap ) = MulStyle (A. maps... )
26
26
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
+
27
34
"""
28
35
rowcolranges(maps, rows)
29
36
@@ -32,24 +39,19 @@ map in `maps`, according to its position in a virtual matrix representation of t
32
39
block linear map obtained from `hvcat(rows, maps...)`.
33
40
"""
34
41
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
53
55
end
54
56
return rowranges, colranges
55
57
end
0 commit comments