UndirNode.tpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2016, University of Freiburg,
  2. // Chair of Algorithms and Data Structures.
  3. // Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
  4. // _____________________________________________________________________________
  5. template <typename N, typename E>
  6. UndirNode<N, E>::UndirNode() : _pl() {
  7. }
  8. // _____________________________________________________________________________
  9. template <typename N, typename E>
  10. UndirNode<N, E>::UndirNode(const N& pl) : _pl(pl) {
  11. }
  12. // _____________________________________________________________________________
  13. template <typename N, typename E>
  14. UndirNode<N, E>::~UndirNode() {
  15. // delete self edges
  16. for (auto e = _adjList.begin(); e != _adjList.end();) {
  17. Edge<N, E>* eP = *e;
  18. if (eP->getTo() == this && eP->getFrom() == this) {
  19. e = _adjList.erase(e);
  20. delete eP;
  21. } else {
  22. e++;
  23. }
  24. }
  25. for (auto e = _adjList.begin(); e != _adjList.end(); e++) {
  26. Edge<N, E>* eP = *e;
  27. if (eP->getTo() != this) {
  28. eP->getTo()->removeEdge(eP);
  29. }
  30. if (eP->getFrom() != this) {
  31. eP->getFrom()->removeEdge(eP);
  32. }
  33. delete eP;
  34. }
  35. }
  36. // _____________________________________________________________________________
  37. template <typename N, typename E>
  38. bool UndirNode<N, E>::hasEdgeIn(const Edge<N, E>* e) const {
  39. return hasEdge(e);
  40. }
  41. // _____________________________________________________________________________
  42. template <typename N, typename E>
  43. bool UndirNode<N, E>::hasEdgeOut(const Edge<N, E>* e) const {
  44. return hasEdge(e);
  45. }
  46. // _____________________________________________________________________________
  47. template <typename N, typename E>
  48. bool UndirNode<N, E>::hasEdge(const Edge<N, E>* e) const {
  49. return e->getFrom() == this || e->getTo() == this;
  50. }
  51. // _____________________________________________________________________________
  52. template <typename N, typename E>
  53. bool UndirNode<N, E>::adjContains(const Edge<N, E>* e) const {
  54. for (size_t i = 0; i < _adjList.size(); i++) if (_adjList[i] == e) return true;
  55. return false;
  56. }
  57. // _____________________________________________________________________________
  58. template <typename N, typename E>
  59. void UndirNode<N, E>::addEdge(Edge<N, E>* e) {
  60. if (adjContains(e)) return;
  61. _adjList.reserve(_adjList.size() + 1);
  62. _adjList.push_back(e);
  63. }
  64. // _____________________________________________________________________________
  65. template <typename N, typename E>
  66. void UndirNode<N, E>::removeEdge(Edge<N, E>* e) {
  67. auto p = std::find(_adjList.begin(), _adjList.end(), e);
  68. if (p != _adjList.end()) _adjList.erase(p);
  69. }
  70. // _____________________________________________________________________________
  71. template <typename N, typename E>
  72. const std::vector<Edge<N, E>*>& UndirNode<N, E>::getAdjList() const {
  73. return _adjList;
  74. }
  75. // _____________________________________________________________________________
  76. template <typename N, typename E>
  77. const std::vector<Edge<N, E>*>& UndirNode<N, E>::getAdjListOut() const {
  78. return _adjList;
  79. }
  80. // _____________________________________________________________________________
  81. template <typename N, typename E>
  82. const std::vector<Edge<N, E>*>& UndirNode<N, E>::getAdjListIn() const {
  83. return _adjList;
  84. }
  85. // _____________________________________________________________________________
  86. template <typename N, typename E>
  87. size_t UndirNode<N, E>::getDeg() const {
  88. return _adjList.size();
  89. }
  90. // _____________________________________________________________________________
  91. template <typename N, typename E>
  92. size_t UndirNode<N, E>::getInDeg() const {
  93. return getDeg();
  94. }
  95. // _____________________________________________________________________________
  96. template <typename N, typename E>
  97. size_t UndirNode<N, E>::getOutDeg() const {
  98. return getDeg();
  99. }
  100. // _____________________________________________________________________________
  101. template <typename N, typename E>
  102. N& UndirNode<N, E>::pl() {
  103. return _pl;
  104. }
  105. // _____________________________________________________________________________
  106. template <typename N, typename E>
  107. const N& UndirNode<N, E>::pl() const {
  108. return _pl;
  109. }