-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicleManager.hpp
102 lines (80 loc) · 2.28 KB
/
VehicleManager.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
#pragma once
#pragma once
#include <vector>
#include <random>
#include <iostream>
#include "Util.hpp"
#include "Vehicle.hpp"
class VehicleManager
{
private:
std::vector<std::unique_ptr<Vehicle>> Vehicles;
public:
VehicleManager() = delete;
VehicleManager(int MaxFactoryWeightProduction)
{
std::mt19937_64 gen;
auto seed = std::random_device{}();
gen.seed(seed);
std::uniform_int_distribution<int> weight_distribution(9000, 16000);
int VehicleCounter = 0;
while (true)
{
char VehicleName = IndexToCapitalLetter(VehicleCounter);
int Capacity = weight_distribution(gen);
std::unique_ptr<Vehicle> NewVehicle = std::make_unique<Vehicle>(Capacity, VehicleName);
std::cout << "Creating vehicle #[" << VehicleCounter + 1 << "]: " << *NewVehicle.get() << std::endl;
Vehicles.push_back(std::move(NewVehicle));
VehicleCounter++;
MaxFactoryWeightProduction -= Capacity;
if (MaxFactoryWeightProduction < 0)
{
break;
}
}
}
void DispatchCollection(std::vector<Product>& Products)
{
std::mutex Mutex;
std::vector<std::thread> Threads;
for (auto& Vehicle : Vehicles)
{
while (true)
{
if (Products.size() == 0)
{
break;
}
Product& TargetProduct = Products.at(Products.size() - 1);
if (!Vehicle->Fill(TargetProduct))
{
break;
}
else
{
Products.pop_back();
}
}
std::thread t([&Vehicle, &Mutex]()
{
if (Mutex.try_lock())
{
Vehicle->DispatchDelivery();
Mutex.unlock();
}
});
Threads.push_back(std::move(t));
}
for (auto& Thread : Threads)
{
Thread.join();
}
}
void PrintAverages()
{
for (const auto& Vehicle : Vehicles)
{
Vehicle->PrintAverages();
}
}
};