Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. #ifndef VIRUS_GENEALOGY_H
  2. #define VIRUS_GENEALOGY_H
  3.  
  4. #include <string>
  5. #include <unordered_map>
  6. #include <vector>
  7.  
  8. class VirusNotFound : std::exception {};
  9. class VirusAlreadyCreated : std::exception {};
  10. class TriedToRemoveStemVirus : std::exception {};
  11.  
  12. template <class Virus> class VirusGenealogy {
  13.   public:
  14.     VirusGenealogy(typename Virus::id_type const &stem_id) noexcept
  15.         : stem_id(stem_id) {}
  16.  
  17.     // Zwraca identyfikator wirusa macierzystego.
  18.     typename Virus::id_type get_stem_id() const noexcept { return stem_id; }
  19.  
  20.     // Zwraca listę identyfikatorów bezpośrednich następników wirusa
  21.     // o podanym identyfikatorze.
  22.     // Zgłasza wyjątek VirusNotFound, jeśli dany wirus nie istnieje.
  23.     std::vector<typename Virus::id_type>
  24.     get_children(typename Virus::id_type const &id) const {
  25.         if (!exists(id)) {
  26.             throw VirusNotFound();
  27.         } else {
  28.             return children[id];
  29.         }
  30.     }
  31.  
  32.     // Zwraca listę identyfikatorów bezpośrednich poprzedników wirusa
  33.     // o podanym identyfikatorze.
  34.     // Zgłasza wyjątek VirusNotFound, jeśli dany wirus nie istnieje.
  35.     std::vector<typename Virus::id_type>
  36.     get_parents(typename Virus::id_type const &id) const {
  37.         if (!exists(id)) {
  38.             throw VirusNotFound();
  39.         } else {
  40.             return parents[id];
  41.         }
  42.     };
  43.  
  44.     // Sprawdza, czy wirus o podanym identyfikatorze istnieje.
  45.     bool exists(typename Virus::id_type const &id) const noexcept {
  46.         return (viruses.count(id) > 0);
  47.     }
  48.  
  49.     // Zwraca referencję do obiektu reprezentującego wirus o podanym
  50.     // identyfikatorze.
  51.     // Zgłasza wyjątek VirusNotFound, jeśli żądany wirus nie istnieje.
  52.     Virus &operator[](typename Virus::id_type const &id) const {
  53.         if (!exists(id)) {
  54.             throw VirusNotFound();
  55.         } else {
  56.             return viruses[id];
  57.         }
  58.     }
  59.  
  60.     // Tworzy węzeł reprezentujący nowy wirus o identyfikatorze id
  61.     // powstały z wirusów o podanym identyfikatorze parent_id lub
  62.     // podanych identyfikatorach parent_ids.
  63.     // Zgłasza wyjątek VirusAlreadyCreated, jeśli wirus o identyfikatorze
  64.     // id już istnieje.
  65.     // Zgłasza wyjątek VirusNotFound, jeśli któryś z wyspecyfikowanych
  66.     // poprzedników nie istnieje.
  67.     void create(typename Virus::id_type const &id,
  68.                 typename Virus::id_type const &parent_id);
  69.     void create(typename Virus::id_type const &id,
  70.                 std::vector<typename Virus::id_type> const &parent_ids);
  71.  
  72.     // Dodaje nową krawędź w grafie genealogii.
  73.     // Zgłasza wyjątek VirusNotFound, jeśli któryś z podanych wirusów nie
  74.     // istnieje.
  75.     void connect(typename Virus::id_type const &child_id,
  76.                  typename Virus::id_type const &parent_id);
  77.  
  78.     // Usuwa wirus o podanym identyfikatorze.
  79.     // Zgłasza wyjątek VirusNotFound, jeśli żądany wirus nie istnieje.
  80.     // Zgłasza wyjątek TriedToRemoveStemVirus przy próbie usunięcia
  81.     // wirusa macierzystego.
  82.     void remove(typename Virus::id_type const &id);
  83.  
  84.   private:
  85.     const typename Virus::id_type stem_id;
  86.  
  87.     std::unordered_map<typename Virus::id_type,
  88.                        std::vector<typename Virus::id_type>>
  89.         children;
  90.     std::unordered_map<typename Virus::id_type,
  91.                        std::vector<typename Virus::id_type>>
  92.         parents;
  93.     std::unordered_map<typename Virus::id_type, Virus> viruses;
  94. };
  95.  
  96. #endif // VIRUS_GENEALOGY_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement