BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IterationStrategy.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Param/Node/IterationStrategy.cpp
6 //! @brief Implements class IterationStrategy and children.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
16 #include "Base/Utils/Assert.h"
18 
20 
22 {
23  return new PreorderStrategy();
24 }
25 
27 {
28  IteratorMemento iterator_stack;
29  iterator_stack.push_state(IteratorState(p_root));
30  return iterator_stack;
31 }
32 
33 void PreorderStrategy::next(IteratorMemento& iterator_stack) const
34 {
35  const INode* node = iterator_stack.getCurrent();
36  ASSERT(node);
37  std::vector<const INode*> children = node->getChildren();
38  if (children.size() > 0) {
39  iterator_stack.push_state(IteratorState(children));
40  return;
41  }
42  iterator_stack.next();
43  while (!iterator_stack.empty() && iterator_stack.get_state().isEnd()) {
44  iterator_stack.pop_state();
45  if (!iterator_stack.empty())
46  iterator_stack.next();
47  }
48 }
49 
50 bool PreorderStrategy::isDone(IteratorMemento& iterator_stack) const
51 {
52  return iterator_stack.empty();
53 }
54 
56 
58 {
59  return new PostorderStrategy();
60 }
61 
63 {
64  IteratorMemento iterator_stack;
65  iterator_stack.push_state(IteratorState(p_root));
66  std::vector<const INode*> children = p_root->getChildren();
67  while (children.size() > 0) {
68  iterator_stack.push_state(IteratorState(children));
69  children = iterator_stack.getCurrent()->getChildren();
70  }
71  return iterator_stack;
72 }
73 
74 void PostorderStrategy::next(IteratorMemento& iterator_stack) const
75 {
76  iterator_stack.next();
77  if (iterator_stack.get_state().isEnd()) {
78  iterator_stack.pop_state();
79  return;
80  }
81  std::vector<const INode*> children = iterator_stack.getCurrent()->getChildren();
82  while (children.size() > 0) {
83  iterator_stack.push_state(IteratorState(children));
84  children = iterator_stack.getCurrent()->getChildren();
85  }
86 }
87 
88 bool PostorderStrategy::isDone(IteratorMemento& iterator_stack) const
89 {
90  return iterator_stack.empty();
91 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:26
Defines class IterationStrategy and children.
Defines classes IteratorState, IteratorMemento and NodeIterator.
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
virtual std::vector< const INode * > getChildren() const
Returns a vector of children (const).
Definition: INode.cpp:64
Holds all iterator states encountered for SampleTreeIterator.
Definition: NodeIterator.h:56
IteratorState & get_state()
Definition: NodeIterator.h:63
const INode * getCurrent()
Definition: NodeIterator.h:70
bool empty() const
Definition: NodeIterator.h:64
void push_state(const IteratorState &state)
Definition: NodeIterator.h:61
Holds state of iterator at single level for SampleTreeIterator.
Definition: NodeIterator.h:27
bool isEnd() const
Definition: NodeIterator.h:35
Traverse tree; visit children before their parents.
virtual PostorderStrategy * clone() const
virtual void next(IteratorMemento &iterator_stack) const
virtual IteratorMemento first(const INode *p_root)
virtual bool isDone(IteratorMemento &iterator_stack) const
Traverse tree; visit parents before their children.
virtual IteratorMemento first(const INode *p_root)
virtual void next(IteratorMemento &iterator_stack) const
virtual bool isDone(IteratorMemento &iterator_stack) const
virtual PreorderStrategy * clone() const