COPASI API  4.16.103
Classes | Public Types | Public Member Functions | Private Attributes | Friends | List of all members
CCopasiTree< _Node > Class Template Reference

#include <CCopasiTree.h>

Collaboration diagram for CCopasiTree< _Node >:
Collaboration graph
[legend]

Classes

class  const_iterator
 
class  iterator
 

Public Types

typedef _Node Node
 

Public Member Functions

bool attachNode (_Node *pNode, _Node *pParent=NULL, _Node *pAfterChild=NULL)
 
iterator begin () const
 
 CCopasiTree ()
 
bool detachNode (_Node *pNode)
 
iterator end () const
 
_Node::Data getData () const
 
_Node * getRoot ()
 
bool moveNode (_Node *pNode, _Node *pParent=NULL, _Node *pAfterChild=NULL)
 
bool removeNode (_Node *pNode)
 
 ~CCopasiTree ()
 

Private Attributes

std::set< _Node * > mList
 
_Node * mpRoot
 

Friends

std::ostream & operator<< (std::ostream &os, const CCopasiTree< _Node > &A)
 

Detailed Description

template<class _Node>
class CCopasiTree< _Node >

CCopasiTree class. The template class CCopasiTree describes a tree of Nodes.

Created for COPASI by Stefan Hoops 2003

The template class CCopasiTree takes as template argument a class derived from the class CCopasiNode. It allows the construction of a tree with four simple methods. It assures that each node in the tree is unique.

In addition it provides a forward iterator useful to traverse the tree.

Note: The tree takes ownership of all nodes. Therefore, you must not use pointers to automatically created nodes in the attachNode() Function unless you use the detach node function prior to the destruction of the tree.

Definition at line 37 of file CCopasiTree.h.

Member Typedef Documentation

template<class _Node>
typedef _Node CCopasiTree< _Node >::Node

Definition at line 59 of file CCopasiTree.h.

Constructor & Destructor Documentation

template<class _Node>
CCopasiTree< _Node >::CCopasiTree ( )
inline

Default constructor

Definition at line 250 of file CCopasiTree.h.

References CCopasiTree< _Node >::mList, and CCopasiTree< _Node >::mpRoot.

250  :
251  mpRoot(new _Node),
252  mList()
253  {mList.insert(mpRoot);}
_Node * mpRoot
Definition: CCopasiTree.h:66
std::set< _Node * > mList
Definition: CCopasiTree.h:72
template<class _Node>
CCopasiTree< _Node >::~CCopasiTree ( )
inline

Destructor

Definition at line 258 of file CCopasiTree.h.

References CCopasiTree< _Node >::mpRoot, and pdelete.

258 {pdelete(mpRoot);}
#define pdelete(p)
Definition: copasi.h:215
_Node * mpRoot
Definition: CCopasiTree.h:66

Member Function Documentation

template<class _Node>
bool CCopasiTree< _Node >::attachNode ( _Node *  pNode,
_Node *  pParent = NULL,
_Node *  pAfterChild = NULL 
)
inline

Attach a Node to the tree Note: If pAfterChild == pParent then the child will be inserted as the first child

Parameters
Node* pNode
Node* pParent (default: NULL equivalent to the root of the tree)
Node* pAfterChild (default: NULL at the end of the children)
Returns
bool Success

Definition at line 293 of file CCopasiTree.h.

References CCopasiTree< _Node >::end(), CCopasiTree< _Node >::mList, and CCopasiTree< _Node >::mpRoot.

Referenced by CCopasiTree< _Node >::moveNode().

296  {
297  bool Success = true;
298 
299  if (mList.count(pNode))
300  return false; // Node already in tree.
301 
302  if (pAfterChild && !mList.count(pAfterChild))
303  return false; // Invalid insertion point.
304 
305  if (pParent)
306  {
307  if (mList.count(pParent))
308  Success = pParent->addChild(pNode, pAfterChild);
309  else
310  Success = false; // Invalid parent.
311  }
312  else
313  Success = mpRoot->addChild(pNode, pAfterChild);
314 
315  if (Success)
316  {
317  iterator it = pNode;
318  iterator end = (_Node *) it->getNextNonChild();
319 
320  for (; it != end; ++it)
321  mList.insert(&*it);
322  }
323 
324  return Success;
325  }
_Node * mpRoot
Definition: CCopasiTree.h:66
iterator end() const
Definition: CCopasiTree.h:270
std::set< _Node * > mList
Definition: CCopasiTree.h:72
template<class _Node>
iterator CCopasiTree< _Node >::begin ( ) const
inline

Retrieve an iterator pointing to the beginning of the tree

Returns
iterator begin

Definition at line 264 of file CCopasiTree.h.

References CCopasiTree< _Node >::mpRoot.

Referenced by operator<<().

264 {return iterator(mpRoot);}
_Node * mpRoot
Definition: CCopasiTree.h:66
template<class _Node>
bool CCopasiTree< _Node >::detachNode ( _Node *  pNode)
inline

Detach node. Node: After detachment of a node the tree no longer has the ownership.

Parameters
Node* pNode
Returns
bool Success

Definition at line 370 of file CCopasiTree.h.

References CCopasiTree< _Node >::end(), CCopasiTree< _Node >::mList, and CCopasiTree< _Node >::mpRoot.

Referenced by CCopasiTree< _Node >::moveNode().

371  {
372  if (!pNode) return false; // Nothing to do.
373 
374  if (pNode == mpRoot) return false; // Root must not be detached
375 
376  iterator it = pNode;
377  iterator end = (_Node *) it->getNextNonChild();
378 
379  for (; it != end; ++it)
380  mList.erase(&*it);
381 
382  return pNode->getParent()->removeChild(pNode);
383  }
_Node * mpRoot
Definition: CCopasiTree.h:66
iterator end() const
Definition: CCopasiTree.h:270
std::set< _Node * > mList
Definition: CCopasiTree.h:72
template<class _Node>
iterator CCopasiTree< _Node >::end ( ) const
inline

Retrieve an iterator pointing beyond the end of the tree

Returns
iterator end

Definition at line 270 of file CCopasiTree.h.

Referenced by CCopasiTree< _Node >::attachNode(), CCopasiTree< _Node >::detachNode(), operator<<(), and CCopasiTree< _Node >::removeNode().

270 {return iterator(NULL);}
template<class _Node>
_Node::Data CCopasiTree< _Node >::getData ( ) const
inline

Retrieve the data of the Tree.

Returns
Data data

Definition at line 282 of file CCopasiTree.h.

References CCopasiTree< _Node >::mpRoot.

282 {return mpRoot->getData();}
_Node * mpRoot
Definition: CCopasiTree.h:66
template<class _Node>
_Node* CCopasiTree< _Node >::getRoot ( )
inline

Retrieve the root node of the tree

Returns
Node * root

Definition at line 276 of file CCopasiTree.h.

References CCopasiTree< _Node >::mpRoot.

276 {return mpRoot;}
_Node * mpRoot
Definition: CCopasiTree.h:66
template<class _Node>
bool CCopasiTree< _Node >::moveNode ( _Node *  pNode,
_Node *  pParent = NULL,
_Node *  pAfterChild = NULL 
)
inline

Move a given node from its current place in the tree to the one specified by pParent and pAfterChild. The insertion behavior is similar to addChild().

Parameters
Node* pNode
Node* pParent (default: NULL equivalent to the root of the tree)
Node* pAfterChild (default: NULL at the end of the children)
Returns
bool Success

Definition at line 358 of file CCopasiTree.h.

References CCopasiTree< _Node >::attachNode(), and CCopasiTree< _Node >::detachNode().

359  {
360  detachNode(pNode);
361  return attachNode(pNode, pParent, pAfterChild);
362  }
bool attachNode(_Node *pNode, _Node *pParent=NULL, _Node *pAfterChild=NULL)
Definition: CCopasiTree.h:293
bool detachNode(_Node *pNode)
Definition: CCopasiTree.h:370
template<class _Node>
bool CCopasiTree< _Node >::removeNode ( _Node *  pNode)
inline

Remove the given node of the tree

Parameters
Node* pNode
Returns
bool Success

Definition at line 332 of file CCopasiTree.h.

References CCopasiTree< _Node >::end(), CCopasiTree< _Node >::mList, and CCopasiTree< _Node >::mpRoot.

333  {
334  if (!pNode) return false; // Nothing to remove.
335 
336  if (pNode == mpRoot) return false; // Root must not be removed.
337 
338  iterator it = pNode;
339  iterator end = (_Node *) it->getNextNonChild();
340 
341  for (; it != end; ++it)
342  mList.erase(&*it);
343 
344  delete pNode;
345 
346  return true;
347  }
_Node * mpRoot
Definition: CCopasiTree.h:66
iterator end() const
Definition: CCopasiTree.h:270
std::set< _Node * > mList
Definition: CCopasiTree.h:72

Friends And Related Function Documentation

template<class _Node>
std::ostream& operator<< ( std::ostream &  os,
const CCopasiTree< _Node > &  A 
)
friend

Definition at line 395 of file CCopasiTree.h.

397 {
398  typename CCopasiTree< _Node >::iterator it = A.begin();
399  typename CCopasiTree< _Node >::iterator end = A.end();
400 
401  for (; it != end && &*it != NULL; ++it)
402  os << &*it << ": parent: " << it->getParent()
403  << ", child: " << it->getChild()
404  << ", sibling: " << it->getSibling() << std::endl;
405 
406  os << std::endl;
407  return os;
408 }
iterator begin() const
Definition: CCopasiTree.h:264
iterator end() const
Definition: CCopasiTree.h:270

Member Data Documentation

template<class _Node>
std::set<_Node *> CCopasiTree< _Node >::mList
private

The list of all nodes. This is used to keep the tree consistent by avoiding multiple inserts of the same node.

Definition at line 72 of file CCopasiTree.h.

Referenced by CCopasiTree< _Node >::attachNode(), CCopasiTree< _Node >::CCopasiTree(), CCopasiTree< _Node >::detachNode(), and CCopasiTree< _Node >::removeNode().

template<class _Node>
_Node* CCopasiTree< _Node >::mpRoot
private

The documentation for this class was generated from the following file: