Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <algorithm>
- using namespace std;
- template <class T>
- class Array
- {
- private:
- T* arr = nullptr;
- int size = 0;
- int grow = 1;
- int bound = 0;
- void CheckIndex(int index)const;
- public:
- Array<T>() = default;
- Array<T>(int size, int grow = 1);
- Array<T>(initializer_list<T> values, int grow = 1);
- Array<T>(const Array<T>& arr);
- Array<T>(Array&& arr);
- int GetSize()const;
- void SetSize(int size, int grow = 1);
- int GetUpperBound()const;
- bool IsEmpty()const;
- void FreeExtra();
- void RemoveAll();
- T GetAt(int index)const;
- void SetAt(int index, T value);
- void Add(T value);
- void Append(const Array& arr);
- T* GetData()const;
- void InsertAt(int index, T value);
- void InsertAt(int index, initializer_list<T> values);
- void RemoveAt(int index);
- void RemoveAt(int first, int last);
- Array<T>& operator=(const Array& arr);
- const T& operator[](int index)const;
- T& operator[](int index);
- ~Array<T>();
- };
- template<class T>
- inline void Array<T>::CheckIndex(int index)const
- {
- if (index < 0 || index > bound - 1) throw "Index out of bounds";
- }
- template<class T>
- inline int Array<T>::GetSize()const
- {
- return size;
- }
- template<class T>
- inline void Array<T>::SetSize(int size, int grow)
- {
- if (grow < 1) throw "Grow must be greater than zero.";
- if (arr == nullptr)
- {
- arr = new T[size]{};
- this->size = size;
- this->grow = grow;
- bound = size;
- }
- else
- {
- T* arr_new = new T[size]{};
- int smaller_size = this->size < size ? this->size : size;
- copy(arr, arr + smaller_size, arr_new);
- bound = smaller_size;
- this->size = size;
- this->grow = grow;
- delete[] arr;
- arr = arr_new;
- }
- }
- template<class T>
- inline int Array<T>::GetUpperBound()const
- {
- return bound - 1;
- }
- template<class T>
- inline bool Array<T>::IsEmpty()const
- {
- return bound == 0;
- }
- template<class T>
- inline void Array<T>::FreeExtra()
- {
- if (bound == size) return;
- T* arr_new = new T[bound]{};
- copy(arr, arr + bound, arr_new);
- delete[] arr;
- arr = arr_new;
- size = bound;
- }
- template<class T>
- inline void Array<T>::RemoveAll()
- {
- delete[] arr;
- arr = nullptr;
- size = bound = 0;
- }
- template<class T>
- inline T Array<T>::GetAt(int index)const
- {
- CheckIndex(index);
- return arr[index];
- }
- template<class T>
- inline void Array<T>::SetAt(int index, T value)
- {
- CheckIndex(index);
- arr[index] = value;
- }
- template<class T>
- inline void Array<T>::Add(T value)
- {
- if (bound == size) SetSize(size + grow, grow);
- arr[bound++ - 1] = value;
- }
- template<class T>
- inline void Array<T>::Append(const Array& arr)
- {
- if (bound + arr.bound > size) SetSize(bound + arr.bound, grow);
- copy(arr.arr, arr.arr + arr.bound, this->arr + bound);
- bound += arr.bound;
- }
- template<class T>
- inline T* Array<T>::GetData() const
- {
- return arr;
- }
- template<class T>
- inline void Array<T>::InsertAt(int index, T value)
- {
- if (index < 0 && index > bound) throw "Index out of bounds";
- if (bound == size) SetSize(size + grow, grow);
- copy(arr + index, arr + bound, arr + index + 1);
- arr[index] = value;
- bound++;
- }
- template<class T>
- inline void Array<T>::InsertAt(int index, initializer_list<T> values)
- {
- if (index < 0 && index > bound) throw "Index out of bounds";
- if (size - bound < values.size()) SetSize(bound + values.size(), grow);
- copy(arr + index, arr + bound, arr + index + values.size() - 1);
- copy(values.begin(), values.end(), arr + index);
- bound++;
- }
- template<class T>
- inline void Array<T>::RemoveAt(int index)
- {
- CheckIndex(index);
- if (index != bound - 1)
- copy(arr + index + 1, arr + bound, arr + index);
- bound--;
- }
- template<class T>
- inline void Array<T>::RemoveAt(int first, int last)
- {
- CheckIndex(first);
- CheckIndex(last);
- if (first == last); return;
- if (first > last) swap(first, last);
- copy(arr, arr + first, arr);
- copy(arr + last, arr + bound, arr);
- bound -= last - first;
- }
- template<class T>
- inline Array<T>& Array<T>::operator=(const Array& arr)
- {
- if (this == &arr) return *this;
- if (this->arr != nullptr) delete[] this->arr;
- SetSize(arr.size, arr.grow);
- copy(arr.arr, arr.arr + bound, this->arr);
- return *this;
- }
- template<class T>
- inline const T& Array<T>::operator[](int index) const
- {
- CheckIndex(index);
- return arr[index];
- }
- template<class T>
- inline T& Array<T>::operator[](int index)
- {
- CheckIndex(index);
- return arr[index];
- }
- template<class T>
- inline Array<T>::Array(int size, int grow)
- {
- SetSize(size, grow);
- }
- template<class T>
- inline Array<T>::Array(initializer_list<T> values, int grow)
- {
- SetSize(values.size(), grow);
- copy(values.begin(), values.end(), arr);
- bound = size;
- }
- template<class T>
- inline Array<T>::Array(const Array<T>& arr)
- {
- SetSize(arr.size, arr.grow);
- copy(arr.arr, arr.arr + arr.bound, this->arr);
- bound = arr.bound;
- }
- template<class T>
- inline Array<T>::Array(Array&& arr) :size(arr.size), grow(grow), bound(bound)
- {
- arr = arr.arr;
- arr.arr = nullptr;
- arr.size = arr.bound = 0;
- }
- template<class T>
- inline Array<T>::~Array()
- {
- if (arr != nullptr) delete[] arr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement