- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Containers/Vector2D.h

Go to the documentation of this file.

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #ifndef VECTOR2D_H
    00015 #define VECTOR2D_H
    00016
    00017 // c++ headers
    00018 #include <iostream>
    00019 #include <vector>
    00020
    00021 namespace Mers {
    00022         namespace Utils {
    00023                 namespace Containers {
    00024
    00034                         template< class T >
    00035                         class Vector2D
    00036                         {
    00037                         public:
    00038                                 //  ---------------- Constructors/Destructors ------------------------
    00039
    00041                                 Vector2D();
    00042
    00044                                 ~Vector2D();
    00045
    00047                                 Vector2D( const Vector2D<T>& other );
    00048
    00050                                 Vector2D<T>& operator=( const Vector2D<T>& other );
    00051
    00052
    00053                                 // ----------------- Add/Delete Methods ------------------------------
    00054
    00055
    00057                                 void clear();
    00058
    00067                                 unsigned int addRow();
    00068
    00077                                 unsigned int addRows( unsigned int rcount );
    00078
    00079
    00084                                 void deleteRow( unsigned int rrindex );
    00085
    00086
    00093                                 void deleteRows(unsigned int fromindex, unsigned int thruindex);
    00094
    00095
    00096                                 // ------------------ Sizing Methods ---------------------------------
    00097
    00099                                 unsigned int size() const;
    00100
    00101
    00107                                 void setSize( unsigned int size );
    00108
    00109
    00110                                 // ------------------ Operator Overloading ----------------------------
    00111
    00115                                 //vector<T*> operator[]( unsigned int rrindex );
    00116
    00120                                 std::vector<T*>& operator[]( unsigned int rrindex ) const;
    00121
    00122                         protected:
    00123                                 // ------------------- Protected Variables ----------------------------
    00124
    00126                                 std::vector< std::vector<T*>* > elements;
    00127                         };
    00128
    00129
    00130                         // ----------------------------------------------------------------------
    00131                         // - Definitions are required to be placed in Header File for Templates -
    00132                         // ----------------------------------------------------------------------
    00133
    00134                         // default constructor
    00135                         template<class T>
    00136                         Vector2D<T>::Vector2D()
    00137                         {}
    00138
    00139
    00140                         // destructor
    00141                         template<class T>
    00142                         Vector2D<T>::~Vector2D()
    00143                         {
    00144                                 clear();
    00145                         }
    00146
    00147
    00148                         // copy constructor
    00149                         template<class T>
    00150                         Vector2D<T>::Vector2D( const Vector2D<T>& other )
    00151                         {
    00152                                 *this = other;
    00153                         }
    00154
    00155
    00156                         // assignment operator
    00157                         template<class T>
    00158                         Vector2D<T>&
    00159                         Vector2D<T>::operator=( const Vector2D<T>& other )
    00160                         {
    00161                                 // set matrix size ( if necessary )
    00162                                 if( other.size() > size() )
    00163                                         setSize( other.size() );
    00164
    00165                                 // copy: all elements
    00166                                 for( unsigned int i = 0; i < size(); i++ )
    00167                                         for( unsigned int j = 0; j < size(); j++ )
    00168                                                 if( (*elements[i])[j] != NULL )
    00169                                                         (*elements[i])[j] =  new T( *other[i][j] );
    00170                                 return *this;
    00171                         }
    00172
    00173
    00174                         // ------------------------- methods ----------------------------------
    00175
    00176
    00177                         template<class T>
    00178                         unsigned int
    00179                         Vector2D<T>::addRow()
    00180                         {
    00181                                 // add a row
    00182                                 unsigned int esize = elements.size();
    00183                                 std::vector<T*> *rowptr = new std::vector<T*>(esize+1);
    00184
    00185                                 // initialize all values of row vector to Null pointer
    00186                                 for ( unsigned int i = 0; i < esize+1; i++ )
    00187                                         (*rowptr)[i] = NULL;
    00188
    00189                                 // insert initialized row to elements 2d vector.
    00190                                 elements.push_back(rowptr);
    00191
    00192                                 // add a column of NULL pointers to elements
    00193                                 for ( unsigned int i2 = 0; i2 < esize; i2++ )
    00194                                         elements[i2]->push_back(NULL);
    00195
    00196                                 // return new size
    00197                                 return esize;
    00198                         }
    00199
    00200
    00201                         template<class T>
    00202                         unsigned int
    00203                         Vector2D<T>::addRows( unsigned int rcount )
    00204                         {
    00205                                 // add rows
    00206                                 unsigned int esize = elements.size();
    00207
    00208                                 // Declare row poiter variable
    00209                                 std::vector<T*> *rowptr = NULL;
    00210                                 // Resize number of rows and columns in elements
    00211                                 elements.resize(esize+rcount);
    00212                                 for (unsigned int i = esize; i < esize+rcount; i++)
    00213                                 {
    00214                                         // Create a new row vector.
    00215                                         rowptr = new std::vector<T*>(esize+rcount);
    00216                                         for (unsigned int j = 0; j < esize+rcount; j++)
    00217                                         {
    00218                                                 // Initialize all values of new row vector to Null pointer.
    00219                                                 (*rowptr)[j] = NULL;
    00220                                         }
    00221                                         // Assign the new row pointer to elements vector
    00222                                         elements[i] = rowptr;
    00223                                 }
    00224
    00225                                 // Add columns
    00226                                 for (unsigned int i2 = 0; i2 < esize; i2++)
    00227                                 {
    00228                                         //elemennts[i]->insert(elements[i]->end(),count,0);
    00229                                         for (unsigned int j = 0; j < rcount; j++)
    00230                                         {
    00231                                                 // Add a Null pointer to each row not created
    00232                                                 elements[i2]->push_back(NULL);
    00233                                         }
    00234                                 }
    00235
    00236                                 // return new size
    00237                                 return esize+rcount-1;
    00238                         }
    00239
    00240
    00241                         template<class T>
    00242                         void
    00243                         Vector2D<T>::clear()
    00244                         {
    00245                                 // delete all elements
    00246                                 unsigned int esize = elements.size();
    00247                                 if( esize > 0 )
    00248                                 {
    00249                                         deleteRows( 0, esize-1 );
    00250                                 }
    00251                         }
    00252
    00253
    00254                         template<class T>
    00255                         void
    00256                         Vector2D<T>::deleteRow( unsigned int rrindex )
    00257                         {
    00258                                 // deletes the row in elements
    00259                                 if ( elements.size() <= rrindex )
    00260                                 {
    00261                                         std::cerr << "Vector2D::deleteRow index too large!!" << std::endl;
    00262                                         return;
    00263                                 }
    00264
    00265                                 if ( !elements[rrindex] )
    00266                                 {
    00267                                         std::cerr << "Vector2D::deleteRow on null element!!" << std::endl;
    00268                                         return;
    00269                                 }
    00270
    00271                                 unsigned int esize = elements[rrindex]->size();
    00272                                 // erase elements of this row vector
    00273                                 for (unsigned int i = 0; i < esize; i++)
    00274                                 {
    00275                                         if ((*elements[rrindex])[i] != NULL )
    00276                                         {
    00277                                                 delete (*elements[rrindex])[i];
    00278                                                 (*elements[rrindex])[i] = NULL;
    00279                                         }
    00280                                 }
    00281
    00282                                 // delete the elements of column (vector cross-section)
    00283                                 for (unsigned int i2 = 0; i2 < esize; i2++)
    00284                                 {
    00285                                         if (i2 == rrindex)  // skip the row we already handled above
    00286                                                 continue;
    00287                                         if (!elements[i2])
    00288                                         {
    00289                                                 std::cerr << "Vector2D::deleteRow has null row for column search!!" << std::endl;
    00290                                                 continue;
    00291                                         }
    00292                                         if (elements[i2]->size() <= rrindex)
    00293                                         {
    00294                                                 std::cerr << "Vector2D::deleteRow index too large for column!!" << std::endl;
    00295                                                 std::cerr << " rrindex " << rrindex << " , num columns " << elements[i2]->size() << std::endl;
    00296                                                 continue;
    00297                                         }
    00298
    00299                                         if ((*elements[i2])[rrindex] != NULL)
    00300                                         {
    00301                                                 delete (*elements[i2])[rrindex];
    00302                                                 (*elements[i2])[rrindex] = NULL;
    00303                                         }
    00304                                         elements[i2]->erase(elements[i2]->begin() + rrindex);
    00305                                 }
    00306
    00307                                 // erase this row vector
    00308                                 elements.erase(elements.begin()+rrindex);
    00309                         }
    00310
    00311
    00312                         template<class T>
    00313                         void
    00314                         Vector2D<T>::deleteRows( unsigned int fromindex, unsigned int thruindex )
    00315                         {
    00316                                 // delete rows in elements in range: fromindex to thruindex
    00317                                 if (elements.size() <= thruindex)
    00318                                 {
    00319                                         std::cerr << "Vector2D:: error, deleteRows thruindex too large!!" << std::endl;
    00320                                         return;
    00321                                 }
    00322                                 if (fromindex > thruindex)
    00323                                 {
    00324                                         std::cerr << "Vector2D:: error, deleteRows indexes out of order!!" << std::endl;
    00325                                         return;
    00326                                 }
    00327
    00328                                 for (unsigned int j = thruindex; j >= fromindex; j--)
    00329                                 {
    00330                                         deleteRow(j);
    00331                                         if ( j == 0 )
    00332                                                 break;
    00333                                 }
    00334                         }
    00335
    00336
    00337                         template< class T>
    00338                         unsigned int
    00339                         Vector2D<T>::size() const
    00340                         {
    00341                                 return elements.size();
    00342                         }
    00343
    00344
    00345                         template< class T>
    00346                         void
    00347                         Vector2D<T>::setSize(unsigned int nsize)
    00348                         {
    00349                                 unsigned int prevsize = elements.size();
    00350                                 elements.resize(nsize);
    00351
    00352                                 if (prevsize < nsize)
    00353                                 {
    00354                                         // std::cerr << " :adding new columns" << std::endl;
    00355                                         for ( unsigned int i = prevsize; i < nsize; i++ )
    00356                                         {
    00357                                                 elements[i] = new std::vector<T*>(nsize);
    00358                                                 for ( unsigned int j = 0; j < nsize; j++ )
    00359                                                 {
    00360                                                         (*elements[i])[j] = NULL;
    00361                                                 }
    00362                                         }
    00363
    00364                                         // std::cerr << " :adding colums on existing rows" << std::endl;
    00365                                         for (unsigned int i2 = 0; i2 < prevsize; i2++)
    00366                                         {
    00367                                                 for (unsigned int j = prevsize; j < nsize; j++)
    00368                                                 {
    00369                                                         elements[i2]->push_back(NULL);
    00370                                                 }
    00371                                         }
    00372                                 }
    00373                                 else
    00374                                 {
    00375                                         std::cerr << "Vector2D<T>::setSize(): Error, new size must be larger than original size"
    00376                                                          << std::endl;
    00377                                 }
    00378                         }
    00379
    00380                         // ---------------------------- overloaded operators --------------------------
    00381                         //template< class T >
    00382                         //vector<T*>
    00383                         //Vector2D<T>::operator[](unsigned int rrindex)
    00384                         //{
    00385                         //      if( elements[rrindex] = NULL )
    00386                         //              std::cerr << "Error, elements[rrindex] has NULL value" << std::endl;
    00387                         //      return *(elements[rrindex]);
    00388                         //}
    00389
    00390
    00391                         template< class T >
    00392                         std::vector<T*>&
    00393                         Vector2D<T>::operator[](unsigned int rrindex) const
    00394                         {
    00395                                 if( elements[rrindex] == NULL )
    00396                                         std::cerr << "Error, elements[rrindex] has NULL value" << std::endl;
    00397                                 return *(elements[rrindex]);
    00398                         }
    00399                 }
    00400         }
    00401 }
    00402
    00403
    00404 #endif // VECTROR2D_H