@@ -355,43 +355,59 @@ function interlace(v::Union{Vector{Any},Tuple})
355
355
interlace (b)
356
356
end
357
357
358
- function interlace (a:: AbstractVector{S} ,b:: AbstractVector{V} ) where {S<: Number ,V<: Number }
359
- na= length (a);nb= length (b)
360
- T= promote_type (S,V)
361
- if nb≥ na
362
- ret= zeros (T,2 nb)
363
- ret[1 : 2 : 1 + 2 * (na- 1 )]= a
364
- ret[2 : 2 : end ]= b
365
- ret
366
- else
367
- ret= zeros (T,2 na- 1 )
368
- ret[1 : 2 : end ]= a
369
- if ! isempty (b)
370
- ret[2 : 2 : 2 + 2 * (nb- 1 )]= b
358
+ initvector (:: Type{T} , n) where {T<: Number } = zeros (T, n)
359
+ initvector (:: Type{T} , n) where {T} = Vector {T} (undef, n)
360
+
361
+ function interlace (a:: AbstractVector , b:: AbstractVector , (ncomponents_a, ncomponents_b) = (1 ,1 ))
362
+ T= promote_type (eltype (a), eltype (b))
363
+
364
+ # we pad the arrays first to ensure that the leading blocks are full,
365
+ # that is if the space corresponding to a is (S1 ⊕ S2) and that for b is S3,
366
+ # and a = [1,2,3] and b = [5,6,7,8], the resulting coefficients would be
367
+ # [1,2, 5, 3,0, 6, 0,0, 7, 0,0, 8], so a is padded to [1,2,3,0,0,0,0,0]
368
+ # Second example: if a = [1,2,3] and b = [5,6], the result would be
369
+ # [1,2, 5, 3,0, 6], so a is padded to [1,2,3,0]
370
+ # Third example: if a = [1,2,3] and b = [5], the result would be
371
+ # [1,2, 5, 3]. In this case there is no padding in either a or b
372
+ # Fourth example: if a = [1,2,3,4,11,12] and b = [5], the result would be
373
+ # [1,2, 5, 3,4, 0, 11,12]. In this case, b is padded to [5,0]
374
+
375
+ nblk_a = cld (length (a), ncomponents_a)
376
+ nblk_b = cld (length (b), ncomponents_b)
377
+
378
+ if nblk_b >= nblk_a
379
+ nblk_a = nblk_b
380
+ elseif nblk_a - 1 > nblk_b
381
+ nblk_b = nblk_a- 1
382
+ end
383
+
384
+ pad_a = pad (a, ncomponents_a * nblk_a)
385
+ pad_b = pad (b, ncomponents_b * nblk_b)
386
+
387
+ blksz_a = Fill (ncomponents_a, nblk_a)
388
+ aPBlk = PseudoBlockArray (pad_a, blksz_a)
389
+ blksz_b = Fill (ncomponents_b, nblk_b)
390
+ bPBkl = PseudoBlockArray (pad_b, blksz_b)
391
+
392
+ nblk_ret = nblk_a + nblk_b
393
+ blksz_ret = zeros (Int, nblk_ret)
394
+ blksz_ret[1 : 2 : end ] = blksz_a
395
+ blksz_ret[2 : 2 : end ] = blksz_b
396
+ nret = sum (blksz_ret)
397
+ ret = initvector (T, nret)
398
+ retPBlk = PseudoBlockArray (ret, blksz_ret)
399
+
400
+ @views begin
401
+ for (ind, i) in enumerate (1 : 2 : nblk_ret)
402
+ retPBlk[Block (i)] = aPBlk[Block (ind)]
371
403
end
372
- ret
373
- end
374
- end
375
-
376
- function interlace (a:: AbstractVector ,b:: AbstractVector )
377
- na= length (a);nb= length (b)
378
- T= promote_type (eltype (a),eltype (b))
379
- if nb≥ na
380
- ret= Vector {T} (undef, 2 nb)
381
- ret[1 : 2 : 1 + 2 * (na- 1 )]= a
382
- ret[2 : 2 : end ]= b
383
- ret
384
- else
385
- ret= Vector {T} (undef, 2 na- 1 )
386
- ret[1 : 2 : end ]= a
387
- if ! isempty (b)
388
- ret[2 : 2 : 2 + 2 * (nb- 1 )]= b
404
+ for (ind, i) in enumerate (2 : 2 : nblk_ret)
405
+ retPBlk[Block (i)] = bPBkl[Block (ind)]
389
406
end
390
- ret
391
407
end
408
+ resize! (ret, findlast (! iszero, ret))
392
409
end
393
410
394
-
395
411
# ## In-place O(n) interlacing
396
412
397
413
function highestleader (n:: Int )
0 commit comments