OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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