Skip to content

Commit f6911ee

Browse files
committed
fix getindex and iterate
1 parent 0d51b19 commit f6911ee

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

NDTensors/src/lib/GradedAxes/src/labelledunitrangedual.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ end
2525

2626
# fix ambiguities
2727
Base.getindex(a::LabelledUnitRangeDual, i::Integer) = dual(nondual(a)[i])
28+
function Base.getindex(a::LabelledUnitRangeDual, indices::AbstractUnitRange{<:Integer})
29+
return dual(nondual(a)[indices])
30+
end
31+
32+
function Base.iterate(a::LabelledUnitRangeDual, i)
33+
i == last(a) && return nothing
34+
next = convert(eltype(a), labelled(i + step(a), label(a)))
35+
return (next, next)
36+
end
2837

2938
function Base.show(io::IO, ::MIME"text/plain", a::LabelledUnitRangeDual)
3039
println(io, typeof(a))

NDTensors/src/lib/GradedAxes/test/test_dual.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ end
7272
@test space_isequal(la, la)
7373
@test label_type(la) == U1
7474

75+
@test iterate(la) == (1, 1)
76+
@test iterate(la) == (1, 1)
77+
@test iterate(la, 1) == (2, 2)
78+
@test isnothing(iterate(la, 2))
79+
7580
lad = dual(la)
7681
@test lad isa LabelledUnitRangeDual
7782
@test label(lad) == U1(-1)
@@ -87,10 +92,22 @@ end
8792
@test dual(lad) === la
8893
@test label_type(lad) == U1
8994

95+
@test iterate(lad) == (1, 1)
96+
@test iterate(lad) == (1, 1)
97+
@test iterate(lad, 1) == (2, 2)
98+
@test isnothing(iterate(lad, 2))
99+
100+
lad2 = lad[1:1]
101+
@test lad2 isa LabelledUnitRangeDual
102+
@test label(lad2) == U1(-1)
103+
@test unlabel(lad2) == 1:1
104+
90105
laf = flip(la)
91106
@test laf isa LabelledUnitRangeDual
92107
@test label(laf) == U1(1)
93108
@test unlabel(laf) == 1:2
109+
@test labelled_isequal(la, laf)
110+
@test !space_isequal(la, laf)
94111

95112
ladf = flip(dual(la))
96113
@test ladf isa LabelledUnitRange

0 commit comments

Comments
 (0)