Skip to content

Commit 89c787b

Browse files
author
Jack Dunham
committed
Constructing a SweepIterator and RegionIterator of length 0 now gives BoundsError.
Constructing `RegionIterator` of length 0 is now undefined. Adjust region iterator `BoundsError` message to align with `SweepIterator`.
1 parent 548654e commit 89c787b

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/solvers/iterators.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ mutable struct RegionIterator{Problem, RegionPlan} <: AbstractNetworkIterator
4747
which_region::Int
4848
const which_sweep::Int
4949
function RegionIterator(problem::P, region_plan::R, sweep::Int) where {P, R}
50+
if length(region_plan) == 0
51+
throw(BoundsError("Cannot construct a region iterator with 0 elements."))
52+
end
5053
return new{P, R}(problem, region_plan, 1, sweep)
5154
end
5255
end
@@ -122,8 +125,15 @@ mutable struct SweepIterator{Problem, Iter} <: AbstractNetworkIterator
122125
which_sweep::Int
123126
function SweepIterator(problem::Prob, sweep_kwargs::Iter) where {Prob, Iter}
124127
stateful_sweep_kwargs = Iterators.Stateful(sweep_kwargs)
125-
first_kwargs, _ = Iterators.peel(stateful_sweep_kwargs)
128+
first_state = Iterators.peel(stateful_sweep_kwargs)
129+
130+
if isnothing(first_state)
131+
throw(BoundsError("Cannot construct a sweep iterator with 0 elements."))
132+
end
133+
134+
first_kwargs, _ = first_state
126135
region_iter = RegionIterator(problem; sweep = 1, first_kwargs...)
136+
127137
return new{Prob, Iter}(region_iter, stateful_sweep_kwargs, 1)
128138
end
129139
end

test/solvers/test_iterators.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Test: @test, @testset
2-
using ITensorNetworks: SweepIterator, islaststep, state, increment!, compute!, eachregion
1+
using Test: @test, @testset, @test_throws
2+
using ITensorNetworks: SweepIterator, RegionIterator, islaststep, state, increment!, compute!, eachregion
33

44
module TestIteratorUtils
55

@@ -61,7 +61,12 @@ end
6161
@test length(cb) == 1
6262
@test length(TI.output) == 1
6363
@test only(cb) == 1
64+
65+
prob = TestIteratorUtils.TestProblem([])
66+
@test_throws BoundsError SweepIterator(prob, 0)
67+
@test_throws BoundsError RegionIterator(prob, [], 1)
6468
end
69+
6570
TI = TestIteratorUtils.TestIterator(1, 4, [])
6671

6772
@test !islaststep((TI))

0 commit comments

Comments
 (0)