/* Example programs from the book Scientific and Engineering Programming in C++: An Introduction with Advanced Techniques and Examples, Addison-Wesley, 1994. (c) COPYRIGHT INTERNATIONAL BUSINESS MACHINES CORPORATION 1994. ALL RIGHTS RESERVED. See README file for further details. */ // WARNING: // An altered copy of this class is used in intrcat. Be sure to // keep them in sync. template CheckedSimpleArray::CheckedSimpleArray(int n) { if (n < 0) throw ArraySizeError(n); num_elts = n; ptr_to_data = new T[n]; } template T& CheckedSimpleArray::operator[](int i) { if (i < 0 || i >= num_elts) throw SubscriptRangeError(i); return ptr_to_data[i]; } template void CheckedSimpleArray::setSize(int n) { if (n != num_elts) { if (n < 0) throw ArraySizeError(n); delete [] ptr_to_data; // Delete old elements, num_elts = n; // set new count, ptr_to_data = new T[n]; // and allocate new elements } } template CheckedSimpleArray::CheckedSimpleArray() { num_elts = 0; ptr_to_data = 0; } template CheckedSimpleArray::CheckedSimpleArray(const CheckedSimpleArray& a) { num_elts = a.num_elts; ptr_to_data = new T[num_elts]; copy(a); // Copy a's elements } template CheckedSimpleArray::~CheckedSimpleArray() { delete [] ptr_to_data; } template int CheckedSimpleArray::numElts() { return num_elts; } template void CheckedSimpleArray::copy(const CheckedSimpleArray& a) { // Copy a's elements into the elements of *this T* p = ptr_to_data + num_elts; T* q = a.ptr_to_data + num_elts; while (p > ptr_to_data) *--p = *--q; } template CheckedSimpleArray& CheckedSimpleArray::operator=(const CheckedSimpleArray& rhs) { if ( ptr_to_data != rhs.ptr_to_data ) { setSize( rhs.num_elts ); copy(rhs); } return *this; } template CheckedSimpleArray& CheckedSimpleArray::operator=(T rhs) { T* p = ptr_to_data + num_elts; while (p > ptr_to_data) *--p = rhs; return *this; }