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

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

    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 MERS_VECTOR_H
    00015 #define MERS_VECTOR_H
    00016
    00017 #include <Mers/Utils/Lang/Object.h>
    00018 #include <stdexcept>
    00019
    00020 // for now, include the STL vector
    00021 #include <vector>
    00022
    00023 namespace Mers {
    00024         namespace Utils {
    00025                 namespace Containers {
    00026
    00027                         /***********************************************************************/
    00028                         /* Implementation                                                      */
    00029                         /***********************************************************************/
    00030
    00045                         class VectorImpl: public std::vector<Mers::Utils::Lang::Object*>
    00046                         {
    00047                         private:
    00048                                 typedef std::vector<Mers::Utils::Lang::Object*> Base;
    00049
    00050                         public:
    00051
    00052                                 //-----------------------------------------
    00053                                 // Construction/Destruction
    00054                                 //-----------------------------------------
    00055
    00057                                 VectorImpl() {}
    00058
    00060                                 explicit VectorImpl(size_type count) : Base(count) {}
    00061
    00063                                 VectorImpl(VectorImpl const &other);
    00064
    00066                                 virtual ~VectorImpl();
    00067
    00068                                 //-----------------------------------------
    00069                                 // Overriden operators
    00070                                 //-----------------------------------------
    00071
    00073                                 reference operator[](size_type index)
    00074                                 {
    00075                                         return at(index);
    00076                                 }
    00077
    00079                                 const Mers::Utils::Lang::Object* operator[](size_type index) const
    00080                                 {
    00081                                         return at(index);
    00082                                 }
    00083
    00084                         private:
    00086                                 VectorImpl& operator=(const VectorImpl& other);
    00087
    00088
    00089                                 //-----------------------------------------
    00090                                 // Overriden functions
    00091                                 //-----------------------------------------
    00092
    00093                         public:
    00094
    00096                                 reference front()
    00097                                 {
    00098                                         return at(0);
    00099                                 }
    00100
    00102                                 const Mers::Utils::Lang::Object* front() const
    00103                                 {
    00104                                         return at(0);
    00105                                 }
    00106
    00108                                 reference back()
    00109                                 {
    00110                                         return at(size()-1);
    00111                                 }
    00112
    00114                                 const Mers::Utils::Lang::Object* back() const
    00115                                 {
    00116                                         return at(size()-1);
    00117                                 }
    00118
    00121                                 void resize(int newSize);
    00122
    00124                                 Mers::Utils::Lang::Object* pop_back();
    00125
    00127                                 void clear();
    00128
    00131                                 void insert(size_type index, const_reference data);
    00132
    00135                                 void insert(size_type index, const_iterator first, const_iterator last);
    00136
    00139                                 void erase(size_type index);
    00140
    00144                                 void erase(size_type first, size_type last);
    00145
    00146 #ifndef EMBEDDED
    00147                                 // Prints out the vector.
    00148                                 virtual void print(std::ostream &out, char const* seperator) const;
    00149
    00150 #endif
    00151
    00152                         };
    00153
    00154
    00155                         /*************************************************************************/
    00156                         /* Range-checking iterators                                              */
    00157                         /*************************************************************************/
    00158
    00160                         template<class Container, class Element>
    00161                         class VectorIterator {
    00162                         private:
    00163
    00165                                 typedef typename Container::size_type size_type;
    00166
    00168                                 Container* vector;
    00169
    00171                                 size_type index;
    00172
    00173                         public:
    00174
    00175                                 //  typedef value_type;
    00176                                 //typedef iterator_category;
    00177                                 //typedef difference_type;
    00178                                 //typedef pointer;
    00179
    00180
    00182                                 VectorIterator(Container* myVector, size_type myIndex)
    00183                                         : vector(myVector), index(myIndex) {}
    00184
    00185                                 // default copy constructor works
    00186                                 // VectorIterator(const VectorIterator& other);
    00187
    00188                                 // default assignment operator works
    00189                                 // VectorIterator & operator=(const VectorIterator &other);
    00190
    00192                                 bool operator ==(const VectorIterator& other) const
    00193                                 {
    00194                                         return (vector == other.vector) && (index == other.index);
    00195                                 }
    00196
    00198                                 bool operator !=(const VectorIterator& other) const
    00199                                 {
    00200                                         return !operator==(other);
    00201                                 }
    00202
    00210                                 Element operator*() const
    00211                                 {
    00212                                         return vector->at(index);
    00213                                 }
    00214
    00215                                 Element operator->() const
    00216                                 {
    00217                                         return &(vector->at(index));
    00218                                 }
    00219
    00224                                 VectorIterator& operator ++()
    00225                                 {
    00226                                         index++;
    00227                                         return *this;
    00228                                 }
    00229
    00236                                 VectorIterator operator ++(int)
    00237                                 {
    00238                                         VectorIterator temp(*this);
    00239                                         index++;
    00240                                         return temp;
    00241                                 }
    00242
    00247                                 VectorIterator& operator --()
    00248                                 {
    00249                                         index--;
    00250                                         return *this;
    00251                                 }
    00252
    00259                                 VectorIterator operator --(int)
    00260                                 {
    00261                                         VectorIterator temp(*this);
    00262                                         index--;
    00263                                         return temp;
    00264                                 }
    00265
    00270                                 VectorIterator& operator+=(int value)
    00271                                 {
    00272                                         index += value;
    00273                                         return *this;
    00274                                 }
    00275
    00279                                 VectorIterator operator+(int value)
    00280                                 {
    00281                                         return VectorIterator(*this) += value;
    00282                                 }
    00283
    00288                                 VectorIterator& operator-=(int value)
    00289                                 {
    00290                                         index -= value;
    00291                                         return *this;
    00292                                 }
    00293
    00297                                 VectorIterator operator-(int value)
    00298                                 {
    00299                                         return VectorIterator(*this) -= value;
    00300                                 }
    00301
    00302                                 Element operator[](int value)
    00303                                 {
    00304                                         return vector[index+value];
    00305                                 }
    00306
    00312                                 int operator-(const VectorIterator& other)
    00313                                 {
    00314                                         if(vector != other.vector)
    00315                                         {
    00316                                                 throw new std::invalid_argument
    00317                                                         ("VectorIterator::operator-(): The two iterators do not point to the same vector");
    00318                                         }
    00319                                         return index - other.index;
    00320                                 }
    00321
    00322                         };
    00323
    00324
    00325                         /*************************************************************************/
    00326                         /* Template                                                              */
    00327                         /*************************************************************************/
    00328
    00329                         template<class T>
    00330                         class Vector;
    00331
    00367                         template<class T>
    00368                         class Vector<T*> : public Mers::Utils::Lang::Object, private VectorImpl
    00369                         {
    00370                         private:
    00371                                 typedef VectorImpl Base;
    00372
    00373                         public:
    00374                                 //-----------------------------------------
    00375                                 // STL-compatible type declarations
    00376                                 //-----------------------------------------
    00377
    00381                                 typedef T* & reference;
    00382
    00387                                 typedef typename VectorImpl::size_type size_type;
    00388
    00393                                 typedef typename VectorImpl::difference_type difference_type;
    00394
    00398                                 typedef VectorIterator<Vector<T*>, reference> iterator;
    00399
    00405                                 typedef VectorIterator<const Vector<T*>, const T*> const_iterator;
    00406
    00407
    00408                                 //-----------------------------------------
    00409                                 // Construction/Destruction
    00410                                 //-----------------------------------------
    00411
    00413                                 Vector(ObjectType myObjectType = Unknown)
    00414                                         : Mers::Utils::Lang::Object(myObjectType) {}
    00415
    00417                                 explicit Vector(size_type count, ObjectType myObjectType = Unknown)
    00418                                         : Mers::Utils::Lang::Object(myObjectType), Base(count) {}
    00419
    00421                                 Vector(const Vector<T*>& other) : Mers::Utils::Lang::Object(other), Base(other) {}
    00422
    00424                                 ~Vector() {}
    00425
    00426                                 //-----------------------------------------
    00427                                 // Operators
    00428                                 //-----------------------------------------
    00429
    00434                                 reference operator[](size_type index)
    00435                                 {
    00436                                         return *static_cast<T**>(static_cast<void*>(&(Base::at(index))));
    00437                                 }
    00438
    00443                                 reference operator[](int index)
    00444                                 {
    00445                                         return *static_cast<T**>(static_cast<void*>(&(Base::at(index))));
    00446                                 }
    00447
    00452                                 const T* operator[](size_type index) const
    00453                                 {
    00454                                         return static_cast<const T*>(Base::at(index));
    00455                                 }
    00456
    00461                                 const T* operator[](int index) const
    00462                                 {
    00463                                         return static_cast<const T*>(Base::at(index));
    00464                                 }
    00465
    00472                                 Vector<T*>& operator+=(const Vector<T*>& other)
    00473                                 {
    00474                                         insert(size(), other);
    00475                                         return *this;
    00476                                 }
    00477
    00484                                 Vector<T*>& operator+=(T* ptr)
    00485                                 {
    00486                                         insert(size(), ptr);
    00487                                         return *this;
    00488                                 }
    00489
    00490                                 //-----------------------------------------
    00491                                 // Functions
    00492                                 //-----------------------------------------
    00493
    00498                                 reference at(size_type index)
    00499                                 {
    00500                                         return *static_cast<T**>(static_cast<void*>(&(Base::at(index))));
    00501                                 }
    00502
    00507                                 const T* at(size_type index) const
    00508                                 {
    00509                                         return static_cast<const T*>(Base::at(index));
    00510                                 }
    00511
    00515                                 iterator begin()
    00516                                 {
    00517                                         return iterator(this, 0);
    00518                                 }
    00519
    00523                                 const_iterator begin() const
    00524                                 {
    00525                                         return const_iterator(this, 0);
    00526                                 }
    00527
    00531                                 iterator end()
    00532                                 {
    00533                                         return iterator(this, size());
    00534                                 }
    00535
    00540                                 const_iterator end() const
    00541                                 {
    00542                                         return const_iterator(this, size());
    00543                                 }
    00544
    00546                                 size_type size() const
    00547                                 {
    00548                                         return Base::size();
    00549                                 }
    00550
    00552                                 size_type maxSize() const
    00553                                 {
    00554                                         return Base::max_size();
    00555                                 }
    00556
    00558                                 size_type max_size() const
    00559                                 {
    00560                                         return maxSize();
    00561                                 }
    00562
    00566                                 size_type capacity() const
    00567                                 {
    00568                                         return Base::capacity();
    00569                                 }
    00570
    00572                                 bool empty() const
    00573                                 {
    00574                                         return Base:: empty();
    00575                                 }
    00576
    00581                                 void reserve(size_type n)
    00582                                 {
    00583                                         return Base::reserve(n);
    00584                                 }
    00585
    00587                                 reference front()
    00588                                 {
    00589                                         return static_cast<reference>(Base::front());
    00590                                 }
    00591
    00593                                 const T* front() const
    00594                                 {
    00595                                         return static_cast<const T*>(Base::front());
    00596                                 }
    00597
    00599                                 reference back()
    00600                                 {
    00601                                         return static_cast<reference>(Base::back());
    00602                                 }
    00603
    00605                                 const T* back() const
    00606                                 {
    00607                                         return static_cast<const T*>(Base::back());
    00608                                 }
    00609
    00618                                 virtual void pushBack(T* ptr)
    00619                                 {
    00620                                         Base::push_back(ptr);
    00621                                 }
    00622
    00625                                 void push_back(T* ptr)
    00626                                 {
    00627                                         pushBack(ptr);
    00628                                 }
    00629
    00634                                 T* popBack()
    00635                                 {
    00636                                         return static_cast<T*>(Base::pop_back());
    00637                                 }
    00638
    00640                                 T* pop_back()
    00641                                 {
    00642                                         return popBack();
    00643                                 }
    00644
    00650                                 virtual void insert(size_type index, T* ptr)
    00651                                 {
    00652                                         Base::insert(index, ptr);
    00653                                 }
    00654
    00661                                 virtual void insert(size_type index, const Vector<T*>& other)
    00662                                 {
    00663                                         const Base* ptr=&other;
    00664                                         Base::insert(index, ptr->begin(), ptr->end());
    00665                                 }
    00666
    00671                                 void erase(size_type index)
    00672                                 {
    00673                                         Base::erase(index);
    00674                                 }
    00675
    00680                                 void erase(size_type first, size_type last)
    00681                                 {
    00682                                         Base::erase(first, last);
    00683                                 }
    00684
    00685                                 void clear()
    00686                                 {
    00687                                         return Base::clear();
    00688                                 }
    00689
    00696                                 void resize(size_t newSize)
    00697                                 {
    00698                                         return VectorImpl::resize(newSize);
    00699                                 }
    00700
    00707                                 Vector<T*>* duplicate() const
    00708                                 {
    00709                                         return new Vector<T*>(*this);
    00710                                 }
    00711
    00712 #ifndef EMBEDDED
    00713
    00722                                 virtual void print(std::ostream &out) const
    00723                                 {
    00724                                         print(out, "\n");
    00725                                 }
    00726
    00733                                 virtual void print(std::ostream &out, char const * separator) const
    00734                                 {
    00735                                         VectorImpl::print(out, separator);
    00736                                 }
    00737
    00738 #endif
    00739                         };
    00740
    00741
    00750                         template<class T>
    00751                         const T* find(const Vector<T*>* vector, const std::string& name)
    00752                         {
    00753                                 for(unsigned int i = 0; i < vector->size(); i++)
    00754                                 {
    00755                                         const T* element = (*vector)[i];
    00756                                         if(NULL != element && element->getName() == name) return element;
    00757                                 }
    00758                                 return NULL;
    00759                         }
    00760
    00761                         template<class T>
    00762                         T* find(Vector<T*>* vector, const std::string& name)
    00763                         {
    00764                                 for(unsigned int i = 0; i < vector->size(); i++)
    00765                                 {
    00766                                         T* element = (*vector)[i];
    00767                                         if(NULL != element && element->getName() == name) return element;
    00768                                 }
    00769                                 return NULL;
    00770                         }
    00771
    00776                         template<class T>
    00777                         bool operator ==(const Vector<T*>& vector1, const Vector<T*>& vector2)
    00778                         {
    00779                                 if(vector1.size() != vector2.size()) return false;
    00780                                 for(unsigned int i = 0; i < vector1.size(); i++)
    00781                                 {
    00782                                         if(*vector1[i] != *vector2[i]) return false;
    00783                                 }
    00784                                 return true;
    00785                         }
    00786
    00791                         template<class T>
    00792                         bool operator !=(const Vector<T*>& vector1, const Vector<T*>& vector2)
    00793                         {
    00794                                 return !(vector1==vector2);
    00795                         }
    00796                 }
    00797         }
    00798 }
    00799
    00800
    00801 #endif // MERS_VECTOR_H
    00802