initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / LinkedLists / linkTypes / DLListBase / DLListBase.C
blobba25e663b3bcc2974270a7d29a84e329e2819d3d
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
29 #include "DLListBase.H"
30 #include "IOstreams.H"
31 #include "long.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 namespace Foam
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 DLListBase::iterator DLListBase::endIter
42     const_cast<DLListBase&>(static_cast<const DLListBase&>(DLListBase()))
45 DLListBase::const_iterator DLListBase::endConstIter
47     static_cast<const DLListBase&>(DLListBase()),
48     reinterpret_cast<const link*>(NULL)
52 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
54 void DLListBase::insert(DLListBase::link* a)
56     nElmts_++;
58     if (!first_)
59     {
60         a->prev_ = a;
61         a->next_ = a;
62         first_ = last_ = a;
63     }
64     else
65     {
66         a->prev_ = a;
67         a->next_ = first_;
68         first_->prev_ = a;
69         first_ = a;
70     }
74 void DLListBase::append(DLListBase::link* a)
76     nElmts_++;
78     if (!first_)
79     {
80         a->prev_ = a;
81         a->next_ = a;
82         first_ = last_ = a;
83     }
84     else
85     {
86         last_->next_ = a;
87         a->prev_ = last_;
88         a->next_ = a;
89         last_ = a;
90     }
94 bool DLListBase::swapUp(DLListBase::link* a)
96     if (first_ != a)
97     {
98         link* ap = a->prev_;
100         if (ap == first_)
101         {
102             first_ = a;
103         }
105         if (a == last_)
106         {
107             last_ = ap;
108         }
110         if (a->next_)
111         {
112             a->next_->prev_ = ap;
113         }
115         if (ap->prev_)
116         {
117             ap->prev_->next_ = a;
118         }
120         a->prev_ = ap->prev_;
121         ap->prev_ = a;
123         ap->next_ = a->next_;
124         a->next_ = ap;
126         return true;
127     }
128     else
129     {
130         return false;
131     }
135 bool DLListBase::swapDown(DLListBase::link* a)
137     if (last_ != a)
138     {
139         link* an = a->next_;
141         if (a == first_)
142         {
143             first_ = an;
144         }
146         if (an == last_)
147         {
148             last_ = a;
149         }
151         if (a->prev_)
152         {
153             a->prev_->next_ = an;
154         }
156         if (an->next_)
157         {
158             an->next_->prev_ = a;
159         }
161         an->prev_ = a->prev_;
162         a->prev_ = an;
164         a->next_ = an->next_;
165         an->next_ = a;
167         return true;
168     }
169     else
170     {
171         return false;
172     }
176 DLListBase::link* DLListBase::removeHead()
178     nElmts_--;
180     if (!first_)
181     {
182         FatalErrorIn("void DLListBase::removeHead()")
183             << "remove from empty list"
184             << abort(FatalError);
185     }
187     DLListBase::link* f = first_;
188     first_ = f->next_;
190     if (!first_)
191     {
192         last_ = 0;
193     }
195     f->deregister();
196     return f;
200 DLListBase::link* DLListBase::remove(DLListBase::link* l)
202     nElmts_--;
204     link* ret = l;
206     if (l == first_ && first_ == last_)
207     {
208         first_ = 0;
209         last_ = 0;
210     }
211     else if (l == first_)
212     {
213         first_ = first_->next_;
214         first_->prev_ = first_;
215     }
216     else if (l == last_)
217     {
218         last_ = last_->prev_;
219         last_->next_ = last_;
220     }
221     else
222     {
223         l->next_->prev_ = l->prev_;
224         l->prev_->next_ = l->next_;
225     }
227     ret->deregister();
228     return ret;
232 DLListBase::link* DLListBase::replace
234     DLListBase::link* oldLink,
235     DLListBase::link* newLink
238     link* ret = oldLink;
240     newLink->prev_ = oldLink->prev_;
241     newLink->next_ = oldLink->next_;
243     if (oldLink == first_ && first_ == last_)
244     {
245         first_ = newLink;
246         last_  = newLink;
247     }
248     else if (oldLink == first_)
249     {
250         first_ = newLink;
251         newLink->next_->prev_ = newLink;
252     }
253     else if (oldLink == last_)
254     {
255         last_ = newLink;
256         newLink->prev_->next_ = newLink;
257     }
258     else
259     {
260         newLink->prev_->next_ = newLink;
261         newLink->next_->prev_ = newLink;
262     }
264     ret->deregister();
265     return ret;
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 } // End namespace Foam
273 // ************************************************************************* //