User Manual, Developers Guide and API Documentation

FastList.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_CONTAINER_FASTLIST_HPP
00029 #define WNS_CONTAINER_FASTLIST_HPP
00030 
00031 #include <WNS/Assure.hpp>
00032 #include <WNS/container/FastListNode.hpp>
00033 #include <WNS/NonCopyable.hpp>
00034 #include <memory>
00035 
00036 namespace wns { namespace container {
00037 
00057     template <typename T>
00058     class FastList
00059     {
00060     public:
00065         class iterator {
00069             friend class FastList;
00070         public:
00076             iterator()
00077                 : fln(0)
00078             {}
00079 
00085             explicit iterator(FastListNode<T>* f)
00086                 : fln(f)
00087             {}
00088 
00092             bool operator==(const iterator& i) const
00093             {
00094                 return fln==i.fln;
00095             }
00096 
00100             bool operator!=(const iterator& i) const
00101             {
00102                 return fln!=i.fln;
00103             }
00104 
00109             T& operator*()
00110             {
00111                 return fln->getData();
00112             }
00113 
00118             T& operator->()
00119             {
00120                 return fln->getData();
00121             }
00122 
00126             iterator& operator++()
00127             {
00128                 assure(fln, "Nothing to iterate on");
00129                 fln = fln->getNext();
00130                 return *this;
00131             };
00132 
00136             iterator operator++(int)
00137             {
00138                 assure(fln, "Nothing to iterate on");
00139                 iterator i = *this;
00140                 fln = fln->getNext();
00141                 return i;
00142             }
00143 
00147             iterator& operator--() {
00148                 assure(fln, "Nothing to iterate on");
00149                 fln = fln->getPrevious();
00150                 return *this;
00151             }
00152 
00156             iterator operator--(int) {
00157                 assure(fln, "Nothing to iterate on");
00158                 iterator i = *this;
00159                 fln = fln->getPrevious();
00160                 return i;
00161             }
00162 
00163         private:
00167             FastListNode<T>* fln;
00168         };
00169 
00173         FastList()
00174             : b(FastListNode<T>()),
00175               s(0)
00176         {}
00177 
00181         ~FastList()
00182         {
00183             clear();
00184         }
00185 
00189         FastList(const FastList& f)
00190             : b(FastListNode<T>()),
00191               s(0)
00192         {
00193             copy(f);
00194         }
00195 
00199         void operator=(const FastList& f)
00200         {
00201             clear();
00202             copy(f);
00203         }
00204 
00208         iterator begin() const
00209         {
00210             return iterator(b.getNext());
00211         }
00212 
00218         iterator end() const
00219         {
00220             return iterator(&b);
00221         }
00222 
00226         T& front() const
00227         {
00228             return b.getNext()->getData();
00229         }
00230 
00234         bool empty() const
00235         {
00236             return s==0;
00237         }
00238 
00242         int size() const
00243         {
00244             return s;
00245         }
00246 
00250         void clear()
00251         {
00252             iterator itr, itrEnd;
00253             itr = begin();
00254             itrEnd = end();
00255             while(itr!=itrEnd) {
00256                 erase(itr);
00257                 itr = begin();
00258                 itrEnd = end();
00259             };
00260         }
00261 
00265         void erase(const iterator& i)
00266         {
00267             assure(i!=end(), "Can't erase this->end()");
00268             assure(s>0, "No elements in list");
00269             --s;
00270             FastListNode<T>* fln = i.fln;
00271             fln->removeFromList();
00272             fln->getData()->removeFastListNode(this);
00273             delete fln;
00274         }
00275 
00279         void remove(const T& x)
00280         {
00281             assure(s>0, "No elements in list");
00282             --s;
00283             FastListNode<T>* fln = x->getFastListNode(this);
00284             fln->removeFromList();
00285             x->removeFastListNode(this);
00286             delete fln;
00287         }
00288 
00292         void remove(T* x)
00293         {
00294             assure(s>0, "No elements in list");
00295             --s;
00296             FastListNode<T>* fln = x->getFastListNode(this);
00297             fln->removeFromList();
00298             x->removeFastListNode(this);
00299             delete fln;
00300         }
00301 
00305         void push_front(const T& x)
00306         {
00307             std::auto_ptr< FastListNode<T> > fln(new FastListNode<T>(x));
00308             x->setFastListNode(this, fln.get());
00309             fln->addToList(b);
00310             ++s;
00311             // everything ok, release the poiner, now handled by FastList
00312             fln.release();
00313         }
00314 
00318         void push_front(T* x)
00319         {
00320             push_front(&x);
00321         };
00322 
00326         void push_back(const T& x)
00327         {
00328             std::auto_ptr< FastListNode<T> > fln(new FastListNode<T>(x));
00329             x->setFastListNode(this, fln.get());
00330             fln->addToList(*b.getPrevious());
00331             ++s;
00332             // everything ok, release the poiner, now handled by FastList
00333             fln.release();
00334         }
00335 
00339         void push_back(T* x)
00340         {
00341             push_back(&x);
00342         };
00343 
00344         void pop_front()
00345         {
00346             remove(front());
00347         }
00348 
00352         bool contains(const T& x)
00353         {
00354             return x->isInList(this);
00355         }
00356 
00357     private:
00361         void copy(const FastList& f)
00362         {
00363             iterator itr;
00364             for(itr=f.begin(); itr!=f.end(); ++itr)
00365             {
00366                 push_front(*itr);
00367             }
00368         }
00369 
00373         mutable FastListNode<T> b;
00374 
00378         int s;
00379     };
00380 }}
00381 #endif

Generated on Thu May 24 03:31:33 2012 for openWNS by  doxygen 1.5.5