<commit_before>package scheduling.triple;

import graph.Graph;
import graph.VertexColoring;

import java.util.BitSet;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by <NAME> on 15.03.17.
 *
 * Class used for swapping colors between components
 * within clw procedure.
 */
class ComponentSwapper {

    private Graph graph;
    private VertexColoring coloring;

    ComponentSwapper(VertexColoring coloring) {
        this.coloring = coloring;
        this.graph = coloring.getGraph();
    }

    /**
     * Try to swap components between color classes
     * so that it will decrease the width of coloring.
     *
     * @param colorBig color of bigger color class
     * @param colorSmall color of smaller color class
     * @param verticesToMove desired change of coloring  width
     * @return how much width was decreased
     */
    int swapBetween(int colorBig, int colorSmall, int verticesToMove, LinkedList<Integer> compensator) {
        BitSet checked = new BitSet(graph.getVertices());
        LinkedList<Integer> bigComponent = new LinkedList<>(), smallComponent = new LinkedList<>();
        int currentColor, sizeDifference, toCompensate, verticesMovedTotal = 0, verticesMoved;

        for(int i = 0; i < graph.getVertices(); i++) {

            if (!checked.get(i) && 0 < verticesToMove) {

                currentColor = coloring.get(i);

                if (currentColor == colorBig || currentColor == colorSmall) {
                    findComponents(i, colorBig, colorSmall, bigComponent, smallComponent, checked);
                    sizeDifference = bigComponent.size() - smallComponent.size();
                    if (0 < sizeDifference && sizeDifference <= verticesToMove + compensator.size()) {
                        toCompensate = sizeDifference - verticesToMove;
                        toCompensate = 0 < toCompensate ? toCompensate : 0;
                        verticesMoved = sizeDifference - toCompensate;
                        verticesMovedTotal += verticesMoved;
                        verticesToMove -= verticesMoved;

                        swapComponents(bigComponent, smallComponent, colorBig, colorSmall);
                        compensate(compensator, colorBig, toCompensate);
                    }
                }
            }
        }

        return verticesMovedTotal;
    }

    void swapBetweenWithoutDecreasing(int colorBig, int colorSmall, LinkedList<Integer> compensator) {
        BitSet checked = new BitSet(graph.getVertices());
        LinkedList<Integer> bigComponent = new LinkedList<>(), smallComponent = new LinkedList<>();
        int currentColor, sizeDifference;

        for(int i = 0; i < graph.getVertices(); i++) {

            if (!checked.get(i)) {

                currentColor = coloring.get(i);

                if (currentColor == colorBig || currentColor == colorSmall) {
                    findComponents(i, colorBig, colorSmall, bigComponent, smallComponent, checked);
                    sizeDifference = bigComponent.size() - smallComponent.size();
                    if (0 < sizeDifference && sizeDifference <= compensator.size()) {
                        swapComponents(bigComponent, smallComponent, colorBig, colorSmall);
                        compensate(compensator, colorBig, sizeDifference);

                        return;
                    }
                }
            }
        }
    }

    public int swapBetweenAndMoveToOther(int colorBig, int colorSmall, int colorOther,
                                         LinkedList<Integer> compensator,
                                         int toDecrease) {
        //TODO
        return 0;
    }

    /**
     * Find components within two given colors
     * starting from vertex with current index.
     *
     * @param current index to start with
     * @param colorBig color of bigger color class
     * @param colorSmall color of smaller color class
     * @param bigComponent list of indexes to fill
     * @param smallComponent list of indexes to fill
     */
    private void findComponents(int current, int colorBig, int colorSmall,
                                LinkedList<Integer> bigComponent,
                                LinkedList<Integer> smallComponent,
                                BitSet checked) {
        Queue<Integer> queue = new LinkedList<>();
        int currentColor, otherColor;

        queue.add(current);

        while (!queue.isEmpty()) {
            current = queue.poll();
            checked.set(current);

            currentColor = coloring.get(current);

            if (currentColor == colorBig) {
                otherColor = colorSmall;
                bigComponent.add(current);
            }
            else {
                otherColor = colorBig;
                smallComponent.add(current);
            }

            for (Integer neighbour : graph.getNeighbours(current)) {
                if (coloring.get(neighbour) == otherColor && !checked.get(neighbour)) {
                    queue.add(neighbour);
                }
            }
        }
    }

    private void swapComponents(LinkedList<Integer> x, LinkedList<Integer> y, int colorX, int colorY) {
        for (Integer i : x) {
            coloring.set(i, colorY);
        }
        for (Integer i : y) {
            coloring.set(i, colorX);
        }
    }

    private void compensate(LinkedList<Integer> compensator, int color, int n) {
        for (int i = 0; i < n; i++) {;
            coloring.set(compensator.poll(), color);
        }
    }
}
<commit_msg>ComponentSwapper: get rid if unnecessary variable.<commit_after>package scheduling.triple;

import graph.Graph;
import graph.VertexColoring;

import java.util.BitSet;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by <NAME> on 15.03.17.
 *
 * Class used for swapping colors between components
 * within clw procedure.
 */
class ComponentSwapper {

    private Graph graph;
    private VertexColoring coloring;

    ComponentSwapper(VertexColoring coloring) {
        this.coloring = coloring;
        this.graph = coloring.getGraph();
    }

    /**
     * Try to swap components between color classes
     * so that it will decrease the width of coloring.
     *
     * @param colorBig color of bigger color class
     * @param colorSmall color of smaller color class
     * @param verticesToMove desired change of coloring  width
     * @return how much width was decreased
     */
    int swapBetween(int colorBig, int colorSmall, int verticesToMove, LinkedList<Integer> compensator) {
        BitSet checked = new BitSet(graph.getVertices());
        LinkedList<Integer> bigComponent = new LinkedList<>(), smallComponent = new LinkedList<>();
        int currentColor, sizeDifference, toCompensate, verticesMoved = 0;

        for(int i = 0; i < graph.getVertices(); i++) {

            if (!checked.get(i) && 0 < verticesToMove) {

                currentColor = coloring.get(i);

                if (currentColor == colorBig || currentColor == colorSmall) {
                    findComponents(i, colorBig, colorSmall, bigComponent, smallComponent, checked);
                    sizeDifference = bigComponent.size() - smallComponent.size();
                    if (0 < sizeDifference && sizeDifference <= verticesToMove + compensator.size()) {
                        toCompensate = sizeDifference - verticesToMove;
                        toCompensate = 0 < toCompensate ? toCompensate : 0;
                        verticesMoved += sizeDifference - toCompensate;
                        verticesToMove -= sizeDifference - toCompensate;

                        swapComponents(bigComponent, smallComponent, colorBig, colorSmall);
                        compensate(compensator, colorBig, toCompensate);
                    }
                }
            }
        }

        return verticesMoved;
    }

    void swapBetweenWithoutDecreasing(int colorBig, int colorSmall, LinkedList<Integer> compensator) {
        BitSet checked = new BitSet(graph.getVertices());
        LinkedList<Integer> bigComponent = new LinkedList<>(), smallComponent = new LinkedList<>();
        int currentColor, sizeDifference;

        for(int i = 0; i < graph.getVertices(); i++) {

            if (!checked.get(i)) {

                currentColor = coloring.get(i);

                if (currentColor == colorBig || currentColor == colorSmall) {
                    findComponents(i, colorBig, colorSmall, bigComponent, smallComponent, checked);
                    sizeDifference = bigComponent.size() - smallComponent.size();
                    if (0 < sizeDifference && sizeDifference <= compensator.size()) {
                        swapComponents(bigComponent, smallComponent, colorBig, colorSmall);
                        compensate(compensator, colorBig, sizeDifference);

                        return;
                    }
                }
            }
        }
    }

    public int swapBetweenAndMoveToOther(int colorBig, int colorSmall, int colorOther,
                                         LinkedList<Integer> compensator,
                                         int toDecrease) {
        //TODO
        return 0;
    }

    /**
     * Find components within two given colors
     * starting from vertex with current index.
     *
     * @param current index to start with
     * @param colorBig color of bigger color class
     * @param colorSmall color of smaller color class
     * @param bigComponent list of indexes to fill
     * @param smallComponent list of indexes to fill
     */
    private void findComponents(int current, int colorBig, int colorSmall,
                                LinkedList<Integer> bigComponent,
                                LinkedList<Integer> smallComponent,
                                BitSet checked) {
        Queue<Integer> queue = new LinkedList<>();
        int currentColor, otherColor;

        queue.add(current);

        while (!queue.isEmpty()) {
            current = queue.poll();
            checked.set(current);

            currentColor = coloring.get(current);

            if (currentColor == colorBig) {
                otherColor = colorSmall;
                bigComponent.add(current);
            }
            else {
                otherColor = colorBig;
                smallComponent.add(current);
            }

            for (Integer neighbour : graph.getNeighbours(current)) {
                if (coloring.get(neighbour) == otherColor && !checked.get(neighbour)) {
                    queue.add(neighbour);
                }
            }
        }
    }

    private void swapComponents(LinkedList<Integer> x, LinkedList<Integer> y, int colorX, int colorY) {
        for (Integer i : x) {
            coloring.set(i, colorY);
        }
        for (Integer i : y) {
            coloring.set(i, colorX);
        }
    }

    private void compensate(LinkedList<Integer> compensator, int color, int n) {
        for (int i = 0; i < n; i++) {;
            coloring.set(compensator.poll(), color);
        }
    }
}

{'fraction_non_alphanumeric': 0.08938244853737812, 'fraction_numerical': 0.00288912964969303, 'mean_word_length': 1.8637538779731126, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4813359', 'n_tokens_mistral': 2861, 'n_tokens_neox': 2687, 'n_words': 749}