BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
NodeIterator.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Param/Node/NodeIterator.h
6 //! @brief Defines classes IteratorState, IteratorMemento and NodeIterator.
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 
15 #ifndef BORNAGAIN_CORE_PARAMETRIZATION_NODEITERATOR_H
16 #define BORNAGAIN_CORE_PARAMETRIZATION_NODEITERATOR_H
17 
18 #include "Param/Node/INode.h"
19 #include <ostream>
20 #include <stack>
21 #include <vector>
22 
23 //! Holds state of iterator at single level for SampleTreeIterator.
24 //! @ingroup samples_internal
25 
27 {
28 public:
29  IteratorState(const INode* single_element);
30  IteratorState(std::vector<const INode*> samples);
31 
32  virtual ~IteratorState() {}
33 
34  const INode* getCurrent() const { return m_samples[m_position]; }
35  bool isEnd() const { return m_position >= m_samples.size(); }
36  void next() { ++m_position; }
37 
38  friend std::ostream& operator<<(std::ostream& output_stream,
39  IteratorState const& iterator_state)
40  {
41  return output_stream << "memento state " << iterator_state.m_position << " "
42  << iterator_state.m_samples.size();
43  }
44 
45 private:
46  std::vector<const INode*> m_samples;
47  size_t m_position;
48 
50 };
51 
52 //! Holds all iterator states encountered for SampleTreeIterator.
53 //! @ingroup samples_internal
54 
56 {
57 public:
59  virtual ~IteratorMemento() {}
60 
61  void push_state(const IteratorState& state) { m_state_stack.push(state); }
62  void pop_state() { m_state_stack.pop(); }
63  IteratorState& get_state() { return m_state_stack.top(); }
64  bool empty() const { return m_state_stack.empty(); }
65  void reset()
66  {
67  while (!m_state_stack.empty())
68  m_state_stack.pop();
69  }
70  const INode* getCurrent() { return m_state_stack.top().getCurrent(); }
71  void next() { m_state_stack.top().next(); }
72  size_t size() const { return m_state_stack.size(); }
73 
74 protected:
75  std::stack<IteratorState> m_state_stack;
76 };
77 
78 //! Iterator through INode tree of objects.
79 //!
80 //! Usage example:
81 //! SampleTreeIterator<Strategy> it(&sample);
82 //! it.first();
83 //! while( !it.is_done() ) {
84 //! INode *p_sample = it.get_current();
85 //! it.next();
86 //! }
87 //! @ingroup samples_internal
88 
89 template <class Strategy> class NodeIterator
90 {
91 public:
92  NodeIterator(const INode* root);
93  virtual ~NodeIterator() {}
94 
95  void first();
96  void next();
97  const INode* getCurrent();
98  bool isDone() const;
99  int depth() const;
100 
101 protected:
102  Strategy m_strategy;
104  const INode* mp_root;
105 };
106 
107 template <class Strategy>
108 inline NodeIterator<Strategy>::NodeIterator(const INode* root) : mp_root(root)
109 {
110 }
111 
112 template <class Strategy> inline void NodeIterator<Strategy>::first()
113 {
114  m_memento_itor = m_strategy.first(mp_root);
115 }
116 
117 template <class Strategy> inline void NodeIterator<Strategy>::next()
118 {
119  m_strategy.next(m_memento_itor);
120 }
121 
122 template <class Strategy> inline const INode* NodeIterator<Strategy>::getCurrent()
123 {
124  return m_memento_itor.getCurrent();
125 }
126 
127 template <class Strategy> inline bool NodeIterator<Strategy>::isDone() const
128 {
129  return m_memento_itor.size() == 0;
130 }
131 
132 template <class Strategy> inline int NodeIterator<Strategy>::depth() const
133 {
134  return static_cast<int>(m_memento_itor.size());
135 }
136 
137 #endif // BORNAGAIN_CORE_PARAMETRIZATION_NODEITERATOR_H
Defines class INode.
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
Holds all iterator states encountered for SampleTreeIterator.
Definition: NodeIterator.h:56
virtual ~IteratorMemento()
Definition: NodeIterator.h:59
IteratorState & get_state()
Definition: NodeIterator.h:63
const INode * getCurrent()
Definition: NodeIterator.h:70
bool empty() const
Definition: NodeIterator.h:64
size_t size() const
Definition: NodeIterator.h:72
void push_state(const IteratorState &state)
Definition: NodeIterator.h:61
std::stack< IteratorState > m_state_stack
Definition: NodeIterator.h:75
Holds state of iterator at single level for SampleTreeIterator.
Definition: NodeIterator.h:27
std::vector< const INode * > m_samples
Definition: NodeIterator.h:46
friend std::ostream & operator<<(std::ostream &output_stream, IteratorState const &iterator_state)
Definition: NodeIterator.h:38
bool isEnd() const
Definition: NodeIterator.h:35
const INode * getCurrent() const
Definition: NodeIterator.h:34
size_t m_position
Definition: NodeIterator.h:47
virtual ~IteratorState()
Definition: NodeIterator.h:32
Iterator through INode tree of objects.
Definition: NodeIterator.h:90
int depth() const
Definition: NodeIterator.h:132
const INode * mp_root
Definition: NodeIterator.h:104
Strategy m_strategy
Definition: NodeIterator.h:102
const INode * getCurrent()
Definition: NodeIterator.h:122
NodeIterator(const INode *root)
Definition: NodeIterator.h:108
IteratorMemento m_memento_itor
Definition: NodeIterator.h:103
virtual ~NodeIterator()
Definition: NodeIterator.h:93
bool isDone() const
Definition: NodeIterator.h:127