@@ -31,7 +31,6 @@ blockappend!(dest::BlockVector, s1, s2, sources...) =
31
31
foldl (blockappend!, (s1, s2, sources... ); init = dest)
32
32
33
33
function blockappend! (dest:: BlockVector{<:Any,T} , src:: BlockVector{<:Any,T} ) where {T}
34
- isempty (src) && return dest
35
34
append! (dest. blocks, src. blocks)
36
35
offset = last (dest. axes[1 ]) + 1 - src. axes[1 ]. first
37
36
append! (dest. axes[1 ]. lasts, (n + offset for n in src. axes[1 ]. lasts))
@@ -43,28 +42,70 @@ function blockappend!(
43
42
src:: PseudoBlockVector{<:Any,T} ,
44
43
) where {T}
45
44
if blocklength (src) == 1
46
- return blockappend ! (dest, src. blocks)
45
+ return _blockpush ! (dest, src. blocks)
47
46
else
48
47
return blockappend_fallback! (dest, src)
49
48
end
50
49
end
51
50
52
- function blockappend! (
51
+ blockappend! (
53
52
dest:: BlockVector{<:Any,<:AbstractArray{T}} ,
54
53
src:: T ,
55
- ) where {T<: AbstractVector }
56
- isempty (src) && return dest
57
- push! (dest. blocks, src)
58
- push! (dest. axes[1 ]. lasts, last (dest. axes[1 ]) + length (src))
59
- return dest
60
- end
54
+ ) where {T<: AbstractVector } = _blockpush! (dest, src)
61
55
62
56
blockappend! (dest:: BlockVector{<:Any,<:Any} , src:: AbstractVector ) =
63
57
blockappend_fallback! (dest, src)
64
58
65
59
blockappend_fallback! (dest:: BlockVector{<:Any,<:AbstractArray{T}} , src) where {T} =
66
60
blockappend! (dest, mortar ([convert (T, @view src[b]) for b in blockaxes (src, 1 )]))
67
61
62
+ """
63
+ blockpush!(dest::BlockVector, blocks...) -> dest
64
+
65
+ Push `blocks` to `dest`.
66
+
67
+ This function avoids copying the elements of the `blocks` when these blocks
68
+ are compatible with `dest`. Importantly, this means that mutating `blocks`
69
+ afterwards alters the items in `dest` and it may even break the invariance
70
+ of `dest` if the length of `blocks` are changed.
71
+
72
+ # Examples
73
+ ```jldoctest
74
+ julia> using BlockArrays
75
+
76
+ julia> blockpush!(mortar([[1], [2, 3]]), [4, 5], [6])
77
+ 4-blocked 6-element BlockArray{Int64,1}:
78
+ 1
79
+ ─
80
+ 2
81
+ 3
82
+ ─
83
+ 4
84
+ 5
85
+ ─
86
+ 6
87
+ ```
88
+ """
89
+ blockpush! (dest:: BlockVector , blocks... ) = foldl (blockpush!, blocks; init = dest)
90
+
91
+ blockpush! (dest:: BlockVector{<:Any,<:AbstractArray{T}} , block:: T ) where {T} =
92
+ _blockpush! (dest, block)
93
+
94
+ function blockpush! (dest:: BlockVector , block)
95
+ if Iterators. IteratorSize (block) isa Union{Base. HasShape,Base. HasLength}
96
+ newblock = copyto! (eltype (dest. blocks)(undef, length (block)), block)
97
+ else
98
+ newblock = foldl (push!, block; init = eltype (dest. blocks)(undef, 0 ))
99
+ end
100
+ return _blockpush! (dest, newblock)
101
+ end
102
+
103
+ function _blockpush! (dest, block)
104
+ push! (dest. blocks, block)
105
+ push! (dest. axes[1 ]. lasts, last (dest. axes[1 ]) + length (block))
106
+ return dest
107
+ end
108
+
68
109
"""
69
110
append!(dest::BlockVector, sources...)
70
111
0 commit comments