From 2b0f02fb66d21d5f5902193514e23f576bb6f5fc Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Mon, 25 Apr 2016 08:20:31 -0500 Subject: [PATCH] Add `value` to support sync over dimensional index/value --- src/ArrayIterationPlayground.jl | 2 +- src/core.jl | 21 +++++++++++++++++++-- test/dense.jl | 12 ++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/ArrayIterationPlayground.jl b/src/ArrayIterationPlayground.jl index 07648e1..ff8c5e2 100644 --- a/src/ArrayIterationPlayground.jl +++ b/src/ArrayIterationPlayground.jl @@ -4,7 +4,7 @@ import Base: getindex, setindex!, start, next, done, length, eachindex, show, pa using Base: ReshapedArray, linearindexing, LinearFast, LinearSlow using Base.PermutedDimsArrays: PermutedDimsArray -export inds, index, stored, each, sync +export inds, index, value, stored, each, sync include("types.jl") include("core.jl") diff --git a/src/core.jl b/src/core.jl index 80fb992..d6c5330 100644 --- a/src/core.jl +++ b/src/core.jl @@ -23,10 +23,23 @@ create an iterator from a hint, call `each` on the resulting object. In contrast to `eachindex` iteration over a subarray of `A`, the indexes are for `A` itself. -See also: `stored`, `each`. +See also: `value`, `stored`, `each`. """ index{A,I,isindex,isstored}(w::ArrayIndexingWrapper{A,I,isindex,isstored}) = ArrayIndexingWrapper{A,I,true,isstored}(w.data, w.indexes) +""" +`value(A)` +`value(A, indexes...)` + +`value` creates an "iteration hint" that records the region of `A` +that you wish to iterate over. The iterator will return the values, +rather than the indexes, of `A`. "iteration hints" are not iterables; to +create an iterator from a hint, call `each` on the resulting object. + +See also: `index`, `stored`, `each`. +""" +value{A,I,isindex,isstored}(w::ArrayIndexingWrapper{A,I,isindex,isstored}) = ArrayIndexingWrapper{A,I,true,isstored}(w.data, w.indexes) + """ `stored(A)` `stored(A, indexes...)` @@ -36,7 +49,7 @@ that you wish to iterate over. The iterator will return just the stored values of `A`. "iteration hints" are not iterables; to create an iterator from a hint, call `each` on the resulting object. -See also: `index`, `each`. +See also: `index`, `value`, `each`. """ stored{A,I,isindex,isstored}(w::ArrayIndexingWrapper{A,I,isindex,isstored}) = ArrayIndexingWrapper{A,I,isindex,true}(w.data, w.indexes) @@ -46,6 +59,10 @@ index(A::AbstractArray) = index(A, allindexes(A)) index(A::AbstractArray, I::IterIndex...) = index(A, I) index{T,N}(A::AbstractArray{T,N}, indexes::NTuple{N,IterIndex}) = ArrayIndexingWrapper{typeof(A),typeof(indexes),true,false}(A, indexes) +value(A::AbstractArray) = value(A, allindexes(A)) +value(A::AbstractArray, I::IterIndex...) = value(A, I) +value{T,N}(A::AbstractArray{T,N}, indexes::NTuple{N,IterIndex}) = ArrayIndexingWrapper{typeof(A),typeof(indexes),false,false}(A, indexes) + stored(A::AbstractArray) = stored(A, allindexes(A)) stored(A::AbstractArray, I::IterIndex...) = stored(A, I) stored{T,N}(A::AbstractArray{T,N}, indexes::NTuple{N,IterIndex}) = ArrayIndexingWrapper{typeof(A),typeof(indexes),false,true}(A, indexes) diff --git a/test/dense.jl b/test/dense.jl index b2133d4..9f89e30 100644 --- a/test/dense.jl +++ b/test/dense.jl @@ -165,3 +165,15 @@ goodcopy!(D, Cc) mysum!(C, D, A) @test C[1,2] == 23 @test C[2,1] == 32 + +# sync with dimension iterators +fill!(B, -1) +for (I, a) in sync(index(B, :, 2), value(A, :, 1)) + B[I] = a +end +@test B == [-1 A[1,1]; -1 A[2,1]] +fill!(B, -1) +for (IB, IA) in sync(index(B, :, 1), index(A, :, 2)) + B[IB] = A[IA] +end +@test B == [A[1,2] -1; A[2,2] -1]