Skip to content

Commit

Permalink
Add value to support sync over dimensional index/value
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Apr 25, 2016
1 parent 60ba537 commit 2b0f02f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ArrayIterationPlayground.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
21 changes: 19 additions & 2 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)`
Expand All @@ -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)

Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 2b0f02f

Please sign in to comment.