@@ -299,21 +299,60 @@ function old_new_cell(system::Particles, i, neighbour_list::LinkedList)
299299 return c, c2
300300end
301301
302+ struct LinkedIterator
303+ neighbour_cells:: Vector{Int}
304+ head:: Vector{Int}
305+ list:: Vector{Int}
306+ end
307+
308+ function Base. iterate (neighbour_list:: LinkedIterator , state= - 1 )
309+
310+ # @inbounds for c2 in neighbour_cells
311+ # j = neighbour_list.head[c2]
312+ # while (j != -1)
313+ # j = neighbour_list.list[j]
314+ # end
315+ # end
316+
317+ # First time in
318+ if state == - 1
319+ c, c_state = iterate (neighbour_list. neighbour_cells)
320+ j = neighbour_list. head[c]
321+ state = (c_state, j)
322+ return j, state
323+ end
324+
325+ c_state, j = state
326+ if j == - 1
327+ next = iterate (neighbour_cells, c_state)
328+ if next == nothing
329+ return nothing
330+ end
331+ c, c_state = next
332+ j = neighbour_list. head[c]
333+ if j == - 1
334+ return nothing
335+ end
336+ state = (c_state, j)
337+ return j, state
338+ end
339+ j = neighbour_list. list[j]
340+ if j == - 1
341+ return nothing
342+ end
343+ state = (c_state, j)
344+ return j, state
345+ end
346+
347+
302348""" Iterate over the particles from adjacent cells.
303349"""
304350function get_neighbour_indices (system:: Particles , neighbour_list:: LinkedList , i:: Int )
305351 position_i = get_position (system, i)
306352 c = get_cell_index (position_i, neighbour_list)
307353 neighbour_cells = neighbour_list. neighbour_cells[c]
308354
309- neighbours = []
310- @inbounds for c2 in neighbour_cells
311- # Scan atoms in cell c2
312- j = neighbour_list. head[c2]
313- while (j != - 1 )
314- push! (neighbours, j)
315- j = neighbour_list. list[j]
316- end
317- end
318- return neighbours
355+ iterator = LinkedIterator (neighbour_cells, neighbour_list. head, neighbour_list. list)
356+ return (j for j in iterator)
357+
319358end
0 commit comments