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

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Containers/intList.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 <cassert>
    00015 #include "intList.h"
    00016
    00017 using namespace std;
    00018
    00019 #ifndef SIMPLE_INTLIST
    00020
    00021 using namespace Mers::Utils::Containers;
    00022
    00024 // Constructors, Copy Constructors, & Destructors
    00026
    00027 intList::intList()
    00028 {
    00029         _head_node_p                                    = new node();
    00030         //_head_node_p->_previous_node_p        = _head_node_p;
    00031         //_head_node_p->_next_node_p            = _head_node_p;
    00032 }
    00033
    00034
    00035 intList::intList(const intList & other)
    00036 {
    00037         if (this != &other)
    00038         {
    00039                 _head_node_p                                    = new node();
    00040                 //_head_node_p->_previous_node_p        = _head_node_p;
    00041                 //_head_node_p->_next_node_p            = _head_node_p;
    00042                 copy(other);
    00043         }
    00044 }
    00045
    00046
    00047 intList::~intList()
    00048 {
    00049         destroy();
    00050         _head_node_p->delete_children();
    00051         delete _head_node_p;
    00052 }
    00053
    00054
    00056 // const_iterator Class
    00058 intList::node::node(int new_int, node *new_previous_node_p, node *new_next_node_p)
    00059 {
    00060         _int                            = new_int;
    00061         _previous_node_p        = new_previous_node_p;
    00062         _next_node_p            = new_next_node_p;
    00063         null_node = false;
    00064 }
    00065
    00066
    00067 intList::node::node(node *new_previous_node_p, node *new_next_node_p)
    00068 {
    00069         _int                            = 0;  //int();
    00070         _previous_node_p        = new_previous_node_p;
    00071         _next_node_p            = new_next_node_p;
    00072         null_node = true;
    00073 }
    00074
    00075
    00076 intList::node::node()
    00077 {
    00078         _int                            = 0;
    00079         _previous_node_p        = this;
    00080         _next_node_p            = this;
    00081         null_node = true;
    00082 }
    00083
    00084 intList::node::~node()
    00085 {
    00086 }
    00087
    00088 void
    00089 intList::node::delete_children()
    00090 {
    00091         if (_next_node_p && !_next_node_p->null_node)
    00092         {
    00093                 _next_node_p->delete_children();
    00094                 delete _next_node_p;
    00095         }
    00096 }
    00097
    00098
    00100 // const_iterator Class
    00102 intList::const_iterator::const_iterator(node *new_current_node_p)
    00103 {
    00104         _current_node_p = new_current_node_p;
    00105 }
    00106
    00107
    00108 int
    00109 intList::const_iterator::operator *() const
    00110 {
    00111         assert(_current_node_p);
    00112         assert(!_current_node_p->null_node);
    00113         return  _current_node_p->_int;
    00114 }
    00115
    00116
    00117 bool
    00118 intList::const_iterator::operator ==(const const_iterator &other_iterator) const
    00119 {
    00120         return (_current_node_p == other_iterator._current_node_p);
    00121 }
    00122
    00123
    00124 bool
    00125 intList::const_iterator::operator !=(const const_iterator &other_iterator) const
    00126 {
    00127         return (_current_node_p != other_iterator._current_node_p);
    00128 }
    00129
    00130
    00131 intList::const_iterator &
    00132 intList::const_iterator::operator =(const const_iterator &other_iterator)
    00133 {
    00134         if (this != &other_iterator)
    00135         {
    00136                 _current_node_p = other_iterator._current_node_p;
    00137         }
    00138         return *this;
    00139 }
    00140
    00141
    00142 intList::const_iterator &
    00143 intList::const_iterator::operator ++()
    00144 {
    00145         assert(_current_node_p);
    00146         assert(!_current_node_p->null_node);
    00147         _current_node_p = _current_node_p->_next_node_p;
    00148         return *this;
    00149 }
    00150
    00151
    00152 intList::const_iterator
    00153 intList::const_iterator::operator ++(int)
    00154 {
    00155         intList::const_iterator temp_it = *this;
    00156         ++*this;
    00157         return (temp_it);
    00158 }
    00159
    00160
    00161 intList::const_iterator &
    00162 intList::const_iterator::operator --()
    00163 {
    00164         assert(_current_node_p);
    00165         assert(!_current_node_p->null_node);
    00166         _current_node_p = _current_node_p->_previous_node_p;
    00167         return *this;
    00168 }
    00169
    00170
    00171 intList::const_iterator
    00172 intList::const_iterator::operator --(int)
    00173 {
    00174         intList::const_iterator temp_it = *this;
    00175         --*this;
    00176         return (temp_it);
    00177 }
    00178
    00179
    00181 // iterator Class
    00183 intList::iterator::iterator(node *new_current_node_p) :
    00184 const_iterator(new_current_node_p)
    00185 {
    00186 }
    00187
    00188
    00189 int &
    00190 intList::iterator::operator *()
    00191 {
    00192         assert(_current_node_p);
    00193         assert(!_current_node_p->null_node);
    00194         return  _current_node_p->_int;
    00195 }
    00196
    00197
    00198 bool
    00199 intList::iterator::operator ==(const iterator &other_iterator) const
    00200 {
    00201         return (_current_node_p == other_iterator._current_node_p);
    00202 }
    00203
    00204
    00205 bool
    00206 intList::iterator::operator !=(const iterator &other_iterator) const
    00207 {
    00208         return (_current_node_p != other_iterator._current_node_p);
    00209 }
    00210
    00211
    00212 intList::iterator &
    00213 intList::iterator::operator =(const iterator &other_iterator)
    00214 {
    00215         if (this != &other_iterator)
    00216         {
    00217                 _current_node_p = other_iterator._current_node_p;
    00218         }
    00219         return *this;
    00220 }
    00221
    00222
    00223 intList::iterator &
    00224 intList::iterator::operator ++()
    00225 {
    00226         assert(_current_node_p);
    00227         assert(!_current_node_p->null_node);
    00228         _current_node_p = _current_node_p->_next_node_p;
    00229         return *this;
    00230 }
    00231
    00232
    00233 intList::iterator
    00234 intList::iterator::operator ++(int)
    00235 {
    00236         intList::iterator temp_it = *this;
    00237         ++*this;
    00238         return (temp_it);
    00239 }
    00240
    00241
    00242 intList::iterator &
    00243 intList::iterator::operator --()
    00244 {
    00245         assert(_current_node_p);
    00246         assert(!_current_node_p->null_node);
    00247         _current_node_p = _current_node_p->_previous_node_p;
    00248         return *this;
    00249 }
    00250
    00251
    00252 intList::iterator
    00253 intList::iterator::operator --(int)
    00254 {
    00255         intList::iterator temp_it = *this;
    00256         --*this;
    00257         return (temp_it);
    00258 }
    00259
    00260
    00262 // Public Function Members
    00264 bool
    00265 intList::empty() const
    00266 {
    00267         //return (_head_node_p == _head_node_p->_next_node_p);
    00268         return _head_node_p->_next_node_p->null_node;
    00269 }
    00270
    00271
    00272 void
    00273 intList::clear()
    00274 {
    00275         if (_head_node_p)
    00276         {
    00277                 _head_node_p->delete_children();
    00278                 delete _head_node_p;
    00279         }
    00280         _head_node_p = new node();
    00281         /*
    00282         iterator it = begin();
    00283         while (it != end())
    00284         {
    00285                 erase(it++);
    00286         }
    00287         */
    00288 }
    00289
    00290
    00291 intList::iterator
    00292 intList::begin()
    00293 {
    00294         return intList::iterator(_head_node_p->_next_node_p);
    00295 }
    00296
    00297
    00298 intList::const_iterator
    00299 intList::begin() const
    00300 {
    00301         return intList::const_iterator(_head_node_p->_next_node_p);
    00302 }
    00303
    00304
    00305 intList::iterator
    00306 intList::end()
    00307 {
    00308         return intList::iterator(_head_node_p);
    00309 }
    00310
    00311
    00312 intList::const_iterator
    00313 intList::end() const
    00314 {
    00315         return intList::const_iterator(_head_node_p);
    00316 }
    00317
    00318
    00319 unsigned int
    00320 intList::size() const
    00321 {
    00322         unsigned int i = 0;
    00323         for (const_iterator it = begin(); it != end(); it++)
    00324         {
    00325                 i++;
    00326         }
    00327         return i;
    00328 }
    00329
    00330
    00331 int &
    00332 intList::front()
    00333 {
    00334         assert(_head_node_p);
    00335         assert(!empty());
    00336         /*# assert(!_head_node_p->null_node); // why did this ever work?? */
    00337         return *begin();
    00338 }
    00339
    00340
    00341 int
    00342 intList::front() const
    00343 {
    00344         assert(_head_node_p);
    00345         assert(!empty());
    00346         /*# assert(!_head_node_p->null_node); // why did this ever work?? */
    00347         return *begin();
    00348 }
    00349
    00350
    00351 int &
    00352 intList::back()
    00353 {
    00354         assert(_head_node_p);
    00355         assert(!empty());
    00356         /*# assert(!_head_node_p->null_node); // why did this ever work?? */
    00357         return *(--end());
    00358 }
    00359
    00360
    00361 int
    00362 intList::back() const
    00363 {
    00364         assert(_head_node_p);
    00365         assert(!empty());
    00366         /*# assert(!_head_node_p->null_node); // why did this ever work?? */
    00367         return *(--end());
    00368 }
    00369
    00370
    00371 intList::iterator
    00372 intList::insert(iterator it, int _int)
    00373 {
    00374         node *_current_node_p = it._current_node_p;
    00375         _current_node_p->_previous_node_p = new node(_int,
    00376         _current_node_p->_previous_node_p, _current_node_p);
    00377         _current_node_p = _current_node_p->_previous_node_p;
    00378         _current_node_p->_previous_node_p->_next_node_p = _current_node_p;
    00379         return intList::iterator(_current_node_p);
    00380 }
    00381
    00382
    00383 void
    00384 intList::push_front(int _int)
    00385 {
    00386         insert(begin(), _int);
    00387 }
    00388
    00389
    00390 void
    00391 intList::push_back(int _int)
    00392 {
    00393         insert(end(), _int);
    00394 }
    00395
    00396
    00397 intList::iterator
    00398 intList::erase(iterator it)
    00399 {
    00400         node *temp_node_p = (it++)._current_node_p;
    00401         temp_node_p->_previous_node_p->_next_node_p = temp_node_p->_next_node_p;
    00402         temp_node_p->_next_node_p->_previous_node_p = temp_node_p->_previous_node_p;
    00403         delete temp_node_p;
    00404         return it;
    00405 }
    00406
    00407
    00408 void
    00409 intList::pop_front()
    00410 {
    00411         assert(_head_node_p);
    00412         if (!_head_node_p->_next_node_p->null_node)
    00413                 erase(begin());
    00414 }
    00415
    00416
    00417 void
    00418 intList::pop_back()
    00419 {
    00420         assert(_head_node_p);
    00421         if (!_head_node_p->_next_node_p->null_node)
    00422                 erase(--end());
    00423 }
    00424
    00425
    00426 void
    00427 intList::remove(int _int)
    00428 {
    00429         for (iterator current_it = begin(); current_it != end();)
    00430         {
    00431                 if (*current_it == _int)
    00432                 {
    00433                         erase(current_it++);
    00434                 } else
    00435                 {
    00436                         ++current_it;
    00437                 }
    00438         }
    00439 }
    00440
    00441
    00443 // Public Operators
    00445 const intList &
    00446 intList::operator =(const intList &other)
    00447 {
    00448         if (this != &other)
    00449         {
    00450                 destroy();
    00451                 copy(other);
    00452         }
    00453         return (*this);
    00454 }
    00455
    00456
    00457 bool
    00458 intList::operator ==(const intList &other) const
    00459 {
    00460         intList::const_iterator this_const_it;
    00461         intList::const_iterator other_const_it;
    00462         for (this_const_it = begin(), other_const_it = other.begin();
    00463         this_const_it != end() && other_const_it != other.end();
    00464         this_const_it++, other_const_it++)
    00465         {
    00466                 if (*this_const_it != *other_const_it) return false;
    00467         }
    00468         if (this_const_it == end() && other_const_it == other.end())
    00469         {
    00470                 return true;
    00471         } else
    00472         {
    00473                 return false;
    00474         }
    00475 }
    00476
    00477
    00478 bool
    00479 intList::operator !=(const intList &other) const
    00480 {
    00481         return !(*this == other);
    00482 }
    00483
    00484
    00485 ostream &
    00486 Mers::Utils::Containers::operator <<(ostream &_ostream, const intList &_intList)
    00487 {
    00488         _ostream << "(";
    00489         intList::const_iterator const_it = _intList.begin();
    00490         while (const_it != _intList.end())
    00491         {
    00492                 _ostream << *const_it;
    00493                 const_it++;
    00494                 if (const_it != _intList.end())
    00495                 {
    00496                         _ostream << " ";
    00497                 }
    00498         }
    00499         _ostream << ")";
    00500         return _ostream;
    00501 }
    00502
    00503
    00505 // Private Function Members
    00507 void
    00508 intList::destroy()
    00509 {
    00510         clear();
    00511 }
    00512 void
    00513 intList::copy(const intList &other)
    00514 {
    00515         clear();
    00516         for (const_iterator const_it = other.begin();
    00517         const_it != other.end(); const_it++)
    00518         {
    00519                 int tmp = (*const_it);
    00520                 push_back(tmp);
    00521         }
    00522 }
    00523
    00524 #endif
    00525
    00526