package scheduling.three;

import graph.Graph;
import graph.VertexColoring;

import java.util.LinkedList;

/**
 * Created by vivace on 04.04.17.
 * <p>
 * X3Y represents a set of vertices in cubic graph
 * that have color X and exactly 3 neighbours of color Y.
 * It is updated lazily. User can specify if the set needs to be updated
 */
class X3Y {
    private Graph graph;
    private VertexColoring coloring;
    private LinkedList<Integer> vertices = new LinkedList<>();

    private int colorX, colorY;

    private boolean upToDate;

    X3Y(VertexColoring coloring, int colorX, int colorY) {
        this.coloring = coloring;
        this.graph = coloring.getGraph();
        this.colorX = colorX;
        this.colorY = colorY;
    }

    boolean empty() {
        return getSize() == 0;
    }

    int getSize() {
        update();
        return vertices.size();
    }

    LinkedList<Integer> getVertices() {
        update();
        return vertices;
    }

    void setForUpdate() {
        upToDate = false;
    }

    private void update() {
        if (!upToDate) {
            vertices.clear();
            int neighboursWithOtherColor;
            for (int i = 0; i < graph.getVertices(); i++) {
                if (coloring.get(i) == colorX) {
                    neighboursWithOtherColor = 0;
                    for (Integer n : graph.getNeighbours(i)) {
                        if (coloring.get(n) == colorY) {
                            neighboursWithOtherColor++;
                        }
                    }

                    if (neighboursWithOtherColor == 3) {
                        vertices.add(i);
                    }
                }
            }

            upToDate = true;
        }
    }

    static boolean hasAllNeighboursInY(int index, int colorY, VertexColoring coloring) {
        for (Integer n : coloring.getGraph().getNeighbours(index)) {
            if (coloring.get(n) != colorY) {
                return false;
            }
        }

        return true;
    }
}
@@ -1744,22 +1744,15 @@
 tic 
-boolean hasAll
+int get
 Neig
@@ -1807,24 +1807,57 @@
 coloring) %7B%0A
+        int neighboursCount = 0;%0A
         for 
@@ -1945,17 +1945,17 @@
 .get(n) 
-!
+=
 = colorY
@@ -1978,20 +1978,25 @@
     
-return false
+neighboursCount++
 ;%0A  
@@ -2037,17 +2037,185 @@
 urn 
-true
+neighboursCount;%0A    %7D%0A%0A    static boolean has3NeighboursInY(int index, int colorY, VertexColoring coloring) %7B%0A        return getNeighboursInY(index, colorY, coloring) == 3
 ;%0A    %7D%0A
 %7D
@@ -2213,9 +2213,11 @@
 ;%0A    %7D%0A
+%0A%0A
 %7D

add getNeighboursInY()