Skip to content

Commit 3b60481

Browse files
authored
Faster iterate method for TrivialInterlacer (#503)
* Faster iterate method for TrivialInterlacer * Version bump to 0.8.43
1 parent ba5e437 commit 3b60481

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.8.42"
3+
version = "0.8.43"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/LinearAlgebra/helper.jl

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ struct BlockInterlacer{DMS<:Tuple{Vararg{AbstractVector{Int}}}}
686686
end
687687

688688

689-
const TrivialInterlacer{d} = BlockInterlacer{<:NTuple{d,Ones{Int}}}
689+
const TrivialInterlacer{d,Ax} = BlockInterlacer{<:NTuple{d,Ones{Int,1,Tuple{Ax}}}}
690690

691691
BlockInterlacer(v::AbstractVector) = BlockInterlacer(Tuple(v))
692692

@@ -732,23 +732,18 @@ function iterate(it::BlockInterlacer, (N,k,blkst,lngs))
732732
return iterate(it,(1,1,blkst,lngs))
733733
end
734734

735-
Bnxtb = iterate(it.blocks[N],blkst[N]...) # B is block size
735+
Bnxtb = iterate(it.blocks[N],blkst[N]...)
736736

737-
if Bnxtb === nothing
737+
if Bnxtb === nothing || k > Bnxtb[1]
738738
# increment to next N
739739
return iterate(it,(N+1,1,blkst,lngs))
740740
end
741741

742-
B,nxtb = Bnxtb
743-
744-
if k > B
745-
#increment to next N
746-
return iterate(it,(N+1,1,blkst,lngs))
747-
end
748-
749-
750742
lngs = Base.setindex(lngs, lngs[N]+1, N)
751743
return (N,lngs[N]),(N,k+1,blkst,lngs)
752744
end
753745

746+
iterate(it::TrivialInterlacer{N,OneToInf{Int}}, st...) where {N} =
747+
iterate(Iterators.product(1:N, axes(it.blocks[1],1)), st...)
748+
754749
cache(Q::BlockInterlacer) = CachedIterator(Q)

test/runtests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ end
5555

5656
@test collect(ApproxFunBase.BlockInterlacer(([2],[2],[2]))) ==
5757
[(1,1),(1,2),(2,1),(2,2),(3,1),(3,2)]
58+
59+
@testset "TrivialInterlacer" begin
60+
o = Ones{Int}(ℵ₀)
61+
f = Fill(1, ℵ₀)
62+
function test_nD(nD)
63+
B1 = ApproxFunBase.BlockInterlacer(ntuple(_->o, nD))
64+
B2 = ApproxFunBase.BlockInterlacer(ntuple(_->f, nD))
65+
it = Iterators.take(zip(B1, B2), 100_000)
66+
for (a,b) in it
67+
@test a == b
68+
end
69+
end
70+
test_nD(1)
71+
test_nD(2)
72+
test_nD(3)
73+
end
5874
end
5975

6076
@testset "issue #94" begin

0 commit comments

Comments
 (0)