FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

DLListBase.C

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <OpenFOAM/error.H>
00027 
00028 #include "DLListBase.H"
00029 #include <OpenFOAM/IOstreams.H>
00030 #include <OpenFOAM/long.H>
00031 
00032 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00033 
00034 Foam::DLListBase::iterator Foam::DLListBase::endIter_
00035 (
00036     const_cast<DLListBase&>(static_cast<const DLListBase&>(DLListBase()))
00037 );
00038 
00039 Foam::DLListBase::const_iterator Foam::DLListBase::endConstIter_
00040 (
00041     static_cast<const DLListBase&>(DLListBase()),
00042     reinterpret_cast<const link*>(0)
00043 );
00044 
00045 
00046 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00047 
00048 void Foam::DLListBase::insert(DLListBase::link* a)
00049 {
00050     nElmts_++;
00051 
00052     if (!first_)
00053     {
00054         a->prev_ = a;
00055         a->next_ = a;
00056         first_ = last_ = a;
00057     }
00058     else
00059     {
00060         a->prev_ = a;
00061         a->next_ = first_;
00062         first_->prev_ = a;
00063         first_ = a;
00064     }
00065 }
00066 
00067 
00068 void Foam::DLListBase::append(DLListBase::link* a)
00069 {
00070     nElmts_++;
00071 
00072     if (!first_)
00073     {
00074         a->prev_ = a;
00075         a->next_ = a;
00076         first_ = last_ = a;
00077     }
00078     else
00079     {
00080         last_->next_ = a;
00081         a->prev_ = last_;
00082         a->next_ = a;
00083         last_ = a;
00084     }
00085 }
00086 
00087 
00088 bool Foam::DLListBase::swapUp(DLListBase::link* a)
00089 {
00090     if (first_ != a)
00091     {
00092         link* ap = a->prev_;
00093 
00094         if (ap == first_)
00095         {
00096             first_ = a;
00097             ap->prev_ = a;
00098         }
00099         else
00100         {
00101             ap->prev_->next_ = a;
00102         }
00103 
00104         if (a == last_)
00105         {
00106             last_ = ap;
00107             a->next_ = ap;
00108         }
00109         else
00110         {
00111             a->next_->prev_ = ap;
00112         }
00113 
00114         a->prev_ = ap->prev_;
00115         ap->prev_ = a;
00116 
00117         ap->next_ = a->next_;
00118         a->next_ = ap;
00119 
00120         return true;
00121     }
00122     else
00123     {
00124         return false;
00125     }
00126 }
00127 
00128 
00129 bool Foam::DLListBase::swapDown(DLListBase::link* a)
00130 {
00131     if (last_ != a)
00132     {
00133         link* an = a->next_;
00134 
00135         if (a == first_)
00136         {
00137             first_ = an;
00138             a->prev_ = an;
00139         }
00140         else
00141         {
00142             a->prev_->next_ = an;
00143         }
00144 
00145         if (an == last_)
00146         {
00147             last_ = a;
00148             an->next_ = a;
00149         }
00150         else
00151         {
00152             an->next_->prev_ = a;
00153         }
00154 
00155         an->prev_ = a->prev_;
00156         a->prev_ = an;
00157 
00158         a->next_ = an->next_;
00159         an->next_ = a;
00160 
00161         return true;
00162     }
00163     else
00164     {
00165         return false;
00166     }
00167 }
00168 
00169 
00170 Foam::DLListBase::link* Foam::DLListBase::removeHead()
00171 {
00172     nElmts_--;
00173 
00174     if (!first_)
00175     {
00176         FatalErrorIn("void DLListBase::removeHead()")
00177             << "remove from empty list"
00178             << abort(FatalError);
00179     }
00180 
00181     DLListBase::link* f = first_;
00182     first_ = f->next_;
00183 
00184     if (!first_)
00185     {
00186         last_ = 0;
00187     }
00188 
00189     f->deregister();
00190     return f;
00191 }
00192 
00193 
00194 Foam::DLListBase::link* Foam::DLListBase::remove(DLListBase::link* l)
00195 {
00196     nElmts_--;
00197 
00198     link* ret = l;
00199 
00200     if (l == first_ && first_ == last_)
00201     {
00202         first_ = 0;
00203         last_ = 0;
00204     }
00205     else if (l == first_)
00206     {
00207         first_ = first_->next_;
00208         first_->prev_ = first_;
00209     }
00210     else if (l == last_)
00211     {
00212         last_ = last_->prev_;
00213         last_->next_ = last_;
00214     }
00215     else
00216     {
00217         l->next_->prev_ = l->prev_;
00218         l->prev_->next_ = l->next_;
00219     }
00220 
00221     ret->deregister();
00222     return ret;
00223 }
00224 
00225 
00226 Foam::DLListBase::link* Foam::DLListBase::replace
00227 (
00228     DLListBase::link* oldLink,
00229     DLListBase::link* newLink
00230 )
00231 {
00232     link* ret = oldLink;
00233 
00234     newLink->prev_ = oldLink->prev_;
00235     newLink->next_ = oldLink->next_;
00236 
00237     if (oldLink == first_ && first_ == last_)
00238     {
00239         first_ = newLink;
00240         last_  = newLink;
00241     }
00242     else if (oldLink == first_)
00243     {
00244         first_ = newLink;
00245         newLink->next_->prev_ = newLink;
00246     }
00247     else if (oldLink == last_)
00248     {
00249         last_ = newLink;
00250         newLink->prev_->next_ = newLink;
00251     }
00252     else
00253     {
00254         newLink->prev_->next_ = newLink;
00255         newLink->next_->prev_ = newLink;
00256     }
00257 
00258     ret->deregister();
00259     return ret;
00260 }
00261 
00262 
00263 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines