/*ident "@(#)cls4:demo/lang/Array.C 1.1" */ /*######################################################################### # Copyright (c) 1991 AT&T # All Rights Reserved # # THIS IS PROPRIETARY SOURCE CODE OF AT&T # # This code has been published in # C++ Primer, 2nd Edition # by Stanley Lippman # Addison_Wesley Publishing Company # #########################################################################*/ #ifndef ARRAY_C #define ARRAY_C #include "Array.h" #ifdef assert #undef assert #endif #ifdef _assert #undef _assert #endif #ifdef NDEBUG #define assert(e) ((void)0) #else #ifndef __SYSENT_H #include #endif #ifndef __STDLIB_H #include #endif #ifndef IOSTREAMH #include #endif #if defined(__STDC__) #define assert(EX) (void)((EX) || ((cerr << "Assertion failed: " # EX ", file " << __FILE__ << ", line " << __LINE__ << endl), abort(), 0)) #else #define assert(EX) (void)((EX) || ((cerr << "Assertion failed: EX, file " << __FILE__ << ", line " << __LINE__ << endl), abort(), 0)) #endif #endif #define _assert(e) assert(e) template ostream& operator<<( ostream& os, Array& ar) { ar.print(os); return os; } template void Array::init(const Type *array, int sz) { ia = new Type[size = sz]; assert( ia != 0 ); for (int ix = 0; ix < size; ++ix) ia[ix] = (array!=0) ? array[ix] : (Type)0; } template Array::Array(int sz) { init(0,sz); } template Array::Array(Type *ar, int sz) { init(ar,sz); } template Array::Array(const Array &iA) { init(iA.ia,iA.size); } template Array& Array::operator=(const Array &iA) { if (this == &iA) return *this; delete ia; init( iA.ia, iA.size ); return *this; } template void Array::grow() { Type *oldia = ia; int oldSize = size; size += size/2 + 1; ia = new Type[size]; assert( ia != 0 ); for (int i=0; i Type Array::min() { Type min_val = ia[0]; for (int i=1; i ia[i]) min_val = ia[i]; return min_val; } template Type Array::max() { Type max_val = ia[0]; for (int i=1; i int Array::find(Type val) { for (int i=0; i void Array::swap(int i, int j) { Type tmp = ia[i]; ia[i] = ia[j]; ia[j] = tmp; } template void Array::sort(int low, int high) { if (low < high) { int lo = low; int hi = high + 1; Type elem = ia[low]; for (;;) { // while (ia[++lo] <= elem) ; while (ia[++lo] < elem) ; while (ia[--hi] > elem) ; if (lo < hi) swap(lo,hi); else break; } swap(low,hi); sort(low,hi-1); sort(hi+1,high); } } template void Array::print(ostream& os) { const int lineLength = 6; os << "( " << size << " )< "; for (int i = 0; i < size; ++i) { if (i % lineLength == 0 && i) os << "\n\t"; os << ia[ i ]; if (i%lineLength != lineLength-1 && i != size-1) os << ", "; } os << " >\n"; } #endif