Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. #ifndef CITATION_GRAPH_H
  2. #define CITATION_GRAPH_H
  3.  
  4. #include <vector>
  5. #include <memory>
  6. #include <set>
  7. #include <map>
  8.  
  9.  
  10. class PublicationNotFound : public std::exception {
  11. const char * what () const noexcept override {
  12. return "PublicationNotFound";
  13. }
  14. };
  15.  
  16. class PublicationAlreadyCreated : public std::exception {
  17. const char * what () const noexcept override {
  18. return "PublicationAlreadyCreated";
  19. }
  20. };
  21.  
  22. class TriedToRemoveRoot : public std::exception {
  23. const char * what () const noexcept override {
  24. return "TriedToRemoveRoot";
  25. }
  26. };
  27.  
  28.  
  29.  
  30. template <class Publication>
  31. class CitationGraph {
  32.  
  33. private:
  34. class Node {
  35. public:
  36. Publication publication;
  37. std::set<std::shared_ptr<Node> > children;
  38. std::set<std::weak_ptr<Node> > parents;
  39. Node (typename Publication::id_type const &stem_id)
  40. : publication(stem_id) {}
  41. };
  42.  
  43. std::shared_ptr<Node> root;
  44. std::map <typename Publication::id_type, std::weak_ptr<Node>> map;
  45.  
  46. public:
  47. // Tworzy nowy graf. Tworzy także węzeł publikacji o identyfikatorze stem_id.
  48. CitationGraph(typename Publication::id_type const &stem_id) {
  49. root = std::make_shared<Node>(stem_id);
  50. }
  51.  
  52. // Konstruktor przenoszący i przenoszący operator przypisania. Powinny być
  53. // noexcept.
  54. CitationGraph(CitationGraph<Publication> &&other) noexcept {
  55. *this = std::move(other);
  56. }
  57. CitationGraph<Publication>& operator=(CitationGraph<Publication> &&other) {
  58. *this = std::move(other);
  59. }
  60.  
  61. // Zwraca identyfikator źródła. Metoda ta powinna być noexcept wtedy i tylko
  62. // wtedy, gdy metoda Publication::get_id jest noexcept. Zamiast pytajnika należy
  63. // wpisać stosowne wyrażenie.
  64. // https://akrzemi1.wordpress.com/2011/06/10/using-noexcept/
  65. //Publication::id_type get_root_id() const noexcept(is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value);
  66. typename Publication::id_type get_root_id() const noexcept(noexcept(std::declval<Publication>().get_id())) {
  67. return root -> publication.get_id();
  68.  
  69.  
  70. }
  71. // Zwraca listę identyfikatorów publikacji cytujących publikację o podanym
  72. // identyfikatorze. Zgłasza wyjątek PublicationNotFound, jeśli dana publikacja
  73. // nie istnieje.
  74. std::vector<typename Publication::id_type> get_children(typename Publication::id_type const &id) const {
  75. if (!exists(id))
  76. throw PublicationNotFound();
  77.  
  78. std::vector<typename Publication::id_type> result;
  79. std::shared_ptr<Node> myNode = map.at(id).lock();
  80. for (auto child : myNode -> children) {
  81. result.push_back(child -> publication.get_id());
  82. }
  83. return result;
  84. }
  85.  
  86. // Zwraca listę identyfikatorów publikacji cytowanych przez publikację o podanym
  87. // identyfikatorze. Zgłasza wyjątek PublicationNotFound, jeśli dana publikacja
  88. // nie istnieje.
  89. std::vector<typename Publication::id_type> get_parents(typename Publication::id_type const &id) const {
  90. if (!exists(id))
  91. throw PublicationNotFound();
  92. std::vector<typename Publication::id_type> result;
  93. std::shared_ptr<Node> myNode = map.at(id).lock();
  94. for (auto parent : myNode -> parents) {
  95. if (std::shared_ptr<Node> parent2 = parent.lock())
  96. result.push_back(parent2 -> publication.get_id());
  97. }
  98. }
  99.  
  100. // Sprawdza, czy publikacja o podanym identyfikatorze istnieje.
  101. bool exists(typename Publication::id_type const &id) const {
  102. auto checkNode = map.find(id);
  103. if (checkNode == map.end()) return false;
  104. if (checkNode -> second.expired()) return false;
  105. return true;
  106. }
  107.  
  108. // Zwraca referencję do obiektu reprezentującego publikację o podanym
  109. // identyfikatorze. Zgłasza wyjątek PublicationNotFound, jeśli żądana publikacja
  110. // nie istnieje.
  111. Publication& operator[](typename Publication::id_type const &id) const {
  112. if (!exists(id))
  113. throw PublicationNotFound();
  114.  
  115. std::shared_ptr<Node> myNode = map.at(id).lock();
  116. return myNode -> publication;
  117. }
  118.  
  119. // Tworzy węzeł reprezentujący nową publikację o identyfikatorze id cytującą
  120. // publikacje o podanym identyfikatorze parent_id lub podanych identyfikatorach
  121. // parent_ids. Zgłasza wyjątek PublicationAlreadyCreated, jeśli publikacja
  122. // o identyfikatorze id już istnieje. Zgłasza wyjątek PublicationNotFound, jeśli
  123. // któryś z wyspecyfikowanych poprzedników nie istnieje.
  124. void create(typename Publication::id_type const &id, typename Publication::id_type const &parent_id) {
  125. if (exists(id))
  126. throw PublicationAlreadyCreated();
  127. if (!exists(parent_id))
  128. throw PublicationNotFound();
  129. // TODO: maybe with add_citation ?
  130.  
  131. }
  132. void create(typename Publication::id_type const &id, std::vector<typename Publication::id_type> const &parent_ids) {
  133. if (exists(id))
  134. throw PublicationAlreadyCreated();
  135. for (auto parent : parent_ids) {
  136. if (!exists(parent))
  137. throw PublicationNotFound();
  138. }
  139. // TODO: maybe with add_citation ?
  140.  
  141. }
  142.  
  143. // Dodaje nową krawędź w grafie cytowań. Zgłasza wyjątek PublicationNotFound,
  144. // jeśli któraś z podanych publikacji nie istnieje.
  145. void add_citation(typename Publication::id_type const &child_id, typename Publication::id_type const &parent_id) {
  146. if (!exists(child_id) || !exists(parent_id))
  147. throw PublicationNotFound();
  148.  
  149. }
  150.  
  151. // Usuwa publikację o podanym identyfikatorze. Zgłasza wyjątek
  152. // PublicationNotFound, jeśli żądana publikacja nie istnieje. Zgłasza wyjątek
  153. // TriedToRemoveRoot przy próbie usunięcia pierwotnej publikacji.
  154. void remove(typename Publication::id_type const &id) {
  155. if (!exists(id))
  156. throw PublicationNotFound();
  157. if (id == root -> publication.get_id())
  158. throw TriedToRemoveRoot();
  159. // TODO: remove this node from every parent, because parents are only ones who got shared_pointers on this node
  160. std::shared_ptr<Node> myNode = map.at(id).lock();
  161. for (auto parent : myNode -> parents) {
  162. if (std::shared_ptr<Node> validParent = parent.lock()) {
  163.  
  164. }
  165.  
  166. }
  167. }
  168.  
  169. };
  170.  
  171. #endif //CITATION_GRAPH_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement