LCOV - code coverage report
Current view: top level - builds/DaniloBorquez/flex-net-sim/src - link.cpp (source / functions) Hit Total Coverage
Test: commit SHA1 Lines: 261 268 97.4 %
Date: 2025-08-25 21:30:02 Functions: 31 31 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "link.hpp"
       2             : 
       3           5 : Link::Link(void) {
       4           5 :   this->id = -1;
       5           5 :   this->length = DEFAULT_LENGTH;
       6          20 :   this->slots = std::vector<std::vector<std::vector<bool>>>
       7          15 :                 (1, std::vector<std::vector<bool>>(1, std::vector<bool>(DEFAULT_SLOTS,false)));
       8           5 :   this->src = -1;
       9           5 :   this->dst = -1;
      10           5 :   this->number_of_cores = DEFAULT_CORES;
      11           5 :   this->number_of_modes = DEFAULT_MODES;
      12           5 :   this->number_of_bands = DEFAULT_BANDS;
      13          10 :   std::map<char, std::vector<std::vector<std::vector<bool>>>> bns;
      14           5 :   this->bands_and_slots = bns;
      15           5 : }
      16             : 
      17          13 : Link::Link(int id) {
      18          13 :   this->id = id;
      19             : 
      20          13 :   this->length = DEFAULT_LENGTH;
      21          52 :   this->slots = std::vector<std::vector<std::vector<bool>>>
      22          39 :                 (1, std::vector<std::vector<bool>>(1, std::vector<bool>(DEFAULT_SLOTS,false)));
      23          13 :   this->src = -1;
      24          13 :   this->dst = -1;
      25          13 :   this->number_of_cores = DEFAULT_CORES;
      26          13 :   this->number_of_modes = DEFAULT_MODES;
      27          13 :   this->number_of_bands = DEFAULT_BANDS;
      28          26 :   std::map<char, std::vector<std::vector<std::vector<bool>>>> bns;
      29          13 :   this->bands_and_slots = bns;
      30          13 : }
      31             : 
      32           5 : Link::Link(int id, float length) {
      33           4 :   this->id = id;
      34             : 
      35           4 :   if (length <= 0)
      36           1 :     throw std::runtime_error("Cannot create a link with non-positive length.");
      37           3 :   this->length = length;
      38             : 
      39          12 :   this->slots = std::vector<std::vector<std::vector<bool>>>
      40           9 :                 (1, std::vector<std::vector<bool>>(1, std::vector<bool>(DEFAULT_SLOTS,false)));
      41           3 :   this->src = -1;
      42           3 :   this->dst = -1;
      43           3 :   this->number_of_cores = DEFAULT_CORES;
      44           3 :   this->number_of_modes = DEFAULT_MODES;
      45           3 :   this->number_of_bands = DEFAULT_BANDS;
      46           6 :   std::map<char, std::vector<std::vector<std::vector<bool>>>> bns;
      47           3 :   this->bands_and_slots = bns;
      48           3 : }
      49             : 
      50         996 : Link::Link(int id, float length, int slots) {
      51         994 :   this->id = id;
      52             : 
      53         994 :   if (length <= 0)
      54           1 :     throw std::runtime_error("Cannot create a link with non-positive length.");
      55         993 :   this->length = length;
      56             : 
      57         993 :   if (slots < 1)
      58           2 :     throw std::runtime_error("Cannot create a link with " +
      59           3 :                              std::to_string(slots) + " slots.");
      60        3968 :   this->slots = std::vector<std::vector<std::vector<bool>>>
      61        2976 :                 (1, std::vector<std::vector<bool>>(1, std::vector<bool>(slots,false)));
      62         992 :   this->src = -1;
      63         992 :   this->dst = -1;
      64         992 :   this->number_of_cores = DEFAULT_CORES;
      65         992 :   this->number_of_modes = DEFAULT_MODES;
      66         992 :   this->number_of_bands = DEFAULT_BANDS;
      67        1984 :   std::map<char, std::vector<std::vector<std::vector<bool>>>> bns;
      68         992 :   this->bands_and_slots = bns;
      69         992 : }
      70             : 
      71           8 : Link::Link(int id, float length, int slots, int number_of_cores) {
      72           5 :   this->id = id;
      73             : 
      74           5 :   if (length <= 0)
      75           1 :     throw std::runtime_error("Cannot create a link with non-positive length.");
      76           4 :   this->length = length;
      77             : 
      78           4 :   if (slots < 1)
      79           2 :     throw std::runtime_error("Cannot create a link with " +
      80           3 :                              std::to_string(slots) + " slots.");
      81             : 
      82           3 :   if (number_of_cores < 1)
      83           2 :     throw std::runtime_error("Cannot create a link with " +
      84           3 :                              std::to_string(number_of_cores) + " cores.");
      85             : 
      86           8 :   this->slots = std::vector<std::vector<std::vector<bool>>>
      87           8 :                 (number_of_cores, std::vector<std::vector<bool>>
      88           6 :                 (DEFAULT_MODES, std::vector<bool>(slots,false)));
      89           2 :   this->src = -1;
      90           2 :   this->dst = -1;
      91           2 :   this->number_of_cores = number_of_cores;
      92           2 :   this->number_of_modes = DEFAULT_MODES;
      93           2 :   this->number_of_bands = DEFAULT_BANDS;
      94           4 :   std::map<char, std::vector<std::vector<std::vector<bool>>>> bns;
      95           2 :   this->bands_and_slots = bns;
      96           2 : }
      97             : 
      98          50 : Link::Link(int id, float length, int slots, int number_of_cores, int number_of_modes) {
      99          46 :   this->id = id;
     100             : 
     101          46 :   if (length <= 0)
     102           1 :     throw std::runtime_error("Cannot create a link with non-positive length.");
     103          45 :   this->length = length;
     104             : 
     105          45 :   if (slots < 1)
     106           2 :     throw std::runtime_error("Cannot create a link with " +
     107           3 :                              std::to_string(slots) + " slots.");
     108             : 
     109          44 :   if (number_of_cores < 1)
     110           2 :     throw std::runtime_error("Cannot create a link with " +
     111           3 :                              std::to_string(number_of_cores) + " cores.");
     112             : 
     113          43 :   if (number_of_modes < 1)
     114           2 :     throw std::runtime_error("Cannot create a link with " +
     115           3 :                              std::to_string(number_of_modes) + " modes.");
     116             : 
     117         168 :   this->slots = std::vector<std::vector<std::vector<bool>>>
     118         168 :                 (number_of_cores, std::vector<std::vector<bool>>
     119         126 :                 (number_of_modes, std::vector<bool>(slots,false)));
     120          42 :   this->src = -1;
     121          42 :   this->dst = -1;
     122          42 :   this->number_of_cores = number_of_cores;
     123          42 :   this->number_of_modes = number_of_modes;
     124          42 :   this->number_of_bands = DEFAULT_BANDS;
     125          84 :   std::map<char, std::vector<std::vector<std::vector<bool>>>> bns;
     126          42 :   this->bands_and_slots = bns;
     127          42 : }
     128             : 
     129          93 : Link::Link(int id, float length, int slots, std::map<char, int> bands_and_slots) {
     130             :   // it can be remove slots to leave only bands_and_slots and that slots be the total
     131          93 :   this->id = id;
     132             : 
     133          93 :   if (length <= 0)
     134           0 :     throw std::runtime_error("Cannot create a link with non-positive length.");
     135          93 :   this->length = length;
     136             : 
     137          93 :   if (slots < 1)
     138           0 :     throw std::runtime_error("Cannot create a link with " +
     139           0 :                              std::to_string(slots) + " slots.");
     140         372 :   this->slots = std::vector<std::vector<std::vector<bool>>>
     141         279 :                 (1, std::vector<std::vector<bool>>(1, std::vector<bool>(slots,false)));
     142          93 :   this->src = -1;
     143          93 :   this->dst = -1;
     144          93 :   this->number_of_cores = DEFAULT_CORES;
     145          93 :   this->number_of_modes = DEFAULT_MODES;
     146             :   
     147         186 :   std::map<char, std::vector<std::vector<std::vector<bool>>>> bands_and_slots_vect;
     148          93 :   int number_of_bands=0;
     149         447 :   for (const auto& b : bands_and_slots) {
     150        1062 :     bands_and_slots_vect[b.first]=std::vector<std::vector<std::vector<bool>>>
     151         708 :                 (1, std::vector<std::vector<bool>>(1, std::vector<bool>(b.second,false)));
     152         354 :     number_of_bands+=1;
     153             :   }
     154          93 :   this->number_of_bands = number_of_bands;
     155          93 :   this->bands_and_slots = bands_and_slots_vect;
     156          93 : }
     157             : 
     158           3 : void Link::setId(int id) {
     159           3 :   if (this->id != -1)
     160             :     throw std::runtime_error(
     161           1 :         "Cannot set Id to a Link with Id different than -1.");
     162             : 
     163           2 :   this->id = id;
     164           2 : }
     165             : 
     166           2 : void Link::setLength(float length) {
     167           2 :   if (length <= 0)
     168           1 :     throw std::runtime_error("Cannot set a link with non-positive length.");
     169           1 :   this->length = length;
     170           1 : }
     171             : 
     172           6 : void Link::setSlots(int slots) {
     173           6 :   if (slots < 1)
     174           6 :     throw std::runtime_error("Cannot set a link with " + std::to_string(slots) +
     175           9 :                              " slots.");
     176           5 :   for (int i = 0; i < this->getCores(); i++)
     177           5 :     for (int j = 0; j < this->getModes(); j++)
     178         453 :       for (int k = 0; k < this->getSlots(i,j); k++)
     179         451 :         if (this->slots[i][j][k] == true)
     180             :           throw std::runtime_error(
     181           1 :               "Cannot change slots number if at least one slot is active within this link.");
     182             : 
     183           4 :   for (int i = 0; i < this->getCores(); i++)
     184           4 :     for (int j = 0; j < this->getModes(); j++)
     185           2 :       this->slots[i][j].resize(slots);
     186           2 : }
     187             : 
     188     5014908 : void Link::setSlot(int pos, bool value) {
     189     5014908 :   if (pos < 0 || pos >= this->getSlots())
     190           3 :     throw std::runtime_error("Cannot set slot in position out of bounds.");
     191             : 
     192     5014905 :   if (this->getSlot(pos) == value)
     193           1 :     throw std::runtime_error("Slot already setted in desired state.");
     194             : 
     195     5014904 :   this->slots[0][0][pos] = value;
     196     5014904 : }
     197             : 
     198          60 : void Link::setSlot(int pos, char band, bool value) {
     199          60 :   if (pos < 0 || pos >= this->getSlots(band))
     200           2 :     throw std::runtime_error("Cannot set slot in position out of bounds.");
     201             : 
     202          57 :   if (this->getSlot(pos, band) == value)
     203           0 :     throw std::runtime_error("Slot already setted in desired state.");
     204          57 :   bool notInRange=true;
     205         172 :   for (char c : this->getBands()) {
     206         115 :       if (c == band){
     207          57 :         notInRange=false;
     208             :       }
     209             :   }
     210          57 :   if(notInRange){
     211           0 :     throw std::runtime_error("Band does not exist!");
     212             :   }
     213             : 
     214          57 :   this->bands_and_slots[band][0][0][pos] = value;
     215          57 : }
     216             : 
     217           4 : void Link::setCores(int number_of_cores) {
     218           4 :   if (number_of_cores < 1)
     219           2 :     throw std::runtime_error("Cannot set a link with " + std::to_string(number_of_cores) +
     220           3 :                              " cores.");
     221             : 
     222           3 :   if (this->getCores() == number_of_cores)
     223           1 :     throw std::runtime_error("Number of cores is already the desired.");
     224             : 
     225           5 :   for (int i = 0; i < this->getCores(); i++)
     226          11 :     for (int j = 0; j < this->getModes(); j++)
     227        1428 :       for (int k = 0; k < this->getSlots(i,j); k++)
     228        1421 :         if (this->slots[i][j][k] == true)
     229             :           throw std::runtime_error(
     230           1 :               "The number of cores cannot be changed if at least one slot is active within this core.");
     231           1 :   this->number_of_cores = number_of_cores;
     232           1 :   this->slots.resize(number_of_cores);
     233           1 : }
     234             : 
     235           4 : void Link::setModes(int number_of_modes) {
     236           4 :   if (number_of_modes < 1)
     237           2 :     throw std::runtime_error("Cannot set a link with " + std::to_string(number_of_modes) +
     238           3 :                              " modes.");
     239             : 
     240           3 :   if (this->getModes() == number_of_modes)
     241           1 :     throw std::runtime_error("Number of slots is already the desired.");
     242             : 
     243           5 :   for (int i = 0; i < this->getCores(); i++)
     244          11 :     for (int j = 0; j < this->getModes(); j++)
     245        1428 :       for (int k = 0; k < this->getSlots(i,j); k++)
     246        1421 :         if (this->slots[i][j][k] == true)
     247             :           throw std::runtime_error(
     248           1 :               "The number of modes cannot be changed if at least one slot is active within this mode.");
     249             : 
     250           1 :   this->number_of_modes = number_of_modes;
     251           4 :   for (int i = 0; i < this->getCores(); i++)
     252           3 :     this->slots[i].resize(number_of_modes);
     253           1 : }
     254             : 
     255         129 : void Link::setSlots(int slots, int core, int mode) {
     256         129 :   if (core < 0 || core >= this->getCores())
     257           1 :     throw std::runtime_error("Cannot set number of slots in core out of bounds.");
     258             : 
     259         128 :   if (mode < 0 || mode >= this->getModes())
     260           1 :     throw std::runtime_error("Cannot set number of slots in mode out of bounds.");
     261             : 
     262         127 :   if (slots < 1)
     263           2 :     throw std::runtime_error("Cannot set a link with " + std::to_string(slots) +
     264           3 :                              " slots.");
     265             : 
     266         126 :   if (this->getSlots(core, mode) == slots)
     267           2 :     throw std::runtime_error("Number of modes is already the desired.");
     268             : 
     269         744 :   for (int i = 0; i < this->getSlots(core, mode); i++) 
     270         622 :     if (this->slots[core][mode][i] == true)
     271             :       throw std::runtime_error(
     272           2 :           "Cannot change slots number if at least one slot is active.");
     273             : 
     274         122 :   this->slots[core][mode].resize(slots);
     275         122 : }
     276             : 
     277         119 : void Link::setSlot(int core, int mode, int pos, bool value) {
     278         119 :   if (core < 0 || core >= this->getCores())
     279           1 :     throw std::runtime_error("Cannot set slot in core out of bounds.");
     280             : 
     281         118 :   if (mode < 0 || mode >= this->getModes())
     282           1 :     throw std::runtime_error("Cannot set slot in mode out of bounds.");
     283             : 
     284         117 :   if (pos < 0 || pos >= this->getSlots(core, mode))
     285           3 :     throw std::runtime_error("Cannot set slot in position out of bounds.");
     286             : 
     287         114 :   if (this->getSlot(core, mode, pos) == value)
     288           1 :     throw std::runtime_error("Slot already setted in desired state.");
     289             : 
     290         113 :   this->slots[core][mode][pos] = value;
     291         113 : }
     292             : 
     293           1 : void Link::setBands(std::map<char, int> bands_and_slots) {
     294           2 :   std::map<char, std::vector<std::vector<std::vector<bool>>>> bands_and_slots_vect;
     295           4 :   for (const auto& b : bands_and_slots) {
     296           9 :     bands_and_slots_vect[b.first]=std::vector<std::vector<std::vector<bool>>>
     297           6 :                 (1, std::vector<std::vector<bool>>(1, std::vector<bool>(b.second,false)));
     298             :   }
     299           1 :   this->bands_and_slots = bands_and_slots_vect;
     300           1 : }
     301             : 
     302             : 
     303      403228 : int Link::getId(void) const { return this->id; }
     304             : 
     305           3 : float Link::getLength(void) const { return this->length; }
     306             : 
     307    30884615 : int Link::getSlots(void) const { return this->slots[0][0].size(); }
     308             : 
     309         201 : int Link::getSlots(char band) const { return this->bands_and_slots.at(band)[0][0].size(); }
     310             : 
     311        4355 : int Link::getSlots(int core, int mode) const { return this->slots[core][mode].size(); }
     312             : 
     313    15368744 : bool Link::getSlot(int pos) const {
     314    15368744 :   if (pos < 0 || pos >= this->getSlots())
     315           2 :     throw std::runtime_error("Cannot get slot in position out of bounds.");
     316             : 
     317    15368742 :   return this->slots[0][0][pos];
     318             : }
     319             : 
     320         155 : bool Link::getSlot(int core, int mode, int pos) const {
     321         155 :   if (core < 0 || core >= this->getCores())
     322           1 :     throw std::runtime_error("Cannot get number of slots in core out of bounds.");
     323             : 
     324         154 :   if (mode < 0 || mode >= this->getModes())
     325           1 :     throw std::runtime_error("Cannot get number of slots in mode out of bounds.");
     326             : 
     327         153 :   if (pos < 0 || pos >= this->getSlots())
     328           2 :     throw std::runtime_error("Cannot get slot in position out of bounds.");
     329             : 
     330         151 :   return this->slots[core][mode][pos];
     331             : }
     332             : 
     333         180 : std::vector<char> Link::getBands(void) const { 
     334         180 :   std::vector<char> bands;
     335         543 :   for (const auto& b : this->bands_and_slots) {
     336         363 :     bands.push_back(b.first);
     337             :   }
     338         180 :   return bands;
     339             : }
     340             : 
     341          98 : bool Link::getSlot(int pos, char band) const {
     342          98 :   if (pos < 0 || pos >= this->getSlots(band))
     343           0 :     throw std::runtime_error("Cannot get slot in position out of bounds.");
     344             : 
     345         117 :   for (char c : this->getBands()) {
     346         117 :       if (c == band){
     347         194 :         return this->bands_and_slots.at(band)[0][0][pos];
     348             :       }
     349             :   }
     350           0 :   throw std::runtime_error("Cannot get slot in position on a non-existent band");
     351             : }
     352             : 
     353         430 : int Link::getCores(void) const { return this->number_of_cores; }
     354             : 
     355         435 : int Link::getModes(void) const { return this->number_of_modes; }
     356             : 
     357           5 : int Link::getSrc(void) const { return this->src; }
     358             : 
     359           5 : int Link::getDst(void) const { return this->dst; }
     360             : 
     361           1 : int Link::getNumberOfBands(void) const { return this->number_of_bands; }
     362             : 

Generated by: LCOV version 1.13