Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef VIRUS_GENEALOGY_H
- #define VIRUS_GENEALOGY_H
- #include <string>
- #include <unordered_map>
- #include <vector>
- class VirusNotFound : std::exception {};
- class VirusAlreadyCreated : std::exception {};
- class TriedToRemoveStemVirus : std::exception {};
- template <class Virus> class VirusGenealogy {
- public:
- VirusGenealogy(typename Virus::id_type const &stem_id) noexcept
- : stem_id(stem_id) {}
- // Zwraca identyfikator wirusa macierzystego.
- typename Virus::id_type get_stem_id() const noexcept { return stem_id; }
- // Zwraca listę identyfikatorów bezpośrednich następników wirusa
- // o podanym identyfikatorze.
- // Zgłasza wyjątek VirusNotFound, jeśli dany wirus nie istnieje.
- std::vector<typename Virus::id_type>
- get_children(typename Virus::id_type const &id) const {
- if (!exists(id)) {
- throw VirusNotFound();
- } else {
- return children[id];
- }
- }
- // Zwraca listę identyfikatorów bezpośrednich poprzedników wirusa
- // o podanym identyfikatorze.
- // Zgłasza wyjątek VirusNotFound, jeśli dany wirus nie istnieje.
- std::vector<typename Virus::id_type>
- get_parents(typename Virus::id_type const &id) const {
- if (!exists(id)) {
- throw VirusNotFound();
- } else {
- return parents[id];
- }
- };
- // Sprawdza, czy wirus o podanym identyfikatorze istnieje.
- bool exists(typename Virus::id_type const &id) const noexcept {
- return (viruses.count(id) > 0);
- }
- // Zwraca referencję do obiektu reprezentującego wirus o podanym
- // identyfikatorze.
- // Zgłasza wyjątek VirusNotFound, jeśli żądany wirus nie istnieje.
- Virus &operator[](typename Virus::id_type const &id) const {
- if (!exists(id)) {
- throw VirusNotFound();
- } else {
- return viruses[id];
- }
- }
- // Tworzy węzeł reprezentujący nowy wirus o identyfikatorze id
- // powstały z wirusów o podanym identyfikatorze parent_id lub
- // podanych identyfikatorach parent_ids.
- // Zgłasza wyjątek VirusAlreadyCreated, jeśli wirus o identyfikatorze
- // id już istnieje.
- // Zgłasza wyjątek VirusNotFound, jeśli któryś z wyspecyfikowanych
- // poprzedników nie istnieje.
- void create(typename Virus::id_type const &id,
- typename Virus::id_type const &parent_id);
- void create(typename Virus::id_type const &id,
- std::vector<typename Virus::id_type> const &parent_ids);
- // Dodaje nową krawędź w grafie genealogii.
- // Zgłasza wyjątek VirusNotFound, jeśli któryś z podanych wirusów nie
- // istnieje.
- void connect(typename Virus::id_type const &child_id,
- typename Virus::id_type const &parent_id);
- // Usuwa wirus o podanym identyfikatorze.
- // Zgłasza wyjątek VirusNotFound, jeśli żądany wirus nie istnieje.
- // Zgłasza wyjątek TriedToRemoveStemVirus przy próbie usunięcia
- // wirusa macierzystego.
- void remove(typename Virus::id_type const &id);
- private:
- const typename Virus::id_type stem_id;
- std::unordered_map<typename Virus::id_type,
- std::vector<typename Virus::id_type>>
- children;
- std::unordered_map<typename Virus::id_type,
- std::vector<typename Virus::id_type>>
- parents;
- std::unordered_map<typename Virus::id_type, Virus> viruses;
- };
- #endif // VIRUS_GENEALOGY_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement