Guest User

Node.cpp

a guest
May 16th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. #include "Node.h"
  2.  
  3.  
  4. Node :: Node (string label)
  5. {
  6.     this->label = label;
  7. }
  8.  
  9. Node :: ~Node()
  10. {
  11.     while (!this->adjacencyList.empty())
  12.     {
  13.         Edge *temp = this->adjacencyList.front();
  14.         this->adjacencyList.erase (this->adjacencyList.begin());
  15.         delete temp;
  16.         temp = NULL;
  17.     }
  18. }
  19.  
  20. string Node :: getLabel()
  21. {
  22.     return this->label;
  23. }
  24.  
  25. void Node :: setLabel (string label)
  26. {
  27.     this->label = label;
  28. }
  29.  
  30. void Node :: addEdge (Edge *edge)
  31. {
  32.     for (int i = 0; i < this->adjacencyList.size(); ++i)
  33.     {
  34.         if (this->adjacencyList[i]->getAdjacent()->getLabel() == edge->getAdjacent()->getLabel())
  35.             throw EDGE_ALREADY_EXISTS;
  36.     }
  37.     this->adjacencyList.push_back (edge);
  38. }
  39.  
  40. void Node :: removeEdge (string destination)
  41. {
  42.     bool deleted = false;
  43.     cout << this->adjacencyList.empty() << endl;
  44.     Edge *t = this->adjacencyList[0];
  45.     cout << *t << endl;
  46.     if (!this->adjacencyList.empty())
  47.     {
  48.         for (vector <Edge *> :: iterator itr = this->adjacencyList.begin(); itr != this->adjacencyList.end(); ++itr)
  49.         {
  50.             if (((*itr)->getAdjacent())->getLabel() == destination)
  51.             {
  52.                 Edge *temp = *itr;
  53.                 this->adjacencyList.erase (itr);
  54.                 delete temp;
  55.                 deleted = true;
  56.             }
  57.         }
  58.     }
  59.     if (!deleted)
  60.         throw EDGE_DOES_NOT_EXIST;
  61. }
  62.  
  63. Node &operator+ (Node &l, Node &r) // merges two nodes - for network merging
  64. {
  65.     Node newNode (l.label);
  66.     for (int i = 0; i < l.adjacencyList.size(); ++i)
  67.     {
  68.         Edge *newEdge = new Edge (l.adjacencyList[i]->getAdjacent(), l.adjacencyList[i]->getWeight());
  69.         newNode.adjacencyList.push_back (newEdge);
  70.     }
  71.     for (int i = 0; i < r.adjacencyList.size(); ++i)
  72.     {
  73.         Edge *newEdge = new Edge (r.adjacencyList[i]->getAdjacent(), r.adjacencyList[i]->getWeight());
  74.         bool exists = false;
  75.         vector <Edge *> :: iterator itr;
  76.         for (itr = newNode.adjacencyList.begin(); itr != newNode.adjacencyList.end(); ++itr)
  77.         {
  78.             if ((*itr)->getAdjacent()->getLabel() == newEdge->getAdjacent()->getLabel())
  79.             {
  80.                 exists = true;
  81.                 break;
  82.             }
  83.         }
  84.         if (!exists)
  85.             newNode.adjacencyList.push_back (newEdge);
  86.         else
  87.         {
  88.             if ((*itr)->getWeight() < newEdge->getWeight())
  89.             {
  90.                 newNode.adjacencyList.erase (itr++);
  91.                 newNode.adjacencyList.insert (itr, newEdge);
  92.             }
  93.         }
  94.     }      
  95.     return newNode;
  96. }
  97.  
  98. bool operator== (Node &l, Node &r)
  99. {
  100.     if (l.label != r.label)
  101.         return false;
  102.     if (l.adjacencyList.size() != r.adjacencyList.size())
  103.         return false;
  104.     vector <Edge *> ltemp = l.adjacencyList;
  105.     vector <Edge *> rtemp = r.adjacencyList;
  106.     for (int i = 0; i < ltemp.size() - 1; i++)
  107.     {
  108.         for (int j = 0; j < ltemp.size() - 1 - i; j++)
  109.         {
  110.             if (ltemp[j]->getAdjacent()->getLabel() > ltemp[j + 1]->getAdjacent()->getLabel())
  111.             {
  112.                 Edge *temp = ltemp[j + 1];
  113.                 ltemp[j + 1] = ltemp[j];
  114.                 ltemp[j] = temp;
  115.             }
  116.             else if (ltemp[j]->getAdjacent()->getLabel() == ltemp[j + 1]->getAdjacent()->getLabel() && ltemp[j]->getWeight() > ltemp[j + 1]->getWeight())
  117.             {
  118.                 Edge *temp = ltemp[j + 1];
  119.                 ltemp[j + 1] = ltemp[j];
  120.                 ltemp[j] = temp;
  121.             }
  122.         }
  123.     }
  124.     for (int i = 0; i < rtemp.size() - 1; i++)
  125.     {
  126.         for (int j = 0; j < rtemp.size() - 1 - i; j++)
  127.         {
  128.             if (rtemp[j]->getAdjacent()->getLabel() > rtemp[j + 1]->getAdjacent()->getLabel())
  129.             {
  130.                 Edge *temp = rtemp[j + 1];
  131.                 rtemp[j + 1] = rtemp[j];
  132.                 rtemp[j] = temp;
  133.             }
  134.             else if (rtemp[j]->getAdjacent()->getLabel() == rtemp[j + 1]->getAdjacent()->getLabel() && rtemp[j]->getWeight() > rtemp[j + 1]->getWeight())
  135.             {
  136.                 Edge *temp = rtemp[j + 1];
  137.                 rtemp[j + 1] = rtemp[j];
  138.                 rtemp[j] = temp;
  139.             }
  140.         }
  141.     }
  142.     for (int i = 0; i < ltemp.size(); ++i)
  143.     {
  144.         if (!(*(ltemp[i]) == *(rtemp[i])))
  145.             return false;
  146.     }
  147.     return true;
  148. }
  149.  
  150. ostream &operator<< (ostream& stream, const Node& node)
  151. {
  152.     if (!node.adjacencyList.empty())
  153.     {
  154.         stream << node.label << "[" << *(node.adjacencyList[0]);
  155.         for (int i = 1; i < node.adjacencyList.size(); ++i)
  156.         {
  157.             stream << "," << *(node.adjacencyList[i]);
  158.         }
  159.         stream << "]";
  160.     }
  161.     else
  162.         stream << node.label << "[]";
  163.     return stream;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment