Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "Array.h"
- #include <exception>
- using namespace std;
- template <class T>
- Array<T>::Array() {
- List = nullptr;
- this->Size = this->Capacity = 0;
- }
- template <class T>
- Array<T>::Array(int capacity) {
- this->Size = 0;
- this->Capacity = capacity;
- List = new T *[capacity];
- }
- template <class T>
- Array<T>::Array(const Array<T>& otherArray) {
- this->Capacity = otherArray.Capacity;
- this->Size = otherArray.Size;
- List = new T *[Capacity];
- for (int i = 0; i < this->Size; ++i) {
- List[i] = new T;
- *List[i] = *otherArray.List[i];
- }
- }
- template <class T>
- T& Array<T>::operator[] (int index) {
- if (index < 0 || index > this->Capacity)
- if (index < 0 || index >= Size) throw "Index out of range!!!";
- return *List[index];
- }
- template <class T>
- Array<T>::~Array() {
- delete List;
- this->Capacity = this->Size = 0;
- }
- template <class T>
- const Array<T>& Array<T>::operator+=(const T& newElem) {
- if (Size == Capacity)
- reallocation(newElem);
- List[Size] = new T;
- *List[Size++] = newElem;
- return (*this);
- }
- template <class T>
- const Array<T>& Array<T>::Insert(int index, const T& newElem) {
- if (index < 0 || index >= Size + 1) throw "Index out of range";
- if (Size == Capacity)
- reallocation(newElem);
- for (int i = Size; i > index; --i)
- List[i] = List[i - 1];
- List[index] = new T;
- *List[index] = newElem;
- this->Size++;
- return (*this);
- }
- template <class T>
- const Array<T>& Array<T>::Delete(int index) {
- if (index < 0 || index >= Size) throw "Index out of range";
- delete List[index];
- for (int i = index; i < Size - 1; --i)
- List[i] = List[i + 1];
- this->Size--;
- return *this;
- }
- template <class T>
- bool Array<T>::operator=(const Array<T>& otherArray) {
- this->Capacity = otherArray.Capacity;
- this->Size = otherArray.Size;
- List = new T *[Capacity];
- for (int i = 0; i < Size; ++i) {
- List[i] = new T;
- *List[i] = *otherArray.List[i];
- }
- }
- template <class T>
- void Array<T>::Sort() {
- }
- template <class T>
- void Array<T>::Sort(int(*compare)(const T&, const T&)) {
- }
- template <class T>
- void Array<T>::Sort(Compare* comparator) {
- }
- template <class T>
- int Array<T>::GetSize() {
- return this->Size;
- }
- template<class T>
- int Array<T>::GetCapacity() {
- return this->Capacity;
- }
- template<class T>
- void reallocation(int newElems) {
- int newCapacity = min(2 * Capacity, Size + newElems);
- T** newBlock = new T *[newCapacity];
- for (int i = 0; i < Size; ++i)
- newBlock[i] = List[i];
- this->Capacity = newCapacity;
- this->List = newBlock;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement