Scippy

SoPlex

Sequential object-oriented simPlex

idlist.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2016 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file idlist.h
17  * @brief Generic Real linked list.
18  */
19 #ifndef _IDLIST_H_
20 #define _IDLIST_H_
21 
22 #include <assert.h>
23 #include <stddef.h>
24 
25 #include "spxdefines.h"
26 #include "islist.h"
27 
28 namespace soplex
29 {
30 //---------------------------------------------------------------------
31 // class IdElement<T>
32 //---------------------------------------------------------------------
33 
34 /**@brief Elements for \ref soplex::IdList "IdList"s.
35  @ingroup Elementary
36 
37  #IdElement%s are derived from the template parameter class T and can hence
38  be used as such. The additional methods next() and prev() provide access
39  to the links for the list. They may freely be used by the programmer as long
40  as an IdElement is not member of a IdList. In this case, the IdList
41  controls members next() and prev(). However, IdList should provide
42  enough functionality for the user not to require any modification to these
43  members.
44  */
45 /* The use of this->the_last and this->the_first instead of just the_last
46  * and the_first is bcause the HP aCC Compiler claims that according to the
47  * Standard these otherwise could not be seen. And since I was not able to
48  * even identify a hint on this in the Draft Standard we just do it, so
49  * the HP compiler is happy since it will not hurt the others.
50  */
51 template < class T >
52 class IdElement : public T
53 {
54  //--------------------------
55  /**@name Data */
56  //@{
57  IdElement<T>* theprev; ///< pointer to previous element in the IdList
58  IdElement<T>* thenext; ///< pointer to next element in the IdList
59  //@}
60 
61 public:
62 
63  //---------------------------------------
64  /**@name Successors and predecessors */
65  //@{
66  /// returns the next element in the IdList (writeable).
68  {
69  return thenext;
70  }
71  /// returns the next element in the IdList.
72  IdElement<T>* const& next() const
73  {
74  return thenext;
75  }
76 
77  /// returns the previous element in the IdList (writeable).
79  {
80  return theprev;
81  }
82  /// returns the previous element in the IdList.
83  IdElement<T>* const& prev() const
84  {
85  return theprev;
86  }
87  //@}
88 
89  //---------------------------------------
90  /**@name Construction / destruction */
91  //@{
92  /// default constructor.
94  : theprev(0)
95  , thenext(0)
96  {}
97 
98  /// copy constructor.
99  /** Only the element itself is copied, while the links to the previous and
100  the next list element are set to zero pointers.
101  */
102  IdElement(const T& old)
103  : T(old)
104  , theprev(0)
105  , thenext(0)
106  {}
107 };
108 
109 //---------------------------------------------------------------------
110 // class IdList<T>
111 //---------------------------------------------------------------------
112 
113 
114 /**@brief Generic Real linked list.
115  @ingroup Elementary
116 
117  Class IdList implements an intrusive Real linked list as a template
118  class. As such, the list elements must provide the links themselfs. For
119  conveniance, we also provide class IdElement that adds both links to an
120  arbitrary class as template parameter.
121  */
122 template < class T >
123 class IdList : public IsList<T>
124 {
125 public:
126 
127  //---------------------------------------
128  /**@name Access */
129  //@{
130  /// returns first element in list.
131  T* first() const
132  {
133  return static_cast<T*>(this->the_first);
134  }
135 
136  /// returns last element in list.
137  T* last() const
138  {
139  return static_cast<T*>(this->the_last);
140  }
141 
142  /// returns successor of \p elem or 0, if \p elem is the last element.
143  T* next(const T *elem) const
144  {
145  return (elem == last()) ? 0 : elem->next();
146  }
147 
148  /// returns predecessor of \p elem or 0, if \p elem is the first element.
149  T* prev(const T *elem) const
150  {
151  return (elem == first()) ? 0 : elem->prev();
152  }
153  //@}
154 
155 
156  //---------------------------------------
157  /**@name Extension */
158  //@{
159  /// appends \p elem to end of list.
160  void append (T* elem)
161  {
162  if (last())
163  {
164  last()->next() = elem;
165  elem->prev() = last();
166  }
167  else
168  this->the_first = elem;
169  this->the_last = elem;
170  }
171 
172  /// prepends \p elem at beginnig of list.
173  void prepend(T* elem)
174  {
175  if (first())
176  {
177  elem->next() = first();
178  first()->prev() = elem;
179  }
180  else
181  this->the_last = elem;
182  this->the_first = elem;
183  }
184 
185  /// inserts \p elem after \p after.
186  void insert (T* elem, T* after)
187  {
188  assert(find(after));
189  if (after == last())
190  append(elem);
191  else
192  {
193  elem->next() = after->next();
194  elem->prev() = after;
195  after->next() = elem->next()->prev() = elem;
196  }
197  }
198 
199  /// appends \p list to end of list.
200  void append (IdList<T>& list)
201  {
202  if (list.first())
203  {
204  append(list.first());
205  this->the_last = list.last();
206  }
207  }
208 
209  /// prepends \p list at beginnig of list.
210  void prepend(IdList<T>& list)
211  {
212  if (list.first())
213  {
214  prepend(list.last());
215  this->the_first = list.the_first;
216  }
217  }
218 
219  /// inserts \p list after \p after.
220  void insert (IdList<T>& list, T* after)
221  {
222  assert(find(after));
223  if (list.first())
224  {
225  list.last()->next() = after->next();
226  list.first()->prev() = after;
227  after->next() = list.first();
228  if (after == last())
229  this->the_last = list.last();
230  else
231  list.last()->next()->prev() = list.last();
232  }
233  }
234  //@}
235 
236  //---------------------------------------
237  /**@name Removal */
238  //@{
239  /// removes element following \p after.
240  void remove_next(T* after)
241  {
242  remove(next(after));
243  }
244 
245  /// removes \p elem from list.
246  void remove(T* elem)
247  {
248  if (elem == first())
249  {
250  this->the_first = next(elem);
251  if (first() == 0)
252  this->the_last = 0;
253  }
254  else if (elem == last())
255  this->the_last = elem->prev();
256  else
257  {
258  elem->next()->prev() = elem->prev();
259  elem->prev()->next() = elem->next();
260  }
261  }
262 
263  /// removes sublist \p list.
264  void remove(IdList<T>& list)
265  {
266  if (first() != 0 && list.first() != 0)
267  {
268  assert(find(list.first()));
269  assert(find(list.last()));
270  if (first() == list.first())
271  {
272  if (last() == list.last())
273  this->the_first = this->the_last = 0;
274  else
275  this->the_first = list.last()->next();
276  }
277  else if (last() == list.last())
278  this->the_last = list.last()->prev();
279  else
280  {
281  T* after = first();
282  for (; after->next() != list.first(); after = after->next())
283  ;
284  if (last() == list.last())
285  this->the_last = after;
286  else
287  after->next() = list.last()->next();
288  }
289  }
290  }
291  //@}
292 
293  //---------------------------------------
294  /**@name Miscellaneous */
295  //@{
296  /// adjusts list pointers to a new memory address.
297  /** When all elements have been moved in memory (e.g. because of
298  reallocation) with a fixed offset \p delta, the list will be reset
299  to the new adresses.
300  */
301  void move(ptrdiff_t delta)
302  {
303  if (this->the_first)
304  {
305  T* elem;
306  IsList<T>::move(delta);
307  for (elem = last(); elem; elem = prev(elem))
308  if (elem != first())
309  elem->prev() = reinterpret_cast<T*>(
310  reinterpret_cast<char*>(elem->prev()) + delta);
311  }
312  }
313 
314  /// consistency check.
315  bool isConsistent() const
316  {
317 #ifdef ENABLE_CONSISTENCY_CHECKS
318  const T* my_first = first();
319  const T* my_last = last();
320  for (const T * it = my_first; it; it = next(it))
321  {
322  if (it != my_first && it->prev()->next() != it)
323  return MSGinconsistent("IdList");
324 
325  if (it != my_last && it->next()->prev() != it)
326  return MSGinconsistent("IdList");
327  }
328  return IsList<T>::isConsistent();
329 #else
330  return true;
331 #endif
332  }
333  //@}
334 
335  //---------------------------------------
336  /**@name Constructors / Destructors */
337  //@{
338  /// default constructor.
339  /** The default constructor may also be used to construct a sublist, by
340  providing a \p first and a \p last element. Element \p last must be a
341  successor of \p first.
342  */
343  explicit
344  IdList(T* pfirst = 0, T* plast = 0, bool pDestroyElements = false)
345  : IsList<T>(pfirst, plast, pDestroyElements)
346  {
347  assert(isConsistent());
348  }
349  //@}
350 };
351 
352 } // namespace soplex
353 #endif // _IDLIST_H_
void remove_next(T *after)
removes element following after.
Definition: idlist.h:240
void append(T *elem)
appends elem to end of list.
Definition: idlist.h:160
IdElement< T > *const & prev() const
returns the previous element in the IdList.
Definition: idlist.h:83
Elements for IdLists.IdElements are derived from the template parameter class T and can hence be used...
Definition: idlist.h:52
Generic Real linked list.Class IdList implements an intrusive Real linked list as a template class...
Definition: idlist.h:123
T * prev(const T *elem) const
returns predecessor of elem or 0, if elem is the first element.
Definition: idlist.h:149
void move(ptrdiff_t delta)
adjusts list pointers to a new memory address.
Definition: islist.h:392
void move(ptrdiff_t delta)
adjusts list pointers to a new memory address.
Definition: idlist.h:301
Generic single linked list.
Generic single linked list.Class IsList implements an intrusive single linked list of elements of a t...
Definition: islist.h:112
T * next(const T *elem) const
returns successor of elem or 0, if elem is the last element.
Definition: idlist.h:143
IdElement< T > *const & next() const
returns the next element in the IdList.
Definition: idlist.h:72
void append(IdList< T > &list)
appends list to end of list.
Definition: idlist.h:200
T * last() const
returns last element in list.
Definition: idlist.h:137
void insert(T *elem, T *after)
inserts elem after after.
Definition: idlist.h:186
IdElement(const T &old)
copy constructor.
Definition: idlist.h:102
IdElement< T > *& prev()
returns the previous element in the IdList (writeable).
Definition: idlist.h:78
DLPSV *& next()
Next SVectorBase.
Definition: svsetbase.h:117
IdElement< T > * thenext
pointer to next element in the IdList
Definition: idlist.h:58
Debugging, floating point type and parameter definitions.
bool isConsistent() const
consistency check.
Definition: islist.h:406
Everything should be within this namespace.
T * first() const
returns first element in list.
Definition: idlist.h:131
IdElement< T > * theprev
pointer to previous element in the IdList
Definition: idlist.h:57
void insert(IdList< T > &list, T *after)
inserts list after after.
Definition: idlist.h:220
IdElement()
default constructor.
Definition: idlist.h:93
void prepend(IdList< T > &list)
prepends list at beginnig of list.
Definition: idlist.h:210
IdElement< T > *& next()
returns the next element in the IdList (writeable).
Definition: idlist.h:67
void prepend(T *elem)
prepends elem at beginnig of list.
Definition: idlist.h:173
T * the_first
the first element in the IsList.
Definition: islist.h:115
DLPSV *const & prev() const
Previous SVectorBase.
Definition: svsetbase.h:129
bool isConsistent() const
consistency check.
Definition: idlist.h:315
#define MSGinconsistent(name)
Definition: spxdefines.h:121
IdList(T *pfirst=0, T *plast=0, bool pDestroyElements=false)
default constructor.
Definition: idlist.h:344