Skip to content

Commit

Permalink
Take care of CartesianIndex in namedgetindex(), fixing #32
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed Jul 30, 2016
1 parent 330c7d6 commit 089deaa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ namedgetindex(a::NamedArray, i1::Integer, i2::Integer, i3::Integer, i4::Integer)
namedgetindex(a::NamedArray, i1::Integer, i2::Integer, i3::Integer, i4::Integer, i5::Integer) = getindex(a.array, i1, i2, i3, i4, i5)
namedgetindex(a::NamedArray, i1::Integer, i2::Integer, i3::Integer, i4::Integer, i5::Integer, I::Integer...) = getindex(a.array, i1, i2, i3, i4, i5, I...)

## A helper function for namedgetindex. We want to know the length of the indices of they are arrays,
## but for a cartesianindex of length N we want a sequence of 1's of length N.
dimlength{N}(ci::CartesianIndex{N}) = fill(1, N)
dimlength(i) = length(i)

## namedgetindex collects the elements from the array, and takes care of the index names
## `index` is an integer now, and has been computed by `indices()`
function namedgetindex(n::NamedArray, index...)
a = getindex(n.array, index...)
dims = map(length, index)
dims = vcat(map(dimlength, index)...)
N = length(dims)
while dims[N] == 1 && N > 1
N -= 1
Expand Down
1 change: 1 addition & 0 deletions src/nomodule.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Compat
using DataStructures

include("compat.jl")
if !isdefined(:NamedArray)
Expand Down
3 changes: 3 additions & 0 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ println(NamedArray([]))
println(n)
zo = [0,1]
println(NamedArray(rand(2,2,2), (zo, zo, zo), ("base", "zero", "indexing")))
for ndim in 1:5
println(NamedArray(rand(fill(2,ndim)...)))
end

println("done!")

Expand Down

0 comments on commit 089deaa

Please sign in to comment.