Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TopoSorter.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright (c) 2018 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. /**
  17. *
  18. * @author James Ahlborn
  19. */
  20. public abstract class TopoSorter<E>
  21. {
  22. public static final boolean REVERSE = true;
  23. // https://en.wikipedia.org/wiki/Topological_sorting
  24. private static final int UNMARKED = 0;
  25. private static final int TEMP_MARK = 1;
  26. private final static int PERM_MARK = 2;
  27. private final List<E> _values;
  28. private final List<Node<E>> _nodes = new ArrayList<Node<E>>();
  29. private final boolean _reverse;
  30. protected TopoSorter(List<E> values, boolean reverse) {
  31. _values = values;
  32. _reverse = reverse;
  33. }
  34. public void sort() {
  35. for(E val : _values) {
  36. Node<E> node = new Node<E>(val);
  37. getDescendents(val, node._descs);
  38. // build the internal list in reverse so that we maintain the "original"
  39. // order of items which we don't need to re-arrange
  40. _nodes.add(0, node);
  41. }
  42. _values.clear();
  43. for(Node<E> node : _nodes) {
  44. if(node._mark != UNMARKED) {
  45. continue;
  46. }
  47. visit(node);
  48. }
  49. }
  50. private void visit(Node<E> node) {
  51. if(node._mark == PERM_MARK) {
  52. return;
  53. }
  54. if(node._mark == TEMP_MARK) {
  55. throw new IllegalStateException("Cycle detected");
  56. }
  57. node._mark = TEMP_MARK;
  58. for(E descVal : node._descs) {
  59. Node<E> desc = findDescendent(descVal);
  60. visit(desc);
  61. }
  62. node._mark = PERM_MARK;
  63. if(_reverse) {
  64. _values.add(node._val);
  65. } else {
  66. _values.add(0, node._val);
  67. }
  68. }
  69. private Node<E> findDescendent(E val) {
  70. for(Node<E> node : _nodes) {
  71. if(node._val == val) {
  72. return node;
  73. }
  74. }
  75. throw new IllegalStateException("Unknown descendent " + val);
  76. }
  77. protected abstract void getDescendents(E from, List<E> descendents);
  78. private static class Node<E>
  79. {
  80. private final E _val;
  81. private final List<E> _descs = new ArrayList<E>();
  82. private int _mark = UNMARKED;
  83. private Node(E val) {
  84. _val = val;
  85. }
  86. }
  87. }