Line data Source code
1 : #include "connection.hpp"
2 : #include "defragmentator.hpp"
3 : #include <vector>
4 :
5 0 : Defragmentator::Defragmentator(std::shared_ptr<Network> network) {
6 0 : this->network = network;
7 0 : this->name = std::string("No name");
8 0 : }
9 :
10 0 : Defragmentator::Defragmentator(void) {
11 0 : this->network = nullptr;
12 0 : this->name = "No name";
13 0 : }
14 :
15 0 : void Defragmentator::exec(std::vector<Connection> connections) {
16 : throw std::runtime_error("You must implement a method to defragmentate the "
17 : "network. You can do this "
18 0 : "by making an inherited class from Allocator.");
19 : }
20 :
21 0 : std::string Defragmentator::getName() { return this->name; }
22 :
23 0 : void Defragmentator::setName(std::string name) { this->name = name; }
24 :
25 0 : void Defragmentator::setNetwork(std::shared_ptr<Network> network) { this->network = network; }
26 :
27 0 : void Defragmentator::realloc(Connection connection) {
28 0 : int idConnection = connection.getId();
29 0 : for (unsigned int i = 0; i < this->connections.size(); i++) {
30 0 : if (this->connections[i].id == idConnection) { // Find the connection
31 0 : for (unsigned int j = 0; j < this->connections[i].links.size(); j++) {
32 0 : for (unsigned int k = 0; k < this->connections[i].slots[j].size();
33 : k++) {
34 0 : this->network->unuseSlot(this->connections[i].links[j],
35 0 : this->connections[i].slots[j][k]);
36 : }
37 : }
38 0 : Connection *con = &(this->connections[i]);
39 :
40 0 : con->slots.clear();
41 0 : con->links.clear();
42 0 : con->links = connection.links;
43 0 : con->slots = connection.slots;
44 :
45 0 : for (unsigned int j = 0; j < con->links.size(); j++) {
46 0 : for (unsigned int k = 0; k < con->slots[j].size(); k++) {
47 0 : this->network->useSlot(con->links[j], con->slots[j][k]);
48 : }
49 : }
50 0 : break;
51 : }
52 : }
53 0 : }
54 :
55 0 : void Defragmentator::setPaths(
56 : std::shared_ptr<Paths> path) {
57 0 : this->paths = path;
58 0 : }
59 :
60 0 : bool Defragmentator::isSlotInConnection(Connection connection, int slot) {
61 0 : std::vector<std::vector<int>> slots = connection.getSlots();
62 0 : if (slots.empty()) return false;
63 :
64 0 : std::vector<int> firstSlot = slots.front();
65 :
66 0 : for (unsigned int i = 0; i < firstSlot.size(); i++) {
67 0 : if (firstSlot[i] == slot) {
68 0 : return true;
69 : }
70 : }
71 0 : return false;
72 : }
|