Seredenko-V

Untitled

Jul 24th, 2022 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. Iterator Insert(ConstIterator pos, const Type& value) {
  2.         if (capacity_ == 0) {
  3.             this->PushBack(value);
  4.             return this->begin();
  5.         }
  6.         // Базовая гарантия безопасности исключений
  7.         if (size_ == capacity_) {
  8.             assert(size_ <= capacity_);
  9.             ArrayPtr<Type> tmp_arr(capacity_ * 2);
  10.             Iterator new_pos = std::copy(cbegin(), pos, tmp_arr.Get());
  11.             *new_pos = value;
  12.             std::copy_backward(pos, cend(), tmp_arr.Get() + size_ + 1);
  13.             ++size_;
  14.             capacity_ *= 2;
  15.             simple_vector_.swap(tmp_arr);
  16.             return new_pos;
  17.         } else {
  18.             Iterator new_pos = std::copy_backward(pos, this->cend(), std::next(this->end()));
  19.             *new_pos = value;
  20.             ++size_;
  21.             return new_pos;
  22.         }
  23.     }
  24.  
  25.     Iterator Insert(ConstIterator pos, Type&& value) {
  26.         if (capacity_ == 0) {
  27.             this->PushBack(std::move(value));
  28.             return this->begin();
  29.         }
  30.         // Базовая гарантия безопасности исключений
  31.         if (size_ == capacity_) {
  32.             assert(size_ <= capacity_);
  33.             ArrayPtr<Type> tmp_arr(capacity_ * 2);
  34.             Iterator new_pos = this->begin() + (std::distance(this->cbegin(), pos)); // текущего вектора
  35.             std::move(begin(), new_pos, tmp_arr.Get());
  36.             // в позицию pos перемещаем значение value (во временный массив)
  37.             *(tmp_arr.Get() + (std::distance(this->cbegin(), pos))) = std::move(value);
  38.             std::move_backward(new_pos, end(), tmp_arr.Get() + size_ + 1);
  39.             ++size_;
  40.             capacity_ *= 2;
  41.             simple_vector_ = std::move(tmp_arr);
  42.             return new_pos;
  43.         } else {
  44.             Iterator new_pos = this->begin() + (std::distance(this->cbegin(), pos));
  45.             std::move_backward(new_pos, this->end(), std::next(this->end()));
  46.             *new_pos = std::move(value);
  47.             ++size_;
  48.             return new_pos;
  49.         }
  50.     }
  51.  
  52. void Resize(size_t new_size) {
  53.         if (new_size > size_) {
  54.             SimpleVector new_vector(new_size);
  55.             std::move(this->begin(), this->end(), new_vector.begin());
  56.             this->simple_vector_ = std::move(new_vector.simple_vector_);
  57.             if (new_size > capacity_) {
  58.                 this->capacity_ = std::max(this->capacity_ * 2, new_size);
  59.             }
  60.         }
  61.         this->size_ = new_size;
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment