Advertisement
lpp

Untitled

lpp
Mar 25th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include "Alias.hpp"
  2. #include <stdexcept>
  3.  
  4. /**
  5.     @brief Odredueje postoji li node s redim brojem x
  6.     @param x ID node-a
  7.     @return Vraca vrijednost suda o postojanju noda
  8.     @sa Alias
  9. */
  10.  
  11. template<class U, class W>
  12. bool Alias<U, W>::Exist(const W & x) const
  13. {
  14.     return ItoS.count(x) > 0;
  15. }
  16.  
  17. /**
  18.     @brief Odredueje postoji li node s ID S
  19.     @param x string koji predstavlja vrijednost tj. alias node
  20.     @return Vraca vrijednost suda o postojanju noda
  21.     @sa Alias
  22. */
  23. template<class U, class W>
  24. bool Alias<U, W>::Exist(const U & S) const
  25. {
  26.     return StoI.count(S) > 0;
  27. }
  28.  
  29. /**
  30.     @brief Postavlja bijektivnu vezu izmedu Stringa i ID
  31.     @param S Strgin koje se rabi za vezu
  32.     @param x ID koje se rabi za vezu
  33.     @sa Alias
  34.     @sa RemAlias
  35. */
  36. template<class U, class W>
  37. void Alias<U, W>::SetAlias(const U & S, const W x)
  38. {
  39.     try {
  40.         if (Exist(S) || Exist(x)) throw std::invalid_argument("Pojam vec postoji u klasi Alias");
  41.         StoI[S] = x;
  42.         ItoS[x] = S;
  43.     } catch (std::exception& e) {
  44.         std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
  45.         throw;
  46.     }
  47. }
  48.  
  49. /**
  50.     @brief Raskida vezu
  51.     @param S string veze koju zelimo raskidati
  52.     @sa SetAlias
  53.     @sa Alias
  54. */
  55. template<class U, class W>
  56. void Alias<U, W>::RemAlias(const U & S)
  57. {
  58.     try {
  59.         if (StoI.count(S) == 0) throw std::invalid_argument("Pojam nije pronaden");
  60.         W x = StoI[S];
  61.         StoI.erase(S);
  62.         ItoS.erase(x);
  63.     } catch (std::exception& e) {
  64.         std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
  65.         throw;
  66.     }
  67. }
  68. /**
  69.     @brief Raskida vezu
  70.     @param x ID veze koju zelimo raskinuti
  71.     @sa Alias
  72.     @sa SetAlias
  73. */
  74. template<class U, class W>
  75. void Alias<U, W>::RemAlias(const W x)
  76. {
  77.     try {
  78.         if (ItoS.count(x) == 0) throw std::invalid_argument("Pojam nije pronaden");
  79.  
  80.         U S = ItoS[x];
  81.         StoI.erase(S);
  82.         ItoS.erase(x);
  83.     } catch (std::exception& e) {
  84.         std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
  85.         throw;
  86.     }
  87. }
  88.  
  89. /**
  90.     @brief S posebnim ID veze dobivamo njen string podatak
  91.     @param x ID veze
  92.     @return string iz opisa
  93.     @sa MissingWord
  94.     @sa GetNo
  95.     @sa Alias
  96. */
  97. template<class U, class W>
  98. const U & Alias<U, W>::GetName(const W x)
  99. {
  100.     try {
  101.         if (ItoS.count(x) == 0) throw std::invalid_argument("Pojam nije pronaden");
  102.         return ItoS[x];
  103.     } catch (std::exception& e) {
  104.         std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
  105.         throw;
  106.     }
  107. }
  108.  
  109. /**
  110.     @brief Iz stringa dobivamo ID
  111.     @param S string koji opisuje vezu
  112.     @return ID node
  113.     @sa GetNo
  114.     @sa Alias
  115.     @sa MissingWord
  116. */
  117. template<class U, class W>
  118. const W & Alias<U, W>::GetNo(const U & S)
  119. {
  120.     try {
  121.         if (StoI.count(S) == 0) throw std::invalid_argument("Pojam nije pronaden");
  122.         return StoI[S];
  123.     } catch (std::exception& e) {
  124.         std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
  125.         throw;
  126.     }
  127. }
  128.  
  129. /**
  130.     @brief Prazni klasu
  131. */
  132. template<class U, class W>
  133. void Alias<U, W>::Clear()
  134. {
  135.     try {
  136.         ItoS.clear();
  137.         StoI.clear();
  138.     } catch (std::exception& e) {
  139.         std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
  140.         throw;
  141.     }
  142. }
  143. /**
  144.     @brief funkcija za ispis klase
  145. */
  146. template<class U, class W>
  147. std::ostream& operator<<( std::ostream & out, const Alias<U, W> & A)
  148. {
  149.     out<<A.ItoS.size()<<std::endl;
  150.     for (typename std::map<W, U>::const_iterator it = A.ItoS.begin(); it != A.ItoS.end(); it++)
  151.         out<<it -> first<<" "<<it -> second<<std::endl;
  152.     return out;
  153. }
  154.  
  155. /**
  156.     @brief Funkcija za upis u klasu
  157. */
  158. template<class U, class W>
  159. std::istream& operator>>( std::istream & in, Alias<U, W> & A)
  160. {
  161.     A.Clear();
  162.     int N;
  163.     W Q;
  164.     U S;
  165.     in >> N;
  166.     for (W i = 0; i < N; i++) {
  167.         in>>Q>>S;
  168.         A.SetAlias(S, Q);
  169.     }
  170.     return in;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement