22
33module Iterators
44
5- import Base: start, done, next, isempty, length, size, eltype, iteratorsize, iteratoreltype, indices, ndims
5+ import Base: start, done, next, isempty, length, size, eltype, iteratorsize, iteratoreltype, indices, ndims, pairs
66
77using Base: tail, tuple_type_head, tuple_type_tail, tuple_type_cons, SizeUnknown, HasLength, HasShape,
88 IsInfinite, EltypeUnknown, HasEltype, OneTo, @propagate_inbounds
@@ -78,14 +78,16 @@ struct IndexValue{I,A<:AbstractArray}
7878end
7979
8080"""
81- enumerate (IndexLinear(), A)
82- enumerate (IndexCartesian(), A)
83- enumerate (IndexStyle(A), A)
81+ pairs (IndexLinear(), A)
82+ pairs (IndexCartesian(), A)
83+ pairs (IndexStyle(A), A)
8484
8585An iterator that accesses each element of the array `A`, returning
86- `(i, x)`, where `i` is the index for the element and `x = A[i]`. This
87- is similar to `enumerate(A)`, except `i` will always be a valid index
88- for `A`.
86+ `(i, x)`, where `i` is the index for the element and `x = A[i]`.
87+ Identical to `pairs(A)`, except that the style of index can be selected.
88+ Also similar to `enumerate(A)`, except `i` will be a valid index
89+ for `A`, while `enumerate` always counts from 1 regardless of the indices
90+ of `A`.
8991
9092Specifying `IndexLinear()` ensures that `i` will be an integer;
9193specifying `IndexCartesian()` ensures that `i` will be a
@@ -96,7 +98,7 @@ been defined as the native indexing style for array `A`.
9698```jldoctest
9799julia> A = ["a" "d"; "b" "e"; "c" "f"];
98100
99- julia> for (index, value) in enumerate (IndexStyle(A), A)
101+ julia> for (index, value) in pairs (IndexStyle(A), A)
100102 println("\$ index \$ value")
101103 end
1021041 a
@@ -108,7 +110,7 @@ julia> for (index, value) in enumerate(IndexStyle(A), A)
108110
109111julia> S = view(A, 1:2, :);
110112
111- julia> for (index, value) in enumerate (IndexStyle(S), S)
113+ julia> for (index, value) in pairs (IndexStyle(S), S)
112114 println("\$ index \$ value")
113115 end
114116CartesianIndex{2}((1, 1)) a
@@ -117,15 +119,14 @@ CartesianIndex{2}((1, 2)) d
117119CartesianIndex{2}((2, 2)) e
118120```
119121
120- Note that `enumerate(A)` returns `i` as a *counter* (always starting
121- at 1), whereas `enumerate(IndexLinear(), A)` returns `i` as an *index*
122- (starting at the first linear index of `A`, which may or may not be
123- 1).
124-
125122See also: [`IndexStyle`](@ref), [`indices`](@ref).
126123"""
127- enumerate (:: IndexLinear , A:: AbstractArray ) = IndexValue (A, linearindices (A))
128- enumerate (:: IndexCartesian , A:: AbstractArray ) = IndexValue (A, CartesianRange (indices (A)))
124+ pairs (:: IndexLinear , A:: AbstractArray ) = IndexValue (A, linearindices (A))
125+ pairs (:: IndexCartesian , A:: AbstractArray ) = IndexValue (A, CartesianRange (indices (A)))
126+
127+ # faster than zip(keys(a), values(a)) for arrays
128+ pairs (A:: AbstractArray ) = pairs (IndexCartesian (), A)
129+ pairs (A:: AbstractVector ) = pairs (IndexLinear (), A)
129130
130131length (v:: IndexValue ) = length (v. itr)
131132indices (v:: IndexValue ) = indices (v. itr)
0 commit comments