@@ -46,15 +46,44 @@ instance is obtained as `values(I)`. For a new `I::Sector`, the following should
4646If `IteratorSize(I) == HasLength()`, also the following must be implemented:
4747* `Base.length(::SectorValues{I})`: the number of different values
4848* `Base.getindex(::SectorValues{I}, i::Int)`: a mapping between an index `i` and an
49- instance of `I`
49+ instance of `I`. A fallback implementation exists that returns the `i`th value
50+ of the `SectorValues` iterator.
5051* `findindex(::SectorValues{I}, c::I)`: reverse mapping between a value `c::I` and an
51- index `i::Integer ∈ 1:length(values(I))`
52+ index `i::Integer ∈ 1:length(values(I))`. A fallback implementation exists that
53+ linearly searches through the `SectorValues` iterator.
5254"""
5355struct SectorValues{I<: Sector } end
5456Base. IteratorEltype (:: Type{<:SectorValues} ) = HasEltype ()
5557Base. eltype (:: Type{SectorValues{I}} ) where {I<: Sector } = I
5658Base. values (:: Type{I} ) where {I<: Sector } = SectorValues {I} ()
5759
60+ Base. @propagate_inbounds function Base. getindex (v:: SectorValues{I} ,
61+ i:: Int ) where {I<: Sector }
62+ @boundscheck begin
63+ if Base. IteratorSize (v) === HasLength ()
64+ 1 ≤ i ≤ length (v) || throw (BoundsError (v, i))
65+ else
66+ 1 ≤ i || throw (BoundsError (v, i))
67+ end
68+ end
69+ for (j, c) in enumerate (v)
70+ j == i && return c
71+ end
72+ throw (BoundsError (v, i))
73+ end
74+
75+ """
76+ findindex(v::SectorValues{I}, c::I)
77+
78+ Reverse mapping between a value `c::I` and an index `i::Integer ∈ 1:length(values(I))`.
79+ """
80+ function findindex (v:: SectorValues{I} , c:: I ) where {I<: Sector }
81+ for (i, cc) in enumerate (v)
82+ cc == c && return i
83+ end
84+ throw (ArgumentError (lazy " Cannot locate sector $c" ))
85+ end
86+
5887"""
5988 one(::Sector) -> Sector
6089 one(::Type{<:Sector}) -> Sector
0 commit comments