|
3 | 3 | ### Multidimensional iterators
|
4 | 4 | module IteratorsMD
|
5 | 5 |
|
6 |
| -import Base: eltype, length, start, done, next, last, getindex, setindex!, linearindexing, min, max, eachindex, ndims |
| 6 | +import Base: eltype, length, size, start, done, next, last, getindex, setindex!, linearindexing, min, max, eachindex, ndims, iteratoreltype, iteratorsize |
7 | 7 | importall ..Base.Operators
|
8 | 8 | import Base: simd_outer_range, simd_inner_length, simd_index, @generated
|
9 | 9 | import Base: @nref, @ncall, @nif, @nexprs, LinearFast, LinearSlow, to_index, AbstractCartesianIndex
|
|
103 | 103 | end
|
104 | 104 |
|
105 | 105 | eltype{I}(::Type{CartesianRange{I}}) = I
|
| 106 | +iteratoreltype{I}(::Type{CartesianRange{I}}) = Base.HasEltype() |
| 107 | +iteratorsize{I}(::Type{CartesianRange{I}}) = Base.HasShape() |
106 | 108 |
|
107 | 109 | @generated function start{I<:CartesianIndex}(iter::CartesianRange{I})
|
108 | 110 | N = length(I)
|
@@ -135,13 +137,15 @@ start{I<:CartesianIndex{0}}(iter::CartesianRange{I}) = false
|
135 | 137 | next{I<:CartesianIndex{0}}(iter::CartesianRange{I}, state) = iter.start, true
|
136 | 138 | done{I<:CartesianIndex{0}}(iter::CartesianRange{I}, state) = state
|
137 | 139 |
|
138 |
| -@generated function length{I<:CartesianIndex}(iter::CartesianRange{I}) |
| 140 | +@generated function size{I<:CartesianIndex}(iter::CartesianRange{I}) |
139 | 141 | N = length(I)
|
140 |
| - N == 0 && return 1 |
| 142 | + N == 0 && return () |
141 | 143 | args = [:(iter.stop[$i]-iter.start[$i]+1) for i=1:N]
|
142 |
| - Expr(:call,:*,args...) |
| 144 | + Expr(:tuple,args...) |
143 | 145 | end
|
144 | 146 |
|
| 147 | +length(iter::CartesianRange) = prod(size(iter)) |
| 148 | + |
145 | 149 | last(iter::CartesianRange) = iter.stop
|
146 | 150 |
|
147 | 151 | @generated function simd_outer_range{I}(iter::CartesianRange{I})
|
|
0 commit comments