/usr/include/tulip/vectorgraph.h is in libtulip-dev 4.4.0dfsg2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 | /*
*
* This file is part of Tulip (www.tulip-software.org)
*
* Authors: David Auber and the Tulip development Team
* from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
*
* Tulip is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tulip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
*/
///@cond DOXYGEN_HIDDEN
#ifndef VECTORGRAPH_H
#define VECTORGRAPH_H
#include <vector>
#include <algorithm>
#include <set>
#include <cassert>
#include <tulip/tulipconf.h>
#include <tulip/Node.h>
#include <tulip/Edge.h>
#include <tulip/vectorgraphproperty.h>
namespace tlp {
template<class itType >
struct Iterator;
//===========================================
/**
* @class VectorGraph
*
* @brief That class provide a simple implementation of graph structure (without subgraphs, observer, metagraph)
* it enables to obtain very efficient access/modification time.
*
* User can both use tulip iterators or direct vector to access to the graph structure for better performance.
* To have maximum speedup, that Graph implementation use only vectors, almost all operations
* are done in constant time (even modification), however since the class use vectors, modification
* of adjacency can change the ordering of edges around nodes. If you use it only for standard
* graph operations there is no problem. However if you want to manipulate maps, be aware that
* a modification can change the graph embedding. EdgeOrdering function can be used to reorder
* correctly elements when necessary.
*
* @warning the class is not thread safe
* @warning modification of the graph structure devalidate iterations.
*
* @warning Use that class only if you need performance.
* @todo split the file in .h .cpp
*/
class TLP_SCOPE VectorGraph {
public:
//=======================================================
VectorGraph();
//=======================================================
~VectorGraph();
//=======================================================
/**
* @brief delete all nodes/edges and Properties of the graph
* @remark o(N + E + NbProp)
*/
void clear();
//=======================================================
/**
* @brief Test if an edge exist between two nodes, in directed mode the orientation
* is taken into account.
* @return the found edge, else an invalid edge.
* @remark o(min(deg(src), deg(tgt))
* @todo test
*/
edge existEdge(const node src, const node tgt, const bool directed = true) const;
//=======================================================
/**
* @brief Return true if n belongs to the graph
* @remark o(1)
*/
bool isElement(const node n) const;
//=======================================================
/**
* @brief Return true if e belongs to the graph
* @remark o(1)
*/
bool isElement(const edge e) const;
//=======================================================
/**
* \brief Set the ordering of edges around n according to their order in v.
* \warning there is no test here, all edge in v must be element of star(e)
* @remark o(v.size())
*/
void setEdgeOrder(const node n, const std::vector<edge> &v );
//=======================================================
/**
* \brief swap to edge in the ordered adjacency vector
* \warning the two edges must be element of star(v)
* @remark o(1)
*/
void swapEdgeOrder(const node n, const edge e1, const edge e2);
//=======================================================
/**
* @brief Enables to reserve memory for nbNodes
* Reserving memory before to addNode enable to reduce the number of vector resizing and then
* to speed up significantly construction of graphs.
* @remark o(N + nbNodes)
*/
void reserveNodes(const size_t nbNodes);
//=======================================================
/**
* @brief Enables to reserve memory for nbEdges
* Reserving memory before to addEdge enable to reduce the number of vector resizing and then
* to speed up significantly construction of graphs.
* @remark o(N + nbNodes)
*/
void reserveEdges(const size_t nbEdges);
//=======================================================
/**
* @brief Enables to reserve memory for adjacency nodes
* Reserving memory before to addEdge enable to reduce the number of vector resizing and then
* to speed up significantly construction of graphs.
* @remark o(E + nbEdges)
*/
void reserveAdj(const size_t nbEdges);
//=======================================================
/**
* @brief Enables to reserve memory for adjacency nodes
* Reserving memory before to addEdge enable to reduce the number of vector resizing and then
* to speed up significantly construction of graphs.
* @remark o(E + nbEdges)
*/
void reserveAdj(const node n, const size_t nbEdges);
//=======================================================
/**
* @brief Return the node at position id in the array of nodes
* @remark o(1)
*/
node operator[](const unsigned int id) const;
//=======================================================
/**
* @brief Return the edge at position id in the array of nodes
* @remark o(1)
*/
edge operator()(const unsigned int id) const;
//=======================================================
/**
* @brief Return the first node of graph (ie first node in the array of nodes)
* @remark o(1)
*/
node getOneNode() const;
//=======================================================
/**
* @brief Return a Tulip Iterator on nodes of the graph
* @warning: The returned iterator should be deleted by the caller to prevent memory leaks
* @remark: o(1)
*/
Iterator<node> * getNodes() const;
//=======================================================
/**
* @brief Return a Tulip Iterator on edges of the graph
* @warning: The returned iterator should be deleted by the caller to prevent memory leaks
* @remark: o(1)
*/
Iterator<edge> * getEdges() const;
//=======================================================
/**
* @brief Return a Tulip Iterator on adjacent edges of the node n
* @warning: The returned iterator should be deleted by the caller to prevent memory leaks
* @remark: o(1)
*/
Iterator<edge> * getInOutEdges(const node n) const;
//=======================================================
/**
* @brief Return a Tulip Iterator on out edges of the node n
* @warning: The returned iterator should be deleted by the caller to prevent memory leaks
* @remark: o(1)
*/
Iterator<edge> * getOutEdges(const node n) const;
//=======================================================
/**
* @brief Return a Tulip Iterator on in edges of the node n
* @warning: The returned iterator should be deleted by the caller to prevent memory leaks
* @remark: o(1)
*/
Iterator<edge> * getInEdges(const node n) const;
//=======================================================
/**
* @brief Return a Tulip Iterator on adjacent nodes of the node n
* @warning: The returned iterator should be deleted by the caller to prevent memory leaks
* @remark: o(1)
*/
Iterator<node> * getInOutNodes(const node n) const;
//=======================================================
/**
* @brief Return a Tulip Iterator on in nodes of the node n
* @warning: The returned iterator should be deleted by the caller to prevent memory leaks
* @remark o(1)
*/
//=======================================================
Iterator<node> * getInNodes(const node n) const;
/**
* @brief Return a Tulip Iterator on out nodes of the node n
* @warning: The returned iterator should be deleted by the caller to prevent memory leaks
* @remark o(1)
*/
//=======================================================
Iterator<node> * getOutNodes(const node n) const;
//=======================================================
/**
* @brief Return the degree of a node
* @remark o(1)
*/
unsigned int deg(const node n) const;
//=======================================================
/**
* @brief Return the out degree of a node
* @remark o(1)
*/
unsigned int outdeg(const node n) const;
//=======================================================
/**
* @brief Return the in degree of a node
* @remark o(1)
*/
unsigned int indeg(const node n) const;
//=======================================================
/**
* @brief Return the number of edges in the graph
* @remark: o(1)
*/
unsigned int numberOfEdges() const;
//=======================================================
/**
* @brief Return the number of nodes in the graph
* @remark: o(1)
*/
unsigned int numberOfNodes() const;
//=======================================================
/**
* @brief Add a new node in the graph and return it
* @warning: That operation modify the array of nodes
* and thus devalidate all iterators on it.
* @remark: o(1)
*/
node addNode();
//=======================================================
/**
* @brief Delete a node and all its adjacent edges in the graph
* @warning That operation modify the array of nodes and the array of edges
* and thus devalidate all iterators on it.
* @warning That operation modify the array of neighboors of extrmities of edges, thus
* it devalidates iterators on adjacency for the nodes at the extremities od the deleted edges.
* @warning Orders of edges in the extremities of the deleted edges are affected
* @remark: o(1)
*/
void delNode(const node n);
//=======================================================
/**
* @brief Add a new edge between src and tgt and return it
* @warning That operation modify the array of neighboors of extrmities of edges, thus
* it devalidates iterators on adjacency for the nodes at the extremities od the deleted edges.
* @remark o(1)
*/
edge addEdge(const node src, const node tgt);
//=======================================================
/**
* @brief Delete an edge in the graph
* @warning: That operation modify the array of edges
* and thus devalidate all iterators on it.
* @warning That operation modify the array of neighboors of extremities of the edge e, thus
* it devalidates iterators on adjacency for the nodes at the extremities od the deleted edge.
* @warning Orders of edges in the extremities of the deleted edge are affected
* @remark o(1)
*/
void delEdge(const edge e);
//=======================================================
/**
* @brief Delete all adjacent edges (in/out) of a node
* @warning: That operation modify the array of edges
* and thus devalidate all iterators on it.
* @warning That operation modify the array of neighboors of extremities of the edge e, thus
* it devalidates iterators on adjacency for the nodes at the extremities od the deleted edge.
* @warning Orders of edges in the extremities of the deleted edge are affected
* @remark o(deg(V))
* @todo test
*/
void delEdges(const node n);
//=======================================================
/**
* @brief Delete all edges in the graph
* @warning: That operation modify the array of edges and all arrays of nodes
* and thus devalidate all iterators, only graph nodes iterators are not affected.
* @remark o(E + V)
*/
void delAllEdges();
//=======================================================
/**
* @brief Delete all nodes in the graph
* @warning: That operation modify the array of edges and all arrays of nodes
* and thus devalidate all iterators.
* @remark o(E + V)
*/
void delAllNodes();
//=======================================================
/**
* @brief return the first extremity (considered as source if the graph is directed) of an edge
* @remark o(1)
*/
node source(const edge e) const;
//=======================================================
/**
* @brief return the second extremity (considered as target if the graph is directed) of an edge
* @remark o(1)
*/
node target(const edge e) const;
//=======================================================
/**
* @brief return the opposite node of n through edge e
* @remark o(1)
*/
node opposite(const edge e, const node n) const;
//=======================================================
/**
* @brief Reverse an edge e, source become target and target become soure
* @remark o(1)
*/
void reverse(const edge e);
//=======================================================
/**
* @brief change the source of an edge
* @warning That operation modify the array of neighboors of extrmities of edges, thus
* it devalidates iterators on adjacency for the nodes at the extremities of the modified edges and nodes.
* @remark o(1)
* \see setEnds
*/
void setSource(const edge e, const node n);
//=======================================================
/**
* @brief change the target of an edge
* @warning That operation modify the array of neighboors of extrmities of edges, thus
* it devalidates iterators on adjacency for the nodes at the extremities of the modified edges and nodes.
* @remark o(1)
* \see setEnds
*/
void setTarget(const edge e, const node n);
//=======================================================
/**
* @brief Return the extremities of an edge (src, target)
* @remark o(1)
*/
std::pair<node, node> ends(const edge e) const;
//=======================================================
/**
* @brief Reconnect the edeg e to have the new given extremities
* @warning That operation modify the array of neighboors of extrmities of edges, thus
* it devalidates iterators on adjacency for the nodes at the extremities of the modified edges and nodes.
* @remark o(1)
*/
void setEnds(const edge e, const node src, const node tgt);
//=======================================================
/**
* @brief Shuffle the array of graph nodes
* @remark dependant of stl::random_shuffle algorithm (should be o(N))
*/
void shuffleNodes();
//=======================================================
/**
* @brief Shuffle the array of graph edges
* @remark dependant of stl::random_shuffle algorithm (should be o(E))
*/
void shuffleEdges();
//=======================================================
/**
* @brief Sort all edges according to comparison functor given in parameter
* if stable is true a stable sort algorithm is applied
* Comparison should be an instance of a class wihch implements operator():
* @remark dependant of stl::sort and stl::stable_sort algorithm (should be o(E log (E)))
* @code
* class Compare {
* //return true if a < b
* bool operator()(const edge a, const edge b);
* };
* @endcode
* \warning that function is not compatible with the Tulip Graph API
*/
template<typename Compare>
void sortEdges(Compare cmp, bool stable = false) {
if (stable)
stable_sort(_edges.begin(), _edges.end(), cmp);
else
sort(_edges.begin(), _edges.end(), cmp);
//recompute indices of edges
for (unsigned int i = 0; i < _edges.size(); ++i) {
_eData[_edges[i]]._edgesId = i;
}
}
//=======================================================
/**
* @brief Sort all nodes according to comparison functor given in parameter
* if stable is true a stable sort algorithm is applied
* Comparison should be an instance of a class wihch implements operator():
* @code
* class Compare {
* //return true if a < b
* bool operator()(const node a, const node b);
* };
* @endcode
* @remark dependant of stl::sort and stl::stable_sort algorithm (should be o(N log (N)))
* \warning that function is not compatible with the Tulip Graph API
*/
template<typename Compare >
void sortNodes(Compare cmp, bool stable = false) {
if (stable)
stable_sort(_nodes.begin(), _nodes.end(), cmp);
else
sort(_nodes.begin(), _nodes.end(), cmp);
//recompute indices of edges
for (unsigned int i = 0; i < _nodes.size(); ++i) {
_nData[_nodes[i]]._nodesId = i;
}
}
//=======================================================
/**
* @brief return the position of an edge in the array of edges.
* \warning that function is not compatible with the Tulip Graph API
* @remark o(1)
*/
unsigned int edgePos(const edge e) const;
//=======================================================
/**
* @brief return the position of a node in the array of nodes.
* \warning that function is not compatible with the Tulip Graph API
* @remark o(1)
*/
unsigned int nodePos(const node n) const;
//=======================================================
/**
* @brief Swap two nodes in the array of graph nodes
* @remark o(1)
* \warning that function is not compatible with the Tulip Graph API
*/
void swap(const node a, const node b);
//=======================================================
/**
* @brief Swap two edges in the array of graph edge
* @remark o(1)
* \warning that function is not compatible with the Tulip Graph API
*/
void swap(const edge a, const edge b);
//=======================================================
/**
* @brief Create a new node array of type TYPE
* NodesAttr can be copied in constant time it is just a pointer
* NodesAttr is not a smart pointer it must be deleted with freeNodesAttribute
* @remark o(log(number of arrays) + new of a vector<TYPE> of size N)
* \warning that function is not compatible with the Tulip Graph API
*/
template<typename TYPE>
void alloc(NodeProperty<TYPE> &prop) {
ValArray<TYPE> *array = new ValArray<TYPE>(_nodes.size() + _freeNodes.size(), _nodes.capacity());
_nodeArrays.insert(array);
prop = NodeProperty<TYPE>(array, this);
}
//=======================================================
/**
* @brief Delete an Array from the set of node arrays
* @warning all copy of the ValArray are no more valid (serious bug if they are used after)
* @remark o(log(number of arrays) + free of a vector<TYPE> of size N)
* \warning that function is not compatible with the Tulip Graph API
*/
template<typename TYPE>
void free(NodeProperty<TYPE> array) {
assert(_nodeArrays.find(array._array) != _nodeArrays.end());
delete array._array;
_nodeArrays.erase(array._array);
}
//=======================================================
/**
* @brief Create a new edge array of type TYPE
* EdgesAttr can be copied in constant time it is just a pointer
* EdgesAttr is not a smart pointer it must be deleted with freeEdgesAttribute
* @remark o(log(number of node arrays) + new of a vector<TYPE> of size E)
* \warning that function is not compatible with the Tulip Graph API
*/
template<typename TYPE>
void alloc(EdgeProperty<TYPE> &prop) {
ValArray<TYPE> *array = new ValArray<TYPE>(_edges.size() + _freeEdges.size(), _edges.capacity());
_edgeArrays.insert(array);
prop = EdgeProperty<TYPE>(array, this);
}
//=======================================================
/**
* @brief Delete an Array from the set of edge arrays
* @warning all copy of the ValArray are no more valid (serious bug if they are used after)
* @remark o(log(number of edge arrays) + free of a vector<TYPE> of size E)
* \warning that function is not compatible with the Tulip Graph API
*/
template<typename TYPE>
void free(EdgeProperty<TYPE> array) {
assert(_edgeArrays.find(array._array) != _edgeArrays.end());
delete array._array;
_edgeArrays.erase(array._array);
}
//=======================================================
/**
* @brief Return a const reference on the vector of adjacent nodes of n
*
* It is the fastest way to access to node adjacency, Iterators are 25% slower.
* \warning code that use that function won't be compatible with Tulip Graph API
*
* @remark o(1)
* \see getInOutNodes
* \see getInNodes
* \see getOutNodes
*/
const std::vector<node>& adj(const node n) const;
//=======================================================
/**
* @brief Return a const reference on the vector of adjacent edges of n
*
* It is the fastest way to access to edge adjacency, Iterators are 25% slower.
* \warning code that use that function won't be compatible with Tulip Graph API
*
* @remark o(1)
* \see getInOutEdges
* \see getInEdges
* \see getOutEdges
*/
const std::vector<edge>& star(const node n) const;
//=======================================================
/**
* @brief Return a const reference on the vector of nodes of the graph
* It is the fastest way to access to edge adjacency, Iterators are 25% slower.
* \warning code that use that function won't be compatible with Tulip Graph API
* @remark o(1)
*/
const std::vector<node>& nodes() const;
//=======================================================
/**
* @brief Return a const reference on the vector of edges of the graph
* It is the fastest way to access to edge adjacency, Iterators are 25% slower.
* \warning code that use that function won't be compatible with Tulip Graph API
* @remark o(1)
*/
const std::vector<edge>& edges() const;
//=======================================================
#ifndef NDEBUG
/**
* these two function are used internally to insure that property has been allocated in debug mode
* @warning never used these function directly even in debug mode !!!
*/
template<typename TYPE>
bool isNodeAttr(ValArray<TYPE> *array) {
return (_nodeArrays.find(array) != _nodeArrays.end());
}
template<typename TYPE>
bool isEdgeAttr(ValArray<TYPE> *array) {
return (_edgeArrays.find(array) != _edgeArrays.end());
}
#endif
//=============================================================
/**
* output the graph in a very simple way for debugging
*/
void dump() const;
//=============================================================
/**
* internal function to test the integrity of the graph
*/
void integrityTest();
private:
struct _iNodes {
_iNodes(unsigned int id = UINT_MAX): _nodesId(id), _outdeg(0) {
}
void clear() {
_outdeg = 0;
_adjt.resize(0);
_adjn.resize(0);
_adje.resize(0);
}
void addEdge(bool t, node n, edge e) {
_adjt.push_back(t);
_adjn.push_back(n);
_adje.push_back(e);
}
unsigned int _nodesId; /** index of a node in the _nodes vector*/
unsigned int _outdeg; /** out degree of nodes */
std::vector<bool> _adjt; /** orientation of the edge, used to separate in and out edges/nodes */
std::vector<node> _adjn; /** inout nodes*/
std::vector<edge> _adje; /** inout edges*/
};
struct _iEdges {
unsigned int _edgesId; /** index of a node in the _edges vector*/
std::pair<node, node> _edgeExtremities; /** source and target of an edge */
std::pair<unsigned int, unsigned int> _edgeExtremitiesPos; /** source and target of an edge */
};
std::vector<_iNodes> _nData; /** internal storage of nodes */
std::vector<_iEdges> _eData; /** internal storage of edges */
std::vector< node > _nodes; /** vector of nodes element of the graph */
std::vector< edge > _edges; /** vector of edges element of the graph */
std::vector<node> _freeNodes; /** vector of nodes that has been deleted and that can be reused */
std::vector<edge> _freeEdges; /** vector of edges that has been deleted and that can be reused */
std::set<ValArrayInterface *> _nodeArrays; /** set of all node properties allocated on that graph */
std::set<ValArrayInterface *> _edgeArrays; /** set of all edge properties allocated on that graph */
//=======================================================
/**
* internal function to break the program and output debug information during
* integrity test.
* @TODO hide that function in the .cpp file
*/
void testCond(std::string str, bool b);
//=======================================================
/**
* internal function to adjust size of node properties when graph is modified
*/
void addNodeToArray(node n);
//=======================================================
/**
* internal function to adjust size of edge properties when graph is modified
*/
void addEdgeToArray(edge e);
//=======================================================
/**
* internal function to remove an edge
*/
void removeEdge(edge e);
//=======================================================
/**
* Internal function to remove the edge e in the adjacency list of n
*/
void moveEdge(node n, unsigned int a, unsigned int b);
/**
* Internal function tp remove the edge e in the adjacency list of n
*/
void partialDelEdge(node n, edge e);
//=======================================================
};
#ifndef NDEBUG //these two function are used to insure that property has been allocated in debug mode
template <typename TYPE>
bool NodeProperty<TYPE>::isValid() const {
if (this->_graph == 0) return false;
if (this->_array == 0) return false;
return this->_graph->isNodeAttr(this->_array);
}
template <typename TYPE>
bool EdgeProperty<TYPE>::isValid() const {
if (this->_graph == 0) return false;
if (this->_array == 0) return false;
return this->_graph->isEdgeAttr(this->_array);
}
#endif
}
#endif // VECTORGRAPH_H
///@endcond
|