Guest User

Network.cpp

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