COPASI API  4.16.103
CCopasiNode.h
Go to the documentation of this file.
1 /* Begin CVS Header
2  $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/utilities/CCopasiNode.h,v $
3  $Revision: 1.24 $
4  $Name: $
5  $Author: shoops $
6  $Date: 2012/05/15 15:57:31 $
7  End CVS Header */
8 
9 // Copyright (C) 2012 - 2011 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
20 // Properties, Inc. and EML Research, gGmbH.
21 // All rights reserved.
22 
23 /**
24  * CCopasiNode class.
25  * The class CCopasiNode is describes a node of the CCopasiTree.
26  *
27  * Created for COPASI by Stefan Hoops 2003
28  */
29 
30 #ifndef COPASI_CCopasiNode
31 #define COPASI_CCopasiNode
32 
33 /**
34  * The class CCopasiNode is the interface used by the template class
35  * CCopasiTree< class Node> to manage the nodes of a tree. Actual
36  * implementation of nodes must be derived from CCopasiNode for the tree
37  * to operate properly.
38  */
39 template < class _Data > class CCopasiNode
40 {
41 public:
42  typedef _Data Data;
43 
44  // Attributes
45 private:
46  /**
47  * A pointer to the parent of the node.
48  */
50 
51  /**
52  * A pointer to the first child of the node.
53  */
55 
56  /**
57  * A pointer to the first sibling of the node.
58  */
60 
61 protected:
62  /**
63  * The data content of the node;
64  */
66 
67  // Operations
68 public:
69  /**
70  * Default constructor.
71  * @param CCopasiNode< Data > * pParent (default: NULL)
72  */
73  CCopasiNode(CCopasiNode< Data > * pParent = NULL):
74  mpParent(pParent),
75  mpChild(NULL),
76  mpSibling(NULL),
77  mData()
78  {}
79 
80  /**
81  * Copy constructor.
82  * @param const CCopasiNode< Data > & src
83  */
85  mpParent(src.mpParent),
86  mpChild(src.mpChild),
87  mpSibling(src.mpSibling),
88  mData(src.mData)
89  {}
90 
91  /**
92  * Specific constructor.
93  * @param const Data & data
94  * @param CCopasiNode< Data > * pParent (default: NULL)
95  */
96  CCopasiNode(const Data & data, CCopasiNode< Data > * pParent = NULL):
97  mpParent(pParent),
98  mpChild(NULL),
99  mpSibling(NULL),
100  mData(data)
101  {}
102 
103  /**
104  * Destructor.
105  * Note: Within a tree the parent of this node has to be corrected.
106  */
107  virtual ~CCopasiNode()
108  {
109  deleteChildren();
110 
111  if (mpParent) mpParent->removeChild(this);
112  }
113 
114  /**
115  * Retrieve the data of the Node.
116  * @return Data data
117  */
118  virtual const Data & getData() const
119  {
120  return mData;
121  }
122 
123  /**
124  * Set the data of the Node.
125  * @param const Data & data
126  * @return bool success
127  */
128  virtual bool setData(const Data & data)
129  {
130  mData = data;
131 
132  return true;
133  }
134 
135  /**
136  * Retrieve the parent of a Node.
137  * @return CCopasiNode< Data > * pParent
138  */
140 
141  /**
142  * Retrieve the parent of a Node.
143  * @return const CCopasiNode< Data > * pParent
144  */
145  const CCopasiNode< Data > * getParent() const {return mpParent;}
146 
147  /**
148  * Add a child to a node.
149  * If pAfter == this the child will be inserted at the front of the list
150  * of children.
151  * @param CCopasiNode< Data > * pChild
152  * @param CCopasiNode< Data > * pAfter
153  * (default: NULL appended to the list of children)
154  * @return bool Success
155  */
156  virtual bool addChild(CCopasiNode< Data > * pChild,
157  CCopasiNode< Data > * pAfter = NULL)
158  {
159  if (!pChild) return false; // Nothing to insert.
160 
161  if (pAfter == this)
162  {
163  pChild->setSibling(mpChild);
164  mpChild = NULL;
165  }
166 
167  if (mpChild)
168  return mpChild->addSibling(pChild, pAfter);
169 
170  mpChild = pChild;
171  mpChild->setParent(this);
172 
173  return true;
174  }
175 
176  /**
177  * Remove a child from a node.
178  * @param CCopasiNode< Data > * pChild
179  * @return bool Success
180  */
181  virtual bool removeChild(CCopasiNode< Data > * pChild)
182  {
183  if (!pChild) return false; // Nothing to remove.
184 
185  if (mpChild != pChild)
186  return mpChild->removeSibling(pChild);
187 
188  mpChild = mpChild->getSibling();
189 
190  pChild->setParent(NULL);
191  pChild->setSibling(NULL);
192 
193  return true;
194  }
195 
196  /**
197  * Delete all children of the node
198  */
200  {
201  while (mpChild) delete mpChild;
202 
203  return true;
204  }
205 
206  /**
207  * Retrieve the child of a node.
208  * @return CCopasiNode< Data > * pChild
209  */
211 
212  /**
213  * Retrieve the child of a node.
214  * @return const CCopasiNode< Data > * pChild
215  */
216  const CCopasiNode< Data > * getChild() const {return mpChild;}
217 
218  /**
219  * Retrieve the child with given index of a node.
220  * If index is out of range NULL is returned.
221  * @return const CCopasiNode< Data > * pChild
222  */
223  CCopasiNode< Data > * getChild(const size_t & index)
224  {
225  size_t count = 0;
226 
227  CCopasiNode< Data > * pChild = getChild();
228 
229  while (count < index && pChild != NULL)
230  {
231  count++;
232  pChild = pChild->getSibling();
233  }
234 
235  return pChild;
236  }
237 
238  /**
239  * Retrieve the child with given index of a node.
240  * If index is out of range NULL is returned.
241  * @return const CCopasiNode< Data > * pChild
242  */
243  const CCopasiNode< Data > * getChild(const size_t & index) const
244  {
245  size_t count = 0;
246 
247  const CCopasiNode< Data > * pChild = getChild();
248 
249  while (count < index && pChild != NULL)
250  {
251  count++;
252  pChild = pChild->getSibling();
253  }
254 
255  return pChild;
256  }
257 
258  /**
259  * Retrieve the number of children
260  * @return size_t numChildren
261  */
262  size_t getNumChildren() const
263  {
264  size_t count = 0;
265 
266  const CCopasiNode< Data > * pChild = getChild();
267 
268  while (pChild != NULL)
269  {
270  count++;
271  pChild = pChild->getSibling();
272  }
273 
274  return count;
275  }
276 
277  /**
278  * Add a sibling to a node.
279  * If pAfter == this the sibling will be inserted at the front of the list
280  * of siblings.
281  * @param CCopasiNode< Data > * pSibling
282  * @param CCopasiNode< Data > * pAfter
283  * (default: NULL appended to the list of siblings)
284  * @return bool Success
285  */
287  CCopasiNode< Data > * pAfter = NULL)
288  {
289  if (!pSibling) return false; // Nothing to insert.
290 
291  if (this == pAfter)
292  {
293  pSibling->setParent(mpParent);
294  pSibling->setSibling(mpSibling);
295  mpSibling = pSibling;
296  return true;
297  }
298 
299  CCopasiNode< Data > * pTmp = this;
300 
301  while (pTmp != pAfter && pTmp->getSibling())
302  pTmp = pTmp->getSibling();
303 
304  if (pTmp == pAfter || pAfter == NULL)
305  return pTmp->addSibling(pSibling, pTmp);
306  else
307  return false; // Insertion point no found.
308  }
309 
310 private:
311  /**
312  * Remove a sibling.
313  * @param CCopasiNode< Data > * pSibling
314  * @return bool Success
315  */
317  {
318  if (!pSibling) return false; // Nothing to remove.
319 
320  if (this == pSibling)
321  {
322  if (mpParent)
323  mpParent->removeChild(pSibling);
324  else
325  return false; // Root can not be removed
326  }
327 
328  CCopasiNode< Data > * pTmp = this;
329  CCopasiNode< Data > * pTmpSibling = this->mpSibling;
330 
331  while (pTmpSibling != pSibling && pTmpSibling != NULL)
332  {
333  pTmp = pTmpSibling;
334  pTmpSibling = pTmpSibling->getSibling();
335  }
336 
337  if (!pTmpSibling)
338  return false; // We did not find a sibling.
339 
340  pTmp->setSibling(pSibling->getSibling());
341 
342  pSibling->setParent(NULL);
343  pSibling->setSibling(NULL);
344 
345  return true;
346  }
347 
348 public:
349  /**
350  * Retrieve the sibling of a node.
351  * @return CCopasiNode< Data > * pSibling
352  */
354 
355  /**
356  * Retrieve the sibling of a node.
357  * @return const CCopasiNode< Data > * pSibling
358  */
359  const CCopasiNode< Data > * getSibling() const {return mpSibling;}
360 
361  /**
362  * Retrieve a pointer to the next node, which is either a
363  * child, a sibling, or an ancestor. The nodes are evaluated
364  * in that order and the first non NULL pointer is returned.
365  * If all evaluate to NULL, NULL will be returned.
366  * @return CCopasiNode< Data > * pNext
367  */
369  {
370  if (mpChild) return mpChild;
371 
372  return getNextNonChild();
373  }
374 
375  /**
376  * Retrieve a pointer to the next node, which is either a
377  * child, a sibling, or an ancestor. The nodes are evaluated
378  * in that order and the first non NULL pointer is returned.
379  * If all evaluate to NULL, NULL will be returned.
380  * @return const CCopasiNode< Data > * pNext
381  */
382  const CCopasiNode< Data > * getNext() const
383  {
384  if (mpChild) return mpChild;
385 
386  return getNextNonChild();
387  }
388 
389  /**
390  * Retrieve a pointer to the next node, which is not a child node
391  * This is either a sibling or an ancestor. The nodes are evaluated
392  * in that order and the first non NULL pointer is returned.
393  * If all evaluate to NULL, NULL will be returned.
394  * @return CCopasiNode< Data > * pNextNonChild
395  */
397  {
398  if (mpSibling) return mpSibling;
399 
400  if (mpParent) return mpParent->getNextNonChild();
401 
402  return NULL;
403  }
404 
405  /**
406  * Retrieve a pointer to the next node, which is not a child node
407  * This is either a sibling or an ancestor. The nodes are evaluated
408  * in that order and the first non NULL pointer is returned.
409  * If all evaluate to NULL, NULL will be returned.
410  * @return const CCopasiNode< Data > * pNextNonChild
411  */
413  {
414  if (mpSibling) return mpSibling;
415 
416  if (mpParent) return mpParent->getNextNonChild();
417 
418  return NULL;
419  }
420 
421 protected:
422  /**
423  * Set the parent of a Node.
424  * @param CCopasiNode< Data > * pParent
425  * @return bool Success
426  */
428  {
429  mpParent = pParent;
430  return true;
431  }
432 
433  /**
434  * Set the child of a node.
435  * @param CCopasiNode< Data > * pChild
436  * @return bool Success
437  */
439  {
440  mpChild = pChild;
441  return true;
442  }
443  /**
444  * Set the sibling of a node.
445  * @param CCopasiNode< Data > * pSibling
446  * @return bool Success
447  */
449  {
450  mpSibling = pSibling;
451  return true;
452  }
453 
454 };
455 
456 #endif // COPASI_CCopasiNode
CCopasiNode< Data > * getSibling()
Definition: CCopasiNode.h:353
const CCopasiNode< Data > * getNextNonChild() const
Definition: CCopasiNode.h:412
virtual bool setData(const Data &data)
Definition: CCopasiNode.h:128
CCopasiNode(const CCopasiNode< Data > &src)
Definition: CCopasiNode.h:84
const CCopasiNode< Data > * getChild(const size_t &index) const
Definition: CCopasiNode.h:243
bool removeSibling(CCopasiNode< Data > *pSibling)
Definition: CCopasiNode.h:316
const CCopasiNode< Data > * getParent() const
Definition: CCopasiNode.h:145
bool setParent(CCopasiNode< Data > *pParent)
Definition: CCopasiNode.h:427
const CCopasiNode< Data > * getNext() const
Definition: CCopasiNode.h:382
bool addSibling(CCopasiNode< Data > *pSibling, CCopasiNode< Data > *pAfter=NULL)
Definition: CCopasiNode.h:286
virtual bool addChild(CCopasiNode< Data > *pChild, CCopasiNode< Data > *pAfter=NULL)
Definition: CCopasiNode.h:156
bool setSibling(CCopasiNode< Data > *pSibling)
Definition: CCopasiNode.h:448
const CCopasiNode< Data > * getSibling() const
Definition: CCopasiNode.h:359
CCopasiNode< Data > * mpParent
Definition: CCopasiNode.h:49
bool setChild(CCopasiNode< Data > *pChild)
Definition: CCopasiNode.h:438
size_t getNumChildren() const
Definition: CCopasiNode.h:262
_Data Data
Definition: CCopasiNode.h:42
CCopasiNode(const Data &data, CCopasiNode< Data > *pParent=NULL)
Definition: CCopasiNode.h:96
bool deleteChildren()
Definition: CCopasiNode.h:199
CCopasiNode< Data > * getParent()
Definition: CCopasiNode.h:139
const CCopasiNode< Data > * getChild() const
Definition: CCopasiNode.h:216
CCopasiNode< Data > * mpChild
Definition: CCopasiNode.h:54
CCopasiNode< Data > * getNext()
Definition: CCopasiNode.h:368
virtual bool removeChild(CCopasiNode< Data > *pChild)
Definition: CCopasiNode.h:181
virtual const Data & getData() const
Definition: CCopasiNode.h:118
CCopasiNode< Data > * mpSibling
Definition: CCopasiNode.h:59
CCopasiNode(CCopasiNode< Data > *pParent=NULL)
Definition: CCopasiNode.h:73
CCopasiNode< Data > * getChild()
Definition: CCopasiNode.h:210
CCopasiNode< Data > * getNextNonChild()
Definition: CCopasiNode.h:396
CCopasiNode< Data > * getChild(const size_t &index)
Definition: CCopasiNode.h:223
virtual ~CCopasiNode()
Definition: CCopasiNode.h:107