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 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 
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 
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  --size_;
165 
166  if (!first_)
167  {
169  << "remove from empty list"
170  << abort(FatalError);
171  }
172 
173  DLListBase::link *ret = first_;
174  first_ = first_->next_;
175 
176  if (!first_)
177  {
178  last_ = nullptr;
179  }
180 
181  ret->deregister();
182  return ret;
183 }
184 
185 
187 {
188  --size_;
189 
190  DLListBase::link *ret = item;
191 
192  if (item == first_ && first_ == last_)
193  {
194  first_ = nullptr;
195  last_ = nullptr;
196  }
197  else if (item == first_)
198  {
199  first_ = first_->next_;
200  first_->prev_ = first_;
201  }
202  else if (item == last_)
203  {
204  last_ = last_->prev_;
205  last_->next_ = last_;
206  }
207  else
208  {
209  item->next_->prev_ = item->prev_;
210  item->prev_->next_ = item->next_;
211  }
212 
213  ret->deregister();
214  return ret;
215 }
216 
217 
219 (
220  DLListBase::link* oldLink,
221  DLListBase::link* newLink
222 )
223 {
224  DLListBase::link *ret = oldLink;
225 
226  newLink->prev_ = oldLink->prev_;
227  newLink->next_ = oldLink->next_;
228 
229  if (oldLink == first_ && first_ == last_)
230  {
231  first_ = newLink;
232  last_ = newLink;
233  }
234  else if (oldLink == first_)
235  {
236  first_ = newLink;
237  newLink->next_->prev_ = newLink;
238  }
239  else if (oldLink == last_)
240  {
241  last_ = newLink;
242  newLink->prev_->next_ = newLink;
243  }
244  else
245  {
246  newLink->prev_->next_ = newLink;
247  newLink->next_->prev_ = newLink;
248  }
249 
250  ret->deregister();
251  return ret;
252 }
253 
254 
255 // ************************************************************************* //
Foam::DLListBase::swapUp
bool swapUp(link *item)
Swap this element with the one above unless it is at the top.
Definition: DLListBase.C:84
Foam::DLListBase::remove
link * remove(link *item)
Remove and return element.
Definition: DLListBase.C:186
Foam::DLListBase::insert
void insert(link *item)
Add at head of list.
Definition: DLListBase.C:34
Foam::DLListBase::swapDown
bool swapDown(link *item)
Swap this element with the one below unless it is at the bottom.
Definition: DLListBase.C:123
error.H
DLListBase.H
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::DLListBase::removeHead
link * removeHead()
Remove and return head.
Definition: DLListBase.C:162
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::DLListBase::append
void append(link *item)
Add at tail of list.
Definition: DLListBase.C:59
Foam::DLListBase::replace
link * replace(link *oldLink, link *newLink)
Replace oldLink with newLink and return element.
Definition: DLListBase.C:219