#pragma once #pragma once #include #include #include namespace yazi { namespace stl { template class Vector { public: Vector(); ~Vector(); void push_back(const T& value); void pop_back(); int size() const; int capacity() const; void reserve(int size); void resize(int size); void show() const; T& at(int index); T& front(); T& back(); bool empty() const; void clear(); T& operator [] (int index); Vector& operator = (const Vector& other); T* data(); void swap(Vector& other); class iterator { public: iterator() : m_pointer(nullptr) {} iterator(T* pointer) : m_pointer(pointer) {} ~iterator() {} bool operator == (const iterator& other) { return m_pointer == other.m_pointer; } bool operator != (const iterator& other) { return m_pointer != other.m_pointer; } iterator& operator = (const iterator& other) { m_pointer = other.m_pointer; return *this; } iterator& operator ++ () { m_pointer += 1; return *this; } iterator operator ++ (int) { iterator it = *this; ++(*this); return it; } iterator operator + (int i) { iterator it = *this; it.m_pointer += i; return it; } iterator& operator += (int i) { m_pointer += i; return *this; } iterator operator - (int i) { iterator it = *this; it.m_pointer -= i; return it; } iterator& operator -= (int i) { m_pointer -= i; return *this; } int operator - (const iterator& other) const { return m_pointer - other.m_pointer; } T& operator * () { return *m_pointer; } T* operator -> () { return m_pointer; } private: T* m_pointer; }; iterator begin(); iterator end(); class reverse_iterator { public: reverse_iterator() : m_pointer(nullptr) {} reverse_iterator(T* pointer) : m_pointer(pointer) {} ~reverse_iterator() {} bool operator == (const reverse_iterator& other) { return m_pointer == other.m_pointer; } bool operator != (const reverse_iterator& other) { return m_pointer != other.m_pointer; } reverse_iterator& operator = (const reverse_iterator& other) { m_pointer = other.m_pointer; return *this; } reverse_iterator& operator ++ () { m_pointer -= 1; return *this; } reverse_iterator operator ++ (int) { reverse_iterator it = *this; ++(*this); return it; } reverse_iterator operator + (int i) { reverse_iterator it = *this; it.m_pointer -= i; return it; } reverse_iterator& operator += (int i) { m_pointer -= i; return *this; } reverse_iterator operator - (int i) { reverse_iterator it = *this; it.m_pointer += i; return it; } reverse_iterator& operator -= (int i) { m_pointer += i; return *this; } T& operator * () { return *m_pointer; } T* operator -> () { return m_pointer; } private: T* m_pointer; }; reverse_iterator rbegin(); reverse_iterator rend(); iterator find(const T& value); iterator rfind(const T& value); iterator insert(iterator pos, const T& value); iterator insert(iterator pos, int n, const T& value); iterator erase(iterator pos); iterator erase(iterator first, iterator last); private: bool is_basic_type(); protected: T* m_data; int m_size; int m_capacity; }; template Vector::Vector() : m_data(nullptr), m_size(0), m_capacity(0) { } template Vector::~Vector() { if (m_data != nullptr) { delete[] m_data; m_data = nullptr; } m_size = 0; m_capacity = 0; } template void Vector::push_back(const T& value) { if (m_size < m_capacity) { m_data[m_size] = value; m_size++; return; } if (m_capacity == 0) { m_capacity = 1; } else { m_capacity *= 2; } T* data = new T[m_capacity]; if (is_basic_type()) { std::memcpy(data, m_data, m_size * sizeof(T)); } else { for (int i = 0; i < m_size; i++) { data[i] = m_data[i]; } } if (m_data != nullptr) { delete[] m_data; m_data = nullptr; } m_data = data; m_data[m_size] = value; m_size++; } template void Vector::pop_back() { if (m_size > 0) { m_size--; } } template int Vector::size() const { return m_size; } template int Vector::capacity() const { return m_capacity; } template void Vector::reserve(int size) { if (m_capacity >= size) { return; } while (m_capacity < size) { if (m_capacity == 0) { m_capacity = 1; } else { m_capacity *= 2; } } T* data = new T[m_capacity]; if (is_basic_type()) { std::memcpy(data, m_data, m_size * sizeof(T)); } else { for (int i = 0; i < m_size; i++) { data[i] = m_data[i]; } } if (m_data != nullptr) { delete[] m_data; m_data = nullptr; } m_data = data; } template void Vector::resize(int size) { if (m_size >= size) { m_size = size; return; } if (size <= m_capacity) { for (int i = m_size; i < size; i++) { m_data[i] = T(); } m_size = size; return; } while (m_capacity < size) { if (m_capacity == 0) { m_capacity = 1; } else { m_capacity *= 2; } } T* data = new T[m_capacity]; if (is_basic_type()) { std::memcpy(data, m_data, m_size * sizeof(T)); } else { for (int i = 0; i < m_size; i++) { data[i] = m_data[i]; } } for (int i = m_size; i < size; i++) { data[i] = T(); } if (m_data != nullptr) { delete[] m_data; m_data = nullptr; } m_data = data; m_size = size; } template void Vector::show() const { std::cout << "size=" << size() << ", capacity=" << capacity() << std::endl; for (int i = 0; i < m_size; i++) { std::cout << m_data[i] << ","; } std::cout << std::endl; } template T& Vector::at(int index) { if (index < 0 || index >= m_size) { throw std::logic_error("out of range"); } return m_data[index]; } template T& Vector::front() { if (m_size <= 0) { throw std::logic_error("vector is empty"); } return m_data[0]; } template T& Vector::back() { if (m_size <= 0) { throw std::logic_error("vector is empty"); } return m_data[m_size - 1]; } template bool Vector::empty() const { return m_size == 0; } template void Vector::clear() { m_size = 0; } template T& Vector::operator [] (int index) { return at(index); } template Vector& Vector::operator = (const Vector& other) { if (m_capacity < other.m_size) { if (m_data != nullptr) { delete[] m_data; m_data = nullptr; m_size = 0; m_capacity = 0; } while (m_capacity < other.m_size) { if (m_capacity == 0) { m_capacity = 1; } else { m_capacity *= 2; } } m_data = new T[m_capacity]; } if (is_basic_type()) { std::memcpy(m_data, other.m_data, m_size * sizeof(T)); } else { for (int i = 0; i < other.m_size; i++) { m_data[i] = other.m_data[i]; } } m_size = other.m_size; return *this; } template T* Vector::data() { return m_data; } template void Vector::swap(Vector& other) { T* data = other.m_data; int size = other.m_size; int capacity = other.m_capacity; other.m_data = m_data; other.m_size = m_size; other.m_capacity = m_capacity; m_data = data; m_size = size; m_capacity = capacity; } template typename Vector::iterator Vector::begin() { Vector::iterator it(m_data); return it; } template typename Vector::iterator Vector::end() { Vector::iterator it(m_data + m_size); return it; } template typename Vector::reverse_iterator Vector::rbegin() { Vector::reverse_iterator it(m_data + m_size - 1); return it; } template typename Vector::reverse_iterator Vector::rend() { Vector::reverse_iterator it(m_data - 1); return it; } template typename Vector::iterator Vector::find(const T& value) { for (Vector::iterator it = begin(); it != end(); it++) { if (*it == value) { return it; } } return end(); } template typename Vector::iterator Vector::rfind(const T& value) { for (Vector::reverse_iterator it = rbegin(); it != rend(); it++) { if (*it == value) { return Vector::iterator(it.operator->()); } } return end(); } template typename Vector::iterator Vector::insert(Vector::iterator pos, const T& value) { return insert(pos, 1, value); } template typename Vector::iterator Vector::insert(Vector::iterator pos, int n, const T& value) { int size = pos - begin(); if (m_size + n <= m_capacity) { if (is_basic_type()) { std::memmove(m_data + size + n, m_data + size, (m_size - size) * sizeof(T)); } else { for (int i = m_size; i > size; i--) { m_data[i + n - 1] = m_data[i - 1]; } } for (int i = 0; i < n; i++) { m_data[size + i] = value; } m_size += n; return Vector::iterator(m_data + size); } while (m_size + n > m_capacity) { if (m_capacity == 0) { m_capacity = 1; } else { m_capacity *= 2; } } T* data = new T[m_capacity]; if (is_basic_type()) { std::memcpy(data, m_data, size * sizeof(T)); } else { for (int i = 0; i < size; i++) { data[i] = m_data[i]; } } for (int i = 0; i < n; i++) { data[size + i] = value; } if (is_basic_type()) { std::memcpy(data + size + n, m_data + size, (m_size - size) * sizeof(T)); } else { for (int i = size; i < m_size; i++) { data[i + n] = m_data[i]; } } if (m_data != nullptr) { delete[] m_data; m_data = nullptr; } m_data = data; m_size += n; return Vector::iterator(m_data + size); } template typename Vector::iterator Vector::erase(Vector::iterator pos) { if (end() - pos == 1) { m_size -= 1; return end(); } int size = pos - begin(); if (is_basic_type()) { std::memmove(m_data + size, m_data + size + 1, (m_size - size - 1) * sizeof(T)); } else { for (int i = size; i < m_size - 1; i++) { m_data[i] = m_data[i + 1]; } } m_size -= 1; return pos; } template typename Vector::iterator Vector::erase(Vector::iterator first, Vector::iterator last) { int f = first - begin(); int l = last - begin(); int n = last - first; if (is_basic_type()) { std::memmove(m_data + f, m_data + l, (m_size - l) * sizeof(T)); } else { for (int i = 0; i < m_size - l; i++) { m_data[f] = m_data[l]; } } m_size -= n; return first; } template bool Vector::is_basic_type() { if (std::is_pointer::value) { return true; } return (typeid(T) == typeid(bool)) || (typeid(T) == typeid(char)) || (typeid(T) == typeid(unsigned char)) || (typeid(T) == typeid(short)) || (typeid(T) == typeid(unsigned short)) || (typeid(T) == typeid(int)) || (typeid(T) == typeid(unsigned int)) || (typeid(T) == typeid(long)) || (typeid(T) == typeid(unsigned long)) || (typeid(T) == typeid(float)) || (typeid(T) == typeid(double)); } } }