OpenFOAM logo
Open Source CFD Toolkit

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