import { Edge, Node, Keyed } from "../../interfaces"; declare type NamedBindings = Keyed; /** * The graph constructor. It initializes the data and the indexes, and binds * the custom indexes and methods to its own scope. * * Recognized parameters: * ********************** * Here is the exhaustive list of every accepted parameters in the settings * object: * * {boolean} clone Indicates if the data have to be cloned in methods * to add nodes or edges. * {boolean} immutable Indicates if nodes "id" values and edges "id", * "source" and "target" values must be set as * immutable. * * @param {?configurable} settings Eventually a settings function. * @return {graph} The new graph instance. */ declare class Graph { settings: (key: string) => any; /** * MAIN DATA: * ********** */ nodesArray: Node[]; edgesArray: Edge[]; /** * GLOBAL INDEXES: * *************** * These indexes just index data by ids. */ nodesIndex: Keyed; edgesIndex: Keyed; /** * LOCAL INDEXES: * ************** * These indexes refer from node to nodes. Each key is an id, and each * value is the array of the ids of related nodes. */ inNeighborsIndex: Keyed>>; outNeighborsIndex: Keyed>>; allNeighborsIndex: Keyed>>; inNeighborsCount: Keyed; outNeighborsCount: Keyed; allNeighborsCount: Keyed; constructor(settings?: any); /** * This method adds a node to the graph. The node must be an object, with a * string under the key "id". Except for this, it is possible to add any * other attribute, that will be preserved all along the manipulations. * * If the graph option "clone" has a truthy value, the node will be cloned * when added to the graph. Also, if the graph option "immutable" has a * truthy value, its id will be defined as immutable. * * @param {object} node The node to add. * @return {object} The graph instance. */ addNode(node: Partial): this; /** * This method adds an edge to the graph. The edge must be an object, with a * string under the key "id", and strings under the keys "source" and * "target" that design existing nodes. Except for this, it is possible to * add any other attribute, that will be preserved all along the * manipulations. * * If the graph option "clone" has a truthy value, the edge will be cloned * when added to the graph. Also, if the graph option "immutable" has a * truthy value, its id, source and target will be defined as immutable. * * @param {object} edge The edge to add. * @return {object} The graph instance. */ addEdge(edge: Partial): this; /** * This method drops a node from the graph. It also removes each edge that is * bound to it, through the dropEdge method. An error is thrown if the node * does not exist. * * @param {string} id The node id. * @return {object} The graph instance. */ dropNode(id: string): this; /** * This method drops an edge from the graph. An error is thrown if the edge * does not exist. * * @param {string} id The edge id. * @return {object} The graph instance. */ dropEdge(id: string): this; /** * This method destroys the current instance. It basically empties each index * and methods attached to the graph. */ kill(): void; /** * This method empties the nodes and edges arrays, as well as the different * indexes. * * @return {object} The graph instance. */ clear(): this; /** * This method reads an object and adds the nodes and edges, through the * proper methods "addNode" and "addEdge". * * Here is an example: * * > var myGraph = new graph(); * > myGraph.read({ * > nodes: [ * > { id: 'n0' }, * > { id: 'n1' } * > ], * > edges: [ * > { * > id: 'e0', * > source: 'n0', * > target: 'n1' * > } * > ] * > }); * > * > console.log( * > myGraph.nodes().length, * > myGraph.edges().length * > ); // outputs 2 1 * * @param {object} g The graph object. * @return {object} The graph instance. */ read(g: { nodes?: Partial[]; edges?: Partial[]; }): this; /** * This methods returns one or several nodes, depending on how it is called. * * To get the array of nodes, call "nodes" without argument. To get a * specific node, call it with the id of the node. The get multiple node, * call it with an array of ids, and it will return the array of nodes, in * the same order. * * @param {?(string|array)} ids Eventually one id, an array of ids. * @return {object|array} The related node or array of nodes. */ nodes(...ids: string[]): Node[]; /** * This methods returns the degree of one or several nodes, depending on how * it is called. It is also possible to get incoming or outcoming degrees * instead by specifying 'in' or 'out' as a second argument. * * @param {string|array} v One id, an array of ids. * @param {?string} which Which degree is required. Values are 'in', * 'out', and by default the normal degree. * @return {number|array} The related degree or array of degrees. */ degree(v: string | string[], inWhich?: "in" | "out"): number | number[]; /** * This methods returns one or several edges, depending on how it is called. * * To get the array of edges, call "edges" without argument. To get a * specific edge, call it with the id of the edge. The get multiple edge, * call it with an array of ids, and it will return the array of edges, in * the same order. * * @param {?(string|array)} ids Eventually one id, an array of ids. * @return {array} The related edge or array of edges. */ edges(...ids: string[]): Edge[]; /** * This global method adds a method that will be bound to the futurly created * graph instances. * * Since these methods will be bound to their scope when the instances are * created, it does not use the prototype. Because of that, methods have to * be added before instances are created to make them available. * * Here is an example: * * > Graph.enableHooks('getNodesCount', function() { * > return this.nodesArray.length; * > }); * > * > var myGraph = new graph(); * > console.log(myGraph.getNodesCount()); // outputs 0 * * @param {string} methodName The name of the method. * @param {function} fn The method itself. * @return {object} The global graph constructor. */ static addMethod(methodName: string, fn: Function): void; /** Adds hook infrastructure for a method already specified on the graph class */ static __addOwnMethod(methodName: string): void; /** * This global method returns true if the method has already been added, and * false else. * * Here are some examples: * * > graph.hasMethod('addNode'); // returns true * > graph.hasMethod('hasMethod'); // returns true * > graph.hasMethod('unexistingMethod'); // returns false * * @param {string} methodName The name of the method. * @return {boolean} The result. */ static hasMethod(methodName: string): boolean; /** * This global methods attaches a function to a method. Anytime the specified * method is called, the attached function is called right after, with the * same arguments and in the same scope. The attached function is called * right before if the last argument is true, unless the method is the graph * constructor. * * To attach a function to the graph constructor, use 'constructor' as the * method name (first argument). * * The main idea is to have a clean way to keep custom indexes up to date, * for instance: * * > var timesAddNodeCalled = 0; * > graph.attach('addNode', 'timesAddNodeCalledInc', function() { * > timesAddNodeCalled++; * > }); * > * > var myGraph = new graph(); * > console.log(timesAddNodeCalled); // outputs 0 * > * > myGraph.addNode({ id: '1' }).addNode({ id: '2' }); * > console.log(timesAddNodeCalled); // outputs 2 * * The idea for calling a function before is to provide pre-processors, for * instance: * * > var colorPalette = { Person: '#C3CBE1', Place: '#9BDEBD' }; * > graph.attach('addNode', 'applyNodeColorPalette', function(n) { * > n.color = colorPalette[n.category]; * > }, true); * > * > var myGraph = new graph(); * > myGraph.addNode({ id: 'n0', category: 'Person' }); * > console.log(myGraph.nodes('n0').color); // outputs '#C3CBE1' * * @param {string} methodName The name of the related method or * "constructor". * @param {string} key The key to identify the function to attach. * @param {function} fn The function to bind. * @param {boolean} before If true the function is called right before. * @return {object} The global graph constructor. */ static attach(methodName: string, key: string, fn: Function, before?: boolean): typeof Graph; /** * Alias of attach(methodName, key, fn, true). */ static attachBefore(methodName: string, key: string, fn: Function): typeof Graph; /** * This methods is just an helper to deal with custom indexes. It takes as * arguments the name of the index and an object containing all the different * functions to bind to the methods. * * Here is a basic example, that creates an index to keep the number of nodes * in the current graph. It also adds a method to provide a getter on that * new index: * * > sigma.classes.graph.addIndex('nodesCount', { * > constructor: function() { * > this.nodesCount = 0; * > }, * > addNode: function() { * > this.nodesCount++; * > }, * > dropNode: function() { * > this.nodesCount--; * > } * > }); * > * > sigma.classes.Graph.addMethod('getNodesCount', function() { * > return this.nodesCount; * > }); * > * > var myGraph = new sigma.classes.graph(); * > console.log(myGraph.getNodesCount()); // outputs 0 * > * > myGraph.addNode({ id: '1' }).addNode({ id: '2' }); * > console.log(myGraph.getNodesCount()); // outputs 2 * * @param {string} name The name of the index. * @param {object} bindings The object containing the functions to bind. * @return {object} The global graph constructor. */ static addIndex(name: string, bindings: NamedBindings): typeof Graph; } export default Graph;