OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen OpenCFD Solutions Contact OpenFOAM

DLListBaseI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2007 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Base singly-linked list.
00027 
00028 \*---------------------------------------------------------------------------*/
00029 
00030 #include "error.H"
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 namespace Foam
00035 {
00036 
00037 // * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * * //
00038 
00039 inline DLListBase::link::link()
00040 :
00041     prev_(0),
00042     next_(0)
00043 {}
00044 
00045 
00046 inline DLListBase::DLListBase()
00047 :
00048     first_(0),
00049     last_(0),
00050     nElmts_(0)
00051 {}
00052 
00053 
00054 inline DLListBase::DLListBase(link* a)
00055 :
00056     first_(a),
00057     last_(a),
00058     nElmts_(1)
00059 {
00060     a->prev_ = a;
00061     a->next_ = a;
00062 }
00063 
00064 
00065 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00066 
00067 inline DLListBase::~DLListBase()
00068 {}
00069 
00070 
00071 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00072 
00073 inline bool DLListBase::link::registered() const
00074 {
00075     return prev_ != 0 && next_ != 0;
00076 }
00077 
00078 
00079 inline void DLListBase::link::deregister()
00080 {
00081     prev_ = 0;
00082     next_ = 0;
00083 }
00084 
00085 
00086 inline label DLListBase::size() const
00087 {
00088     return nElmts_;
00089 }
00090 
00091 
00092 inline DLListBase::link* DLListBase::first()
00093 {
00094     if (!nElmts_)
00095     {
00096         FatalErrorIn("DLListBase::first()")
00097             << "list is empty"
00098             << abort(FatalError);
00099     }
00100     return first_;
00101 }
00102 
00103 
00104 inline const DLListBase::link* DLListBase::first() const
00105 {
00106     if (!nElmts_)
00107     {
00108         FatalErrorIn("DLListBase::first() const")
00109             << "list is empty"
00110             << abort(FatalError);
00111     }
00112     return first_;
00113 }
00114 
00115 
00116 inline DLListBase::link* DLListBase::last()
00117 {
00118     if (!nElmts_)
00119     {
00120         FatalErrorIn("DLListBase::last()")
00121             << "list is empty"
00122             << abort(FatalError);
00123     }
00124     return last_;
00125 }
00126 
00127 
00128 inline const DLListBase::link* DLListBase::last() const
00129 {
00130     if (!nElmts_)
00131     {
00132         FatalErrorIn("DLListBase::last() const")
00133             << "list is empty"
00134             << abort(FatalError);
00135     }
00136     return last_;
00137 }
00138 
00139 
00140 inline void DLListBase::clear()
00141 {
00142     nElmts_ = 0;
00143     first_ = 0;
00144     last_ = 0;
00145 }
00146 
00147 
00148 inline DLListBase::link* DLListBase::remove(DLListBase::iterator& it)
00149 {
00150     return remove(it.curElmt_);
00151 }
00152 
00153 
00154 // * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * * //
00155 
00156 inline DLListBase::iterator::iterator(DLListBase& s, link* elmt)
00157 :
00158     curList_(s),
00159     curElmt_(elmt),
00160     curLink_(*curElmt_)
00161 {}
00162 
00163 
00164 inline DLListBase::iterator::iterator(DLListBase& s)
00165 :
00166     curList_(s),
00167     curElmt_(NULL),
00168     curLink_()
00169 {}
00170 
00171 
00172 inline void DLListBase::iterator::operator=(const iterator& iter)
00173 {
00174     curElmt_ = iter.curElmt_;
00175     curLink_ = iter.curLink_;
00176 }
00177 
00178 
00179 inline bool DLListBase::iterator::operator==(const iterator& iter) const
00180 {
00181     return curElmt_ == iter.curElmt_;
00182 }
00183 
00184 
00185 inline bool DLListBase::iterator::operator!=(const iterator& iter) const
00186 {
00187     return curElmt_ != iter.curElmt_;
00188 }
00189 
00190 
00191 inline DLListBase::link& DLListBase::iterator::operator*()
00192 {
00193     return *curElmt_;
00194 }
00195 
00196 
00197 inline DLListBase::iterator& DLListBase::iterator::operator++()
00198 {
00199     // Check if the curElmt_ is the last element (if it points to itself)
00200     // or if the list is empty because the last element may have been removed
00201     if (curLink_.next_ == curElmt_ || curList_.last_ == 0)
00202     {
00203         curElmt_ = 0;
00204     }
00205     else
00206     {
00207         curElmt_ = curLink_.next_;
00208         curLink_ = *curElmt_;
00209     }
00210 
00211     return *this;
00212 }
00213 
00214 
00215 inline DLListBase::iterator DLListBase::iterator::operator++(int)
00216 {
00217     iterator tmp = *this;
00218     ++*this;
00219     return tmp;
00220 }
00221 
00222 
00223 inline DLListBase::iterator DLListBase::begin()
00224 {
00225     if (size())
00226     {
00227         return iterator(*this, first());
00228     }
00229     else
00230     {
00231         return endIter;
00232     }
00233 }
00234 
00235 
00236 inline const DLListBase::iterator& DLListBase::end()
00237 {
00238     return endIter;
00239 }
00240 
00241 
00242 // * * * * * * * * * * * * * * STL const_iterator  * * * * * * * * * * * * * //
00243 
00244 inline DLListBase::const_iterator::const_iterator
00245 (
00246     const DLListBase& s,
00247     const link* elmt
00248 )
00249 :
00250     curList_(s),
00251     curElmt_(elmt)
00252 {}
00253 
00254 
00255 inline DLListBase::const_iterator::const_iterator(const iterator& iter)
00256 :
00257     curList_(iter.curList_),
00258     curElmt_(iter.curElmt_)
00259 {}
00260 
00261 
00262 inline void DLListBase::const_iterator::operator=(const const_iterator& iter)
00263 {
00264     curElmt_ = iter.curElmt_;
00265 }
00266 
00267 
00268 inline bool DLListBase::const_iterator::operator==
00269 (
00270     const const_iterator& iter
00271 ) const
00272 {
00273     return curElmt_ == iter.curElmt_;
00274 }
00275 
00276 
00277 inline bool DLListBase::const_iterator::operator!=
00278 (
00279     const const_iterator& iter
00280 ) const
00281 {
00282     return curElmt_ != iter.curElmt_;
00283 }
00284 
00285 
00286 inline const DLListBase::link& DLListBase::const_iterator::operator*()
00287 {
00288     return *curElmt_;
00289 }
00290 
00291 
00292 inline DLListBase::const_iterator& DLListBase::const_iterator::operator++()
00293 {
00294     if (curElmt_ == curList_.last_)
00295     {
00296         curElmt_ = 0;
00297     }
00298     else
00299     {
00300         curElmt_ = curElmt_->next_;
00301     }
00302 
00303     return *this;
00304 }
00305 
00306 
00307 inline DLListBase::const_iterator DLListBase::const_iterator::operator++(int)
00308 {
00309     const_iterator tmp = *this;
00310     ++*this;
00311     return tmp;
00312 }
00313 
00314 
00315 inline DLListBase::const_iterator DLListBase::begin() const
00316 {
00317     if (size())
00318     {
00319         return const_iterator(*this, first());
00320     }
00321     else
00322     {
00323         return endConstIter;
00324     }
00325 }
00326 
00327 
00328 inline const DLListBase::const_iterator& DLListBase::end() const
00329 {
00330     return endConstIter;
00331 }
00332 
00333 
00334 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00335 
00336 } // End namespace Foam
00337 
00338 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd