Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #ifndef _PARTS_H_
  2. #define _PARTS_H_
  3.  
  4. #include <vector>
  5. #include <map>
  6. #include <string>
  7.  
  8. //**************** Part ****************
  9. class Part
  10. {
  11. public:
  12.     std::string name;
  13.     int count = 0;
  14.     Part* parent;
  15.     std::vector<Part*> children;
  16.     int count_howmanyUp(Part const* p);
  17.  
  18.     Part(std::string const& n) : name(n) {};
  19.     void describe(){};
  20.     int count_howmany(Part const* p)
  21.     {
  22.         if (p == this)
  23.             return count;
  24.         int ans = 0;
  25.         for (auto elem : children)
  26.             if (elem == p)
  27.                 ans += count_howmany(elem) * count;
  28.         return ans;
  29.     };
  30. };
  31.  
  32. //**************** NameContainer ****************
  33. class NameContainer  
  34. {
  35. public:
  36.  
  37.     NameContainer() {};
  38.  
  39.     //~NameContainer();
  40.  
  41.  
  42.     Part* lookup(std::string const &name)
  43.     {
  44.         return name_map[name];
  45.     };
  46.  
  47.  
  48.     void add_part(std::string const &x, int q, std::string const &y)
  49.     {
  50.         lookup(x)->children.push_back(lookup(y));
  51.         lookup(y)->parent = this;
  52.         lookup(y)->count = q;
  53.         lookup(y)->parent = lookup(x);
  54.     };
  55.  
  56. private:
  57.     std::map<std::string, Part*> name_map;
  58. };
  59.  
  60. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement