def _neighbor_iterator(dist, radius_matrix, buffer_matrix, n, connection_matrix
    ):
    """An iterator function to work out neighbor shells.

    Parameters
    ----------
    dist : array
        Distance matrix for the atoms object.
    radius_matrix : array
        An NxN matrix of summed radii between each pair of atoms.
    buffer_matrix : array
        An NxN matrix of buffer values between each pair of atoms.
    n : int
        The current neighbor shell.
    connection_matrix : array
        The current connection matrix being built.

    Returns
    -------
    dist : array
        Modified distance matrix for the atoms object. Modified to avoid
        recounting neighbors.
    connection_matrix : array
        Modified current connection matrix. Contains results for current shell.
    unconnected : int
        The number of atom pairs that dont have a neighbor shell assigned.
    """
    res = dist - (radius_matrix + buffer_matrix) * (n + 1)
    res[res > 0.0] = 0.0
    res[res < 0.0] = n + 1.0
    dist += res * 10000.0
    connection_matrix += res
    unconnected = len(connection_matrix[connection_matrix == 0.0])
    return dist, connection_matrix, unconnected
