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) 2011-2014 OpenFOAM Foundation
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 "error.H"
27 
28 #include "DLListBase.H"
29 #include "IOstreams.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 Foam::DLListBase::iterator Foam::DLListBase::endIter_
34 (
35  const_cast<DLListBase&>(static_cast<const DLListBase&>(DLListBase()))
36 );
37 
38 Foam::DLListBase::const_iterator Foam::DLListBase::endConstIter_
39 (
40  static_cast<const DLListBase&>(DLListBase()),
41  reinterpret_cast<const link*>(0)
42 );
43 
44 Foam::DLListBase::const_reverse_iterator Foam::DLListBase::endConstRevIter_
45 (
46  static_cast<const DLListBase&>(DLListBase()),
47  reinterpret_cast<const link*>(0)
48 );
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
54 {
55  nElmts_++;
56 
57  if (!first_)
58  {
59  a->prev_ = a;
60  a->next_ = a;
61  first_ = last_ = a;
62  }
63  else
64  {
65  a->prev_ = a;
66  a->next_ = first_;
67  first_->prev_ = a;
68  first_ = a;
69  }
70 }
71 
72 
74 {
75  nElmts_++;
76 
77  if (!first_)
78  {
79  a->prev_ = a;
80  a->next_ = a;
81  first_ = last_ = a;
82  }
83  else
84  {
85  last_->next_ = a;
86  a->prev_ = last_;
87  a->next_ = a;
88  last_ = a;
89  }
90 }
91 
92 
94 {
95  if (first_ != a)
96  {
97  link* ap = a->prev_;
98 
99  if (ap == first_)
100  {
101  first_ = a;
102  ap->prev_ = a;
103  }
104  else
105  {
106  ap->prev_->next_ = a;
107  }
108 
109  if (a == last_)
110  {
111  last_ = ap;
112  a->next_ = ap;
113  }
114  else
115  {
116  a->next_->prev_ = ap;
117  }
118 
119  a->prev_ = ap->prev_;
120  ap->prev_ = a;
121 
122  ap->next_ = a->next_;
123  a->next_ = ap;
124 
125  return true;
126  }
127  else
128  {
129  return false;
130  }
131 }
132 
133 
135 {
136  if (last_ != a)
137  {
138  link* an = a->next_;
139 
140  if (a == first_)
141  {
142  first_ = an;
143  a->prev_ = an;
144  }
145  else
146  {
147  a->prev_->next_ = an;
148  }
149 
150  if (an == last_)
151  {
152  last_ = a;
153  an->next_ = a;
154  }
155  else
156  {
157  an->next_->prev_ = a;
158  }
159 
160  an->prev_ = a->prev_;
161  a->prev_ = an;
162 
163  a->next_ = an->next_;
164  an->next_ = a;
165 
166  return true;
167  }
168  else
169  {
170  return false;
171  }
172 }
173 
174 
176 {
177  nElmts_--;
178 
179  if (!first_)
180  {
181  FatalErrorIn("void DLListBase::removeHead()")
182  << "remove from empty list"
183  << abort(FatalError);
184  }
185 
186  DLListBase::link* f = first_;
187  first_ = f->next_;
188 
189  if (!first_)
190  {
191  last_ = 0;
192  }
193 
194  f->deregister();
195  return f;
196 }
197 
198 
200 {
201  nElmts_--;
202 
203  link* ret = l;
204 
205  if (l == first_ && first_ == last_)
206  {
207  first_ = 0;
208  last_ = 0;
209  }
210  else if (l == first_)
211  {
212  first_ = first_->next_;
213  first_->prev_ = first_;
214  }
215  else if (l == last_)
216  {
217  last_ = last_->prev_;
218  last_->next_ = last_;
219  }
220  else
221  {
222  l->next_->prev_ = l->prev_;
223  l->prev_->next_ = l->next_;
224  }
225 
226  ret->deregister();
227  return ret;
228 }
229 
230 
232 (
233  DLListBase::link* oldLink,
234  DLListBase::link* newLink
235 )
236 {
237  link* ret = oldLink;
238 
239  newLink->prev_ = oldLink->prev_;
240  newLink->next_ = oldLink->next_;
241 
242  if (oldLink == first_ && first_ == last_)
243  {
244  first_ = newLink;
245  last_ = newLink;
246  }
247  else if (oldLink == first_)
248  {
249  first_ = newLink;
250  newLink->next_->prev_ = newLink;
251  }
252  else if (oldLink == last_)
253  {
254  last_ = newLink;
255  newLink->prev_->next_ = newLink;
256  }
257  else
258  {
259  newLink->prev_->next_ = newLink;
260  newLink->next_->prev_ = newLink;
261  }
262 
263  ret->deregister();
264  return ret;
265 }
266 
267 
268 // ************************************************************************* //
labelList f(nPoints)
link * removeHead()
Remove and return head.
Definition: DLListBase.C:175
link * prev_
Pointer to next entry in list.
Definition: DLListBase.H:59
Link structure.
Definition: DLListBase.H:56
void append(link *)
Add at tail of list.
Definition: DLListBase.C:73
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void insert(link *)
Add at head of list.
Definition: DLListBase.C:53
An STL-conforming const_iterator.
Definition: DLListBase.H:228
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
link * replace(link *oldLink, link *newLink)
Replace oldLink with newLink and return element.
Definition: DLListBase.C:232
error FatalError
void deregister()
Deregister the link after removal.
Definition: DLListBaseI.H:70
An STL-conforming iterator.
Definition: DLListBase.H:181
An STL-conforming const_reverse_iterator.
Definition: DLListBase.H:269
bool swapDown(link *)
Swap this element with the one below unless it is at the bottom.
Definition: DLListBase.C:134
bool swapUp(link *)
Swap this element with the one above unless it is at the top.
Definition: DLListBase.C:93
link * remove(link *)
Remove and return element.
Definition: DLListBase.C:199