-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathblockdeque.jl
336 lines (278 loc) · 8.1 KB
/
blockdeque.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"""
blockappend!(dest::BlockVector, sources...) -> dest
Append blocks from `sources` to `dest`. The number of blocks in `dest` are
increased by `sum(blocklength, sources)`.
This function avoids copying the elements of the blocks in `sources` when
these blocks are compatible with `dest`. Importantly, this means that
mutating `sources` afterwards alters the items in `dest` and it may even
break the invariance of `dest` if the length of `sources` are changed.
The blocks in `dest` must not alias with `sources` or components of them.
For example, the result of `blockappend!(x, x)` is undefined.
# Examples
```jldoctest
julia> using BlockArrays
julia> blockappend!(mortar([[1], [2, 3]]), mortar([[4, 5]]))
3-blocked 5-element BlockVector{Int64}:
1
─
2
3
─
4
5
```
"""
blockappend!(dest::BlockVector, s1, s2, sources...) =
foldl(blockappend!, (s1, s2, sources...); init = dest)
function blockappend!(dest::BlockVector{<:Any,T}, src::BlockVector{<:Any,T}) where {T}
append!(dest.blocks, src.blocks)
offset = last(dest.axes[1]) + 1 - first(src.axes[1])
append!(blocklasts(dest.axes[1]), (n + offset for n in blocklasts(src.axes[1])))
return dest
end
function blockappend!(
dest::BlockVector{<:Any,<:AbstractArray{T}},
src::PseudoBlockVector{<:Any,T},
) where {T}
if blocklength(src) == 1
return _blockpush!(dest, src.blocks)
else
return blockappend_fallback!(dest, src)
end
end
blockappend!(
dest::BlockVector{<:Any,<:AbstractArray{T}},
src::T,
) where {T<:AbstractVector} = _blockpush!(dest, src)
blockappend!(dest::BlockVector{<:Any,<:Any}, src::AbstractVector) =
blockappend_fallback!(dest, src)
blockappend_fallback!(dest::BlockVector{<:Any,<:AbstractArray{T}}, src) where {T} =
blockappend!(dest, mortar([convert(T, @view src[b]) for b in blockaxes(src, 1)]))
"""
blockpush!(dest::BlockVector, blocks...) -> dest
Push `blocks` to the end of `dest`.
This function avoids copying the elements of the `blocks` when these blocks
are compatible with `dest`. Importantly, this means that mutating `blocks`
afterwards alters the items in `dest` and it may even break the invariance
of `dest` if the length of `blocks` are changed.
# Examples
```jldoctest
julia> using BlockArrays
julia> blockpush!(mortar([[1], [2, 3]]), [4, 5], [6])
4-blocked 6-element BlockVector{Int64}:
1
─
2
3
─
4
5
─
6
```
"""
blockpush!(dest::BlockVector, blocks...) = foldl(blockpush!, blocks; init = dest)
blockpush!(dest::BlockVector{<:Any,<:AbstractArray{T}}, block::T) where {T} =
_blockpush!(dest, block)
blockpush!(dest::BlockVector, block) = _blockpush!(dest, _newblockfor(dest, block))
_newblockfor(dest, block) =
if Iterators.IteratorSize(block) isa Union{Base.HasShape,Base.HasLength}
copyto!(eltype(dest.blocks)(undef, length(block)), block)
else
foldl(push!, block; init = eltype(dest.blocks)(undef, 0))
end
function _blockpush!(dest, block)
push!(dest.blocks, block)
push!(blocklasts(dest.axes[1]), last(dest.axes[1]) + length(block))
return dest
end
"""
blockpushfirst!(dest::BlockVector, blocks...) -> dest
Push `blocks` to the beginning of `dest`. See also [`blockpush!`](@ref).
This function avoids copying the elements of the `blocks` when these blocks
are compatible with `dest`. Importantly, this means that mutating `blocks`
afterwards alters the items in `dest` and it may even break the invariance
of `dest` if the length of `blocks` are changed.
# Examples
```jldoctest
julia> using BlockArrays
julia> blockpushfirst!(mortar([[1], [2, 3]]), [4, 5], [6])
4-blocked 6-element BlockVector{Int64}:
4
5
─
6
─
1
─
2
3
```
"""
blockpushfirst!(A::BlockVector, b1, b2, blocks...) =
foldl(blockpushfirst!, reverse((b1, b2, blocks...)); init = A)
blockpushfirst!(dest::BlockVector{<:Any,<:AbstractArray{T}}, block::T) where {T} =
_blockpushfirst!(dest, block)
blockpushfirst!(dest::BlockVector{<:Any,<:Any}, block) =
_blockpushfirst!(dest, _newblockfor(dest, block))
function _blockpushfirst!(dest, block)
pushfirst!(dest.blocks, block)
blocklasts(dest.axes[1]) .+= length(block) - 1 + first(dest.axes[1])
pushfirst!(blocklasts(dest.axes[1]), length(block))
return dest
end
"""
blockpop!(A::BlockVector) -> block
Pop a `block` from the end of `dest`.
# Examples
```jldoctest
julia> using BlockArrays
julia> A = mortar([[1], [2, 3]]);
julia> blockpop!(A)
2-element Vector{Int64}:
2
3
julia> A
1-blocked 1-element BlockVector{Int64}:
1
```
"""
function blockpop!(A::BlockVector)
block = pop!(A.blocks)
pop!(blocklasts(A.axes[1]))
return block
end
"""
blockpopfirst!(dest::BlockVector) -> block
Pop a `block` from the beginning of `dest`.
# Examples
```jldoctest
julia> using BlockArrays
julia> A = mortar([[1], [2, 3]]);
julia> blockpopfirst!(A)
1-element Vector{Int64}:
1
julia> A
1-blocked 2-element BlockVector{Int64}:
2
3
```
"""
function blockpopfirst!(A::BlockVector)
block = popfirst!(A.blocks)
n = popfirst!(blocklasts(A.axes[1]))
blocklasts(A.axes[1]) .-= n
return block
end
"""
append!(dest::BlockVector, sources...)
Append items from `sources` to the last block of `dest`.
The blocks in `dest` must not alias with `sources` or components of them.
For example, the result of `append!(x, x)` is undefined.
# Examples
```jldoctest
julia> using BlockArrays
julia> append!(mortar([[1], [2, 3]]), mortar([[4], [5]]))
2-blocked 5-element BlockVector{Int64}:
1
─
2
3
4
5
```
"""
Base.append!(dest::BlockVector, sources...) = foldl(append!, sources; init = dest)
Base.append!(dest::BlockVector, src) = append_itr!(dest, Base.IteratorSize(src), src)
@inline function _append_itr_foldfn!(block,i,x)
i += 1
@inbounds block[i] = x
return i
end
function append_itr!(dest::BlockVector, ::Union{Base.HasShape,Base.HasLength}, src)
block = dest.blocks[end]
li = lastindex(block)
resize!(block, length(block) + length(src))
# Equivalent to `i = li; for x in src; ...; end` but (maybe) faster:
foldl(src, init = li) do i, x
_append_itr_foldfn!(block,i,x)
end
da, = dest.axes
blocklasts(da)[end] += length(src)
return dest
end
function append_itr!(dest::BlockVector, ::Base.SizeUnknown, src)
block = dest.blocks[end]
# Equivalent to `n = 0; for x in src; ...; end` but (maybe) faster:
n = foldl(src, init = 0) do n, x
push!(block, x)
return n + 1
end
da, = dest.axes
blocklasts(da)[end] += n
return dest
end
# remove empty blocks at the end
function _squash_lasts!(A::BlockVector)
while !isempty(A.blocks) && isempty(A.blocks[end])
pop!(A.blocks)
pop!(blocklasts(A.axes[1]))
end
end
# remove empty blocks at the beginning
function _squash_firsts!(A::BlockVector)
while !isempty(A.blocks) && isempty(A.blocks[1])
popfirst!(A.blocks)
popfirst!(blocklasts(A.axes[1]))
end
end
"""
pop!(A::BlockVector)
Pop the last element from the last non-empty block. Remove all empty
blocks at the end.
"""
function Base.pop!(A::BlockVector)
isempty(A) && throw(Argument("array must be nonempty"))
_squash_lasts!(A)
x = pop!(A.blocks[end])
lasts = blocklasts(A.axes[1])
if isempty(A.blocks[end])
pop!(A.blocks)
pop!(lasts)
else
lasts[end] -= 1
end
return x
end
"""
popfirst!(A::BlockVector)
Pop the first element from the first non-empty block. Remove all empty
blocks at the beginning.
"""
function Base.popfirst!(A::BlockVector)
isempty(A) && throw(Argument("array must be nonempty"))
_squash_firsts!(A)
x = popfirst!(A.blocks[1])
ax, = A.axes
if isempty(A.blocks[1])
popfirst!(A.blocks)
popfirst!(blocklasts(ax))
else
blocklasts(ax)[1] -= 1
end
return x
end
"""
push!(dest::BlockVector, items...)
Push items to the end of the last block.
"""
Base.push!(dest::BlockVector, items...) = append!(dest, items)
"""
pushfirst!(A::BlockVector, items...)
Push items to the beginning of the first block.
"""
function Base.pushfirst!(A::BlockVector, items...)
pushfirst!(A.blocks[1], items...)
blocklasts(A.axes[1]) .+= length(items)
return A
end