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

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