//***************************************************************************** // // STringArray.C // //***************************************************************************** // // Copyright (C) 1996 // // Ronald A. MacCracken // Graduate Student // Computer Science Department // University of California // Davis, CA 95616 // // Permission is granted to use at your own risk and // distribute this software in source and binary forms // provided the above copyright notice and this paragraph // are preserved on all copies. This software is provided // "as is" with no express or implied warranty. // //***************************************************************************** #include #include "STringArray.h" // // Constructors and Destructors // STringArray :: STringArray ( void ) : _length ( 10 ) { _array = new STring [ _length ] ; } STringArray :: STringArray ( const int& length ) : _length ( length ) { assert ( _length != 0 ) ; _array = new STring [ _length ] ; } STringArray :: STringArray ( const int& length , const STring& data ) : _length ( length ) { assert ( _length != 0 ) ; _array = new STring [ _length ] ( data ) ; } STringArray :: STringArray ( const int& length , const STring* new_array ) : _length ( length ) { assert ( _length != 0 ) ; _array = new STring [ _length ] ; if ( new_array != NULL ) { for ( int i = 0 ; i < _length ; ++i ) _array[i] = new_array[i] ; } } STringArray :: STringArray ( const STringArray& a ) { Copy ( a ) ; } STringArray :: ~STringArray ( void ) { Delete ( ) ; } // // Private Member Functions // void STringArray :: Copy ( const STringArray& a ) { _length = a._length ; _array = new STring [ _length ] ; for ( int i = 0 ; i < _length ; ++i ) _array[i] = a._array[i] ; } void STringArray :: Delete ( void ) { _length = 0 ; delete [] _array ; _array = NULL ; } // // Protected Member Functions // void STringArray :: Output ( ostream& co ) const { co << "STringArray" << endl ; co << " " << "Length: " << _length << endl ; co << " " << "Data: " << endl ; co << " {" << endl ; for ( int i = 0 ; i < _length ; ++i ) co << " " << _array[i] << endl ; co << " }" << endl ; } void STringArray :: Input ( istream& ci ) { char s[100] ; char c ; ci >> s ; ci >> s >> _length ; _array = new STring [ _length ] ; ci >> s >> c ; for ( int i = 0 ; i < _length ; i++ ) ci >> _array[i] ; ci >> c ; } // // Public Member Functions // STringArray& STringArray :: operator= ( const STringArray& a ) { if ( this == &a ) return ( *this ) ; Delete ( ) ; Copy ( a ) ; return ( *this ) ; } STring& STringArray :: operator[] ( const int& i ) { assert ( ( i >= 0 ) && ( i < _length ) ) ; return ( _array[i] ) ; } const STring& STringArray :: operator[] ( const int& i ) const { assert ( ( i >= 0 ) && ( i < _length ) ) ; return ( _array[i] ) ; } void STringArray :: Set_All ( const STring& x ) { for ( int i = 0 ; i < _length ; i++ ) _array[i] = x ; } void STringArray :: Swap ( const int& i , const int& j ) { if ( i == j ) return ; assert ( ( i >= 0 ) && ( i < _length ) ) ; assert ( ( j >= 0 ) && ( j < _length ) ) ; STring tmp = _array[i] ; _array[i] = _array[j] ; _array[j] = tmp ; } // // Friend Functions // ostream& operator<< ( ostream& co , const STringArray& A ) { A.Output ( co ) ; return ( co ) ; } istream& operator>> ( istream& ci , STringArray& A ) { A.Input ( ci ) ; return ( ci ) ; }