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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2017-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "DLListBase.H"
30 #include "error.H"
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
35 {
36  if (!item)
37  {
38  return;
39  }
40 
41  ++size_;
42 
43  if (!first_)
44  {
45  item->prev_ = item;
46  item->next_ = item;
47  first_ = last_ = item;
48  }
49  else
50  {
51  item->prev_ = item;
52  item->next_ = first_;
53  first_->prev_ = item;
54  first_ = item;
55  }
56 }
57 
58 
59 void Foam::DLListBase::push_back(DLListBase::link* item)
60 {
61  if (!item)
62  {
63  return;
64  }
65 
66  ++size_;
67 
68  if (!first_)
69  {
70  item->prev_ = item;
71  item->next_ = item;
72  first_ = last_ = item;
73  }
74  else
75  {
76  last_->next_ = item;
77  item->prev_ = last_;
78  item->next_ = item;
79  last_ = item;
80  }
81 }
82 
83 
84 bool Foam::DLListBase::swapUp(DLListBase::link* a)
85 {
86  if (first_ == a)
87  {
88  return false;
89  }
90 
91  DLListBase::link *ap = a->prev_;
92 
93  if (ap == first_)
94  {
95  first_ = a;
96  ap->prev_ = a;
97  }
98  else
99  {
100  ap->prev_->next_ = a;
101  }
102 
103  if (a == last_)
104  {
105  last_ = ap;
106  a->next_ = ap;
107  }
108  else
109  {
110  a->next_->prev_ = ap;
111  }
112 
113  a->prev_ = ap->prev_;
114  ap->prev_ = a;
115 
116  ap->next_ = a->next_;
117  a->next_ = ap;
118 
119  return true;
120 }
121 
122 
124 {
125  if (last_ == a)
126  {
127  return false;
128  }
129 
130  DLListBase::link *an = a->next_;
131 
132  if (a == first_)
133  {
134  first_ = an;
135  a->prev_ = an;
136  }
137  else
138  {
139  a->prev_->next_ = an;
140  }
141 
142  if (an == last_)
143  {
144  last_ = a;
145  an->next_ = a;
146  }
147  else
148  {
149  an->next_->prev_ = a;
150  }
151 
152  an->prev_ = a->prev_;
153  a->prev_ = an;
154 
155  a->next_ = an->next_;
156  an->next_ = a;
157 
158  return true;
159 }
160 
161 
163 {
164  if (!first_)
165  {
167  << "remove from empty list"
168  << abort(FatalError);
169 
170  // return nullptr;
171  }
172 
173  --size_;
174 
175  DLListBase::link *ret = first_;
176  first_ = first_->next_;
177 
178  if (!first_)
179  {
180  last_ = nullptr;
181  }
182 
183  ret->deregister();
184  return ret;
185 }
186 
187 
188 Foam::DLListBase::link* Foam::DLListBase::remove(DLListBase::link* item)
189 {
190  --size_;
191 
192  DLListBase::link *ret = item;
193 
194  if (item == first_ && first_ == last_)
195  {
196  first_ = nullptr;
197  last_ = nullptr;
198  }
199  else if (item == first_)
200  {
201  first_ = first_->next_;
202  first_->prev_ = first_;
203  }
204  else if (item == last_)
205  {
206  last_ = last_->prev_;
207  last_->next_ = last_;
208  }
209  else
210  {
211  item->next_->prev_ = item->prev_;
212  item->prev_->next_ = item->next_;
213  }
215  ret->deregister();
216  return ret;
217 }
218 
219 
221 (
222  DLListBase::link* oldLink,
223  DLListBase::link* newLink
224 )
225 {
226  DLListBase::link *ret = oldLink;
227 
228  newLink->prev_ = oldLink->prev_;
229  newLink->next_ = oldLink->next_;
230 
231  if (oldLink == first_ && first_ == last_)
232  {
233  first_ = newLink;
234  last_ = newLink;
235  }
236  else if (oldLink == first_)
237  {
238  first_ = newLink;
239  newLink->next_->prev_ = newLink;
240  }
241  else if (oldLink == last_)
242  {
243  last_ = newLink;
244  newLink->prev_->next_ = newLink;
245  }
246  else
247  {
248  newLink->prev_->next_ = newLink;
249  newLink->next_->prev_ = newLink;
250  }
251 
252  ret->deregister();
253  return ret;
254 }
255 
256 
257 // ************************************************************************* //
link * remove(link *item)
Remove and return element.
Definition: DLListBase.C:181
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
link * removeHead()
Remove and return first entry.
Definition: DLListBase.C:155
bool swapDown(link *item)
Swap this element with the one below unless it is at the bottom.
Definition: DLListBase.C:116
link * replace(link *oldLink, link *newLink)
Replace oldLink with newLink and return element.
Definition: DLListBase.C:214
errorManip< error > abort(error &err)
Definition: errorManip.H:139
void push_front(link *item)
Add at front of list.
Definition: DLListBase.C:27
void push_back(link *item)
Add at back of list.
Definition: DLListBase.C:52
bool swapUp(link *item)
Swap this element with the one above unless it is at the top.
Definition: DLListBase.C:77