package etomica.nbr.cell;

import etomica.atom.IAtom;
import etomica.atom.IAtomList;
import etomica.box.Box;
import etomica.lattice.CellLattice;

import java.util.function.BiConsumer;

public class ApiAACellFast {
    private final Box box;
    private CellLattice lattice;

    public ApiAACellFast(Box box) {
        this.box = box;
    }

    public void setLattice(CellLattice lattice) {
        this.lattice = lattice;
    }

    public void forEachPair(AtomPairConsumer action) {
        int[][] nbrCells = lattice.getUpNeighbors();
        for (int centralCellIdx = 0; centralCellIdx < lattice.sites().length; centralCellIdx++) {

            Cell centralCell = (Cell) lattice.sites()[centralCellIdx];
            IAtomList centralCellAtoms = centralCell.occupants();

            // loop over pairs within the cell
            for (int i = 0; i < centralCellAtoms.size(); i++) {
                for (int j = i + 1; j < centralCellAtoms.size(); j++) {
                    action.accept(centralCellAtoms.get(i), centralCellAtoms.get(j));
                }
            }

            for (int nbrCellIdx : nbrCells[centralCellIdx]) {
                Cell nbrCell = (Cell) lattice.sites()[nbrCellIdx];
                IAtomList nbrCellAtoms = nbrCell.occupants();

                for (int i = 0; i < centralCellAtoms.size(); i++) {
                    for (int j = 0; j < nbrCellAtoms.size(); j++) {
                        action.accept(centralCellAtoms.get(i), nbrCellAtoms.get(j));
                    }
                }

            }
        }
    }

    @FunctionalInterface
    public interface AtomPairConsumer extends BiConsumer<IAtom, IAtom> {
        @Override
        void accept(IAtom atom1, IAtom atom2);
    }
}

@@ -761,32 +761,84 @@
 l.occupants();%0A%0A
+            if (!centralCellAtoms.isEmpty()) %7B%0A%0A    
             // l
@@ -872,32 +872,36 @@
 ell%0A            
+  
+  
 for (int i = 0; 
@@ -928,32 +928,36 @@
 .size(); i++) %7B%0A
+    
                 
@@ -1024,32 +1024,36 @@
                 
+ 
+   
 action.accept(ce
@@ -1109,32 +1109,36 @@
                 
+    
 %7D%0A            %7D%0A
@@ -1127,35 +1127,43 @@
   %7D%0A            
+ 
-%7D%0A%0A
+   %7D%0A%0A    
             for 
@@ -1220,24 +1220,28 @@
             
+ 
+   
 Cell nbrCell
@@ -1287,32 +1287,36 @@
                 
+    
 IAtomList nbrCel
@@ -1354,32 +1354,36 @@
                 
+ 
+   
 for (int i = 0; 
@@ -1410,32 +1410,36 @@
 .size(); i++) %7B%0A
+    
                 
@@ -1506,32 +1506,36 @@
                 
+    
 action.accept(ce
@@ -1591,34 +1591,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -1620,32 +1620,50 @@
              %7D%0A%0A
+                %7D%0A
             %7D%0A  

Add check to ApiAACellFast