circularArrayList.cpp
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
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(ostream &out) const = 0;
};
template<class T>
void circularCopy(T *start, T *end, T *initial, int arrayLength, T *dest)
{
int size = end - start;
size = size >= 0 ? size : size + arrayLength;
for (int i = 0; i < size; i++)
{
dest[i] = *(initial + ((i + (start - initial)) % arrayLength));
}
}
template<class T>
void changeLength1D(T *&a, int oldLength, int newLength)
{
if (newLength < 0)
throw logic_error("new length must be >= 0");
T *temp = new T[newLength];
int number = min(oldLength, newLength);
copy(a, a + number, temp);
delete [] a;
a = temp;
}
template<class T>
class circularArrayList : public linearList<T>
{
public:
circularArrayList(int initialCapacity = 10);
circularArrayList(const circularArrayList<T> &);
~circularArrayList() {delete[] element;}
bool empty() const override { return listSize == 0; }
int size() const override { return listSize; }
T &get(int theIndex) const override;
int indexOf(const T &theElement) const override;
void erase(int theIndex) override;
void insert(int theIndex, const T &theElement) override;
void output(ostream &out) const override;
int capacity() const { return arrayLength; }
circularArrayList<T> &trimToSize();
circularArrayList<T> &setSize(int length);
T &operator[](int i);
bool operator==(const circularArrayList<T> &right) const;
bool operator!=(const circularArrayList<T> &right) const;
bool operator<(const circularArrayList<T> &right) const;
void push_back(const T &theElement);
T &pop_back();
circularArrayList<T> &swap(circularArrayList<T> &theList);
circularArrayList<T> &reserve(int theCapacity);
T set(int theIndex, const T &theElement);
circularArrayList<T> &clear();
circularArrayList<T> &removeRange(int startIndex, int endIndex);
int lastIndexOf(const T &theElement) const;
circularArrayList<T> &decreaseArray(int capacity);
circularArrayList<T> &reverse();
circularArrayList<T> &leftShift(int shift);
circularArrayList<T> &circularShift(int shift);
circularArrayList<T> &half();
circularArrayList<T> &meld(circularArrayList<T> &a, circularArrayList<T> &b);
circularArrayList<T> &merge(circularArrayList<T> &a, circularArrayList<T> &b);
circularArrayList<T> &split(circularArrayList<T> &a, circularArrayList<T> &b);
public:
class iterator
{
public:
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T *pointer;
typedef T &reference;
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;
}
T &operator[](int i)
{
return *(position + i);
}
ptrdiff_t operator-(const circularArrayList<T>::iterator &right) const
{
return position - right.position;
}
T &operator-(const ptrdiff_t &diff) const
{
return *(position - diff);
}
T &operator+(const ptrdiff_t &diff) const
{
return *(position + diff);
}
protected:
T *position;
};
iterator begin() { return iterator(element); }
iterator end() { return iterator(element + listSize); }
protected:
void checkIndex(int theIndex) const;
void changeArraySize(int capacity);
void increaseArray(int capacity);
T *element = NULL;
int arrayLength = 0;
int listSize = 0;
int initialCapacity = 10;
int first = 0;
int last = 0;
};
template<class T>
circularArrayList<T>::circularArrayList(int initialCapacity)
{
if (initialCapacity < 1)
{
ostringstream s;
s << "Initial capacity = " << initialCapacity << " must be > 0";
throw logic_error(s.str());
}
arrayLength = initialCapacity;
initialCapacity = initialCapacity;
element = new T[arrayLength];
listSize = 0;
first = 0;
last = 0;
}
template<class T>
circularArrayList<T>::circularArrayList(const circularArrayList<T> &theList)
{
arrayLength = theList.arrayLength;
initialCapacity = theList.initialCapacity;
listSize = theList.listSize;
element = new T[arrayLength];
circularCopy(theList.element + theList.first, theList.element + theList.last + 1, theList.element, theList.arrayLength, element);
first = 0;
last = listSize;
}
template<class T>
void circularArrayList<T>::checkIndex(int theIndex) const
{
if (theIndex < 0 || theIndex >= listSize)
{
ostringstream s;
s << "index = " << theIndex << " size = " << listSize;
throw logic_error(s.str());
}
}
template<class T>
T &circularArrayList<T>::get(int theIndex) const
{
checkIndex(theIndex);
return element[theIndex];
}
template<class T>
int circularArrayList<T>::indexOf(const T &theElement) const
{
for (int i = 0; i < listSize; i++)
{
if (element[(i + first) % arrayLength] == theElement)
{
return i;
}
}
return -1;
}
template<class T>
void circularArrayList<T>::erase(int theIndex)
{
checkIndex(theIndex);
if (theIndex >= (listSize + 1) / 2)
{
for (int i = theIndex; i < listSize - 1; i++)
{
element[(i + first) % arrayLength] = element[(i + first + 1) % arrayLength];
}
element[last - 1].~T();
last--;
last = (last + arrayLength) % arrayLength;
}
else
{
for (int i = theIndex; i > 0; i--)
{
element[(i + first) % arrayLength] = element[(i + first - 1 + arrayLength) % arrayLength];
}
element[first].~T();
first++;
first %= arrayLength;
}
listSize--;
if (listSize < arrayLength / 4)
decreaseArray(arrayLength / 2);
}
template<class T>
void circularArrayList<T>::insert(int theIndex, const T &theElement)
{
if (theIndex < 0 || theIndex > listSize)
{
ostringstream s;
s << "index = " << theIndex << " size = " << listSize;
throw logic_error(s.str());
}
if (listSize == arrayLength - 1)
{
increaseArray(2 * arrayLength);
}
if(theIndex >= (listSize + 1) / 2)
{
for(int i = listSize; i > theIndex; i--)
{
element[(i + first) % arrayLength] = element[(i + first - 1 + arrayLength) % arrayLength];
}
last++;
last %= arrayLength;
}
else
{
for(int i = 0; i < theIndex; i++)
{
element[(i + first - 1 + arrayLength) % arrayLength] = element[(i + first) % arrayLength];
}
first--;
first = (first + arrayLength) % arrayLength;
}
element[(theIndex + first) % arrayLength] = theElement;
listSize++;
}
template<class T>
void circularArrayList<T>::output(ostream &out) const
{
for (int i = 0; i < listSize; i++)
{
cout<< element[(i + first) % arrayLength] << " ";
}
}
template<class T>
ostream &operator<<(ostream &out, const circularArrayList<T> &x)
{
x.output(out);
return out;
}
template<class T>
void circularArrayList<T>::increaseArray(int capacity)
{
if (capacity <= arrayLength)
{
cout << "input capacity <= arrayLength";
return;
}
changeArraySize(capacity);
arrayLength = capacity;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::trimToSize()
{
if (listSize == arrayLength) return *this;
int oldLength = arrayLength;
arrayLength = max(listSize, 1);
changeLength1D(element, oldLength, arrayLength);
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::setSize(int length)
{
if (length < 0 || length > listSize)
return *this;
listSize = length;
return *this;
}
template<class T>
T &circularArrayList<T>::operator[](int i)
{
checkIndex(i);
return element[i];
}
template<class T>
bool circularArrayList<T>::operator==(const circularArrayList<T> &right) const
{
if (right.listSize != listSize) return false;
for (int i = 0; i < listSize; i++)
{
if (element[i] != right.element[i]) return false;
}
return true;
}
template<class T>
bool circularArrayList<T>::operator!=(const circularArrayList<T> &right) const
{
if (*this == right)
{
return false;
}
else
{
return true;
}
}
template<class T>
bool circularArrayList<T>::operator<(const circularArrayList<T> &right) const
{
int minSize = min(size(), right.size());
for (int i = 0; i < size(); i++)
{
if (element[i] < right.element[i]) return true;
}
if (size() < right.size()) return true;
return false;
}
template<class T>
void circularArrayList<T>::push_back(const T &theElement)
{
if (arrayLength == listSize)
increaseArray(2 * arrayLength);
element[listSize++] = theElement;
}
template<class T>
T &circularArrayList<T>::pop_back()
{
if (listSize == 0)
{
throw logic_error("circularArrayList is empty");
}
T &theElement = element[--listSize];
if (listSize < arrayLength / 4)
decreaseArray(arrayLength / 2);
return theElement;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::swap(circularArrayList<T> &theList)
{
std::swap(listSize, theList.listSize);
std::swap(arrayLength, theList.arrayLength);
std::swap(element, theList.element);
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::reserve(int theCapacity)
{
if (theCapacity <= arrayLength) return *this;
changeLength1D(element, arrayLength, theCapacity);
arrayLength = theCapacity;
return *this;
}
template<class T>
T circularArrayList<T>::set(int theIndex, const T &theElement)
{
checkIndex(theIndex);
T tmp = element[theIndex];
element[theIndex] = theElement;
return tmp;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::clear()
{
for (int i = 0; i < size(); i++)
{
element[i].~T();
}
listSize = 0;
decreaseArray(initialCapacity);
arrayLength = initialCapacity;
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::removeRange(int startIndex, int endIndex)
{
checkIndex(startIndex);
checkIndex(endIndex);
int count = endIndex - startIndex + 1;
copy(element + endIndex, element + listSize, element + startIndex);
for (int i = startIndex + count; i < listSize; i++)
element[i].~T();
listSize -= count;
if (listSize < arrayLength / 4)
{
decreaseArray(arrayLength / 2);
}
return *this;
}
template<class T>
int circularArrayList<T>::lastIndexOf(const T &theElement) const
{
for (int i = listSize - 1; i >= 0; i--)
{
if (element[i] == theElement)
return i;
}
return -1;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::decreaseArray(int capacity)
{
if (capacity > arrayLength || capacity < 0)
{
cout<<"capacity > arrayLength || capacity < 0"<<endl;
return *this;
}
if (capacity < initialCapacity) capacity = initialCapacity;
if (capacity == arrayLength)
{
cout<<"capacity > arrayLength || capacity < 0"<<endl;
return *this;
}
changeArraySize(capacity);
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::reverse()
{
for (int i = 0; i < listSize / 2; i++)
{
std::swap(element[(i + first) % arrayLength], element[(listSize - i - 1 + arrayLength) % arrayLength]);
}
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::leftShift(int shift)
{
if (shift < 0 || shift > listSize)
{
ostringstream s;
s << "shift shift < 0 || shift > listSize";
throw logic_error(s.str());
}
copy(element + shift, element + listSize, element);
listSize -= shift;
for (int i = listSize - shift; i < listSize; i++)
{
element[i].~T();
}
if (listSize < arrayLength / 4)
{
changeLength1D(element, arrayLength, arrayLength / 2);
}
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::circularShift(int shift)
{
if (shift < 0)
{
ostringstream s;
s << "circularShift shift < 0";
throw logic_error(s.str());
}
shift %= listSize;
reverse();
std::reverse(element, element + listSize - shift);
std::reverse(element + listSize - shift, element + listSize);
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::half()
{
if (listSize <= 1) return *this;
for (int i = 1; i < (listSize + 1) / 2; i++)
{
element[i] = element[i * 2];
}
int oldSize = listSize;
listSize = (listSize + 1) / 2;
for (int i = listSize; i < oldSize; i++)
{
element[i].~T();
}
if (listSize < arrayLength / 4)
{
changeLength1D(element, arrayLength, arrayLength / 2);
}
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::meld(circularArrayList<T> &a, circularArrayList<T> &b)
{
if (a.listSize == 0 && b.listSize == 0) return *this;
int minSize = a.listSize < b.listSize ? a.listSize : b.listSize;
int maxSize = a.listSize > b.listSize ? a.listSize : b.listSize;
int newLength = a.listSize > b.listSize ? a.arrayLength : b.arrayLength;
circularArrayList<T>* maxList = a.listSize > b.listSize ? &a : &b;
if (maxSize + minSize > arrayLength)
{
newLength = a.arrayLength + b.arrayLength;
}
increaseArray(newLength);
listSize = maxSize + minSize;
for (int i = 0; i < minSize; i++)
{
element[i * 2] = a.element[(i + a.first) % a.arrayLength];
element[i * 2 + 1] = b.element[(i + b.first) % b.arrayLength];
}
for (int i = minSize; i < maxSize; i++)
{
element[minSize + i] = maxList->element[(i + maxList->first) % maxList->arrayLength];
}
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::merge(circularArrayList<T> &a, circularArrayList<T> &b)
{
if (a.listSize == 0 && b.listSize == 0) return *this;
int minSize = a.listSize < b.listSize ? a.listSize : b.listSize;
int maxSize = a.listSize > b.listSize ? a.listSize : b.listSize;
int newLength = a.listSize > b.listSize ? a.arrayLength : b.arrayLength;
if (minSize == 0) return *this;
if (maxSize + minSize > arrayLength)
{
newLength = a.arrayLength + b.arrayLength;
}
listSize = maxSize + minSize;
increaseArray(newLength);
int index = 0, aindex = 0, bindex = 0;
cout << listSize << endl;
for (int i = 0; i < listSize; i++)
{
if (aindex < a.size() && bindex < b.size())
{
if (a.element[(aindex + a.first) % a.arrayLength] <= b.element[(bindex + b.first) % b.arrayLength])
element[index] = a.element[((aindex++) + a.first) % a.arrayLength];
else
element[index] = b.element[((bindex++) + b.first) % b.arrayLength];
}
else
{
if (aindex < a.size() && bindex >= b.size())
element[index] = a.element[((aindex++) + a.first) % a.arrayLength];
else if (aindex >= a.size() && bindex < b.size())
element[index] = b.element[((bindex++) + b.first) % b.arrayLength];
}
}
return *this;
}
template<class T>
circularArrayList<T> &circularArrayList<T>::split(circularArrayList<T> &a, circularArrayList<T> &b)
{
if (listSize == 0)
{
cout << "listSize == empty";
return *this;
}
a.increaseArray(listSize);
b.increaseArray(listSize);
int index, aindex = 0, bindex = 0;
for (int i = 0; i < listSize; i++)
{
if (i % 2 == 0)
{
a.element[aindex++] = element[i];
}
else
{
b.element[bindex++] = element[i];
}
}
a.listSize = (listSize + 1) / 2;
b.listSize = listSize / 2;
return *this;
}
template <class T>
void circularArrayList<T>::changeArraySize(int capacity)
{
T *temp = new T[capacity];
circularCopy(&(element[first]), &(element[last]), element, arrayLength, temp);
delete[]element;
element = temp;
arrayLength = capacity;
first = 0;
last = listSize;
}
int main()
{
linearList<double> *x = (linearList<double> *)new circularArrayList<int>(100);
circularArrayList<double> y(100);
circularArrayList<char> z;
circularArrayList<double> w(y);
circularArrayList<double> list;
cout << "----------------------insert start--------------------" << endl;
cout << "list capacity = " << list.capacity() << endl;
cout << "list size = " << list.size() << endl;
for (int i = 0; i < 50; i++)
{
list.insert(0, i);
}
cout << "list capacity = " << list.capacity() << endl;
cout << "list size = " << list.size() << endl;
cout << list << endl;
cout << "----------------------insert end--------------------" << endl;
cout << "----------------------拷贝构造 start--------------------" << endl;
circularArrayList<double> list5(list);
cout << "list capacity = " << list.capacity() << endl;
cout << "list size = " << list.size() << endl;
cout << list << endl;
cout << "----------------------拷贝构造 end--------------------" << endl;
cout << "----------------------indexOf start--------------------" << endl;
for(int i = 0;i<10;i++)
{
list5.insert(50,i+50);
}
for(int i = 0;i < 60;i++)
{
std::cout<<list5.indexOf(i)<<" ";
}
cout<<endl;
cout << "----------------------indexOf end--------------------" << endl;
cout << "----------------------erase start--------------------" << endl;
cout << "list capacity = " << list.capacity() << endl;
cout << "list size = " << list.size() << endl;
cout << list << endl;
for(int i = 0; i < 20; i++)
{
list.erase(10);
}
cout << "list capacity = " << list.capacity() << endl;
cout << "list size = " << list.size() << endl;
cout << list << endl;
cout << "----------------------erase end--------------------" << endl;
#if 0
cout << "----------------------test trimToSize start--------------------" << endl;
cout << "list capacity = " << list.capacity() << endl;
cout << "list size = " << list.size() << endl;
list.trimToSize();
cout << "trimToSize " << endl;
cout << "list capacity = " << list.capacity() << endl;
cout << "list size = " << list.size() << endl;
cout << "----------------------test trimToSize end--------------------" << endl;
cout << "----------------------setSize trimToSize start--------------------" << endl;
list.setSize(50);
cout << "setSize " << endl;
cout << "list capacity = " << list.capacity() << endl;
cout << "list size = " << list.size() << endl;
cout << list << endl;
cout << "----------------------setSize trimToSize end--------------------" << endl;
cout << "----------------------operator [] trimToSize start--------------------" << endl;
cout << "list[10] = " << list[10] << endl;
list[10] = 100;
cout << "list[10] = " << list[10] << endl;
cout << "----------------------operator [] trimToSize end--------------------" << endl;
cout << "----------------------operator == trimToSize start--------------------" << endl;
circularArrayList<double> list1;
for (int i = 0; i < 3; i++)
{
list1.insert(0, i);
}
cout << (list1 == list) << endl;
circularArrayList<double> list2;
list2.insert(0, 2);
list2.insert(1, 1);
list2.insert(2, 0);
cout << (list1 == list2) << endl;
cout << "----------------------operator == end--------------------" << endl;
cout << "----------------------operator != start--------------------" << endl;
cout << (list1 != list) << endl;
cout << (list1 != list2) << endl;
cout << "----------------------operator != end--------------------" << endl;
cout << "----------------------operator < start--------------------" << endl;
cout << (list1 < list) << endl;
cout << (list1 < list2) << endl;
cout << "----------------------operator < end--------------------" << endl;
cout << "----------------------push_back start--------------------" << endl;
list1.trimToSize();
cout << list1 << endl;
cout << list1.capacity() << endl;
list1.push_back(111);
list1.push_back(111);
list1.push_back(111);
cout << list1 << endl;
cout << list1.capacity() << endl;
cout << "----------------------push_back end--------------------" << endl;
cout << "----------------------pop_back start--------------------" << endl;
cout << list1 << endl;
cout << list1.capacity() << endl;
cout << list1.pop_back() << endl;
cout << list1.pop_back() << endl;
cout << list1.pop_back() << endl;
cout << list1 << endl;
cout << list1.capacity() << endl;
cout << "----------------------pop_back end--------------------" << endl;
cout << "----------------------swap start--------------------" << endl;
cout << list << endl;
cout << list1 << endl;
cout << "--------------------------" << endl;
list1.swap(list);
cout << list << endl;
cout << list1 << endl;
cout << "--------------------------" << endl;
list1.swap(list);
cout << list << endl;
cout << list1 << endl;
cout << "----------------------swap end--------------------" << endl;
cout << "----------------------reserve start--------------------" << endl;
std::cout << list1.capacity() << endl;
list1.reserve(20);
std::cout << list1.capacity() << endl;
cout << "----------------------reserve end--------------------" << endl;
cout << "----------------------set start--------------------" << endl;
cout << list << endl;
cout << list.set(3, 100.0);
cout << list << endl;
cout << "----------------------set end--------------------" << endl;
cout << "----------------------clear start--------------------" << endl;
for (int i = 0; i < 10; i++)
{
list2.insert(0, i);
}
cout << list2.capacity() << endl;
cout << list2 << endl;
list2.clear();
cout << list2 << endl;
cout << list2.capacity() << endl;
cout << "----------------------clear end--------------------" << endl;
cout << "----------------------removeRange start--------------------" << endl;
for (int i = 0; i < 10; i++)
{
list2.insert(0, i);
}
cout << list2 << endl;
cout << list2.size() << endl;
list2.removeRange(0, 3);
cout << list2 << endl;
cout << list2.size() << endl;
list2.removeRange(3, 5);
cout << list2 << endl;
cout << "----------------------removeRange end--------------------" << endl;
cout << "----------------------lastIndexOf start--------------------" << endl;
list2.insert(0, 10);
list2.insert(1, 10);
list2.insert(2, 10);
cout << list2 << endl;
cout << list2.lastIndexOf(10) << endl;
cout << list2 << endl;
cout << "----------------------lastIndexOf end--------------------" << endl;
#endif
cout << "----------------------reverse start--------------------" << endl;
cout << list5 << endl;
list5.reverse();
cout << list5 << endl;
cout << "----------------------reverse end--------------------" << endl;
#if 0
cout << "----------------------leftShift start--------------------" << endl;
cout << list2 << endl;
list2.leftShift(4);
cout << list2 << endl;
cout << "----------------------leftShift end--------------------" << endl;
cout << "----------------------circularShift start--------------------" << endl;
list2.clear();
for (int i = 0; i < 10; i++)
{
list2.insert(0, i);
}
cout << list2 << endl;
list2.circularShift(1);
cout << list2 << endl;
list2.circularShift(1);
cout << list2 << endl;
list2.circularShift(1);
cout << list2 << endl;
list2.circularShift(32);
cout << list2 << endl;
cout << "----------------------circularShift end--------------------" << endl;
cout << "----------------------half start--------------------" << endl;
cout << list2 << endl;
list2.half();
cout << list2 << endl;
cout << "----------------------half end--------------------" << endl;
cout << "----------------------iterator start--------------------" << endl;
cout << list2 << endl;
cout << (*list2.begin()) << endl;
circularArrayList<double>::iterator iter;
cout << "size = " << list2.size() << endl;
for (iter = list2.begin(); iter != list2.end(); iter++)
{
cout << *iter << " ";
}
cout << endl;
for (auto value : list2)
{
cout << value << " ";
}
cout << endl;
cout << (*list2.end()) << endl;
cout << list2 << endl;
cout << "----------------------iterator end--------------------" << endl;
#endif
cout << "----------------------meld start--------------------" << endl;
circularArrayList<double> list4;
cout << list << "\ncapacity= " << list.capacity() << " size = " << list.size() << endl;
cout << list5 << "\ncapacity= " << list5.capacity() << " size = " << list5.size() << endl;
cout << list4 << "\ncapacity= " << list4.capacity() << " size = " << list4.size() << endl;
list4.meld(list, list5);
cout << endl;
cout << list << "\ncapacity= " << list.capacity() << " size = " << list.size() << endl;
cout << list5 << "\ncapacity= " << list5.capacity() << " size = " << list5.size() << endl;
cout << list4 << "\ncapacity= " << list4.capacity() << " size = " << list4.size() << endl;
cout << "----------------------meld end--------------------" << endl;
cout << "----------------------merge start--------------------" << endl;
circularArrayList<double> list1;
circularArrayList<double> list2;
circularArrayList<double> list3;
list1.clear();
list2.clear();
list3.clear();
for (int i = 0; i < 10; i++)
{
list1.push_back(2 * i);
}
for (int i = 0; i < 20; i++)
{
list2.push_back(2 * i + 1);
}
cout << list1 << "capacity= " << list1.capacity() << " size = " << list1.size() << endl;
cout << list2 << "capacity= " << list2.capacity() << " size = " << list2.size() << endl;
cout << list3 << "capacity= " << list3.capacity() << " size = " << list4.size() << endl;
list3.merge(list1, list2);
cout << list1 << "capacity= " << list1.capacity() << " size = " << list1.size() << endl;
cout << list2 << "capacity= " << list2.capacity() << " size = " << list2.size() << endl;
cout << list3 << "capacity= " << list3.capacity() << " size = " << list4.size() << endl;
cout << "----------------------merge end--------------------" << endl;
#if 0
cout << "----------------------split start--------------------" << endl;
cout << list1 << "capacity= " << list1.capacity() << " size = " << list1.size() << endl;
cout << list2 << "capacity= " << list2.capacity() << " size = " << list2.size() << endl;
cout << list3 << "capacity= " << list3.capacity() << " size = " << list4.size() << endl;
list3.split(list1, list2);
cout << list1 << "capacity= " << list1.capacity() << " size = " << list1.size() << endl;
cout << list2 << "capacity= " << list2.capacity() << " size = " << list2.size() << endl;
cout << list3 << "capacity= " << list3.capacity() << " size = " << list4.size() << endl;
cout << "----------------------split end--------------------" << endl;
#endif
return 0;
}
测试输出
xz@xiaqiu:~/study/algorithm/c++/1/build$ ./test
----------------------insert start--------------------
list capacity = 10
list size = 0
list capacity = 80
list size = 50
49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
----------------------insert end--------------------
----------------------拷贝构造 start--------------------
list capacity = 80
list size = 50
49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
----------------------拷贝构造 end--------------------
----------------------indexOf start--------------------
49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 59 58 57 56 55 54 53 52 51 50
----------------------indexOf end--------------------
----------------------erase start--------------------
list capacity = 80
list size = 50
49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
list capacity = 80
list size = 30
49 48 47 46 45 44 43 42 41 40 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
----------------------erase end--------------------
----------------------reverse start--------------------
49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 59 58 57 56 55 54 53 52 51 50
50 51 52 53 54 55 56 57 58 59 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
----------------------reverse end--------------------
----------------------meld start--------------------
49 48 47 46 45 44 43 42 41 40 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
capacity= 80 size = 30
50 51 52 53 54 55 56 57 58 59 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
capacity= 80 size = 60
capacity= 10 size = 0
49 48 47 46 45 44 43 42 41 40 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
capacity= 80 size = 30
50 51 52 53 54 55 56 57 58 59 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
capacity= 80 size = 60
49 50 48 51 47 52 46 53 45 54 44 55 43 56 42 57 41 58 40 59 19 0 18 1 17 2 16 3 15 4 14 5 13 6 12 7 11 8 10 9 9 10 8 11 7 12 6 13 5 14 4 15 3 16 2 17 1 18 0 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
capacity= 160 size = 90
----------------------meld end--------------------
----------------------merge start--------------------
capacity > arrayLength || capacity < 0
capacity > arrayLength || capacity < 0
capacity > arrayLength || capacity < 0
0 2 4 6 8 capacity= 10 size = 5
1 3 5 7 9 capacity= 10 size = 5
capacity= 10 size = 90
input capacity <= arrayLength10
0 2 4 6 8 capacity= 10 size = 5
1 3 5 7 9 capacity= 10 size = 5
9 0 0 0 0 0 0 0 0 0 capacity= 10 size = 90
----------------------merge end--------------------
xz@xiaqiu:~/study/algorithm/c++/1/build$
|