|
| 1 | +using BlockArrays: blocks, eachstoredindex, undef_blocks |
1 | 2 | using DerivableInterfaces: @interface, AbstractArrayInterface, interface
|
2 | 3 | using GPUArraysCore: @allowscalar
|
| 4 | +using SparseArraysBase: SparseArrayDOK |
3 | 5 |
|
4 | 6 | # TODO: Rewrite this so that it takes the blocking structure
|
5 | 7 | # made by combining the blocking of the axes (i.e. the blocking that
|
@@ -94,14 +96,17 @@ function map_zero_dim! end
|
94 | 96 | return a_dest
|
95 | 97 | end
|
96 | 98 |
|
97 |
| -# TODO: Decide what to do with these. |
| 99 | +# TODO: Do we need this function or can we just use `map`? |
| 100 | +# Probably it should be a special version of `map` where we |
| 101 | +# specify the function preserves zeros, i.e. |
| 102 | +# `map(f, a; preserves_zeros=true)` or `@preserves_zeros map(f, a)`. |
98 | 103 | function map_stored_blocks(f, a::AbstractArray)
|
99 |
| - bs = collect(eachblockstoredindex(a)) |
100 |
| - ds = map(b -> f(@view(a[b])), bs) |
101 |
| - # TODO: Use `similartype` instead? |
102 |
| - a′ = BlockSparseArray{eltype(eltype(ds)),ndims(a),eltype(ds)}(undef, axes(a)) |
103 |
| - for (b, d) in zip(bs, ds) |
104 |
| - a′[b] = d |
| 104 | + blocks_a = blocks(a) |
| 105 | + stored_indices = collect(eachstoredindex(a)) |
| 106 | + stored_blocks = map(I -> f(blocks_a[I]), stored_indices) |
| 107 | + blocks_a′ = SparseArrayDOK{eltype(stored_blocks)}(undef_blocks, axes(a)) |
| 108 | + for (I, b) in zip(stored_indices, stored_blocks) |
| 109 | + blocks_a′[I] = b |
105 | 110 | end
|
106 |
| - return a′ |
| 111 | + return sparsemortar(blocks_a′, axes(a)) |
107 | 112 | end
|
0 commit comments