Skip to content

Add fallback definitions for getindex(::SectorValues) and findindex(::SectorValues) #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/anyons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Base.length(::SectorValues{FibonacciAnyon}) = 2
function Base.iterate(::SectorValues{FibonacciAnyon}, i=0)
return i == 0 ? (FibonacciAnyon(:I), 1) : (i == 1 ? (FibonacciAnyon(:τ), 2) : nothing)
end
function Base.getindex(S::SectorValues{FibonacciAnyon}, i)
function Base.getindex(S::SectorValues{FibonacciAnyon}, i::Int)
if i == 1
return FibonacciAnyon(:I)
elseif i == 2
Expand Down Expand Up @@ -177,7 +177,7 @@ const all_isinganyons = (IsingAnyon(:I), IsingAnyon(:σ), IsingAnyon(:ψ))
Base.IteratorSize(::Type{SectorValues{IsingAnyon}}) = HasLength()
Base.length(::SectorValues{IsingAnyon}) = length(all_isinganyons)
Base.iterate(::SectorValues{IsingAnyon}, i=1) = iterate(all_isinganyons, i)
Base.getindex(S::SectorValues{IsingAnyon}, i) = getindex(all_isinganyons, i)
Base.getindex(::SectorValues{IsingAnyon}, i::Int) = getindex(all_isinganyons, i)

function findindex(::SectorValues{IsingAnyon}, a::IsingAnyon)
a == all_isinganyons[1] && return 1
Expand Down
2 changes: 1 addition & 1 deletion src/fermions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Base.length(::SectorValues{FermionParity}) = 2
function Base.iterate(::SectorValues{FermionParity}, i=0)
return i == 2 ? nothing : (FermionParity(i), i + 1)
end
function Base.getindex(::SectorValues{FermionParity}, i)
function Base.getindex(::SectorValues{FermionParity}, i::Int)
return 1 <= i <= 2 ? FermionParity(i - 1) : throw(BoundsError(values(FermionParity), i))
end
findindex(::SectorValues{FermionParity}, f::FermionParity) = f.isodd ? 2 : 1
Expand Down
33 changes: 31 additions & 2 deletions src/sectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,44 @@
If `IteratorSize(I) == HasLength()`, also the following must be implemented:
* `Base.length(::SectorValues{I})`: the number of different values
* `Base.getindex(::SectorValues{I}, i::Int)`: a mapping between an index `i` and an
instance of `I`
instance of `I`. A fallback implementation exists that returns the `i`th value
of the `SectorValues` iterator.
* `findindex(::SectorValues{I}, c::I)`: reverse mapping between a value `c::I` and an
index `i::Integer ∈ 1:length(values(I))`
index `i::Integer ∈ 1:length(values(I))`. A fallback implementation exists that
linearly searches through the `SectorValues` iterator.
"""
struct SectorValues{I<:Sector} end
Base.IteratorEltype(::Type{<:SectorValues}) = HasEltype()
Base.eltype(::Type{SectorValues{I}}) where {I<:Sector} = I
Base.values(::Type{I}) where {I<:Sector} = SectorValues{I}()

Base.@propagate_inbounds function Base.getindex(v::SectorValues{I},
i::Int) where {I<:Sector}
@boundscheck begin
if Base.IteratorSize(v) === HasLength()
1 ≤ i ≤ length(v) || throw(BoundsError(v, i))

Check warning on line 64 in src/sectors.jl

View check run for this annotation

Codecov / codecov/patch

src/sectors.jl#L64

Added line #L64 was not covered by tests
else
1 ≤ i || throw(BoundsError(v, i))
end
end
for (j, c) in enumerate(v)
j == i && return c
end
throw(BoundsError(v, i))

Check warning on line 72 in src/sectors.jl

View check run for this annotation

Codecov / codecov/patch

src/sectors.jl#L72

Added line #L72 was not covered by tests
end

"""
findindex(v::SectorValues{I}, c::I)

Reverse mapping between a value `c::I` and an index `i::Integer ∈ 1:length(values(I))`.
"""
function findindex(v::SectorValues{I}, c::I) where {I<:Sector}
for (i, cc) in enumerate(v)
cc == c && return i
end
throw(ArgumentError(lazy"Cannot locate sector $c"))

Check warning on line 84 in src/sectors.jl

View check run for this annotation

Codecov / codecov/patch

src/sectors.jl#L84

Added line #L84 was not covered by tests
end

"""
one(::Sector) -> Sector
one(::Type{<:Sector}) -> Sector
Expand Down
5 changes: 0 additions & 5 deletions test/newsectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,4 @@ end
Base.hash(s::NewSU2Irrep, h::UInt) = hash(s.j, h)
Base.isless(s1::NewSU2Irrep, s2::NewSU2Irrep) = isless(s1.j, s2.j)

function Base.getindex(::SectorValues{NewSU2Irrep}, i::Int)
return 1 <= i ? NewSU2Irrep(half(i - 1)) : throw(BoundsError(values(NewSU2Irrep), i))
end
TensorKitSectors.findindex(::SectorValues{NewSU2Irrep}, s::NewSU2Irrep) = twice(s.j) + 1

end # module NewSectors
Loading