Samchon Framework for CPP  1.0.0
Packer.hpp
1 #pragma once
2 #include <samchon/protocol/SharedEntityArray.hpp>
3 #include <samchon/examples/packer/WrapperArray.hpp>
4 
5 #include <samchon/library/CombinedPermutationGenerator.hpp>
6 
7 #include <iostream>
8 
9 namespace samchon
10 {
11 namespace examples
12 {
13 namespace packer
14 {
15  using namespace std;
16 
17  using namespace library;
18  using namespace protocol;
19 
36  class Packer
37  : public SharedEntityArray<WrapperArray>
38  {
39  private:
41 
42  protected:
46  shared_ptr<ProductArray> productArray;
47 
48  public:
49  /* ---------------------------------------------------------
50  CONSTRUCTORS
51  --------------------------------------------------------- */
56  : super()
57  {
58  this->productArray = make_shared<ProductArray>();
59  };
60 
67  Packer(shared_ptr<ProductArray> productArray)
68  : super()
69  {
70  this->productArray = productArray;
71  };
72 
80  Packer(const Packer &packer)
81  : Packer(packer.productArray)
82  {
83  this->reserve(packer.size());
84 
85  for (size_t i = 0; i < packer.size(); i++)
86  this->emplace_back(new WrapperArray(*packer[i]));
87  };
88 
89  virtual void construct(shared_ptr<XML> xml) override
90  {
91  super::construct(xml);
92 
93  productArray->construct(xml->get(productArray->TAG())->at(0));
94  };
95 
96  protected:
97  virtual auto createChild(shared_ptr<XML>) -> WrapperArray* override
98  {
99  return new WrapperArray();
100  };
101 
102  public:
103  /* ---------------------------------------------------------
104  GETTERS
105  --------------------------------------------------------- */
109  auto productSize() const -> size_t
110  {
111  return productArray->size();
112  };
113 
117  auto calcPrice() const -> int
118  {
119  int price = 0;
120  for (size_t i = 0; i < size(); i++)
121  price += at(i)->calcPrice();
122 
123  return price;
124  };
125 
126  /* ---------------------------------------------------------
127  OPERATORS
128  --------------------------------------------------------- */
132  void optimize(size_t start = 0, size_t size = -1)
133  {
134  if(empty() == true || productArray->empty() == true)
135  return;
136 
137  CombinedPermutationGenerator caseGenerator(this->size(), productArray->size());
138  shared_ptr<Packer> minPacker = nullptr;
139 
140  if (size == -1 || start + size > caseGenerator.size())
141  size = caseGenerator.size() - start;
142 
143  for (size_t i = start; i < start + size; i++)
144  {
145  vector<size_t> &row = caseGenerator[i];
146  shared_ptr<Packer> packer(new Packer(*this));
147 
148  bool validity = true;
149 
150  for (size_t j = 0; j < row.size(); j++)
151  {
152  shared_ptr<Product> &product = productArray->at(j);
153  shared_ptr<WrapperArray> &wrapperArray = packer->at( row[j] );
154 
155  if (wrapperArray->tryInsert(product) == false)
156  {
157  validity = false;
158  break;
159  }
160  }
161 
162  if(validity == false)
163  continue;
164 
165  //OPTIMIZE ALL WRAPPERS IN A PACKER
166  for (size_t j = 0; j < packer->size(); j++)
167  packer->at(j)->optimize();
168 
169  if (minPacker == nullptr ||
170  packer->calcPrice() < minPacker->calcPrice())
171  {
172  minPacker = packer;
173  }
174  }
175 
176  //COPY
177  assign(minPacker->begin(), minPacker->end());
178  };
179 
180  /* ---------------------------------------------------------
181  EXPORT
182  --------------------------------------------------------- */
183  virtual auto TAG() const -> string override
184  {
185  return "packer";
186  };
187  virtual auto CHILD_TAG() const -> string override
188  {
189  return "wrapperArray";
190  };
191 
192  virtual auto toXML() const -> shared_ptr<XML> override
193  {
194  shared_ptr<XML> &xml = super::toXML();
195  xml->push_back(productArray->toXML());
196 
197  return xml;
198  };
199 
203  auto toString() const -> string
204  {
205  string str = "$" + std::to_string(calcPrice()) + "\n";
206  for (size_t i = 0; i < size(); i++)
207  str += at(i)->toString() + "\n";
208 
209  return str;
210  };
211 
212  /* ---------------------------------------------------------
213  MAIN
214  --------------------------------------------------------- */
215  static void main()
216  {
217  // CONSTRUCT PRODUCTS
218  shared_ptr<ProductArray> productArray(new ProductArray());
219  productArray->emplace_back(new Product("Eraser", 500, 10, 70));
220  productArray->emplace_back(new Product("Pencil", 400, 30, 35));
221  /*productArray->emplace_back(new Product("Pencil", 400, 30, 35));
222  productArray->emplace_back(new Product("Pencil", 400, 30, 35));
223  productArray->emplace_back(new Product("Book", 8000, 150, 300));*/
224  productArray->emplace_back(new Product("Book", 8000, 150, 300));
225  productArray->emplace_back(new Product("Drink", 1000, 75, 250));
226  productArray->emplace_back(new Product("Umbrella", 4000, 200, 1000));
227  productArray->emplace_back(new Product("Notebook-PC", 800000, 150, 850));
228  productArray->emplace_back(new Product("Tablet-PC", 600000, 120, 450));
229 
230  // CONSTRUCT PACKER
231  Packer packer(productArray);
232  packer.emplace_back(new WrapperArray("Large", 100, 200, 1000));
233  packer.emplace_back(new WrapperArray("Medium", 70, 150, 500));
234  packer.emplace_back(new WrapperArray("Small", 50, 100, 250));
235 
236  // OPTIMIZE
237  packer.optimize();
238 
239  // PRINT
240  cout << packer.toString();
241  };
242  };
243 };
244 };
245 };
void optimize(size_t start=0, size_t size=-1)
Find the best packaging method.
Definition: Packer.hpp:132
auto size() const -> size_t
Get size of all cases.
auto productSize() const -> size_t
Get number of products to package.
Definition: Packer.hpp:109
shared_ptr< ProductArray > productArray
Prodcut(s) to package in some Wrapper(s)
Definition: Packer.hpp:46
auto toString() const -> string
Return a string represents an packaging method.
Definition: Packer.hpp:203
auto calcPrice() const -> int
Calculate price of the wrappers.
Definition: Packer.hpp:117
virtual auto toXML() const -> shared_ptr< XML > override
Get an XML object represents the EntityGroup.
Definition: Packer.hpp:192
An Entity and a container of children Entity objects.
Definition: EntityGroup.hpp:40
Packer()
Default Constructor.
Definition: Packer.hpp:55
virtual auto CHILD_TAG() const -> string override
A tag name of children.
Definition: Packer.hpp:187
A product, merchandise.
Definition: Product.hpp:26
An array of Product objects.
Packer(shared_ptr< ProductArray > productArray)
Construct from products and wrapper.
Definition: Packer.hpp:67
A packer planning the best packaging.
Definition: Packer.hpp:36
An array of wrapper with same category (name).
Packer(const Packer &packer)
Copy Constructor.
Definition: Packer.hpp:80