FreeFOAM The Cross-Platform CFD Toolkit
DLListBase.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include <OpenFOAM/error.H>
27 
28 #include "DLListBase.H"
29 #include <OpenFOAM/IOstreams.H>
30 #include <OpenFOAM/long.H>
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 Foam::DLListBase::iterator Foam::DLListBase::endIter_
35 (
36  const_cast<DLListBase&>(static_cast<const DLListBase&>(DLListBase()))
37 );
38 
39 Foam::DLListBase::const_iterator Foam::DLListBase::endConstIter_
40 (
41  static_cast<const DLListBase&>(DLListBase()),
42  reinterpret_cast<const link*>(0)
43 );
44 
45 
46 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
47 
49 {
50  nElmts_++;
51 
52  if (!first_)
53  {
54  a->prev_ = a;
55  a->next_ = a;
56  first_ = last_ = a;
57  }
58  else
59  {
60  a->prev_ = a;
61  a->next_ = first_;
62  first_->prev_ = a;
63  first_ = a;
64  }
65 }
66 
67 
69 {
70  nElmts_++;
71 
72  if (!first_)
73  {
74  a->prev_ = a;
75  a->next_ = a;
76  first_ = last_ = a;
77  }
78  else
79  {
80  last_->next_ = a;
81  a->prev_ = last_;
82  a->next_ = a;
83  last_ = a;
84  }
85 }
86 
87 
89 {
90  if (first_ != a)
91  {
92  link* ap = a->prev_;
93 
94  if (ap == first_)
95  {
96  first_ = a;
97  ap->prev_ = a;
98  }
99  else
100  {
101  ap->prev_->next_ = a;
102  }
103 
104  if (a == last_)
105  {
106  last_ = ap;
107  a->next_ = ap;
108  }
109  else
110  {
111  a->next_->prev_ = ap;
112  }
113 
114  a->prev_ = ap->prev_;
115  ap->prev_ = a;
116 
117  ap->next_ = a->next_;
118  a->next_ = ap;
119 
120  return true;
121  }
122  else
123  {
124  return false;
125  }
126 }
127 
128 
130 {
131  if (last_ != a)
132  {
133  link* an = a->next_;
134 
135  if (a == first_)
136  {
137  first_ = an;
138  a->prev_ = an;
139  }
140  else
141  {
142  a->prev_->next_ = an;
143  }
144 
145  if (an == last_)
146  {
147  last_ = a;
148  an->next_ = a;
149  }
150  else
151  {
152  an->next_->prev_ = a;
153  }
154 
155  an->prev_ = a->prev_;
156  a->prev_ = an;
157 
158  a->next_ = an->next_;
159  an->next_ = a;
160 
161  return true;
162  }
163  else
164  {
165  return false;
166  }
167 }
168 
169 
171 {
172  nElmts_--;
173 
174  if (!first_)
175  {
176  FatalErrorIn("void DLListBase::removeHead()")
177  << "remove from empty list"
178  << abort(FatalError);
179  }
180 
181  DLListBase::link* f = first_;
182  first_ = f->next_;
183 
184  if (!first_)
185  {
186  last_ = 0;
187  }
188 
189  f->deregister();
190  return f;
191 }
192 
193 
195 {
196  nElmts_--;
197 
198  link* ret = l;
199 
200  if (l == first_ && first_ == last_)
201  {
202  first_ = 0;
203  last_ = 0;
204  }
205  else if (l == first_)
206  {
207  first_ = first_->next_;
208  first_->prev_ = first_;
209  }
210  else if (l == last_)
211  {
212  last_ = last_->prev_;
213  last_->next_ = last_;
214  }
215  else
216  {
217  l->next_->prev_ = l->prev_;
218  l->prev_->next_ = l->next_;
219  }
220 
221  ret->deregister();
222  return ret;
223 }
224 
225 
227 (
228  DLListBase::link* oldLink,
229  DLListBase::link* newLink
230 )
231 {
232  link* ret = oldLink;
233 
234  newLink->prev_ = oldLink->prev_;
235  newLink->next_ = oldLink->next_;
236 
237  if (oldLink == first_ && first_ == last_)
238  {
239  first_ = newLink;
240  last_ = newLink;
241  }
242  else if (oldLink == first_)
243  {
244  first_ = newLink;
245  newLink->next_->prev_ = newLink;
246  }
247  else if (oldLink == last_)
248  {
249  last_ = newLink;
250  newLink->prev_->next_ = newLink;
251  }
252  else
253  {
254  newLink->prev_->next_ = newLink;
255  newLink->next_->prev_ = newLink;
256  }
257 
258  ret->deregister();
259  return ret;
260 }
261 
262 
263 // ************************ vim: set sw=4 sts=4 et: ************************ //