Skip to content

Commit 3ecb945

Browse files
committed
Fix CartesianIndex indexing (fix #62 #66)
1 parent 68bb4e0 commit 3ecb945

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/indexing.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,27 @@ end
197197
# set- and get- index.
198198
@generated function to_index{T,N,D,Ax}(A::AxisArray{T,N,D,Ax}, I...)
199199
ex = Expr(:tuple)
200+
n = 0
200201
for i=1:length(I)
201202
if I[i] <: Idx
202203
push!(ex.args, :(I[$i]))
204+
n += 1
203205
elseif I[i] <: AbstractArray{Bool}
204206
push!(ex.args, :(find(I[$i])))
207+
n += 1
205208
elseif I[i] <: CartesianIndex
206209
for j = 1:length(I[i])
207210
push!(ex.args, :(I[$i][$j]))
208211
end
212+
n += length(I[i])
209213
elseif i <= length(Ax.parameters)
210214
push!(ex.args, :(axisindexes(A.axes[$i], I[$i])))
215+
n += 1
211216
else
212217
push!(ex.args, :(error("dimension ", $i, " does not have an axis to index")))
213218
end
214219
end
215-
for _=length(I)+1:N
220+
for _=n+1:N
216221
push!(ex.args, :(Colon()))
217222
end
218223
meta = Expr(:meta, :inline)

test/indexing.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,15 @@ for (r, Irel) in ((0.1:0.1:10.0, -0.5..0.5), # FloatRange
129129
@test_throws BoundsError A[atindex(Irel, [5, 15, 25]), :]
130130
end
131131

132-
# Indexing with CartesianIndex{0}
132+
# Indexing with CartesianIndex
133133
A = AxisArray(reshape(1:15, 3, 5), :x, :y)
134134
@test A[2,2,CartesianIndex(())] == 5
135135
@test A[2,CartesianIndex(()),2] == 5
136136
@test A[CartesianIndex(()),2,2] == 5
137+
A3 = AxisArray(reshape(1:24, 4, 3, 2), :x, :y, :z)
138+
@test A3[2,CartesianIndex(2,2)] == 18
139+
@test A3[CartesianIndex(2,2),2] == 18
140+
@test A3[CartesianIndex(2,2,2)] == 18
137141

138142
# Extracting the full axis
139143
axx = @inferred(A[Axis{:x}])
@@ -148,3 +152,11 @@ axy = @inferred(A[Axis{:y}])
148152
A = AxisArray(rand(2,2), :x, :y)
149153
@test_throws ArgumentError A[Axis{:x}(1), Axis{:x}(1)]
150154
@test_throws ArgumentError A[Axis{:y}(1), Axis{:y}(1)]
155+
156+
# Reductions (issues #66, #62)
157+
@test maximum(A3, 1) == reshape([4 16; 8 20; 12 24], 1, 3, 2)
158+
@test maximum(A3, 2) == reshape([9 21; 10 22; 11 23; 12 24], 4, 1, 2)
159+
@test maximum(A3, 3) == reshape(A3[:,:,2], 4, 3, 1)
160+
acc = zeros(Int, 4, 1, 2)
161+
Base.mapreducedim!(x->x>5, +, acc, A3)
162+
@test acc == reshape([1 3; 2 3; 2 3; 2 3], 4, 1, 2)

0 commit comments

Comments
 (0)