package algorithms.rotationalplanesweep;

public class RPSEdgeHeap {

    private RPSScanner.Edge[] edges;
    private int heapSize;

    public RPSEdgeHeap(RPSScanner.Edge[] edges) {
        this.edges = edges;
        heapSize = 0;
        for (int i=0; i<edges.length; ++i) {
            edges[i].heapIndex = i;
        }
    }

    public final RPSScanner.Edge[] getEdgeList() {
        return edges;
    }

    public final void clear() {
        heapSize = 0;
    }

    public final int size() {
        return heapSize;
    }

    public final boolean isEmpty() {
        return heapSize == 0;
    }

    public final RPSScanner.Edge getMin() {
        return edges[0];
    }

    public final void delete(RPSScanner.Edge edge, int sx,int sy) {
        // Safety check for Debugging:
        if (edge.heapIndex >= heapSize) throw new UnsupportedOperationException("ELEMENT NOT IN HEAP: " + edge);

        int currentIndex = edge.heapIndex;
        swap(currentIndex, heapSize-1);
        --heapSize;

        if (currentIndex >= heapSize) return;
        bubbleUp(currentIndex, sx,sy);
        bubbleDown(currentIndex, sx,sy);
    }

    public final void insert(RPSScanner.Edge edge, int sx,int sy) {
        // Safety check for Debugging:
        if (edge.heapIndex < heapSize) throw new UnsupportedOperationException("ELEMENT ALREADY EXISTS: " + edge);

        swap(edge.heapIndex, heapSize);
        ++heapSize;
        bubbleUp(edge.heapIndex, sx,sy);
    }

    private final void bubbleDown(int i, int sx,int sy) {
        //double distance = edges[i].distance(sx,sy,midX,midY);
        while (true) {
            int left = 2*i + 1;
            if (left >= heapSize) return;

            int right = 2*i + 2;
            
            // Swap with the smaller one
            int swapTarget = right;
            //if (right >= heapSize || edges[left].distance(sx,sy,midX,midY) < edges[right].distance(sx,sy,midX,midY)) {
            if (right >= heapSize || edges[left].isLessThan(edges[right], sx, sy)) {
                swapTarget = left;
            }

            //if (edges[swapTarget].distance(sx,sy,midX,midY) < distance) {
            if (edges[swapTarget].isLessThan(edges[i], sx, sy)) {
                swap(i, swapTarget);
                i = swapTarget;
            } else {
                return;
            }
        }
    }

    private final void bubbleUp(int i, int sx,int sy) {
        //double distance = edges[i].distance(sx,sy,midX,midY);
        while (i > 0) {
            int parent = (i-1)/2;
            //if (edges[parent].distance(sx,sy,midX,midY) > distance) {
            if (edges[i].isLessThan(edges[parent], sx, sy)) {
                swap(i, parent);
                i = parent;
            } else {
                break;
            }
        }
    }

    private final void swap(int i, int j) {
        RPSScanner.Edge temp = edges[i];
        edges[i] = edges[j];
        edges[j] = temp;

        edges[i].heapIndex = i;
        edges[j].heapIndex = j;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<heapSize; ++i) {
            RPSScanner.Edge e = edges[i];
            sb.append(e.heapIndex + ": " + e.u.x + ", " + e.u.y + ", " + e.v.x + ", " + e.v.y);// + " | " + e.distance(sx,sy,tx,ty));
            sb.append("\n");
        }
        return sb.toString();
    }
}
@@ -785,32 +785,34 @@
 ugging:%0A        
+//
 if (edge.heapInd
@@ -1245,32 +1245,34 @@
 ugging:%0A        
+//
 if (edge.heapInd
@@ -1533,72 +1533,8 @@
 ) %7B%0A
-        //double distance = edges%5Bi%5D.distance(sx,sy,midX,midY);%0A
     
@@ -1754,129 +1754,8 @@
 ht;%0A
-            //if (right %3E= heapSize %7C%7C edges%5Bleft%5D.distance(sx,sy,midX,midY) %3C edges%5Bright%5D.distance(sx,sy,midX,midY)) %7B%0A
     
@@ -1889,84 +1889,8 @@
  %7D%0A%0A
-            //if (edges%5BswapTarget%5D.distance(sx,sy,midX,midY) %3C distance) %7B%0A
     
@@ -2164,193 +2164,58 @@
     
-//double distance = edges%5Bi%5D.distance(sx,sy,midX,midY);%0A        while (i %3E 0) %7B%0A            int parent = (i-1)/2;%0A            //if (edges%5Bparent%5D.distance(sx,sy,midX,midY) %3E distance) %7B
+while (i %3E 0) %7B%0A            int parent = (i-1)/2;%0A
 %0A   
@@ -2895,46 +2895,8 @@
 .y);
-// + %22 %7C %22 + e.distance(sx,sy,tx,ty));
 %0A   

Comment out safety checks in RPSEdgeHeap and remove unused code