mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
2 lines
6.7 KiB
JavaScript
2 lines
6.7 KiB
JavaScript
define([],function(){function a(a,b){for(var c in b)if(b.hasOwnProperty(c)&&(!a.hasOwnProperty(c)||a[c]!==b[c]))return!1;return!0}function b(b,c){var d="function"==typeof c?c:void 0,e="object"==typeof c?c:void 0,f=[],g=0;for(var h in b)if(b.hasOwnProperty(h)){var i=b[h];d?f.push(d.call(i,i,h,g)):e?"object"==typeof i&&a(i,e)&&f.push(i):f.push(i),g+=1}return f}function c(a,b,c){var d=this;return d.source=void 0!==a?a:null,d.target=void 0!==b?b:null,d.data=c||null,d}function d(a,b){var c=this;return c.name=void 0!==a?a:"(unnamed)",c.data=b||null,c.edges={},c.degree=0,c}function e(a,b,c){return this.directed=a||!1,this.init(c).read(b)}function f(a,b,c){function d(a){return Math.floor(Math.random()*a)}for(var f={nodes:[],links:[]},g=0;b>g;g++)f.nodes.push({name:g});for(g=0;c>g;g++)f.links.push({source:d(b),target:d(b)});return new e(a,f)}c.prototype.toString=function(){return this.source+"->"+this.target},c.prototype.toJSON=function(){var a={source:this.source,target:this.target};return this.data&&(a.data=this.data),a},d.prototype.toString=function(){return"Vertex("+this.name+")"},d.prototype.eachEdge=function(a){return b(this.edges,a)},d.prototype.toJSON=function(){return{name:this.name,data:this.data}};var g=function(a,b){var c=this;return c.graph=a,c.processFns=b||{vertexEarly:function(){},edge:function(){},vertexLate:function(){}},c._cache={},c};g.prototype.search=function(a){var b=this;return a in b._cache?b._cache[a]:(a instanceof d||(a=b.graph.vertices[a]),b._cache[a.name]=b._search(a))},g.prototype._search=function(a,b){return b=b||{discovered:{},edges:[]}},g.prototype.searchTree=function(a){return this._searchTree(this.search(a))},g.prototype._searchTree=function(a){var b=this;return new e(!0,{edges:a.edges,vertices:Object.keys(a.discovered).map(function(a){return b.graph.vertices[a].toJSON()})})};var h=function(a,b){var c=this;return g.call(this,a,b),c};h.prototype=new g,h.prototype.constructor=h,h.prototype._search=function(a,b){function c(a,c){var f=this;d.processFns.edge&&d.processFns.edge.call(d,f,c,b),b.discovered[a.name]||(b.discovered[a.name]=!0,b.edges.push({source:f.name,target:a.name}),e.push(a))}b=b||{discovered:{},edges:[]};var d=this,e=[];for(b.discovered[a.name]=!0,e.push(a);e.length;){var f=e.shift();d.processFns.vertexEarly&&d.processFns.vertexEarly.call(d,f,b),d.graph.eachAdjacent(f,c),d.processFns.vertexLate&&d.processFns.vertexLate.call(d,f,b)}return b};var i=function(a,b){var c=this;return g.call(this,a,b),c};return i.prototype=new g,i.prototype.constructor=i,i.prototype._search=function(a,b){function c(a,c){var f=this;e.processFns.edge&&e.processFns.edge.call(e,f,c,b),b.discovered[a.name]||(b.edges.push({source:f.name,target:a.name}),d(a))}function d(a){b.discovered[a.name]=!0,e.processFns.vertexEarly&&e.processFns.vertexEarly.call(e,a,b),b.entryTimes[a.name]=f++,e.graph.eachAdjacent(a,c),e.processFns.vertexLate&&e.processFns.vertexLate.call(e,a,b),b.exitTimes[a.name]=f++}b=b||{discovered:{},edges:[],entryTimes:{},exitTimes:{}};var e=this,f=0;return d(a),b},window.Graph=e,e.prototype.init=function(a){a=a||{};var b=this;return b.allowReflexiveEdges=a.allowReflexiveEdges||!1,b.vertices={},b.numEdges=0,b},e.prototype.read=function(a){if(!a)return this;var b=this;return a.hasOwnProperty("nodes")?b.readNodesAndLinks(a):a.hasOwnProperty("vertices")?b.readVerticesAndEdges(a):b},e.prototype.readNodesAndLinks=function(a){if(!a||!a.hasOwnProperty("nodes"))return this;var b=this;return a.nodes.forEach(function(a){b.createVertex(a.name,a.data)}),(a.links||[]).forEach(function(c){var d=a.nodes[c.source].name,e=a.nodes[c.target].name;b.createEdge(d,e,b.directed)}),b},e.prototype.readVerticesAndEdges=function(a){if(!a||!a.hasOwnProperty("vertices"))return this;var b=this;return a.vertices.forEach(function(a){b.createVertex(a.name,a.data)}),(a.edges||[]).forEach(function(a){b.createEdge(a.source,a.target,b.directed)}),b},e.prototype.createVertex=function(a,b){return this.vertices[a]?this.vertices[a]:this.vertices[a]=new d(a,b)},e.prototype.createEdge=function(a,b,d,e){var f=a===b;if(!this.allowReflexiveEdges&&f)return null;if(sourceVertex=this.vertices[a],targetVertex=this.vertices[b],!sourceVertex||!targetVertex)return null;var g=this,h=new c(a,b,e);return sourceVertex.edges[b]=h,sourceVertex.degree+=1,g.numEdges+=1,f||d||g.createEdge(b,a,!0),h},e.prototype.edges=function(a){return Array.prototype.concat.apply([],this.eachVertex(function(b){return b.eachEdge(a)}))},e.prototype.eachVertex=function(a){return b(this.vertices,a)},e.prototype.adjacent=function(a){var c=this;return b(a.edges,function(a){return c.vertices[a.target]})},e.prototype.eachAdjacent=function(a,c){var d=this;return b(a.edges,function(b){var e=d.vertices[b.target];return c.call(a,e,b)})},e.prototype.print=function(){var a=this;return console.log("Graph has "+Object.keys(a.vertices).length+" vertices"),a.eachVertex(function(a){console.log(a.toString()),a.eachEdge(function(a){console.log(" "+a)})}),a},e.prototype.toDOT=function(){var a=this,b=[];return b.push("graph bler {"),a.edges(function(a){b.push(" "+a.from+" -- "+a.to+";")}),b.push("}"),b.join("\n")},e.prototype.toNodesAndLinks=function(){var a=this,b={};return{nodes:a.eachVertex(function(a,c,d){return b[a.name]=d,a.toJSON()}),links:a.edges(function(a){var c=a.toJSON();return c.source=b[a.source],c.target=b[a.target],c})}},e.prototype.toVerticesAndEdges=function(){var a=this;return{vertices:a.eachVertex(function(a){return a.toJSON()}),edges:a.edges(function(a){return a.toJSON()})}},e.prototype.breadthFirstSearch=function(a){return new h(this).search(a)},e.prototype.breadthFirstSearchTree=function(a){return new h(this).searchTree(a)},e.prototype.depthFirstSearch=function(a){return new i(this).search(a)},e.prototype.depthFirstSearchTree=function(a){return new i(this).searchTree(a)},e.prototype.weakComponents=function(){function a(a){var e=new i(d)._search(a);return b=b.filter(function(a){return!(a in e.discovered)}),{vertices:Object.keys(e.discovered).map(function(a){return c.vertices[a].toJSON()}),edges:e.edges.map(function(a){var b=void 0!==c.vertices[a.target].edges[a.source];if(c.directed&&b){var d=a.source;a.source=a.target,a.target=d}return a})}}var b,c=this,d=this,f=[];for(c.directed&&(d=new e(!1,c.toNodesAndLinks())),b=Object.keys(d.vertices);b.length;){var g=d.vertices[b.shift()];f.push(a(g))}return f},e.prototype.weakComponentGraph=function(){var a=this.weakComponents();return new e(this.directed,{vertices:a.reduce(function(a,b){return a.concat(b.vertices)},[]),edges:a.reduce(function(a,b){return a.concat(b.edges)},[])})},e.prototype.weakComponentGraphArray=function(){var a=this;return this.weakComponents().map(function(b){return new e(a.directed,b)})},{Vertex:d,Edge:c,BreadthFirstSearch:h,DepthFirstSearch:i,Graph:e,randGraph:f}});
|
|
//# sourceMappingURL=../../maps/utils/graph.js.map
|