#include <bits/stdc++.h>
template<class T>
void changeLength1D(T *&a, int oldLength, int newLength) {
if (newLength < 0) {
throw "error";
}
T *temp = new T[newLength];
int number = std::min(oldLength, newLength);
std::copy(a, a + number, temp);
delete[]a;
a = temp;
}
template<class T>
class linearList {
public:
virtual ~linearList() {};
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T &get(int theIndex) const = 0;
virtual int indexOf(const T &theElement) const = 0;
virtual void erase(int theIndex) = 0;
virtual void insert(int theIndex,const T &theElement) = 0;
virtual void Output(std::ostream &out) const = 0;
};
template<class T>
class arrayList : public linearList<T> {
public:
arrayList(int initialCapacity = 10);
arrayList(const arrayList<T> &);
~arrayList() { delete[]element; }
bool empty() const { return listSize == 0; }
int size() const { return listSize; }
T &get(int theIndex) const;
int indexOf(const T &theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T &theElement);
void Output(std::ostream &out) const;
class iterator {
public:
iterator(T *thePosition = 0) { position = thePosition; }
T &operator*() const { return *position; }
T *operator->() const { return &*position; }
iterator &operator++() {
++position;
return *this;
}
iterator operator++(int) {
iterator old = *this;
++position;
return old;
}
iterator &operator--() {
--position;
return *this;
}
iterator &operator--(int) {
iterator old = *this;
--position;
return old;
}
bool operator!=(const iterator right) const {
return position != right.position;
}
bool operator==(const iterator right) const {
return position == right.position;
}
protected:
T *position;
};
iterator begin() { return iterator(element); }
iterator end() { return iterator(element + listSize); }
protected:
void checkIndex(int theIndex) const;
T *element;
int arrayLenth;
int listSize;
};
template<class T>
arrayList<T>::arrayList(int initialCapacity) {
if (initialCapacity < 1) {
throw "error";
}
arrayLenth = initialCapacity;
element = new T[arrayLenth];
listSize = 0;
}
template<class T>
arrayList<T>::arrayList(const arrayList<T> &theList) {
arrayLenth = theList.arrayLenth;
listSize = theList.listSize;
element = new T[arrayLenth];
copy(theList.element, theList.element + listSize, element);
}
template<class T>
void arrayList<T>::checkIndex(int theIndex) const {
if (theIndex < 0 || theIndex >= listSize) {
throw "error";
}
}
template<class T>
T &arrayList<T>::get(int theIndex) const {
checkIndex(theIndex);
return element[theIndex];
}
template<class T>
int arrayList<T>::indexOf(const T &theElement) const {
int theIndex = (int) (std::find(element, element + listSize, theElement) - element);
if (theIndex == listSize)return -1;
return theIndex;
}
template<class T>
void arrayList<T>::erase(int theIndex) {
checkIndex(theIndex);
std::copy(element + theIndex + 1, element + listSize, element + theIndex);
element[--listSize].~T();
}
template<class T>
void arrayList<T>:: insert(int theIndex, const T &theElement) {
if (theIndex < 0 || theIndex > listSize) {
throw "error";
}
if (listSize == arrayLenth) {
changeLength1D(element, arrayLenth, 2 * arrayLenth);
arrayLenth *= 2;
}
for (int i = listSize - 1; i >= theIndex; --i) {
element[1 + i] = element[i];
}
element[theIndex] = theElement;
++listSize;
}
template <class T>
void arrayList<T>::Output(std::ostream &out) const {
for (int i = 0; i < listSize; ++i)
out << element[i] << ' ';
}
template <class T>
std::ostream& operator<< (std::ostream& out,const arrayList<T>& x) {
x.Output(out);
return out;
}