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

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

    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 #include "Vector.h"
    00015 #include <Mers/Utils/Exceptions/OutOfRangeException.h>
    00016 #include <Mers/Utils/Exceptions/UnsupportedOperationException.h>
    00017
    00018 using namespace Mers::Utils::Containers;
    00019 using Mers::Utils::Lang::Object;
    00020 using Mers::Utils::Exceptions::UnsupportedOperationException;
    00021 using Mers::Utils::Exceptions::OutOfRangeException;
    00022 using std::ostream;
    00023
    00025 // Construction/Destruction
    00027
    00028 VectorImpl::VectorImpl(VectorImpl const &other) : Base()
    00029 {
    00030   insert(0,other.begin(),other.end());
    00031 }
    00032
    00033 VectorImpl::~VectorImpl()
    00034 {
    00035   clear();
    00036 }
    00037
    00038
    00040 // Functions
    00042
    00043
    00044 void VectorImpl::resize(int newSize)
    00045 {
    00046   if(newSize<(int)size()) erase(newSize,size());
    00047   Base::resize(newSize);
    00048 }
    00049
    00050 Object* VectorImpl::pop_back()
    00051 {
    00052   Object* temp = at(size()-1);
    00053   Base::pop_back();
    00054   return temp;
    00055 }
    00056
    00057 void VectorImpl::clear()
    00058 {
    00059   resize(0);
    00060 }
    00061
    00062
    00063 void VectorImpl::insert(size_type index, const_reference data)
    00064 {
    00065   Base::insert(begin()+index,data);
    00066 }
    00067
    00068
    00069 void VectorImpl::insert(size_type index, VectorImpl::const_iterator first,
    00070                         VectorImpl::const_iterator last)
    00071 {
    00072   size_type old_index = index;
    00073   size_type length = last - first;
    00074
    00075   Base::insert(begin()+index, length, NULL);
    00076
    00077   try {
    00078     for(;first!=last;first++)
    00079     {
    00080       if((*first) != NULL) Base::operator[](index)=(*first)->duplicate();
    00081       index++;
    00082     }
    00083   } catch(UnsupportedOperationException) {
    00084     // leave the vector in a consistent state in case duplicate() fails
    00085     for(size_type i = old_index; i < index; i++)
    00086     {
    00087       if(Base::operator[](i) != NULL) delete Base::operator[](i);
    00088     }
    00089     Base::erase(begin()+old_index, begin()+old_index+length);
    00090     throw; // rethrow the exception
    00091   }
    00092 }
    00093
    00094 void VectorImpl::erase(size_type index)
    00095 {
    00096   delete at(index);
    00097   Base::erase(begin()+index);
    00098 }
    00099
    00100 void VectorImpl::erase(VectorImpl::size_type first,
    00101   VectorImpl::size_type last)
    00102 {
    00103   if(last>size())
    00104   {
    00105     throw OutOfRangeException(0, size()-1, last, "Vector.cpp", 64);
    00106   }
    00107   for(size_type i = first;i!=last;i++)
    00108   {
    00109     if(Base::operator[](i) != NULL) delete Base::operator[](i);
    00110   }
    00111   Base::erase(begin()+first, begin()+last);
    00112 }
    00113
    00114
    00115 #ifndef EMBEDDED
    00116
    00117 void VectorImpl::print(ostream &out, char const * seperator) const
    00118 {
    00119   for(const_iterator i=begin();i!=end();i++)
    00120   {
    00121      if(i != begin()) out << seperator;
    00122     out << *i;
    00123   }
    00124 }
    00125
    00126 #endif
    00127