package pathgame.algorithm; import java.util.ArrayList; /** Class for storing and managing a list of graph vertexes with odd degrees * */ public class OddDegreeList { private ArrayList items = new ArrayList<>(); private ArrayList itemUsed = new ArrayList<>(); /** Adds a vertex to the list * * @param vertex index of the vertex in the list of all vertexes */ public void add(int vertex) { items.add(vertex); itemUsed.add(false); } /** Returns the size of the list of vertexes * * @return the size of the list of vertexes */ public int size() { return items.size(); } /** Resets which vertexes have already been used for the permutation * */ public void resetUsed() { for(int i = 0; i < itemUsed.size(); i++) { itemUsed.set(i, false); } } /** Returns the next unused vertex after the given offset * * @param offSet number of unused vertexes that are skipped before returning a vertex * @return the next unused vertex after the offset */ public int getUnused(int offSet) { return items.get(getOffsetPos(offSet)); } /** Makes an unused vertex used after the given offset * * @param offSet number of unused vertexes that are skipped before making the vertex used * @return the index of the vertex that was made used */ public int makeOffsetUsed(int offSet) { int pos = getOffsetPos(offSet); itemUsed.set(pos, true); return pos; } /** makes the vertex at the given position unused * * @param pos index of the vertex that is made unused */ public void makeUnused(int pos) { itemUsed.set(pos, false); } private int getOffsetPos(int offSet) { int foundValid = 0; for (int i = 0; i < items.size(); i++) { if (!itemUsed.get(i)) { if (offSet == foundValid) { return i; } foundValid++; } } return -1; } }