Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Alias.hpp"
- #include <stdexcept>
- /**
- @brief Odredueje postoji li node s redim brojem x
- @param x ID node-a
- @return Vraca vrijednost suda o postojanju noda
- @sa Alias
- */
- template<class U, class W>
- bool Alias<U, W>::Exist(const W & x) const
- {
- return ItoS.count(x) > 0;
- }
- /**
- @brief Odredueje postoji li node s ID S
- @param x string koji predstavlja vrijednost tj. alias node
- @return Vraca vrijednost suda o postojanju noda
- @sa Alias
- */
- template<class U, class W>
- bool Alias<U, W>::Exist(const U & S) const
- {
- return StoI.count(S) > 0;
- }
- /**
- @brief Postavlja bijektivnu vezu izmedu Stringa i ID
- @param S Strgin koje se rabi za vezu
- @param x ID koje se rabi za vezu
- @sa Alias
- @sa RemAlias
- */
- template<class U, class W>
- void Alias<U, W>::SetAlias(const U & S, const W x)
- {
- try {
- if (Exist(S) || Exist(x)) throw std::invalid_argument("Pojam vec postoji u klasi Alias");
- StoI[S] = x;
- ItoS[x] = S;
- } catch (std::exception& e) {
- std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
- throw;
- }
- }
- /**
- @brief Raskida vezu
- @param S string veze koju zelimo raskidati
- @sa SetAlias
- @sa Alias
- */
- template<class U, class W>
- void Alias<U, W>::RemAlias(const U & S)
- {
- try {
- if (StoI.count(S) == 0) throw std::invalid_argument("Pojam nije pronaden");
- W x = StoI[S];
- StoI.erase(S);
- ItoS.erase(x);
- } catch (std::exception& e) {
- std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
- throw;
- }
- }
- /**
- @brief Raskida vezu
- @param x ID veze koju zelimo raskinuti
- @sa Alias
- @sa SetAlias
- */
- template<class U, class W>
- void Alias<U, W>::RemAlias(const W x)
- {
- try {
- if (ItoS.count(x) == 0) throw std::invalid_argument("Pojam nije pronaden");
- U S = ItoS[x];
- StoI.erase(S);
- ItoS.erase(x);
- } catch (std::exception& e) {
- std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
- throw;
- }
- }
- /**
- @brief S posebnim ID veze dobivamo njen string podatak
- @param x ID veze
- @return string iz opisa
- @sa MissingWord
- @sa GetNo
- @sa Alias
- */
- template<class U, class W>
- const U & Alias<U, W>::GetName(const W x)
- {
- try {
- if (ItoS.count(x) == 0) throw std::invalid_argument("Pojam nije pronaden");
- return ItoS[x];
- } catch (std::exception& e) {
- std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
- throw;
- }
- }
- /**
- @brief Iz stringa dobivamo ID
- @param S string koji opisuje vezu
- @return ID node
- @sa GetNo
- @sa Alias
- @sa MissingWord
- */
- template<class U, class W>
- const W & Alias<U, W>::GetNo(const U & S)
- {
- try {
- if (StoI.count(S) == 0) throw std::invalid_argument("Pojam nije pronaden");
- return StoI[S];
- } catch (std::exception& e) {
- std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
- throw;
- }
- }
- /**
- @brief Prazni klasu
- */
- template<class U, class W>
- void Alias<U, W>::Clear()
- {
- try {
- ItoS.clear();
- StoI.clear();
- } catch (std::exception& e) {
- std::cerr<<"ERROR : "<<"LINE : "<<__LINE__<<"FILE: "<<__FILE__<<" e :"<<e.what()<<std::endl;
- throw;
- }
- }
- /**
- @brief funkcija za ispis klase
- */
- template<class U, class W>
- std::ostream& operator<<( std::ostream & out, const Alias<U, W> & A)
- {
- out<<A.ItoS.size()<<std::endl;
- for (typename std::map<W, U>::const_iterator it = A.ItoS.begin(); it != A.ItoS.end(); it++)
- out<<it -> first<<" "<<it -> second<<std::endl;
- return out;
- }
- /**
- @brief Funkcija za upis u klasu
- */
- template<class U, class W>
- std::istream& operator>>( std::istream & in, Alias<U, W> & A)
- {
- A.Clear();
- int N;
- W Q;
- U S;
- in >> N;
- for (W i = 0; i < N; i++) {
- in>>Q>>S;
- A.SetAlias(S, Q);
- }
- return in;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement