Advertisement
Neveles

© 2020 Neveles. All rights reserved.

May 31st, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. void fokin::MatrixShape::addShape(const shape_ptr &shape)
  2. {
  3.   if (!shape)
  4.   {
  5.     throw std::invalid_argument("Object does not exist!");
  6.   }
  7.  
  8.   size_t row = 0;
  9.   size_t column = 0;
  10.   bool add = false;
  11.  
  12.   for (size_t i = 0; (i < rows_) && (column != columns_) && !add; i++)
  13.   {
  14.     for (size_t j = 0; j < columns_; j++)
  15.     {
  16.       if (array_[i * columns_ + j] == nullptr)
  17.       {
  18.         column = j;
  19.         add = true;
  20.         break;
  21.       }
  22.  
  23.       if (isOverlapped(shape, array_[i * columns_ + j]))
  24.       {
  25.         row = i + 1;
  26.         break;
  27.       }
  28.  
  29.       if (j == columns_ - 1)
  30.       {
  31.         column = columns_;
  32.         break;
  33.       }
  34.     }
  35.   }
  36.  
  37.   if (row == rows_)
  38.   {
  39.     if (columns_ == 0)
  40.     {
  41.       columns_++;
  42.     }
  43.  
  44.     shape_array temp = std::make_unique<shape_ptr[]>(++rows_ * columns_);
  45.  
  46.     for (size_t i = 0; i < (rows_ - 1) * columns_; i++)
  47.     {
  48.       temp[i] = array_[i];
  49.     }
  50.  
  51.     for (size_t i = (rows_ - 1) * columns_; i < rows_ * columns_; i++)
  52.     {
  53.       temp[i] = nullptr;
  54.     }
  55.  
  56.     array_.swap(temp);
  57.     array_[(rows_ - 1) * columns_] = shape;
  58.   }
  59.  
  60.   else if (column == columns_)
  61.   {
  62.     shape_array temp = std::make_unique<shape_ptr[]>(rows_ * ++columns_);
  63.  
  64.     for (size_t i = 0; i < rows_; i++)
  65.     {
  66.       for (size_t j = 0; j < columns_ - 1; j++)
  67.       {
  68.         temp[i * columns_ + j] = array_[i * columns_ + j];
  69.       }
  70.  
  71.       temp[i * columns_ + columns_ - 1] = nullptr;
  72.     }
  73.  
  74.     array_.swap(temp);
  75.     array_[row * columns_ + columns_ - 1] = shape;
  76.   }
  77.  
  78.   else
  79.   {
  80.     array_[row * columns_ + column] = shape;
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement