Skip to content

Commit e6ee869

Browse files
authored
Add fallback definitions for getindex(::SectorValues) and findindex(::SectorValues) (#6)
1 parent 57e4aab commit e6ee869

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/anyons.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Base.length(::SectorValues{FibonacciAnyon}) = 2
5656
function Base.iterate(::SectorValues{FibonacciAnyon}, i=0)
5757
return i == 0 ? (FibonacciAnyon(:I), 1) : (i == 1 ? (FibonacciAnyon(), 2) : nothing)
5858
end
59-
function Base.getindex(S::SectorValues{FibonacciAnyon}, i)
59+
function Base.getindex(S::SectorValues{FibonacciAnyon}, i::Int)
6060
if i == 1
6161
return FibonacciAnyon(:I)
6262
elseif i == 2
@@ -177,7 +177,7 @@ const all_isinganyons = (IsingAnyon(:I), IsingAnyon(:σ), IsingAnyon(:ψ))
177177
Base.IteratorSize(::Type{SectorValues{IsingAnyon}}) = HasLength()
178178
Base.length(::SectorValues{IsingAnyon}) = length(all_isinganyons)
179179
Base.iterate(::SectorValues{IsingAnyon}, i=1) = iterate(all_isinganyons, i)
180-
Base.getindex(S::SectorValues{IsingAnyon}, i) = getindex(all_isinganyons, i)
180+
Base.getindex(::SectorValues{IsingAnyon}, i::Int) = getindex(all_isinganyons, i)
181181

182182
function findindex(::SectorValues{IsingAnyon}, a::IsingAnyon)
183183
a == all_isinganyons[1] && return 1

src/fermions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Base.length(::SectorValues{FermionParity}) = 2
2323
function Base.iterate(::SectorValues{FermionParity}, i=0)
2424
return i == 2 ? nothing : (FermionParity(i), i + 1)
2525
end
26-
function Base.getindex(::SectorValues{FermionParity}, i)
26+
function Base.getindex(::SectorValues{FermionParity}, i::Int)
2727
return 1 <= i <= 2 ? FermionParity(i - 1) : throw(BoundsError(values(FermionParity), i))
2828
end
2929
findindex(::SectorValues{FermionParity}, f::FermionParity) = f.isodd ? 2 : 1

src/sectors.jl

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,44 @@ instance is obtained as `values(I)`. For a new `I::Sector`, the following should
4646
If `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
"""
5355
struct SectorValues{I<:Sector} end
5456
Base.IteratorEltype(::Type{<:SectorValues}) = HasEltype()
5557
Base.eltype(::Type{SectorValues{I}}) where {I<:Sector} = I
5658
Base.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

test/newsectors.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,4 @@ end
7575
Base.hash(s::NewSU2Irrep, h::UInt) = hash(s.j, h)
7676
Base.isless(s1::NewSU2Irrep, s2::NewSU2Irrep) = isless(s1.j, s2.j)
7777

78-
function Base.getindex(::SectorValues{NewSU2Irrep}, i::Int)
79-
return 1 <= i ? NewSU2Irrep(half(i - 1)) : throw(BoundsError(values(NewSU2Irrep), i))
80-
end
81-
TensorKitSectors.findindex(::SectorValues{NewSU2Irrep}, s::NewSU2Irrep) = twice(s.j) + 1
82-
8378
end # module NewSectors

0 commit comments

Comments
 (0)