OpenFOAM logo
Open Source CFD Toolkit

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-2005 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 //- Null construct
00040 inline DLListBase::link::link()
00041 :
00042     prev_(0),
00043     next_(0)
00044 {}
00045 
00046 
00047 //- Construct given pointers to previous and next links
00048 inline DLListBase::link::link(link* prev, link* next)
00049 :
00050     prev_(prev),
00051     next_(next)
00052 {}
00053 
00054 
00055 // Null construct
00056 inline DLListBase::DLListBase()
00057 :
00058     first_(0),
00059     last_(0),
00060     nElmts_(0)
00061 {}
00062 
00063 
00064 // Construct given initial entry
00065 inline DLListBase::DLListBase(link* a)
00066 :
00067     first_(a),
00068     last_(a),
00069     nElmts_(1)
00070 {
00071     a->prev_ = 0;
00072     a->next_ = 0;
00073 }
00074 
00075 
00076 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00077 
00078 inline DLListBase::~DLListBase()
00079 {}
00080 
00081 
00082 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00083 
00084 // Return number of elements in list
00085 inline label DLListBase::size() const
00086 {
00087     return nElmts_;
00088 }
00089 
00090 
00091 // Return first entry
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 // Return const access to first entry
00105 inline const DLListBase::link* DLListBase::first() const
00106 {
00107     if (!nElmts_)
00108     {
00109         FatalErrorIn("DLListBase::first() const")
00110             << "list is empty"
00111             << abort(FatalError);
00112     }
00113     return first_;
00114 }
00115 
00116 
00117 // Return last entry
00118 inline DLListBase::link* DLListBase::last()
00119 {
00120     if (!nElmts_)
00121     {
00122         FatalErrorIn("DLListBase::last()")
00123             << "list is empty"
00124             << abort(FatalError);
00125     }
00126     return last_;
00127 }
00128 
00129 
00130 // Return const access to last entry
00131 inline const DLListBase::link* DLListBase::last() const
00132 {
00133     if (!nElmts_)
00134     {
00135         FatalErrorIn("DLListBase::last() const")
00136             << "list is empty"
00137             << abort(FatalError);
00138     }
00139     return last_;
00140 }
00141 
00142 
00143 // Clear the list
00144 inline void DLListBase::clear()
00145 {
00146     nElmts_ = 0;
00147     first_ = 0;
00148     last_ = 0;
00149 }
00150 
00151 
00152 // * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * * //
00153 
00154 // Construct for a given DLListBase and link
00155 inline DLListBase::iterator::iterator(DLListBase& s, link* elmt)
00156 :
00157     curList_(s),
00158     curElmt_(elmt)
00159 {}
00160 
00161 
00162 inline void DLListBase::iterator::operator=(const iterator& iter)
00163 {
00164     curElmt_ = iter.curElmt_;
00165 }
00166 
00167 
00168 inline bool DLListBase::iterator::operator==(const iterator& iter) const
00169 {
00170     return curElmt_ == iter.curElmt_;
00171 }
00172 
00173 
00174 inline bool DLListBase::iterator::operator!=(const iterator& iter) const
00175 {
00176     return curElmt_ != iter.curElmt_;
00177 }
00178 
00179 
00180 inline DLListBase::link& DLListBase::iterator::operator*()
00181 {
00182     return *curElmt_;
00183 }
00184 
00185 
00186 inline DLListBase::iterator& DLListBase::iterator::operator++()
00187 {
00188     if (curElmt_ == curList_.last_)
00189     {
00190         curElmt_ = 0;
00191     }
00192     else
00193     {
00194         curElmt_ = curElmt_->next_;
00195     }
00196 
00197     return *this;
00198 }
00199 
00200 
00201 inline DLListBase::iterator DLListBase::iterator::operator++(int)
00202 {
00203     iterator tmp = *this;
00204     ++*this;
00205     return tmp;
00206 }
00207 
00208 
00209 inline DLListBase::iterator DLListBase::begin()
00210 {
00211     if (size())
00212     {
00213         return iterator(*this, first());
00214     }
00215     else
00216     {
00217         return endIter;
00218     }
00219 }
00220 
00221 
00222 inline const DLListBase::iterator& DLListBase::end()
00223 {
00224     return endIter;
00225 }
00226 
00227 
00228 // * * * * * * * * * * * * * * STL const_iterator  * * * * * * * * * * * * * //
00229 
00230 inline DLListBase::const_iterator::const_iterator
00231 (
00232     const DLListBase& s,
00233     const link* elmt
00234 )
00235 :
00236     curList_(s),
00237     curElmt_(elmt)
00238 {}
00239 
00240 
00241 inline DLListBase::const_iterator::const_iterator(const iterator& iter)
00242 :
00243     curList_(iter.curList_),
00244     curElmt_(iter.curElmt_)
00245 {}
00246 
00247 
00248 inline void DLListBase::const_iterator::operator=(const const_iterator& iter)
00249 {
00250     curElmt_ = iter.curElmt_;
00251 }
00252 
00253 
00254 inline bool DLListBase::const_iterator::operator==
00255 (
00256     const const_iterator& iter
00257 ) const
00258 {
00259     return curElmt_ == iter.curElmt_;
00260 }
00261 
00262 
00263 inline bool DLListBase::const_iterator::operator!=
00264 (
00265     const const_iterator& iter
00266 ) const
00267 {
00268     return curElmt_ != iter.curElmt_;
00269 }
00270 
00271 
00272 inline const DLListBase::link& DLListBase::const_iterator::operator*()
00273 {
00274     return *curElmt_;
00275 }
00276 
00277 
00278 inline DLListBase::const_iterator& DLListBase::const_iterator::operator++()
00279 {
00280     if (curElmt_ == curList_.last_)
00281     {
00282         curElmt_ = 0;
00283     }
00284     else
00285     {
00286         curElmt_ = curElmt_->next_;
00287     }
00288 
00289     return *this;
00290 }
00291 
00292 
00293 inline DLListBase::const_iterator DLListBase::const_iterator::operator++(int)
00294 {
00295     const_iterator tmp = *this;
00296     ++*this;
00297     return tmp;
00298 }
00299 
00300 
00301 inline DLListBase::const_iterator DLListBase::begin() const
00302 {
00303     if (size())
00304     {
00305         return const_iterator(*this, first());
00306     }
00307     else
00308     {
00309         return endConstIter;
00310     }
00311 }
00312 
00313 
00314 inline const DLListBase::const_iterator& DLListBase::end() const
00315 {
00316     return endConstIter;
00317 }
00318 
00319 
00320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00321 
00322 } // End namespace Foam
00323 
00324 // ************************************************************************* //
For further information go to www.openfoam.org