COPASI API  4.16.103
CMathDependencyNodeIterator.h
Go to the documentation of this file.
1 // Copyright (C) 2012 - 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 /*
7  * CMathDependencyNodeIterator.h
8  *
9  * Created on: May 11, 2012
10  * Author: shoops
11  */
12 
13 #ifndef COPASI_CMathDependencyNodeIterator
14 #define COPASI_CMathDependencyNodeIterator
15 
16 #include <stack>
17 #include <set>
18 
19 #include "copasi/math/CMathEnum.h"
20 
22 
23 /**
24  * This class allows to iterator over the nodes of a dependency graph in both directions
25  * (Dependents and Prerequisites) which is indicated by the type.
26  */
28 {
29 public:
30  /**
31  * Type defining whether we iterate over the dependents or prerequisites
32  */
33  enum Type
34  {
37  };
38 
39  /**
40  * The state of the iterator. Please note that the graph might be recursive
41  * which will cause the iterator to stop.
42  */
43  enum State
44  {
45  Start = 0x00,
46  Before = 0x01,
47  After = 0x02,
48  Intermediate = 0x04,
49  End = 0x08,
50  Recursive = 0x10
51  };
52 
53  /**
54  * A flag indicating to which state the iterator should advance. These may be any
55  * combination of the enumeration value State. Please note the iterator will always
56  * return and End and Recursive
57  */
59 
60 private:
61  /**
62  * An internal class managing the context of the each nesting level of the iterator
63  */
65  {
66  public:
67  /**
68  * Default constructor
69  */
70  CStackElement();
71 
72  /**
73  * Copy constructor
74  * @param const CStackElement & src
75  */
76  CStackElement(const CStackElement & src);
77 
78  /**
79  * Specific constructor
80  * @param CMathDependencyNode * pNode
81  * @param const Type & type
82  * @param const CMathDependencyNode * pParent
83  */
84  CStackElement(CMathDependencyNode * pNode, const Type & type, const CMathDependencyNode * pParent);
85 
86  /**
87  * Destructor
88  */
90 
91  /**
92  * The node the iterator points to in the current nesting level
93  */
95 
96  /**
97  * The iterator type (Dependents or Prerequisites)
98  */
100 
101  /**
102  * An iterator to the next child to be processed.
103  */
104  std::vector< CMathDependencyNode * >::iterator mItChild;
105 
106  /**
107  * An iterator pointing beyond the last child.
108  */
109  std::vector< CMathDependencyNode * >::iterator mEndChild;
110 
111  /**
112  * A pointer to the parent node of the current node if know
113  * otherwise NULL
114  */
116  };
117 
118 public:
119  /**
120  * Default constructor
121  */
123 
124  /**
125  * Copy constructor
126  * @param const CMathDependencyNodeIterator & src
127  */
129 
130  /**
131  * Specific constructor
132  * @param CMathDependencyNode * pNode
133  * @param const Type & type (default: Dependents)
134  */
136 
137  /**
138  * Destructor
139  */
141 
142 private:
143  /**
144  * This method advances the iterator to the next internal state. The tree is
145  * traversed depth first.
146  *
147  * Please note that the iterator points to the same node multiple times, once
148  * before every child, and once after the last child to allow pre and post
149  * processing. This behavior is also true for childless nodes. To determine
150  * whether the iterator is in the pre, intermediate or post processing phase
151  * one may check the processing mode with:
152  * const State & state()
153  */
154  void increment();
155 
156 public:
157  /**
158  * This method advances the iterator to the next node and state combination for
159  * which the state is in the Flag of processing modes. A return value of true indicates
160  * that such a node has been found whereas false indicates the iteration has been completed
161  * or failed. To determine the current state of the iterator use:
162  * const State & state()
163  * @return bool success
164  */
165  bool next();
166 
167  /**
168  * Calling this method causes the iterator to skip processing the children of the current node
169  * and advance to the State to next state allowed by the processing mode
170  * @return const State & state
171  */
172  const State & skipChildren();
173 
174  /**
175  * This operator dereferences the iterator and returns a
176  * pointer the node of the graph
177  * @return CMathDependencyNode * pNode
178  */
180 
181  /**
182  * This operator dereferences the iterator and returns a
183  * pointer the node of the graph
184  * @return CMathDependencyNode * pNode
185  */
187 
188  /**
189  * Retrieve the pointer to the parent node of the current node if known
190  * otherwise NULL
191  * @return const CMathDependencyNode * pParent
192  */
193  const CMathDependencyNode * parent();
194 
195  /**
196  * Retrieve the current state of the iterator
197  * @return const State & state
198  */
199  const State & state() const;
200 
201  /**
202  * Retrieve the current nesting level of the iterator
203  * @return size_t level
204  */
205  size_t level() const;
206 
207  /**
208  * Set the valid states for which the method next() should
209  * return
210  * @param const Flag & processingModes
211  */
212  void setProcessingModes(const Flag & processingModes);
213 
214  /**
215  * Retrieve the valid states for which the method next() should
216  * return
217  * @return Flag processingModes
218  */
219  Flag getProcessingModes() const;
220 
221 private:
222  /**
223  * A stack of context for each nesting level of the iterator
224  */
225  std::stack< CStackElement > mStack;
226 
227  /**
228  * A set of currently visited nodes in the stack used to determine
229  * loops or recursive dependencies
230  */
231  std::set< const CMathDependencyNode * > mVisited;
232 
233  /**
234  * The iterator type (Dependents or Prerequisites)
235  */
237 
238  /**
239  * The current state of the iterator
240  */
242 
243  /**
244  * The flag indicating to which state the iterator should advance. These may be any
245  * combination of the enumeration value State. Please note that this flag will
246  * always include return and End and Recursive
247  */
249 };
250 
251 #endif // COPASI_CMathDependencyNodeIterator
std::vector< CMathDependencyNode * >::iterator mEndChild
std::stack< CStackElement > mStack
std::vector< CMathDependencyNode * >::iterator mItChild
const CMathDependencyNode * parent()
void setProcessingModes(const Flag &processingModes)
std::set< const CMathDependencyNode * > mVisited