toposort.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /****
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 Gustavo Henke and Aaron Trent
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. ****/
  25. (function( global, factory ) {
  26. if( typeof define === "function" && define.amd ) {
  27. define( "Toposort", ["exports", "module"], factory );
  28. } else if( typeof exports !== "undefined" && typeof module !== "undefined" ) {
  29. factory( exports, module );
  30. } else {
  31. var mod = {
  32. exports: {}
  33. };
  34. factory( mod.exports, mod );
  35. global.Toposort = mod.exports;
  36. }
  37. })( this, function( exports, module ) {
  38. "use strict";
  39. function _classCallCheck( instance, Constructor ) {
  40. if( !(instance instanceof Constructor) ) {
  41. throw new TypeError( "Cannot call a class as a function" );
  42. }
  43. }
  44. var Toposort = (function() {
  45. function Toposort() {
  46. _classCallCheck( this, Toposort );
  47. this.edges = [];
  48. this.Toposort = Toposort;
  49. }
  50. /**
  51. * Adds dependency edges.
  52. *
  53. * @since 0.1.0
  54. * @param {String} item An dependent name. Must be an string and not empty
  55. * @param {String[]|String} [deps] An dependency or array of dependencies
  56. * @returns {Toposort} The Toposort instance
  57. */
  58. Toposort.prototype.add = function add( item, deps ) {
  59. if( typeof item !== "string" || !item ) {
  60. throw new TypeError( "Dependent name must be given as a not empty string" );
  61. }
  62. deps = Array.isArray( deps ) ? deps : [deps];
  63. if( deps.length > 0 ) {
  64. for( var _iterator = deps, _isArray = Array.isArray( _iterator ), _i = 0, _iterator = _isArray ?
  65. _iterator :
  66. _iterator[Symbol.iterator](); ; ) {
  67. var _ref;
  68. if( _isArray ) {
  69. if( _i >= _iterator.length ) {
  70. break;
  71. }
  72. _ref = _iterator[_i++];
  73. } else {
  74. _i = _iterator.next();
  75. if( _i.done ) {
  76. break;
  77. }
  78. _ref = _i.value;
  79. }
  80. var dep = _ref;
  81. if( typeof dep !== "string" || !dep ) {
  82. throw new TypeError( "Dependency name must be given as a not empty string" );
  83. }
  84. this.edges.push( [item, dep] );
  85. }
  86. } else {
  87. this.edges.push( [item] );
  88. }
  89. return this;
  90. };
  91. /**
  92. * Runs the toposorting and return an ordered array of strings
  93. *
  94. * @since 0.1.0
  95. * @returns {String[]} The list of items topologically sorted.
  96. */
  97. Toposort.prototype.sort = function sort() {
  98. var _this = this;
  99. var nodes = [];
  100. //accumulate unique nodes into a large list
  101. for( var _iterator2 = this.edges, _isArray2 = Array.isArray( _iterator2 ), _i2 = 0, _iterator2 = _isArray2 ?
  102. _iterator2 :
  103. _iterator2[Symbol.iterator](); ; ) {
  104. var _ref2;
  105. if( _isArray2 ) {
  106. if( _i2 >= _iterator2.length ) {
  107. break;
  108. }
  109. _ref2 = _iterator2[_i2++];
  110. } else {
  111. _i2 = _iterator2.next();
  112. if( _i2.done ) {
  113. break;
  114. }
  115. _ref2 = _i2.value;
  116. }
  117. var edge = _ref2;
  118. for( var _iterator3 = edge, _isArray3 = Array.isArray( _iterator3 ), _i3 = 0, _iterator3 = _isArray3 ?
  119. _iterator3 :
  120. _iterator3[Symbol.iterator](); ; ) {
  121. var _ref3;
  122. if( _isArray3 ) {
  123. if( _i3 >= _iterator3.length ) {
  124. break;
  125. }
  126. _ref3 = _iterator3[_i3++];
  127. } else {
  128. _i3 = _iterator3.next();
  129. if( _i3.done ) {
  130. break;
  131. }
  132. _ref3 = _i3.value;
  133. }
  134. var node = _ref3;
  135. if( nodes.indexOf( node ) === -1 ) {
  136. nodes.push( node );
  137. }
  138. }
  139. }
  140. //initialize the placement of nodes into the sorted array at the end
  141. var place = nodes.length;
  142. //initialize the sorted array with the same length as the unique nodes array
  143. var sorted = new Array( nodes.length );
  144. //define a visitor function that recursively traverses dependencies.
  145. var visit = function visit( node, predecessors ) {
  146. //check if a node is dependent of itself
  147. if( predecessors.length !== 0 && predecessors.indexOf( node ) !== -1 ) {
  148. throw new Error( "Cyclic dependency found. " + node + " is dependent of itself.\nDependency chain: "
  149. + predecessors.join( " -> " ) + " => " + node );
  150. }
  151. var index = nodes.indexOf( node );
  152. //if the node still exists, traverse its dependencies
  153. if( index !== -1 ) {
  154. var copy = false;
  155. //mark the node as false to exclude it from future iterations
  156. nodes[index] = false;
  157. //loop through all edges and follow dependencies of the current node
  158. for( var _iterator4 = _this.edges, _isArray4 = Array.isArray( _iterator4 ), _i4 = 0, _iterator4 = _isArray4 ?
  159. _iterator4 :
  160. _iterator4[Symbol.iterator](); ; ) {
  161. var _ref4;
  162. if( _isArray4 ) {
  163. if( _i4 >= _iterator4.length ) {
  164. break;
  165. }
  166. _ref4 = _iterator4[_i4++];
  167. } else {
  168. _i4 = _iterator4.next();
  169. if( _i4.done ) {
  170. break;
  171. }
  172. _ref4 = _i4.value;
  173. }
  174. var edge = _ref4;
  175. if( edge[0] === node ) {
  176. //lazily create a copy of predecessors with the current node concatenated onto it
  177. copy = copy || predecessors.concat( [node] );
  178. //recurse to node dependencies
  179. visit( edge[1], copy );
  180. }
  181. }
  182. //add the node to the next place in the sorted array
  183. sorted[--place] = node;
  184. }
  185. };
  186. for( var i = 0; i < nodes.length; i++ ) {
  187. var node = nodes[i];
  188. //ignore nodes that have been excluded
  189. if( node !== false ) {
  190. //mark the node as false to exclude it from future iterations
  191. nodes[i] = false;
  192. //loop through all edges and follow dependencies of the current node
  193. for( var _iterator5 = this.edges, _isArray5 = Array.isArray( _iterator5 ), _i5 = 0, _iterator5 = _isArray5 ?
  194. _iterator5 :
  195. _iterator5[Symbol.iterator](); ; ) {
  196. var _ref5;
  197. if( _isArray5 ) {
  198. if( _i5 >= _iterator5.length ) {
  199. break;
  200. }
  201. _ref5 = _iterator5[_i5++];
  202. } else {
  203. _i5 = _iterator5.next();
  204. if( _i5.done ) {
  205. break;
  206. }
  207. _ref5 = _i5.value;
  208. }
  209. var edge = _ref5;
  210. if( edge[0] === node ) {
  211. //recurse to node dependencies
  212. visit( edge[1], [node] );
  213. }
  214. }
  215. //add the node to the next place in the sorted array
  216. sorted[--place] = node;
  217. }
  218. }
  219. return sorted;
  220. };
  221. /**
  222. * Clears edges
  223. *
  224. * @since 0.4.0
  225. * @returns {Toposort} The Toposort instance
  226. */
  227. Toposort.prototype.clear = function clear() {
  228. this.edges = [];
  229. return this;
  230. };
  231. return Toposort;
  232. })();
  233. module.exports = Toposort;
  234. } );