index.js 5.6 KB

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