/
dolpement
/
OOP
Обзор
Документация
Войти
/
dolpement
/
OOP
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
dynamic_array_prj/dynamic_array/dynamic_array.cpp
198 строк
4 KB
dolpement
OOP
16 июл 2025, 15:40
16 июл 2025, 15:40
d149512
Код
Авторство
О чём код?
#include "dynamic_array.hpp" #include <stdexcept> #include <algorithm> DynamicArray::DynamicArray(int64_t size, int value) : size(size), capacity(size), data(size > 0 ? new int[size] : nullptr) { if (data) { std::fill(data, data + size, value); } } DynamicArray::DynamicArray(const std::initializer_list<int>& list) : size(list.size()), capacity(list.size()), data(size > 0 ? new int[size] : nullptr) { std::copy(list.begin(), list.end(), data); } DynamicArray::DynamicArray(const DynamicArray& other) : size(other.size), capacity(other.capacity), data(other.size > 0 ? new int[other.capacity] : nullptr) { std::copy(other.data, other.data + size, data); } DynamicArray::~DynamicArray() { delete[] data; } int64_t DynamicArray::Size() const { return size; } int64_t DynamicArray::Capacity() const { return capacity; } bool DynamicArray::empty() const { return size == 0; } void DynamicArray::push_back(int value) { if (size == capacity) { int64_t new_capacity = (capacity == 0) ? 1 : capacity * 2; int* new_data = new int[new_capacity]; std::copy(data, data+size, new_data); delete[] data; data = new_data; capacity = new_capacity; } data[size] = value; ++size; } void DynamicArray::pop_back() { if (size > 0) { --size; } else { throw std::out_of_range("Index out of range"); } } void DynamicArray::clear() { size = 0; } void DynamicArray::erase(int64_t index) { if (index < 0 || index >= size) { throw std::out_of_range("Index out of range"); } if (size == 0) { return; } for (int64_t i = index; i < size - 1; ++i) { data[i] = data[i + 1]; } --size; } void DynamicArray::resize(int64_t new_size) { if (new_size < 0) { throw std::invalid_argument("New size must be non-negative"); } if (new_size > capacity) { int64_t new_capacity = new_size * 2; int* new_data = new int[new_capacity]; std::copy(data, data+size, new_data); delete[] data; data = new_data; capacity = new_capacity; } if (new_size > size) { for (int64_t i = size; i < new_size; ++i) { data[i] = 0; } } size = new_size; } void DynamicArray::assign(int64_t new_size, int value) { resize(new_size); if (data) { std::fill(data, data + size, value); } else { throw std::bad_alloc(); } } void DynamicArray::insert(int64_t index, const int value) { if (index < 0 || index > size) { throw std::out_of_range("Index out of range"); } if (size >= capacity) { int64_t new_capacity = (capacity == 0) ? 1 : capacity * 2; int* new_data = new int[new_capacity]; for (int64_t i = 0; i < size; ++i) { new_data[i] = data[i]; } delete[] data; data = new_data; capacity = new_capacity; } for (int64_t i = size; i > index; --i) { data[i] = data[i - 1]; } data[index] = value; ++size; } void DynamicArray::swap(DynamicArray& other) { std::swap(size, other.size); std::swap(capacity, other.capacity); std::swap(data, other.data); } int* DynamicArray::begin() { return data; } int* DynamicArray::end() { return data + size; } int& DynamicArray::at(int64_t i) const { if (i < 0 || i >= size) { throw std::out_of_range("Индекс вне массива"); } return data[i]; } int& DynamicArray::operator[](int64_t i) const { if (i < 0 || i >= size) { throw std::out_of_range("Index out of range"); } return data[i]; } DynamicArray& DynamicArray::operator=(const DynamicArray& rhs) { if (this != &rhs) { delete[] data; size = rhs.size; capacity = rhs.capacity; data = new int[capacity]; std::copy(rhs.data, rhs.data + size, data); } return *this; } bool DynamicArray::operator==(const DynamicArray &rhs) const { if (size != rhs.size) { return false; } for (int i = 0; i<size; i++) { if (data[i] != rhs.data[i]) { return false; } } return true; } bool DynamicArray::operator!=(const DynamicArray &rhs) const { if (size != rhs.size) { return true; } else { for (int i = 0; i<size; i++) { if (data[i] == rhs.data[i]) { return false; } } return true; } }