//***************************************************************************** // // DynSTringArray.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 "DynSTringArray.h" // // Constructors and Destructors // DynSTringArray :: DynSTringArray ( void ) : _chunk ( 10 ) , _allocated ( 0 ) , _length ( 0 ) , _array ( NULL ) { } DynSTringArray :: DynSTringArray ( const int& chunk ) : _chunk ( chunk ) , _allocated ( 0 ) , _length ( 0 ) , _array ( NULL ) { assert ( _chunk != 0 ) ; } DynSTringArray :: DynSTringArray ( const int& length , const STring& data , const int& chunk ) : _chunk ( chunk ) , _allocated ( length ) , _length ( length ) { assert ( _chunk != 0 ) ; if ( length > 0 ) _array = new STring [ _length ] ( data ) ; else _array = NULL ; } DynSTringArray :: DynSTringArray ( const int& length , const STring* new_array , const int& chunk ) : _chunk ( chunk ) , _allocated ( length ) , _length ( length ) { assert ( _chunk != 0 ) ; if ( length > 0 ) { assert ( new_array != NULL ) ; _array = new STring [ _length ] ; for ( int i = 0 ; i < _length ; ++i ) _array[i] = new_array[i] ; } else _array = NULL ; } DynSTringArray :: DynSTringArray ( const DynSTringArray& a ) { Copy ( a ) ; } DynSTringArray :: ~DynSTringArray ( void ) { Delete ( ) ; } // // Private Member Functions // void DynSTringArray :: Copy ( const DynSTringArray& a ) { _chunk = a._chunk ; _length = a._length ; _array = new STring [ _length ] ; _allocated = _length ; for ( int i = 0 ; i < _length ; i++ ) _array[i] = a._array[i] ; } void DynSTringArray :: Delete ( void ) { _length = 0 ; _allocated = 0 ; delete [] _array ; _array = NULL ; } // // Protected Member Functions // void DynSTringArray :: Output ( ostream& co ) const { co << "DynSTringArray" << endl ; co << " " << "Chunk: " << _chunk << endl ; co << " " << "Length: " << _length << endl ; co << " " << "Data: " << endl ; co << " {" << endl ; for ( int i = 0 ; i < _length ; ++i ) { co << " " << _array[i] << endl ; } co << " }" << endl ; } void DynSTringArray :: Input ( istream& ci ) { char s[100] ; char c ; ci >> s ; ci >> s >> _chunk ; ci >> s >> _length ; _array = new STring [ _length ] ; _allocated = _length ; ci >> s >> c ; for ( int i = 0 ; i < _length ; i++ ) ci >> _array[i] ; ci >> c ; } // // Public Member Functions // DynSTringArray& DynSTringArray :: operator= ( const DynSTringArray& a ) { if ( this == &a ) return ( *this ) ; Delete ( ) ; Copy ( a ) ; return ( *this ) ; } STring& DynSTringArray :: operator[] ( const int& i ) { assert ( ( i >= 0 ) && ( i < _length ) ) ; return ( _array[i] ) ; } const STring& DynSTringArray :: operator[] ( const int& i ) const { assert ( ( i >= 0 ) && ( i < _length ) ) ; return ( _array[i] ) ; } void DynSTringArray :: Add ( const STring& data ) { // allocate more space if have filled up allocated space if ( _length >= _allocated ) { _allocated += _chunk ; STring* new_array = new STring [ _allocated ] ; for ( int i = 0 ; i < _length ; i++ ) new_array[i] = _array[i] ; if ( _length > 0 ) delete [] _array ; _array = new_array ; } // add new element _array[_length] = data ; _length++ ; } void DynSTringArray :: Add ( const DynSTringArray& a ) { for ( int i = 0 ; i < a._length ; ++i ) Add ( a[i] ) ; } void DynSTringArray :: Remove ( const int& i ) { assert ( ( i >= 0 ) && ( i < _length ) ) ; for ( int j = i ; j < _length ; ++j ) _array[j] = _array[j+1] ; _length-- ; } void DynSTringArray :: Set_All ( const STring& data ) { for ( int i = 0 ; i < _length ; ++i ) _array[i] = data ; } void DynSTringArray :: 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 DynSTringArray& A ) { A.Output ( co ) ; return ( co ) ; } istream& operator>> ( istream& ci , DynSTringArray& A ) { A.Input ( ci ) ; return ( ci ) ; }