Samchon Framework for CPP  1.0.0
WrapperArray.hpp
1 #pragma once
2 #include <samchon/protocol/SharedEntityArray.hpp>
3 #include <samchon/examples/packer/Wrapper.hpp>
4 
5 #include <samchon/library/FactorialGenerator.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 
33  : public SharedEntityArray<Wrapper>
34  {
35  private:
37 
38  protected:
42  shared_ptr<ProductArray> reserved;
43 
47  shared_ptr<Wrapper> sample;
48 
49  public:
50  /* ---------------------------------------------------------
51  CONSTRUCTORS
52  --------------------------------------------------------- */
57  : super()
58  {
59  this->reserved = make_shared<ProductArray>();
60  this->sample = make_shared<Wrapper>();
61  };
62 
71  WrapperArray(const string &name, int price, int volume, int weight)
72  : super()
73  {
74  this->reserved = make_shared<ProductArray>();
75  this->sample = make_shared<Wrapper>(name, price, volume, weight);
76  };
77 
78 
86  WrapperArray(const WrapperArray &wrapperArray)
87  : super()
88  {
89  this->reserved = make_shared<ProductArray>();
90  this->sample = wrapperArray.sample;
91  };
92 
93  virtual ~WrapperArray() = default;
94 
95  virtual void construct(shared_ptr<XML> xml) override
96  {
97  super::construct(xml);
98 
99  sample->construct(xml);
100  };
101 
102  protected:
103  virtual auto createChild(shared_ptr<XML>) -> Wrapper* override
104  {
105  return new Wrapper();
106  };
107 
108  public:
119  auto tryInsert(shared_ptr<Product> product) -> bool
120  {
121  if (product->getVolume() > sample->getVolume() ||
122  product->getWeight() > sample->getWeight())
123  {
124  return false;
125  }
126 
127  reserved->push_back(product);
128  return true;
129  };
130 
131  /* ---------------------------------------------------------
132  OPERATORS
133  --------------------------------------------------------- */
149  void optimize()
150  {
151  if(reserved->empty() == true)
152  return;
153 
154  FactorialGenerator factorial(reserved->size());
155  shared_ptr<WrapperArray> minWrapperArray = nullptr;
156 
157  for (size_t i = 0; i < factorial.size(); i++)
158  {
159  shared_ptr<WrapperArray> wrapperArray(new WrapperArray(*this));
160  vector<size_t> &row = factorial[i];
161 
162  for (size_t j = 0; j < row.size(); j++)
163  {
164  shared_ptr<Product> &product = this->reserved->at(row[j]);
165 
166  if (wrapperArray->empty() == true ||
167  wrapperArray->at(wrapperArray->size() - 1)->tryInsert(product) == false)
168  {
169  Wrapper *wrapper = new Wrapper(*this->sample);
170  wrapper->tryInsert(product);
171 
172  wrapperArray->emplace_back(wrapper);
173  }
174  }
175 
176  //unique_lock<mutex> uk(mtx);
177  if (minWrapperArray == nullptr ||
178  wrapperArray->size() < minWrapperArray->size())
179  {
180  minWrapperArray = wrapperArray;
181  }
182  }
183 
184  assign(minWrapperArray->begin(), minWrapperArray->end());
185  };
186 
192  auto calcPrice() const -> int
193  {
194  return sample->getPrice() * (int)size();
195  };
196 
197  /* ---------------------------------------------------------
198  EXPORT
199  --------------------------------------------------------- */
200  virtual auto TAG() const -> string override
201  {
202  return "wrapperArray";
203  }
204  virtual auto CHILD_TAG() const -> string override
205  {
206  return "wrapper";
207  };
208 
209  virtual auto toXML() const -> shared_ptr<XML> override
210  {
211  shared_ptr<XML> &xml = super::toXML();
212  xml->insertAllProperties(sample->toXML());
213 
214  return xml;
215  };
216 
220  auto toString() const -> string
221  {
222  string str = "Category - " + sample->get_name() + "\n";
223  for (size_t i = 0; i < size(); i++)
224  str += at(i)->toString() + "\n";
225 
226  return str;
227  };
228  };
229 };
230 };
231 };
auto tryInsert(shared_ptr< Product > product) -> bool
Try to insert a product into reserved list.
auto tryInsert(shared_ptr< Product > product) -> bool
Try to insert a product into the wrapper.
Definition: Wrapper.hpp:91
WrapperArray(const WrapperArray &wrapperArray)
Copy Constructor.
virtual auto CHILD_TAG() const -> string override
A tag name of children.
shared_ptr< ProductArray > reserved
A list for reserved Product(s).
auto toString() const -> string
Return a string represents Wrapper(s) of same type.
An Entity and a container of children Entity objects.
Definition: EntityGroup.hpp:40
auto calcPrice() const -> int
Calculate price of the Wrapper(s)
WrapperArray(const string &name, int price, int volume, int weight)
Construct from arguments of sample.
void optimize()
Optimize to retrieve the best solution.
virtual auto toXML() const -> shared_ptr< XML > override
Get an XML object represents the EntityGroup.
shared_ptr< Wrapper > sample
A sample wrapper used to copy.
A wrapper can contain products.
Definition: Wrapper.hpp:27
An array of wrapper with same category (name).