|  | 
									
										| JVector#include "JVector.h" 
 
 template <class Etype>
 JVector<Etype>::JVector( UINT initialCapacity, UINT capacityIncrement )
 {
 m_size = 0;
 m_capacity = initialCapacity;
 m_pData = new Etype*[ m_capacity ];
 
 m_increment = capacityIncrement;
 }
 
 template <class Etype>
 JVector<Etype>::JVector( const JVector<Etype>& rhv )
 {
 m_size = rhv.m_size;
 m_capacity = rhv.m_capacity;
 m_pData = new Etype*[ m_capacity ];
 m_increment = rhv.m_increment;
 
 
 for( UINT i = 0; i < m_size; i++ )
 {
 m_pData[i] = new Etype( *(rhv.m_pData[i]) );
 }
 }
 
 template <class Etype>
 JVector<Etype>::~JVector()
 {
 removeAllElements();
 delete [] m_pData;
 }
 
 template <class Etype>
 UINT
 JVector<Etype>::capacity() const
 {
 return m_capacity;
 }
 
 template <class Etype>
 bool
 JVector<Etype>::contains( const Etype &elem ) const
 {
 for ( UINT i = 0; i < m_size; i++ )
 {
 if ( *m_pData[i] == elem )
 return true;
 }
 
 return false;
 }
 
 template <class Etype>
 void
 JVector<Etype>::copyInto( Etype* array ) const
 {
 for( UINT i = 0; i < m_size; i++ )
 array[i] = *m_pData[i];
 }
 
 
 template <class Etype>
 Etype &
 JVector<Etype>::elementAt( UINT index ) const
 {
 verifyIndex( index );
 return *m_pData[index];
 }
 
 template <class Etype>
 const Etype &
 JVector<Etype>::firstElement() const
 {
 if ( m_size == 0 )
 {
 //throw "Empty JVector Exception";
 cerr << "firstElement() called on empty JVector" << endl;
 exit(1);
 }
 
 return *m_pData[ 0 ];
 }
 
 template <class Etype>
 int
 JVector<Etype>::indexOf( const Etype &elem ) const
 {
 for ( UINT i = 0; i < m_size; i++ )
 {
 if ( *m_pData[ i ] == elem )
 return i;
 }
 
 return -1;
 }
 
 template <class Etype>
 bool
 JVector<Etype>::isEmpty() const
 {
 return m_size == 0;
 }
 
 template <class Etype>
 const Etype &
 JVector<Etype>::lastElement() const
 {
 if ( m_size == 0 )
 {
 //throw "Empty JVector Exception"
 cerr << "lastElement() called on empty JVector" << endl;
 exit(1);
 }
 
 return *m_pData[ m_size - 1 ];
 }
 
 template <class Etype>
 int
 JVector<Etype>::lastIndexOf( const Etype &elem ) const
 {
 //check for empty vector
 if ( m_size == 0 )
 return -1;
 
 UINT i = m_size;
 
 do
 {
 i -= 1;
 if ( *m_pData[i] == elem )
 return i;
 
 }
 while ( i != 0 );
 
 return -1;
 }
 
 template <class Etype>
 UINT
 JVector<Etype>::size() const
 {
 return m_size;
 }
 
 template <class Etype>
 void
 JVector<Etype>::addElement( const Etype &obj )
 {
 if ( m_size == m_capacity )
 ensureCapacity( m_capacity + m_increment );
 
 m_pData[ m_size++ ] = new Etype( obj );
 }
 
 template <class Etype>
 void
 JVector<Etype>::ensureCapacity( UINT minCapacity )
 {
 if ( minCapacity > m_capacity )
 {
 UINT i;
 m_capacity = minCapacity;
 Etype** temp = new Etype*[ m_capacity ];
 
 
 //copy all the elements over upto newsize
 memcpy( temp, m_pData, sizeof(Etype*) * m_size );
 //for ( i = 0; i < m_size; i++ )
 //    temp[i] = m_pData[i];
 
 
 delete [] m_pData;
 m_pData = temp;
 
 }
 }
 
 template <class Etype>
 void
 JVector<Etype>::insertElementAt( const Etype &obj, UINT index )
 {
 if ( index == m_size )
 addElement( obj );
 else
 {
 verifyIndex( index );    //this will throw if true
 
 if ( m_size == m_capacity )
 ensureCapacity( m_capacity + m_increment);
 
 Etype* newItem = new Etype(obj);    //pointer to new item
 Etype* tmp;                            //temp to hold item to be moved over.
 
 for( UINT i = index; i <= m_size; i++ )
 {
 tmp = m_pData[i];
 m_pData[i] = newItem;
 
 if ( i != m_size )
 newItem = tmp;
 else
 break;
 }
 }
 m_size++;
 }
 
 template <class Etype>
 void
 JVector<Etype>::removeAllElements()
 {
 //avoid memory leak
 for ( UINT i = 0; i < m_size; i++ )
 delete m_pData[i];
 
 m_size = 0;
 }
 
 template <class Etype>
 bool
 JVector<Etype>::removeElement( const Etype &obj )
 {
 for ( UINT i = 0; i < m_size; i++ )
 {
 if ( *m_pData[i] == obj )
 {
 removeElementAt( i );
 return true;
 }
 }
 
 return false;
 }
 
 template <class Etype>
 void
 JVector<Etype>::removeElementAt( UINT index )
 {
 verifyIndex( index );
 
 delete m_pData[ index ];
 
 UINT i;
 for ( i = index+1; i < m_size; i++ )
 m_pData[ i - 1 ] = m_pData[ i ];
 
 m_pData[i];
 m_size--;
 }
 
 template <class Etype>
 void
 JVector<Etype>::setElementAt( const Etype &obj, UINT index )
 {
 verifyIndex( index );
 
 *m_pData[ index ] = obj;
 }
 
 template <class Etype>
 void
 JVector<Etype>::setSize( UINT newSize )
 {
 if ( newSize > m_capacity )
 ensureCapacity( newSize );
 else if ( newSize < m_size )
 {
 for( UINT i = newSize; i < m_size; i++ )
 delete m_pData[i];
 
 m_size = newSize;
 }
 }
 
 template <class Etype>
 void
 JVector<Etype>::trimToSize()
 {
 if ( m_size != m_capacity )
 {
 Etype** temp = new Etype*[ m_size ];
 UINT i;
 
 for ( i = 0; i < m_size; i++ )
 temp[i] = m_pData[i];
 
 delete [] m_pData;
 
 m_pData = temp;
 m_capacity = m_size;
 }
 }
 
 template <class Etype>
 int
 JVector<Etype>::min( UINT left, UINT right ) const
 {
 return left < right ? left : right;
 }
 
 template <class Etype>
 void
 JVector<Etype>::verifyIndex( UINT index ) const
 {
 if ( index >= m_size )
 {
 //throw "Index Out Of Bounds";
 cerr << "Index Out Of Bounds";
 exit(1);
 }
 }
 
 template <class Etype>
 const Etype &
 JVector<Etype>::operator[]( UINT index ) const
 {
 return elementAt( index );
 }
 
 template <class Etype>
 Etype &
 JVector<Etype>::operator[]( UINT index )
 {
 verifyIndex( index );
 return *m_pData[ index ];
 }
 
 
 |  |  |