Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keys(::Array) and values(::Array) #12811

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -981,3 +981,18 @@ end
symdiff(a) = a
symdiff(a, b) = union(setdiff(a,b), setdiff(b,a))
symdiff(a, b, rest...) = symdiff(a, symdiff(b, rest...))

# Lightweight wrapper type for values(::Array)
immutable ArrayIterator{T,N}
array::Array{T,N}
end
start(ai::ArrayIterator) = start(ai.array)
next(ai::ArrayIterator,k) = next(ai.array,k)
done(ai::ArrayIterator,k) = done(ai.array,k)

length(v::ArrayIterator) = length(v.array)
isempty(v::ArrayIterator) = isempty(v.array)
eltype{T,N}(::Type{ArrayIterator{T,N}}) = T

keys(x::Array) = CartesianRange(size(x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keys(x::AbstractArray) = eachindex(x)?

And since arrays are already iterable, why not just values(x::AbstractArray) = x? (discussed below)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with that as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually wait, eachindex(x) just gives the linear indexing 1:n. I prefer the multidimensional Cartesian range since you can use the individual components directly:

for I in keys(x)
    x[I] += c[I[1]]
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eachindex chooses the most efficient index iterator for the given array. For Array and other contiguous arrays (LinearFast()), that's simply 1:n. But for SparseMatrixCSC and other non-contiguous arrays (LinearSlow()), it's a cartesian range.

values(x::Array) = ArrayIterator(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this equivalent to just values(x::Array) = x? (e.g. an array is its own iterator?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is not to allow mutation of the underlying array with something like values(x)[2] = 3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have this at all, I would prefer values(x::AbstractArray) = x; if the main purpose is to use this as an iterator, then I don't understand why you would need to prevent mutation. for x in values(a) or for x in a already doesn't allow mutation (unless x is mutable, of course).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its main purpose is for iteration, but that's certainly not the only way it could be used. Maybe this approach is too paternalistic, though.

13 changes: 13 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1239,3 +1239,16 @@ module RetTypeDecl
@test @inferred(m.*[m,m]) == [m2,m2]
@test @inferred([m,m].*m) == [m2,m2]
end

# keys(::Array) and values(::Array)
A = rand(4,2,3)
@test length(keys(A)) == length(values(A)) == 4 * 2 * 3
@test eltype(typeof(keys(A))) === Base.IteratorsMD.CartesianIndex{3}
@test eltype(typeof(values(A))) === Float64

seen = falses(size(A))
for (index,(I,v)) in enumerate(zip(keys(A),values(A)))
@test A[I] === A[index] === v
seen[I] = true
end
@test all(seen)