Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

dep-graph.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Generated by CoffeeScript 1.3.3
  2. (function() {
  3. var DepGraph, _,
  4. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  5. _ = require('underscore');
  6. DepGraph = (function() {
  7. function DepGraph() {
  8. this.map = {};
  9. }
  10. DepGraph.prototype.add = function(id, depId) {
  11. var _base, _ref;
  12. if ((_ref = (_base = this.map)[id]) == null) {
  13. _base[id] = [];
  14. }
  15. if (__indexOf.call(this.map[id], depId) >= 0) {
  16. return false;
  17. }
  18. this.map[id].push(depId);
  19. return this.map[id];
  20. };
  21. DepGraph.prototype.getChain = function(id) {
  22. var chain, deps, leafNode, visit, visited, _i, _len, _ref,
  23. _this = this;
  24. deps = this.descendantsOf(id);
  25. chain = [];
  26. visited = {};
  27. visit = function(node) {
  28. var parent, _i, _len, _ref;
  29. if (visited[node] || node === id) {
  30. return;
  31. }
  32. visited[node] = true;
  33. _ref = _this.parentsOf(node);
  34. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  35. parent = _ref[_i];
  36. if (__indexOf.call(deps, parent) >= 0) {
  37. visit(parent);
  38. }
  39. }
  40. return chain.unshift(node);
  41. };
  42. _ref = _.intersection(deps, this.leafNodes()).reverse();
  43. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  44. leafNode = _ref[_i];
  45. visit(leafNode);
  46. }
  47. return chain;
  48. };
  49. DepGraph.prototype.leafNodes = function() {
  50. var allNodes, node, _i, _len, _ref, _results;
  51. allNodes = _.uniq(_.flatten(_.values(this.map)));
  52. _results = [];
  53. for (_i = 0, _len = allNodes.length; _i < _len; _i++) {
  54. node = allNodes[_i];
  55. if (!((_ref = this.map[node]) != null ? _ref.length : void 0)) {
  56. _results.push(node);
  57. }
  58. }
  59. return _results;
  60. };
  61. DepGraph.prototype.parentsOf = function(child) {
  62. var node, _i, _len, _ref, _results;
  63. _ref = _.keys(this.map);
  64. _results = [];
  65. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  66. node = _ref[_i];
  67. if (__indexOf.call(this.map[node], child) >= 0) {
  68. _results.push(node);
  69. }
  70. }
  71. return _results;
  72. };
  73. DepGraph.prototype.descendantsOf = function(parent, descendants, branch) {
  74. var child, _i, _len, _ref, _ref1;
  75. if (descendants == null) {
  76. descendants = [];
  77. }
  78. if (branch == null) {
  79. branch = [];
  80. }
  81. descendants.push(parent);
  82. branch.push(parent);
  83. _ref1 = (_ref = this.map[parent]) != null ? _ref : [];
  84. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  85. child = _ref1[_i];
  86. if (__indexOf.call(branch, child) >= 0) {
  87. throw new Error("Cyclic dependency from " + parent + " to " + child);
  88. }
  89. if (__indexOf.call(descendants, child) >= 0) {
  90. continue;
  91. }
  92. this.descendantsOf(child, descendants, branch.slice(0));
  93. }
  94. return descendants.slice(1);
  95. };
  96. return DepGraph;
  97. })();
  98. if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
  99. module.exports = DepGraph;
  100. } else {
  101. this.DepGraph = DepGraph;
  102. }
  103. }).call(this);