Skip to content

Commit f0aa650

Browse files
committed
Implement indexing by arrays of Values.
1 parent 6b5f8ae commit f0aa650

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/indexing.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct Value{T}
88
end
99
Value(x, tol=Base.rtoldefault(typeof(x))*abs(x)) = Value(promote(x,tol)...)
1010
atvalue(x; rtol=Base.rtoldefault(typeof(x)), atol=zero(x)) = Value(x, atol+rtol*abs(x))
11+
const Values = AbstractArray{<:Value}
1112

1213
# For throwing a BoundsError with a Value index, we need to define the following
1314
# (note that we could inherit them for free, were Value <: Number)
@@ -281,6 +282,9 @@ end
281282
elseif I[i] <: AbstractArray{Bool}
282283
push!(ex.args, :(find(I[$i])))
283284
n += 1
285+
elseif I[i] <: Values
286+
push!(ex.args, :(axisindexes.(A.axes[$i], I[$i])))
287+
n += 1
284288
elseif I[i] <: CartesianIndex
285289
for j = 1:length(I[i])
286290
push!(ex.args, :(I[$i][$j]))

test/indexing.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ A = AxisArray([1 2; 3 4], Axis{:x}([1.0,4.0]), Axis{:y}([2.0,6.1]))
196196
@test @inferred(A[Axis{:x}(atvalue(2.0, atol=5))]) == [1,2]
197197
@test_throws BoundsError A[Axis{:x}(atvalue(4.00000001, rtol=0))]
198198

199+
# Indexing by array of values
200+
A = AxisArray([1 2 3 4; 5 6 7 8; 9 10 11 12], -1:1, [5.1, 5.4, 5.7, 5.8])
201+
@test @inferred(A[atvalue(-1), atvalue.([5.1, 5.7])]) == [1, 3]
202+
@test_throws BoundsError A[atvalue.([1,2])]
203+
199204
# Indexing by value into an OffsetArray
200205
A = AxisArray(OffsetArrays.OffsetArray([1 2; 3 4], 0:1, 1:2),
201206
Axis{:x}([1.0,4.0]), Axis{:y}([2.0,6.1]))

0 commit comments

Comments
 (0)