OddDegreeList.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package pathgame.algorithm;
  2. import java.util.ArrayList;
  3. /** Class for storing and managing a list of graph vertexes with odd degrees
  4. *
  5. */
  6. public class OddDegreeList {
  7. private ArrayList<Integer> items = new ArrayList<>();
  8. private ArrayList<Boolean> itemUsed = new ArrayList<>();
  9. /** Adds a vertex to the list
  10. *
  11. * @param vertex index of the vertex in the list of all vertexes
  12. */
  13. public void add(int vertex) {
  14. items.add(vertex);
  15. itemUsed.add(false);
  16. }
  17. /** Returns the size of the list of vertexes
  18. *
  19. * @return the size of the list of vertexes
  20. */
  21. public int size() {
  22. return items.size();
  23. }
  24. /** Resets which vertexes have already been used for the permutation
  25. *
  26. */
  27. public void resetUsed() {
  28. for(int i = 0; i < itemUsed.size(); i++) {
  29. itemUsed.set(i, false);
  30. }
  31. }
  32. /** Returns the next unused vertex after the given offset
  33. *
  34. * @param offSet number of unused vertexes that are skipped before returning a vertex
  35. * @return the next unused vertex after the offset
  36. */
  37. public int getUnused(int offSet) {
  38. return items.get(getOffsetPos(offSet));
  39. }
  40. /** Makes an unused vertex used after the given offset
  41. *
  42. * @param offSet number of unused vertexes that are skipped before making the vertex used
  43. * @return the index of the vertex that was made used
  44. */
  45. public int makeOffsetUsed(int offSet) {
  46. int pos = getOffsetPos(offSet);
  47. itemUsed.set(pos, true);
  48. return pos;
  49. }
  50. /** makes the vertex at the given position unused
  51. *
  52. * @param pos index of the vertex that is made unused
  53. */
  54. public void makeUnused(int pos) {
  55. itemUsed.set(pos, false);
  56. }
  57. private int getOffsetPos(int offSet) {
  58. int foundValid = 0;
  59. for (int i = 0; i < items.size(); i++) {
  60. if (!itemUsed.get(i)) {
  61. if (offSet == foundValid) {
  62. return i;
  63. }
  64. foundValid++;
  65. }
  66. }
  67. return -1;
  68. }
  69. }