@@ -2,12 +2,22 @@ const Idx = Union{Real,Colon,AbstractArray{Int}}
2
2
3
3
using Base: ViewIndex, @propagate_inbounds , tail
4
4
5
- struct Value{T}
5
+ abstract type Value{T} end
6
+
7
+ struct TolValue{T} <: Value{T}
6
8
val:: T
7
9
tol:: T
8
10
end
9
- Value (x, tol= Base. rtoldefault (typeof (x))* abs (x)) = Value (promote (x,tol)... )
10
- atvalue (x; rtol= Base. rtoldefault (typeof (x)), atol= zero (x)) = Value (x, atol+ rtol* abs (x))
11
+
12
+ TolValue (x, tol= Base. rtoldefault (typeof (x))* abs (x)) = TolValue (promote (x,tol)... )
13
+
14
+ struct ExactValue{T} <: Value{T}
15
+ val:: T
16
+ end
17
+
18
+ atvalue (x:: Number ; rtol= Base. rtoldefault (typeof (x)), atol= zero (x)) = TolValue (x, atol+ rtol* abs (x))
19
+ atvalue (x) = ExactValue (x)
20
+
11
21
const Values = AbstractArray{<: Value }
12
22
13
23
# For throwing a BoundsError with a Value index, we need to define the following
@@ -17,8 +27,9 @@ Base.next(x::Value, state) = (x, true)
17
27
Base. done (x:: Value , state) = state
18
28
19
29
# How to show Value objects (e.g. in a BoundsError)
20
- Base. show (io:: IO , v:: Value ) =
21
- print (io, string (" Value(" , v. val, " , tol=" , v. tol, " )" ))
30
+ Base. show (io:: IO , v:: TolValue ) =
31
+ print (io, string (" TolValue(" , v. val, " , tol=" , v. tol, " )" ))
32
+ Base. show (io:: IO , v:: ExactValue ) = print (io, string (" ExactValue(" , v. val, " )" ))
22
33
23
34
# Defer IndexStyle to the wrapped array
24
35
Base. IndexStyle (:: Type{AxisArray{T,N,D,Ax}} ) where {T,N,D,Ax} = IndexStyle (D)
@@ -168,7 +179,7 @@ function axisindexes(::Type{Dimensional}, ax::AbstractVector, idx)
168
179
idxs[1 ]
169
180
end
170
181
# Dimensional axes may always be indexed by value if in a Value type wrapper.
171
- function axisindexes (:: Type{Dimensional} , ax:: AbstractVector , idx:: Value )
182
+ function axisindexes (:: Type{Dimensional} , ax:: AbstractVector , idx:: TolValue )
172
183
idxs = searchsorted (ax, ClosedInterval (idx. val,idx. val))
173
184
length (idxs) > 1 && error (" more than one datapoint lies on axis value $idx ; use an interval to return all values" )
174
185
if length (idxs) == 1
@@ -179,6 +190,15 @@ function axisindexes(::Type{Dimensional}, ax::AbstractVector, idx::Value)
179
190
throw (BoundsError (ax, idx))
180
191
end
181
192
end
193
+ function axisindexes (:: Type{Dimensional} , ax:: AbstractVector , idx:: ExactValue )
194
+ idxs = searchsorted (ax, ClosedInterval (idx. val,idx. val))
195
+ length (idxs) > 1 && error (" more than one datapoint lies on axis value $idx ; use an interval to return all values" )
196
+ if length (idxs) == 1
197
+ idxs[1 ]
198
+ else # it's zero
199
+ throw (BoundsError (ax, idx))
200
+ end
201
+ end
182
202
183
203
# Dimensional axes may be indexed by intervals to select a range
184
204
axisindexes (:: Type{Dimensional} , ax:: AbstractVector , idx:: ClosedInterval ) = searchsorted (ax, idx)
0 commit comments