-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactoryManager.hpp
103 lines (82 loc) · 2.79 KB
/
FactoryManager.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once
#include <vector>
#include <random>
#include <iostream>
#include <mutex>
#include "Util.hpp"
#include "Factory.hpp"
class FactoryManager
{
private:
std::vector<std::unique_ptr<Factory>> Factories;
public:
struct FactoryProduction
{
char FactoryName = '?';
std::vector<Product> ProducedProducts;
};
FactoryManager() = delete;
FactoryManager(int FactoryCount, const int N)
{
std::mt19937_64 gen;
auto seed = std::random_device{}();
gen.seed(seed);
std::uniform_real_distribution<float> float_distribution(-0.3f, 0.3f);
std::uniform_int_distribution<int> weight_distribution(10, 128);
std::uniform_int_distribution<int> wrapper_distribution(0, static_cast<int>(WrapperType::Max) - 1);
for (int i = 0; i < FactoryCount; ++i)
{
int ProductsPerHour = static_cast<int>(N + (N * float_distribution(gen)));
char FactoryName = IndexToCapitalLetter(i);
char ProductName = IndexToLowercaseLetter(i);
int ProductWeight = weight_distribution(gen);
WrapperType ProductWrapper = static_cast<WrapperType>(wrapper_distribution(gen));
std::unique_ptr<Factory> NewFactory = std::make_unique<Factory>(ProductsPerHour, FactoryName, ProductName, ProductWeight, ProductWrapper);
std::cout << "Creating factory #[" << i + 1 << "]: " << *NewFactory.get() << "\n";
Factories.push_back(std::move(NewFactory));
}
std::cout << "\n";
}
void GatherProducts(std::vector<FactoryProduction>& Production)
{
std::mutex Mutex;
std::vector<std::thread> Threads;
for (const auto& Factory : Factories)
{
std::thread t([&Factory, &Mutex, &Production]()
{
FactoryProduction NewProduction;
NewProduction.FactoryName = Factory->GetFactoryName();
Factory->Produce(NewProduction.ProducedProducts);
if (Mutex.try_lock())
{
Production.push_back(NewProduction);
Mutex.unlock();
}
});
Threads.push_back(std::move(t));
}
for (auto& Thread : Threads)
{
Thread.join();
}
}
int GetFactoriesMaxWeightProduction()
{
int Result = 0;
for (const auto& Factory : Factories)
{
Result += Factory->GetProductsPerHour() * Factory->GetProductsWeight();
}
return Result;
}
int GetFactoriesProduction()
{
int Result = 0;
for (const auto& Factory : Factories)
{
Result += Factory->GetProductsPerHour();
}
return Result;
}
};