2 #include <samchon/protocol/SharedEntityArray.hpp> 3 #include <samchon/examples/packer/WrapperArray.hpp> 5 #include <samchon/library/CombinedPermutationGenerator.hpp> 17 using namespace library;
18 using namespace protocol;
58 this->productArray = make_shared<ProductArray>();
67 Packer(shared_ptr<ProductArray> productArray)
70 this->productArray = productArray;
81 :
Packer(packer.productArray)
83 this->reserve(packer.size());
85 for (
size_t i = 0; i < packer.size(); i++)
89 virtual void construct(shared_ptr<XML> xml)
override 91 super::construct(xml);
93 productArray->construct(xml->get(productArray->TAG())->at(0));
97 virtual auto createChild(shared_ptr<XML>) ->
WrapperArray*
override 111 return productArray->size();
120 for (
size_t i = 0; i < size(); i++)
121 price += at(i)->calcPrice();
134 if(empty() ==
true || productArray->empty() ==
true)
138 shared_ptr<Packer> minPacker =
nullptr;
140 if (size == -1 || start + size > caseGenerator.size())
141 size = caseGenerator.
size() - start;
143 for (
size_t i = start; i < start + size; i++)
145 vector<size_t> &row = caseGenerator[i];
146 shared_ptr<Packer> packer(
new Packer(*
this));
148 bool validity =
true;
150 for (
size_t j = 0; j < row.size(); j++)
152 shared_ptr<Product> &product = productArray->at(j);
153 shared_ptr<WrapperArray> &wrapperArray = packer->at( row[j] );
155 if (wrapperArray->tryInsert(product) ==
false)
162 if(validity ==
false)
166 for (
size_t j = 0; j < packer->size(); j++)
167 packer->at(j)->optimize();
169 if (minPacker ==
nullptr ||
170 packer->calcPrice() < minPacker->calcPrice())
177 assign(minPacker->begin(), minPacker->end());
183 virtual auto TAG()
const ->
string override 189 return "wrapperArray";
192 virtual auto toXML() const -> shared_ptr<
XML>
override 194 shared_ptr<XML> &xml = super::toXML();
195 xml->push_back(productArray->toXML());
205 string str =
"$" + std::to_string(calcPrice()) +
"\n";
206 for (
size_t i = 0; i < size(); i++)
207 str += at(i)->toString() +
"\n";
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));
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));
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));
void optimize(size_t start=0, size_t size=-1)
Find the best packaging method.
A combined-permutation case generator.
auto size() const -> size_t
Get size of all cases.
auto productSize() const -> size_t
Get number of products to package.
shared_ptr< ProductArray > productArray
Prodcut(s) to package in some Wrapper(s)
auto toString() const -> string
Return a string represents an packaging method.
auto calcPrice() const -> int
Calculate price of the wrappers.
virtual auto toXML() const -> shared_ptr< XML > override
Get an XML object represents the EntityGroup.
An Entity and a container of children Entity objects.
Packer()
Default Constructor.
virtual auto CHILD_TAG() const -> string override
A tag name of children.
An array of Product objects.
Packer(shared_ptr< ProductArray > productArray)
Construct from products and wrapper.
A packer planning the best packaging.
An array of wrapper with same category (name).
Packer(const Packer &packer)
Copy Constructor.