Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.js 5.9 KiB

há 3 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. 'use strict';
  2. // Load modules
  3. const Hoek = require('hoek');
  4. // Declare internals
  5. const internals = {};
  6. exports = module.exports = internals.Topo = function () {
  7. this._items = [];
  8. this.nodes = [];
  9. };
  10. internals.Topo.prototype.add = function (nodes, options) {
  11. options = options || {};
  12. // Validate rules
  13. const before = [].concat(options.before || []);
  14. const after = [].concat(options.after || []);
  15. const group = options.group || '?';
  16. const sort = options.sort || 0; // Used for merging only
  17. Hoek.assert(before.indexOf(group) === -1, 'Item cannot come before itself:', group);
  18. Hoek.assert(before.indexOf('?') === -1, 'Item cannot come before unassociated items');
  19. Hoek.assert(after.indexOf(group) === -1, 'Item cannot come after itself:', group);
  20. Hoek.assert(after.indexOf('?') === -1, 'Item cannot come after unassociated items');
  21. ([].concat(nodes)).forEach((node, i) => {
  22. const item = {
  23. seq: this._items.length,
  24. sort: sort,
  25. before: before,
  26. after: after,
  27. group: group,
  28. node: node
  29. };
  30. this._items.push(item);
  31. });
  32. // Insert event
  33. const error = this._sort();
  34. Hoek.assert(!error, 'item', (group !== '?' ? 'added into group ' + group : ''), 'created a dependencies error');
  35. return this.nodes;
  36. };
  37. internals.Topo.prototype.merge = function (others) {
  38. others = [].concat(others);
  39. for (let i = 0; i < others.length; ++i) {
  40. const other = others[i];
  41. if (other) {
  42. for (let j = 0; j < other._items.length; ++j) {
  43. const item = Hoek.shallow(other._items[j]);
  44. this._items.push(item);
  45. }
  46. }
  47. }
  48. // Sort items
  49. this._items.sort(internals.mergeSort);
  50. for (let i = 0; i < this._items.length; ++i) {
  51. this._items[i].seq = i;
  52. }
  53. const error = this._sort();
  54. Hoek.assert(!error, 'merge created a dependencies error');
  55. return this.nodes;
  56. };
  57. internals.mergeSort = function (a, b) {
  58. return a.sort === b.sort ? 0 : (a.sort < b.sort ? -1 : 1);
  59. };
  60. internals.Topo.prototype._sort = function () {
  61. // Construct graph
  62. const graph = {};
  63. const graphAfters = Object.create(null); // A prototype can bungle lookups w/ false positives
  64. const groups = Object.create(null);
  65. for (let i = 0; i < this._items.length; ++i) {
  66. const item = this._items[i];
  67. const seq = item.seq; // Unique across all items
  68. const group = item.group;
  69. // Determine Groups
  70. groups[group] = groups[group] || [];
  71. groups[group].push(seq);
  72. // Build intermediary graph using 'before'
  73. graph[seq] = item.before;
  74. // Build second intermediary graph with 'after'
  75. const after = item.after;
  76. for (let j = 0; j < after.length; ++j) {
  77. graphAfters[after[j]] = (graphAfters[after[j]] || []).concat(seq);
  78. }
  79. }
  80. // Expand intermediary graph
  81. let graphNodes = Object.keys(graph);
  82. for (let i = 0; i < graphNodes.length; ++i) {
  83. const node = graphNodes[i];
  84. const expandedGroups = [];
  85. const graphNodeItems = Object.keys(graph[node]);
  86. for (let j = 0; j < graphNodeItems.length; ++j) {
  87. const group = graph[node][graphNodeItems[j]];
  88. groups[group] = groups[group] || [];
  89. for (let k = 0; k < groups[group].length; ++k) {
  90. expandedGroups.push(groups[group][k]);
  91. }
  92. }
  93. graph[node] = expandedGroups;
  94. }
  95. // Merge intermediary graph using graphAfters into final graph
  96. const afterNodes = Object.keys(graphAfters);
  97. for (let i = 0; i < afterNodes.length; ++i) {
  98. const group = afterNodes[i];
  99. if (groups[group]) {
  100. for (let j = 0; j < groups[group].length; ++j) {
  101. const node = groups[group][j];
  102. graph[node] = graph[node].concat(graphAfters[group]);
  103. }
  104. }
  105. }
  106. // Compile ancestors
  107. let children;
  108. const ancestors = {};
  109. graphNodes = Object.keys(graph);
  110. for (let i = 0; i < graphNodes.length; ++i) {
  111. const node = graphNodes[i];
  112. children = graph[node];
  113. for (let j = 0; j < children.length; ++j) {
  114. ancestors[children[j]] = (ancestors[children[j]] || []).concat(node);
  115. }
  116. }
  117. // Topo sort
  118. const visited = {};
  119. const sorted = [];
  120. for (let i = 0; i < this._items.length; ++i) {
  121. let next = i;
  122. if (ancestors[i]) {
  123. next = null;
  124. for (let j = 0; j < this._items.length; ++j) {
  125. if (visited[j] === true) {
  126. continue;
  127. }
  128. if (!ancestors[j]) {
  129. ancestors[j] = [];
  130. }
  131. const shouldSeeCount = ancestors[j].length;
  132. let seenCount = 0;
  133. for (let k = 0; k < shouldSeeCount; ++k) {
  134. if (sorted.indexOf(ancestors[j][k]) >= 0) {
  135. ++seenCount;
  136. }
  137. }
  138. if (seenCount === shouldSeeCount) {
  139. next = j;
  140. break;
  141. }
  142. }
  143. }
  144. if (next !== null) {
  145. next = next.toString(); // Normalize to string TODO: replace with seq
  146. visited[next] = true;
  147. sorted.push(next);
  148. }
  149. }
  150. if (sorted.length !== this._items.length) {
  151. return new Error('Invalid dependencies');
  152. }
  153. const seqIndex = {};
  154. for (let i = 0; i < this._items.length; ++i) {
  155. const item = this._items[i];
  156. seqIndex[item.seq] = item;
  157. }
  158. const sortedNodes = [];
  159. this._items = sorted.map((value) => {
  160. const sortedItem = seqIndex[value];
  161. sortedNodes.push(sortedItem.node);
  162. return sortedItem;
  163. });
  164. this.nodes = sortedNodes;
  165. };