private int addNeighbors(List<Acre> left, List<Acre> right) { assert left.size() == right.size(); int count = 0; for (int i = 0, j = left.size(); i < j; i++) { Acre l = left.get(i); Acre r = right.get(i); if (l != null && r != null) { l.addNeighbor(r, indexOfMyNeighbor(l, r)); r.addNeighbor(l, indexOfMyNeighbor(r, l)); count++; } } return count; }
private int addNeighbors( Acre[] acresA, int rowA, int lengthA, Acre[] acresB, int rowB, int lengthB) { if (lengthA <= 0 || lengthB <= 0) { return 0; // no-op } if (lengthB > lengthA) { int t = rowA; rowA = rowB; rowB = t; t = lengthA; lengthA = lengthB; lengthB = t; } if (lengthA == lengthB) { for (int i = 0; i < lengthA; i++) { Acre acreA = acresA[rowA + i]; Acre acreB = acresB[rowB + i]; acreA.addNeighbor(acreB, indexOfMyNeighbor(acreA, acreB)); acreB.addNeighbor(acreA, indexOfMyNeighbor(acreB, acreA)); } return lengthA * 2; } else { assert lengthA <= lengthB + 2; if (lengthA == lengthB + 1) { for (int i = 0; i < lengthB; i++) { Acre acreA = acresA[rowA + i]; Acre acreB = acresB[rowB + i]; Acre acreC = acresA[rowA + i + 1]; acreA.addNeighbor(acreB, indexOfMyNeighbor(acreA, acreB)); acreB.addNeighbor(acreA, indexOfMyNeighbor(acreB, acreA)); acreB.addNeighbor(acreC, indexOfMyNeighbor(acreB, acreC)); acreC.addNeighbor(acreB, indexOfMyNeighbor(acreC, acreB)); } return lengthB * 4; } else { for (int i = 0; i < lengthB; i++) { Acre acreA = acresA[rowA + i + 1]; Acre acreB = acresB[rowB + i]; acreA.addNeighbor(acreB, indexOfMyNeighbor(acreA, acreB)); acreB.addNeighbor(acreA, indexOfMyNeighbor(acreB, acreA)); } return lengthB * 4; } } }