Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by stas on 6/3/19.
- //
- #pragma once
- #include <iterator>
- template <typename T>
- class MyVector {
- public:
- MyVector() = default;
- explicit MyVector(unsigned int size);
- MyVector(unsigned int size, const T& value);
- template <typename InputIt>
- MyVector(InputIt fst, InputIt lst);
- MyVector(const MyVector& other);
- ~MyVector();
- MyVector<T>& operator= (const MyVector& other);
- void assign(unsigned int count, const T& value);
- template <typename InputIt>
- void assign(InputIt fst, InputIt lst);
- T& operator[] (unsigned int pos);
- const T& operator[] (unsigned int pos) const;
- T& at(unsigned int pos);
- const T& at(unsigned pos) const;
- T& back();
- const T& back() const;
- T& front();
- const T& front() const;
- T* data();
- const T* data() const;
- bool empty() const;
- unsigned int size() const;
- unsigned int capacity() const;
- void resize(unsigned int new_size);
- void resize(unsigned int new_size, const T& value);
- void reserve(unsigned int new_cap);
- void shrink_to_fit();
- void clear();
- void push_back(const T& value);
- void pop_back();
- private:
- unsigned int size_ = 0u;
- unsigned int capacity_ = 0u;
- T* data_ = nullptr;
- };
- template <typename T>
- bool operator== (const MyVector<T>& lhs, const MyVector<T>& rhs);
- template <typename T>
- bool operator!= (const MyVector<T>& lhs, const MyVector<T>& rhs);
- template <typename T>
- bool operator< (const MyVector<T>& lhs, const MyVector<T>& rhs);
- template <typename T>
- bool operator<= (const MyVector<T>& lhs, const MyVector<T>& rhs);
- template <typename T>
- bool operator> (const MyVector<T>& lhs, const MyVector<T>& rhs);
- template <typename T>
- bool operator>= (const MyVector<T>& lhs, const MyVector<T>& rhs);
- template <typename T>
- MyVector<T>::MyVector(unsigned int size) :
- size_(size),
- capacity_(size)
- {
- data_ = new T[capacity_];
- }
- template <typename T>
- MyVector<T>::MyVector(unsigned int size, const T& value) :
- MyVector(size)
- {
- for (unsigned i = 0; i < size; ++i)
- data_[i] = value;
- }
- template <typename T>
- template <typename InputIt>
- MyVector<T>::MyVector(InputIt fst, InputIt lst) :
- size_(std::distance(fst, lst)),
- capacity_(size_)
- {
- data_ = new T[capacity_];
- auto it = fst;
- T* a = data_;
- while (it != lst) {
- *a = *it;
- ++a;
- ++it;
- }
- }
- template <typename T>
- MyVector<T>::MyVector(const MyVector<T>& other) :
- MyVector(other.size_, other.capacity_)
- {
- for (unsigned i = 0; i < size_; ++i)
- data_[i] = other.data_[i];
- }
- template <typename T>
- MyVector<T>::~MyVector() {
- delete[] data_;
- }
- template <typename T>
- MyVector<T>& MyVector<T>::operator= (const MyVector<T>& other) {
- if (this == &other)
- return *this;
- delete[] data_;
- size_ = other.size_;
- capacity_ = other.capacity_;
- data_ = new T[capacity_];
- for (unsigned i = 0; i < size_; ++i)
- data_[i] = other.data_[i];
- return *this;
- }
- template <typename T>
- void MyVector<T>::assign(unsigned int count, const T& value) {
- delete[] data_;
- size_ = count;
- capacity_ = count;
- data_ = new T[capacity_];
- for (unsigned i = 0; i < count; ++i)
- data_[i] = value;
- }
- template <typename T>
- template <typename InputIt>
- void MyVector<T>::assign(InputIt fst, InputIt lst) {
- delete[] data_;
- size_ = std::distance(fst, lst);
- capacity_ = size_;
- data_ = new T[capacity_];
- auto it = fst;
- T* a = data_;
- while (it != lst) {
- *a = *it;
- ++a;
- ++it;
- }
- }
- template <typename T>
- const T& MyVector<T>::operator[] (unsigned int pos) const {
- return *(data_ + pos);
- }
- template <typename T>
- T& MyVector<T>::operator[] (unsigned int pos) {
- const T& res = *this[pos];
- return const_cast<T&>(res);
- }
- template <typename T>
- const T& MyVector<T>::at (unsigned int pos) const {
- if (pos >= size_)
- throw std::out_of_range(std::to_string(pos) + " > " + std::to_string(size_));
- else
- return *(data_ + pos);
- }
- template <typename T>
- T& MyVector<T>::at (unsigned int pos) {
- const T& res = this->at(pos);
- return const_cast<T&>(res);
- }
- template <typename T>
- const T& MyVector<T>::back() const {
- if (size_ == 0)
- throw std::out_of_range("The vector is empty");
- else
- return data_[size_ - 1];
- }
- template <typename T>
- T& MyVector<T>::back() {
- const T& res = this->back();
- return const_cast<T&>(res);
- }
- template <typename T>
- const T& MyVector<T>::front() const {
- if (size_ == 0)
- throw std::out_of_range("The vector is empty");
- else
- return *data_;
- }
- template <typename T>
- T& MyVector<T>::front() {
- const T& res = this->front();
- return const_cast<T&>(res);
- }
- template <typename T>
- const T* MyVector<T>::data() const {
- return data_;
- }
- template <typename T>
- T* MyVector<T>::data() {
- return data_;
- }
- template <typename T>
- bool MyVector<T>::empty() const {
- return size_ == 0;
- }
- template <typename T>
- unsigned int MyVector<T>::size() const {
- return size_;
- }
- template <typename T>
- unsigned int MyVector<T>::capacity() const {
- return capacity_;
- }
- template <typename T>
- void MyVector<T>::reserve(unsigned int new_cap) {
- if (capacity_ >= new_cap)
- return;
- T*& old_data = data_;
- data_ = new T[new_cap];
- for (unsigned int i = 0; i < size_; ++i)
- data_[i] = old_data[i];
- delete[] old_data;
- capacity_ = new_cap;
- }
- template <typename T>
- void MyVector<T>::shrink_to_fit() {
- if (capacity_ > size_) {
- delete[] (data_ + size_, capacity_ - size_);
- capacity_ = size_;
- }
- }
- template <typename T>
- void MyVector<T>::clear() {
- size_ = 0;
- }
- template <typename T>
- void MyVector<T>::resize (unsigned int new_size) {
- reserve(new_size);
- size_ = new_size;
- }
- template <typename T>
- void MyVector<T>::resize(unsigned int new_size, const T& value) {
- reserve(new_size);
- for (unsigned i = size_; i < new_size; ++i)
- data_[i] = value;
- size_ = new_size;
- }
- template <typename T>
- void MyVector<T>::push_back(const T& value) {
- if (size_ == capacity_)
- reserve(capacity_ *= 1.3);
- data_[size_] = value;
- ++size_;
- }
- template <typename T>
- void MyVector<T>::pop_back() {
- if (size_ == 0)
- throw std::out_of_range("The vector is empty");
- else
- --size_;
- }
- template <typename T>
- bool operator== (const MyVector<T>& lhs, const MyVector<T>& rhs) {
- if (lhs.size() != rhs.size())
- return false;
- const T* lhdata = lhs.data(), rhdata = rhs.data();
- for (unsigned i = 0; i < lhs.size(); ++i) {
- if (lhdata[i] != rhdata[i])
- return false;
- }
- return true;
- }
- template <typename T>
- bool operator!= (const MyVector<T>& lhs, const MyVector<T>& rhs) {
- return !(lhs == rhs);
- }
- template <typename T>
- bool operator< (const MyVector<T>& lhs, const MyVector<T>& rhs) {
- const T* lhdata = lhs.data(), rhdata = rhs.data();
- unsigned int size = std::min(lhs.size(), rhs.size());
- for (unsigned i = 0; i < size; ++i) {
- if (lhdata[i] < rhdata[i])
- return true;
- }
- return lhs.size() < rhs.size();
- }
- template <typename T>
- bool operator>= (const MyVector<T>& lhs, const MyVector<T>& rhs) {
- return !(lhs < rhs);
- }
- template <typename T>
- bool operator<= (const MyVector<T>& lhs, const MyVector<T>& rhs) {
- return rhs >= lhs;
- }
- template <typename T>
- bool operator> (const MyVector<T>& lhs, const MyVector<T>& rhs) {
- return rhs < lhs;
- }
Advertisement
Add Comment
Please, Sign In to add comment