Skip to content

Commit a8277fb

Browse files
committed
fix: properly handle -1 values in heads
1 parent 29edf39 commit a8277fb

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/neighbours.jl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function (cell_list::CellList)(system::Particles, i::Int)
214214
neighbour_cells = cell_list.neighbour_cells[c]
215215
# Scan the neighbourhood of cell mc (including itself)
216216
# and from there scan atoms in cell c2
217-
return (j for c2 in @inbounds neighbour_cells for j in @inbounds cell_list.cells[c2])
217+
return (j for c2 in neighbour_cells for j in @inbounds cell_list.cells[c2])
218218
end
219219

220220

@@ -326,16 +326,26 @@ end
326326
function Base.iterate(neighbour_list::LinkedIterator, state=-1)
327327
# First time in
328328
if state == -1
329-
next = iterate(neighbour_list.neighbour_cells)
330-
if next == nothing
331-
return nothing
329+
j = -1
330+
c_state = nothing
331+
# The while loop is necessary, in case the first head is -1
332+
while j == -1
333+
if c_state == nothing
334+
next = iterate(neighbour_list.neighbour_cells)
335+
else
336+
next = iterate(neighbour_list.neighbour_cells, c_state)
337+
end
338+
if next == nothing
339+
return nothing
340+
end
341+
c, c_state = next
342+
@inbounds j = neighbour_list.head[c]
332343
end
333-
c, c_state = next
334-
@inbounds j = neighbour_list.head[c]
335344
else
336345
c_state, j = state
337346
@inbounds j = neighbour_list.list[j]
338-
if j == -1
347+
# The while loop is necessary, in case a head is -1
348+
while j == -1
339349
next = iterate(neighbour_list.neighbour_cells, c_state)
340350
if next == nothing
341351
return nothing

0 commit comments

Comments
 (0)