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

# /Users/fpy/titan-1-5-MBARI/Mers/Planning/Reactive/CCA/rpTransitionPtrs.cpp

    00001 //=============================================================================
    00002 //
    00003 // Copyright (c) 2001           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 // Author:                      Seung H. Chung
    00010 // Organization:        Model-based Embedded and Robotic Systems Group
    00011 //                                      Massachusetts Institute of Technology
    00012 //
    00013 // rpTransitionPtrs class
    00014 //
    00015 //=============================================================================
    00016
    00017 #include <cstdio>
    00018 #include <cassert>
    00019
    00020 #include "rpTransitionPtrs.h"
    00021 #include "rpTransition.h"
    00022 //------------------------------------------------------------------------------
    00023 // Constructors, Copy Constructors, & Destructors
    00024 //------------------------------------------------------------------------------
    00025 rpTransitionPtrs::rpTransitionPtrs() {
    00026         _rpTransitionPtr_array  = NULL;
    00027         _size                           = 0;
    00028 }
    00029 rpTransitionPtrs::rpTransitionPtrs(sizeType numb_of_rpTransitionPtrs) {
    00030         _size                                   = numb_of_rpTransitionPtrs;
    00031         _rpTransitionPtr_array  = new const rpTransition *[size()];
    00032         for (sizeType i = 0; i < size(); i++) {
    00033                 _rpTransitionPtr_array[i] = NULL;
    00034         }
    00035 }
    00036 rpTransitionPtrs::rpTransitionPtrs(const rpTransitionPtrs &other) {
    00037         if (this != &other) {
    00038                 copy(other);
    00039         }
    00040 }
    00041 rpTransitionPtrs::~rpTransitionPtrs() {
    00042         destroy();
    00043 }
    00044 //------------------------------------------------------------------------------
    00045 // Public Function Members
    00046 //------------------------------------------------------------------------------
    00047 const rpTransitionPtrs::sizeType rpTransitionPtrs::size() const {
    00048         return _size;
    00049 }
    00050 const bool rpTransitionPtrs::empty() const {
    00051         return (_size == 0);
    00052 }
    00053 //------------------------------------------------------------------------------
    00054 void rpTransitionPtrs::clear() {
    00055         for (sizeType i = 0; i < size(); i++) {
    00056                 _rpTransitionPtr_array[i] = NULL;
    00057         }
    00058 }
    00059 //------------------------------------------------------------------------------
    00060 // Public Operators
    00061 //------------------------------------------------------------------------------
    00062 const rpTransition *&rpTransitionPtrs::operator [](const sizeType index_) {
    00063         if (index_ < size()) {
    00064                 return _rpTransitionPtr_array[index_];
    00065         } else {
    00066                 return _rpTransitionPtr_array[0];
    00067         }
    00068 }
    00069 const rpTransition *const & rpTransitionPtrs::operator [](const sizeType index_) const {
    00070         if (index_ < size()) {
    00071                 return _rpTransitionPtr_array[index_];
    00072         } else {
    00073                 return _rpTransitionPtr_array[0];
    00074         }
    00075 }
    00076 const rpTransitionPtrs &rpTransitionPtrs::operator =(const rpTransitionPtrs &other) {
    00077         if (this != &other) {
    00078                 destroy();
    00079                 copy(other);
    00080         }
    00081         return (*this);
    00082 }
    00083 const bool rpTransitionPtrs::operator ==(const rpTransitionPtrs &other) const {
    00084         if (_size == other._size) {
    00085                 for (sizeType i = 0; i < size(); i++) {
    00086                         if (_rpTransitionPtr_array[i] != other._rpTransitionPtr_array[i]) return false;
    00087                 }
    00088                 return true;
    00089         }
    00090         return false;
    00091 }
    00092 const bool rpTransitionPtrs::operator !=(const rpTransitionPtrs &other) const {
    00093         return !(*this == other);
    00094 }
    00095 std::ostream &operator <<(std::ostream &_ostream,
    00096                                                   const rpTransitionPtrs &_rpTransitionPtrs) {
    00097         _ostream << "rpTransitionPtrs:" << std::endl;
    00098         rpTransitionPtrs::sizeType i = 0;
    00099         while (i < _rpTransitionPtrs.size()) {
    00100                 _ostream << *_rpTransitionPtrs[i];
    00101                 i++;
    00102                 if (i < _rpTransitionPtrs.size()) {
    00103                         _ostream << std::endl;
    00104                 }
    00105         }
    00106         return _ostream;
    00107 }
    00108 //------------------------------------------------------------------------------
    00109 // Private Function Members
    00110 //------------------------------------------------------------------------------
    00111 void rpTransitionPtrs::destroy() {
    00112         clear();
    00113         delete [] _rpTransitionPtr_array;
    00114         _size = 0;
    00115 }
    00116 void rpTransitionPtrs::copy(const rpTransitionPtrs &other) {
    00117         _size                                   = other._size;
    00118         _rpTransitionPtr_array  = new const rpTransition *[size()];
    00119         for (sizeType i = 0; i < size(); i++) {
    00120                 _rpTransitionPtr_array[i] = other[i];
    00121         }
    00122 }
    00123
    00124
    00125 /*
    00126 //------------------------------------------------------------------------------
    00127 // Constructors, Copy Constructors, & Destructors
    00128 //------------------------------------------------------------------------------
    00129 rpTransitionPtrs::rpTransitionPtrs() {
    00130         _head_node_p                                    = new node();
    00131         _head_node_p->_previous_node_p  = _head_node_p;
    00132         _head_node_p->_next_node_p              = _head_node_p;
    00133 }
    00134 rpTransitionPtrs::rpTransitionPtrs(const rpTransitionPtrs &other) {
    00135         if (this != &other) {
    00136                 _head_node_p                                    = new node();
    00137                 _head_node_p->_previous_node_p  = _head_node_p;
    00138                 _head_node_p->_next_node_p              = _head_node_p;
    00139                 copy(other);
    00140         }
    00141 }
    00142 rpTransitionPtrs::~rpTransitionPtrs() {
    00143         destroy();
    00144         delete _head_node_p;
    00145 }
    00146 //------------------------------------------------------------------------------
    00147 // node Class
    00148 //------------------------------------------------------------------------------
    00149 rpTransitionPtrs::node::node(const rpTransition *new_rpTransition_p,
    00150                                                    node *new_previous_node_p,
    00151                                                    node *new_next_node_p) {
    00152         _rpTransition_p         = new_rpTransition_p;
    00153         _previous_node_p        = new_previous_node_p;
    00154         _next_node_p            = new_next_node_p;
    00155 }
    00156 rpTransitionPtrs::node::node(node *new_previous_node_p, node *new_next_node_p) {
    00157         _rpTransition_p         = NULL;
    00158         _previous_node_p        = new_previous_node_p;
    00159         _next_node_p            = new_next_node_p;
    00160 }
    00161 rpTransitionPtrs::node::~node(){
    00162 }
    00163 //------------------------------------------------------------------------------
    00164 // const_iterator Class
    00165 //------------------------------------------------------------------------------
    00166 rpTransitionPtrs::const_iterator::const_iterator(node *new_current_node_p) {
    00167         _current_node_p = new_current_node_p;
    00168 }
    00169 const rpTransition *const rpTransitionPtrs::const_iterator::operator *() const {
    00170         return _current_node_p->_rpTransition_p;
    00171 }
    00172 bool rpTransitionPtrs::const_iterator::
    00173 operator ==(const const_iterator &other_iterator) const {
    00174         return (_current_node_p == other_iterator._current_node_p);
    00175 }
    00176 bool rpTransitionPtrs::const_iterator::
    00177 operator !=(const const_iterator &other_iterator) const {
    00178         return (_current_node_p != other_iterator._current_node_p);
    00179 }
    00180 rpTransitionPtrs::const_iterator &
    00181 rpTransitionPtrs::const_iterator::operator =(const const_iterator &
    00182                                                                                    other_iterator) {
    00183         if (this != &other_iterator) {
    00184                 _current_node_p = other_iterator._current_node_p;
    00185         }
    00186         return *this;
    00187 }
    00188 rpTransitionPtrs::const_iterator& rpTransitionPtrs::const_iterator::operator ++() {
    00189         _current_node_p = _current_node_p->_next_node_p;
    00190         return *this;
    00191 }
    00192 rpTransitionPtrs::const_iterator
    00193 rpTransitionPtrs::const_iterator::operator ++(int) {
    00194         rpTransitionPtrs::const_iterator temp_it = *this;
    00195         ++*this;
    00196         return (temp_it);
    00197 }
    00198 rpTransitionPtrs::const_iterator& rpTransitionPtrs::const_iterator::operator --() {
    00199         if (_current_node_p->_previous_node_p != NULL) {
    00200                 _current_node_p = _current_node_p->_previous_node_p;
    00201         }
    00202         return *this;
    00203 }
    00204 rpTransitionPtrs::const_iterator
    00205 rpTransitionPtrs::const_iterator::operator --(int) {
    00206         rpTransitionPtrs::const_iterator temp_it = *this;
    00207         --*this;
    00208         return (temp_it);
    00209 }
    00210 //------------------------------------------------------------------------------
    00211 // iterator Class
    00212 //------------------------------------------------------------------------------
    00213 rpTransitionPtrs::iterator::iterator(node *new_current_node_p) :
    00214 const_iterator(new_current_node_p) {
    00215 }
    00216 const rpTransition *rpTransitionPtrs::iterator::operator *() {
    00217         return  _current_node_p->_rpTransition_p;
    00218 }
    00219 bool rpTransitionPtrs::iterator::operator ==(const iterator &other_iterator) const {
    00220         return (_current_node_p == other_iterator._current_node_p);
    00221 }
    00222 bool rpTransitionPtrs::iterator::operator !=(const iterator &other_iterator) const {
    00223         return (_current_node_p != other_iterator._current_node_p);
    00224 }
    00225 rpTransitionPtrs::iterator &
    00226 rpTransitionPtrs::iterator::operator =(const iterator &other_iterator) {
    00227         if (this != &other_iterator) {
    00228                 _current_node_p = other_iterator._current_node_p;
    00229         }
    00230         return *this;
    00231 }
    00232 rpTransitionPtrs::iterator& rpTransitionPtrs::iterator::operator ++() {
    00233         _current_node_p = _current_node_p->_next_node_p;
    00234         return *this;
    00235 }
    00236 rpTransitionPtrs::iterator rpTransitionPtrs::iterator::operator ++(int) {
    00237         rpTransitionPtrs::iterator temp_it = *this;
    00238         ++*this;
    00239         return (temp_it);
    00240 }
    00241 rpTransitionPtrs::iterator& rpTransitionPtrs::iterator::operator --() {
    00242         if (_current_node_p->_previous_node_p != NULL) {
    00243                 _current_node_p = _current_node_p->_previous_node_p;
    00244         }
    00245         return *this;
    00246 }
    00247 rpTransitionPtrs::iterator rpTransitionPtrs::iterator::operator --(int) {
    00248         rpTransitionPtrs::iterator temp_it = *this;
    00249         --*this;
    00250         return (temp_it);
    00251 }
    00252 //------------------------------------------------------------------------------
    00253 // Public Function Members
    00254 //------------------------------------------------------------------------------
    00255 bool rpTransitionPtrs::empty() const {
    00256         return (_head_node_p == _head_node_p->_next_node_p);
    00257 }
    00258 void rpTransitionPtrs::clear() {
    00259         iterator it = begin();
    00260         while (it != end()) {
    00261                 erase(it++);
    00262         }
    00263 }
    00264 rpTransitionPtrs::iterator rpTransitionPtrs::begin() {
    00265         return rpTransitionPtrs::iterator(_head_node_p->_next_node_p);
    00266 }
    00267 rpTransitionPtrs::const_iterator rpTransitionPtrs::begin() const {
    00268         return rpTransitionPtrs::const_iterator(_head_node_p->_next_node_p);
    00269 }
    00270 rpTransitionPtrs::iterator rpTransitionPtrs::end() {
    00271         return rpTransitionPtrs::iterator(_head_node_p);
    00272 }
    00273 rpTransitionPtrs::const_iterator rpTransitionPtrs::end() const {
    00274         return rpTransitionPtrs::const_iterator(_head_node_p);
    00275 }
    00276 //------------------------------------------------------------------------------
    00277 rpTransitionPtrs::const_iterator
    00278 rpTransitionPtrs::insert_rpTransition(const rpTransition &new_rpTransition) {
    00279         for (const_iterator const_it = begin(); const_it != end(); const_it++) {
    00280                 if (*const_it == &new_rpTransition) return end();
    00281         }
    00282         push_back(&new_rpTransition);
    00283         return --end();
    00284 }
    00285 //------------------------------------------------------------------------------
    00286 // Public Operators
    00287 //------------------------------------------------------------------------------
    00288 const rpTransitionPtrs &rpTransitionPtrs::operator =(const rpTransitionPtrs &other) {
    00289         if (this != &other) {
    00290                 destroy();
    00291                 copy(other);
    00292         }
    00293         return (*this);
    00294 }
    00295 bool rpTransitionPtrs::operator ==(const rpTransitionPtrs &other) const {
    00296         rpTransitionPtrs::const_iterator this_const_it;
    00297         rpTransitionPtrs::const_iterator other_const_it;
    00298         for (this_const_it = begin(), other_const_it = other.begin();
    00299         this_const_it != end() && other_const_it != other.end();
    00300         this_const_it++, other_const_it++) {
    00301                 if (*this_const_it != *other_const_it) return false;
    00302         }
    00303         if (this_const_it == end() && other_const_it == other.end()) {
    00304                 return true;
    00305         } else {
    00306                 return false;
    00307         }
    00308 }
    00309 bool rpTransitionPtrs::operator !=(const rpTransitionPtrs &other) const {
    00310         return !(*this == other);
    00311 }
    00312 std::ostream &operator <<(std::ostream &_ostream,
    00313                                                   const rpTransitionPtrs &_rpTransitionPtrs) {
    00314         _ostream << "rpTransitions:" << std::endl;
    00315         rpTransitionPtrs::const_iterator const_it = _rpTransitionPtrs.begin();
    00316         while (const_it != _rpTransitionPtrs.end()) {
    00317                 _ostream << **const_it;
    00318                 const_it++;
    00319                 if (const_it != _rpTransitionPtrs.end()) {
    00320                         _ostream << std::endl;
    00321                 }
    00322         }
    00323         return _ostream;
    00324 }
    00325 //------------------------------------------------------------------------------
    00326 // Private Function Members
    00327 //------------------------------------------------------------------------------
    00328 const rpTransition *rpTransitionPtrs::front() {
    00329         return *begin();
    00330 }
    00331 const rpTransition *const rpTransitionPtrs::front() const {
    00332         return *begin();
    00333 }
    00334 const rpTransition *rpTransitionPtrs::back() {
    00335         return *(--end());
    00336 }
    00337 const rpTransition *const rpTransitionPtrs::back() const {
    00338         return *(--end());
    00339 }
    00340 rpTransitionPtrs::iterator
    00341 rpTransitionPtrs::insert(iterator it,const rpTransition *_rpTransition_p) {
    00342         node *_current_node_p = it._current_node_p;
    00343         _current_node_p->_previous_node_p = new node(_rpTransition_p,
    00344                 _current_node_p->_previous_node_p, _current_node_p);
    00345         _current_node_p = _current_node_p->_previous_node_p;
    00346         _current_node_p->_previous_node_p->_next_node_p = _current_node_p;
    00347         return rpTransitionPtrs::iterator(_current_node_p);
    00348 }
    00349 void rpTransitionPtrs::push_front(const rpTransition *_rpTransition_p) {
    00350         insert(begin(), _rpTransition_p);
    00351 }
    00352 void rpTransitionPtrs::push_back(const rpTransition *_rpTransition_p) {
    00353         insert(end(), _rpTransition_p);
    00354 }
    00355 rpTransitionPtrs::iterator rpTransitionPtrs::erase(iterator it) {
    00356         node *temp_node_p = (it++)._current_node_p;
    00357         temp_node_p->_previous_node_p->_next_node_p = temp_node_p->_next_node_p;
    00358         temp_node_p->_next_node_p->_previous_node_p = temp_node_p->_previous_node_p;
    00359         delete temp_node_p;
    00360         return it;
    00361 }
    00362 void rpTransitionPtrs::pop_front() {
    00363         erase(begin());
    00364 }
    00365 void rpTransitionPtrs::pop_back() {
    00366         erase(--end());
    00367 }
    00368 void rpTransitionPtrs::remove(const rpTransition *_rpTransition_p) {
    00369         iterator it = end();
    00370         for (iterator current_it = begin(); current_it != end();) {
    00371                 if (*current_it == _rpTransition_p) {
    00372                         erase(current_it++);
    00373                 } else {
    00374                         ++current_it;
    00375                 }
    00376         }
    00377 }
    00378 //------------------------------------------------------------------------------
    00379 void rpTransitionPtrs::destroy() {
    00380         clear();
    00381 }
    00382 void rpTransitionPtrs::copy(const rpTransitionPtrs &other) {
    00383         for (const_iterator const_it = other.begin();
    00384         const_it != other.end(); const_it++) {
    00385                 push_back(*const_it);
    00386         }
    00387 }
    00388 */