Advertisement
Ihmemies

10- Book

Nov 17th, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.18 KB | Source Code | 0 0
  1. (the struct from header:)
  2. // Struct for a book chapter
  3. struct Chapter
  4. {
  5.     std::string id_ = EMPTY;
  6.     std::string fullName_ = EMPTY;
  7.     int length_ = 0;
  8.     int running_id_;
  9.     bool isopen_ = true;
  10.     std::shared_ptr<Chapter> parentChapter_ = nullptr;
  11.     std::vector<std::shared_ptr<Chapter>> subchapters_;
  12. };
  13.  
  14. ---
  15.  
  16. #include "book.hh"
  17. #include <algorithm>
  18. using namespace std;
  19.  
  20. Book::Book() {
  21.  
  22. }
  23.  
  24. Book::~Book(){
  25.  
  26. }
  27.  
  28. void Book::addNewChapter(const std::string &id,
  29.                          const std::string &fullName, int length) {
  30.  
  31.     shared_ptr<Chapter> chapter {new Chapter};
  32.     chapter -> id_ = id;
  33.     chapter -> fullName_ = fullName;
  34.     chapter -> length_ = length;
  35.     chapter -> running_id_ = running_id;
  36.     running_id++;
  37.  
  38.     // try insert for a change instad of map::operator[]
  39.     idstore.insert(pair<string, shared_ptr<Chapter>>(id, chapter));
  40. }
  41.  
  42. void Book::addRelation(const std::string &subchapter,
  43.                        const std::string &parentChapter) {
  44.     if (parentChapter.length() > 0) {
  45.         shared_ptr<Chapter> s_ch = findChapter(subchapter);
  46.         shared_ptr<Chapter> p_ch = findChapter(parentChapter);
  47.  
  48.         p_ch-> subchapters_.push_back(s_ch);
  49.         s_ch -> parentChapter_ = p_ch;
  50.     }
  51. }
  52.  
  53. void Book::printIds(Params) const {
  54.     cout << "Book has " << idstore.size() << " chapters:" << endl;
  55.  
  56.     // TODO need to do ascii ABC print order so save the full names to vector
  57.     // or something and sort it and only then print it..
  58.     for(auto& [id, chapter] : idstore) {
  59.         cout << chapter->fullName_ << " --- " << id << endl;;
  60.     }
  61. }
  62.  
  63. void Book::printContents(Params) const {
  64.  
  65.     // get a list of top chapters
  66.     // map sorts running id's automatically to asc order
  67.     std::map<int, std::string> top_chapters;
  68.     for (auto& [id, chapter] : idstore) {
  69.         if (chapter->parentChapter_ == nullptr) {
  70.             top_chapters.insert(
  71.                 pair<int, std::string> (chapter->running_id_,
  72.                                         chapter->id_));
  73.         }
  74.     }
  75.  
  76.     // iterate through top chapters, printing them and their subchapters
  77.     int i = 1;
  78.     for (auto& val : top_chapters) {
  79.         // avoid typing idstore.at(val.second) repeatedly
  80.         auto& chapter = idstore.at(val.second);
  81.  
  82.         // is chapter open or closed?
  83.         string status = chapter->isopen_ ? "-" : "+";
  84.  
  85.         // print chapter details
  86.         cout << status << " " << i << ". " << chapter->fullName_
  87.              << " ( " << chapter->length_ << " )" << endl;
  88.         i++;
  89.  
  90.         // recursively call func again if chapter has subchapters
  91.         if(chapter->subchapters_.size() > 0 and chapter->isopen_) {
  92.             print_subchapters(chapter->id_, 2);
  93.         }
  94.     }
  95. }
  96.  
  97. void Book::print_subchapters(std::string id, int padding) const
  98. {
  99.     int i = 1;
  100.     for (auto& val : idstore.at(id)->subchapters_) {
  101.         // is chapter open or closed?
  102.         string status = val->isopen_ ? "-" : "+";
  103.         cout << status;
  104.  
  105.         // string() prints padding n times without a loop
  106.         cout << string(padding, ' ');
  107.  
  108.         // print chapter details
  109.         cout << i << ". " << val->fullName_
  110.              << " ( " << val->length_ << " )" << endl;
  111.         i++;
  112.  
  113.         // recursively call func again if chapter has subchapters
  114.         if (val->subchapters_.size() > 0 and val->isopen_) {
  115.             print_subchapters(val->id_, padding+2);
  116.         }
  117.     }
  118.     return;
  119. }
  120.  
  121. void Book::get_subchapters(shared_ptr<vector<string>> list,
  122.                            string id,
  123.                            int cur_depth,
  124.                            int max_depth) const
  125. {
  126.     cur_depth++;
  127.  
  128.     // look for subchapters at current depth
  129.     shared_ptr<Chapter> chapter = findChapter(id);
  130.     for (auto& val : chapter->subchapters_) {
  131.         list->push_back(val->id_);
  132.         // go deeper if needed
  133.         if (cur_depth < max_depth) {
  134.             get_subchapters(list, val->id_, cur_depth, max_depth);
  135.         }
  136.     }
  137. }
  138.  
  139. void Book::get_parents(shared_ptr<vector<string>> list,
  140.                        std::shared_ptr<Chapter> chapter,
  141.                        int cur_level,
  142.                        int max_level) const
  143. {
  144.     // add current level's parent if one exists
  145.     if (chapter->parentChapter_ != nullptr) {
  146.         list->push_back(chapter->parentChapter_->id_);
  147.         cur_level++;
  148.         // go higher if needed
  149.         if (cur_level < max_level) {
  150.             get_parents(list, chapter->parentChapter_, cur_level, max_level);
  151.         }
  152.     }
  153. }
  154.  
  155. // Recursively closes all chapters and subchapters
  156. // starting from the given chapter.
  157. void Book::close(Params params) const
  158. {
  159.     string to_close = params.front();
  160.  
  161.     // check if chapter exists
  162.     shared_ptr<Chapter> chapter = findChapter(to_close);
  163.     if(chapter == nullptr) { return; }
  164.  
  165.     // close chapter if open
  166.     if (chapter->isopen_)
  167.     {
  168.         chapter->isopen_ = false;
  169.  
  170.         // if subchapters exist, check if they need closing too
  171.         if (chapter->subchapters_.size() > 0) {
  172.             for (auto& val : chapter->subchapters_) {
  173.                 close(vector<string> {val->id_});
  174.             }
  175.         }
  176.     }
  177. }
  178.  
  179. void Book::open(Params params) const {
  180.     string to_open = params.front();
  181.  
  182.     // check if chapter exists
  183.     shared_ptr<Chapter> chapter = findChapter(to_open);
  184.     if(chapter == nullptr) { return; }
  185.  
  186.     chapter->isopen_ = true;
  187. }
  188.  
  189. void Book::openAll(Params) const {
  190.     for (auto& [id, chapter] : idstore) {
  191.         chapter->isopen_ = true;
  192.     }
  193. }
  194.  
  195. void Book::printParentsN(Params params) const {
  196.     string id = params.at(0);
  197.     int level = stoi(params.at(1));
  198.  
  199.     // level must be at least 1.
  200.     if (level < 1) {
  201.         cout << "Error. Level can't be less than 1." << endl;
  202.         return;
  203.     }
  204.  
  205.     shared_ptr<Chapter> chapter = findChapter(id);
  206.  
  207.     // no chapter?
  208.     if (chapter == nullptr) {
  209.         cout << "Error: Not found: " << id << endl;
  210.         return;
  211.     }
  212.     // no parent chapter?
  213.     if (chapter->parentChapter_ == nullptr) {
  214.         cout << id << " has no parent chapters." << endl;
  215.         return;
  216.     }
  217.  
  218.     // else print subchapters to depth n
  219.     auto list = make_shared<vector<string>>();
  220.     get_parents(list, chapter, 0, level);
  221.  
  222.     // TODO sort to ASCII order... with custom function as 3rd parameter
  223.     std::sort (list->begin(), list->end());
  224.  
  225.     cout << id << " has " << list->size() << " parent chapters:" << endl;
  226.     for (auto& val : *list) {
  227.         cout << val << endl;
  228.     }
  229. }
  230.  
  231. void Book::printSubchaptersN(Params params) const {
  232.     string id = params.at(0);
  233.     int max_depth = stoi(params.at(1));
  234.     shared_ptr<Chapter> chapter = findChapter(id);
  235.  
  236.     // no chapter?
  237.     if (chapter == nullptr) {
  238.         cout << "Error: Not found: " << id << endl;
  239.         return;
  240.     }
  241.     // no subchapters?
  242.     if (chapter->subchapters_.size() == 0) {
  243.         cout << id << " has no subchapters." << endl;
  244.         return;
  245.     }
  246.  
  247.     // else print subchapters to depth n
  248.     auto list = make_shared<vector<string>>();
  249.     get_subchapters(list, id, 0, max_depth);
  250.  
  251.     // TODO sort to ASCII order... with custom function as 3rd parameter
  252.     std::sort (list->begin(), list->end());
  253.  
  254.     cout << id << " has " << list->size() << " subchapters:" << endl;
  255.     for (auto& val : *list) {
  256.         cout << val << endl;
  257.     }
  258. }
  259.  
  260. void Book::printSiblingChapters(Params params) const {
  261.     string id = params.at(0);
  262.     shared_ptr<Chapter> chapter = findChapter(id);
  263.     if (chapter == nullptr) { return; }
  264.  
  265.     // top tier chapters
  266.     if (chapter->parentChapter_ == nullptr) {
  267.  
  268.         // get a list of top chapters
  269.         // map sorts running id's automatically to asc order
  270.         // TODO use the ASCII sort function for this map
  271.         // TODO is there any way to avoid this copy paste?
  272.         // because printcontents is const and can only edit book class
  273.         // I can't get a list of top chapters generated anywhere else
  274.         // %)="#?(ยค(#%)="
  275.         std::map<int, std::string> top_chapters;
  276.         for (auto& [id, chapter_ptr] : idstore) {
  277.             if (chapter_ptr->parentChapter_ == nullptr) {
  278.                 top_chapters.insert(
  279.                     pair<int, std::string> (chapter_ptr->running_id_,
  280.                                             chapter_ptr->id_));
  281.             }
  282.         }
  283.  
  284.         for (auto& [key, chapter_id] : top_chapters) {
  285.             if (chapter_id != id) {
  286.                 cout << chapter_id << endl;
  287.             }
  288.         }
  289.         return;
  290.     }
  291.  
  292.     // get parent chapter and print its subchapters except id
  293.     for (auto& val : chapter->parentChapter_->subchapters_) {
  294.         if (val->id_ != id) {
  295.             cout << val-> id_ << endl;
  296.         }
  297.     }
  298. }
  299.  
  300. void Book::printTotalLength(Params params) const{
  301.     string id = params.at(0);
  302.     int length = 0;
  303.  
  304.     // add provided chapter to length
  305.     length += idstore.at(id)->length_;
  306.  
  307.     // get subchapters to depth n
  308.     // TODO get_subchapters to work with inf depth
  309.     auto list = make_shared<vector<string>>();
  310.     get_subchapters(list, id, 0, 9999);
  311.  
  312.     // add subchapters to length
  313.     for (auto& val : *list) {
  314.         length += idstore.at(val)->length_;
  315.     }
  316.  
  317.     cout << "Total length of " << id << " is " << length << "." << endl;
  318. }
  319.  
  320. void Book::printLongestInHierarchy(Params params) const {
  321.     for(auto& val : params) {
  322.         cout << val;
  323.     }
  324. }
  325.  
  326. void Book::printShortestInHierarchy(Params params) const {
  327.     for(auto& val : params) {
  328.         cout << val;
  329.     }
  330. }
  331.  
  332. std::shared_ptr<Chapter> Book::findChapter(const std::string &id) const {
  333.     auto iter = idstore.find(id);
  334.     if (iter != idstore.end()) {
  335.       return iter->second;
  336.     }
  337.     cout << "Error: Not found: " << id << endl;
  338.     return nullptr;
  339. }
  340.  
  341. void Book::printGroup(const std::string &id,
  342.                       const std::string &group,
  343.                       const IdSet &container) const {
  344.     cout << id << group;
  345.     for (auto& val: container) {
  346.         cout << val;
  347.     }
  348.  
  349. }
  350.  
  351. IdSet Book::vectorToIdSet(const std::vector<Chapter *> &container) const {
  352.     for (auto& val: container) {
  353.         cout << val;
  354.     }
  355. }
  356.  
  357.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement