Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <cstddef>
- #include <utility>
- template <typename T>
- class Vector {
- public:
- Vector() = default;
- Vector(size_t alloc_number)
- : _capacity(alloc_number), _size(alloc_number) {
- _data = reinterpret_cast<T*>(new unsigned char[alloc_number * sizeof(T)]);
- for (size_t i = 0; i != alloc_number; ++i)
- new (_data + i) T();
- }
- Vector(const Vector<T>& other)
- : _capacity(other._capacity), _size(other._size) {
- _data = reinterpret_cast<T*>(new unsigned char[other._size * sizeof(T)]);
- for (size_t i = 0; i != other._size; ++i)
- new (_data + i) T(other._data[i]);
- }
- Vector<T>& operator=(const Vector<T>& other) {
- if (other._size <= _capacity) {
- for (size_t i = 0; i != other._size; ++i)
- _data[i] = other._data[i];
- for (size_t i = other._size; i < _size; ++i)
- (_data + i)->~T();
- _size = other._size;
- } else {
- T* new_data = reinterpret_cast<T*>(new unsigned char[other._size * sizeof(T)]);
- for (size_t i = 0; i != other._size; ++i)
- new (new_data + i) T(other._data[i]);
- std::swap(_data, new_data);
- _size = other._size;
- _capacity = other._capacity;
- for (size_t i = 0; i != _size; ++i)
- (new_data + i)->~T();
- delete[] reinterpret_cast<unsigned char*>(new_data);
- }
- return *this;
- }
- T* begin() {
- return _data;
- }
- const T* begin() const {
- return _data;
- }
- T* end() {
- return _data + _size;
- }
- const T* end() const {
- return _data + _size;
- }
- size_t size() const {
- return _size;
- }
- size_t capacity() const {
- return _capacity;
- }
- T& operator[](size_t index) {
- return _data[index];
- }
- const T& operator[](size_t index) const {
- return _data[index];
- }
- void reserve(size_t new_capacity) {
- if (new_capacity > _capacity) {
- T* new_data = reinterpret_cast<T*>(new unsigned char[new_capacity * sizeof(T)]);
- for (size_t i = 0; i != _size; ++i) {
- new (new_data + i) T(_data[i]);
- }
- std::swap(_data, new_data);
- for (size_t i = 0; i != _size; ++i)
- (new_data + i)->~T();
- delete[] reinterpret_cast<unsigned char*>(new_data);
- _capacity = new_capacity;
- }
- }
- void resize(size_t new_size) {
- if (new_size >= _size) {
- if (new_size > _capacity) {
- T* new_data = reinterpret_cast<T*>(new unsigned char[new_size * sizeof(T)]);
- for (size_t i = 0; i != _size; ++i)
- new (new_data + i) T(_data[i]);
- for (size_t i = _size; i != new_size; ++i)
- new (new_data + i) T();
- std::swap(_data, new_data);
- _capacity = new_size;
- for (size_t i = 0; i != _size; ++i)
- (new_data + i)->~T();
- delete[] reinterpret_cast<unsigned char*>(new_data);
- } else {
- for (size_t i = _size; i != new_size; ++i)
- new (_data + i) T();
- }
- } else {
- for (size_t cnt = 0; cnt != _size - new_size; ++cnt)
- (_data + _size - 1 - cnt)->~T();
- }
- _size = new_size;
- }
- void clear() {
- for (size_t i = 0; i != _size; ++i)
- (_data + _size - 1 - i)->~T();
- _data = nullptr;
- _size = 0;
- }
- void push_back(const T& value) {
- if (!_capacity) {
- this->reserve(1);
- } else if (_capacity == _size) {
- this->reserve(_capacity * 2);
- }
- new (_data + _size) T(value);
- _size++;
- }
- void push_back(T&& value) {
- if (!_capacity) {
- this->reserve(1);
- } else if (_capacity == _size) {
- this->reserve(_capacity * 2);
- }
- new (_data + _size) T(std::move(value));
- _size++;
- }
- void pop_back() {
- --_size;
- (_data + _size)->~T();
- }
- void swap(Vector<T>& other) noexcept {
- std::swap(_data, other._data);
- std::swap(_size, other._size);
- std::swap(_capacity, other._capacity);
- }
- ~Vector() {
- for (size_t i = 0; i != _size; ++i)
- (_data + i)->~T();
- delete[] reinterpret_cast<unsigned char*>(_data);
- }
- private:
- T * _data = nullptr;
- size_t _size = 0;
- size_t _capacity = 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement