-
Notifications
You must be signed in to change notification settings - Fork 17
/
iterator.jl
78 lines (73 loc) · 2.52 KB
/
iterator.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
using OffsetArrays: OffsetArray
struct BlockedIndices{C<:GridChunks}
c::C
end
# Base methods
Base.length(b::BlockedIndices) = prod(last.(last.(b.c.chunks)))
Base.IteratorEltype(::Type{<:BlockedIndices}) = Base.HasEltype()
Base.IteratorSize(::Type{<:BlockedIndices{<:GridChunks{N}}}) where {N} = Base.HasShape{N}()
Base.size(b::BlockedIndices)::NTuple{<:Any,Int} = map(last ∘ last, b.c.chunks)
Base.eltype(b::BlockedIndices) = CartesianIndex{ndims(b.c)}
function Base.iterate(a::BlockedIndices)
chunkiter = Iterators.Stateful(a.c)
ii = iterate(chunkiter)
ii === nothing && return nothing
innerinds = Iterators.Stateful(CartesianIndices(first(ii)))
ind = iterate(innerinds)
ind === nothing && return nothing
return first(ind), (chunkiter, innerinds)
end
function Base.iterate(::BlockedIndices, i)
chunkiter, innerinds = i
r = iterate(innerinds)
if r === nothing
ii = iterate(chunkiter)
ii === nothing && return nothing
innerinds = Iterators.Stateful(CartesianIndices(first(ii)))
r = iterate(innerinds)
r === nothing && return nothing
return first(r), (chunkiter, innerinds)
else
return first(r), (chunkiter, innerinds)
end
return first(r), (chunkiter, innerinds)
end
# Implementaion macros
@noinline function _iterate_disk(
a::AbstractArray{T}, i::I
) where {T,I<:Tuple{A,B,C}} where {A,B,C}
datacur::A, bi::B, bstate::C = i
(chunkiter, innerinds) = bstate
cistateold = length(chunkiter)
biter = iterate(bi, bstate)
if biter === nothing
return nothing
else
innernow, bstatenew = biter
(chunkiter, innerinds) = bstatenew
if length(chunkiter) !== cistateold
curchunk = innerinds.itr.indices
datacur = OffsetArray(a[curchunk...], innerinds.itr)
return datacur[innernow]::T, (datacur, bi, bstatenew)::I
else
return datacur[innernow]::T, (datacur, bi, bstatenew)::I
end
end
end
@noinline function _iterate_disk(a)
bi = BlockedIndices(eachchunk(a))
it = iterate(bi)
isnothing(it) && return nothing
innernow, (chunkiter, innerinds) = it
curchunk = innerinds.itr.indices
datacur = OffsetArray(a[curchunk...], innerinds.itr)
return datacur[innernow], (datacur, bi, (chunkiter, innerinds))
end
macro implement_iteration(t)
t = esc(t)
quote
Base.eachindex(a::$t) = BlockedIndices(eachchunk(a))
Base.iterate(a::$t) = _iterate_disk(a)
Base.iterate(a::$t, i) = _iterate_disk(a, i)
end
end