FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tree_bfs_explorer.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (1 Mar. 2011)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 #include <queue>
19 #include "pusher.h"
20 
21 namespace cpp {
22 
44 template<typename NODE, typename TREE_MODEL>
46 {
47 public:
49 
50  void begin(NODE* node = nullptr);
51  void goNext();
52  bool atEnd() const;
53 
54  NODE* current() const;
55  unsigned depth() const;
56 
57 private:
58  NODE* m_current;
59  std::queue<NODE*> m_levelNodes;
60  unsigned m_depth;
61 };
62 
63 
64 // --
65 // -- Implementation
66 // --
67 
68 template<typename NODE, typename TREE_MODEL>
70  : m_current(nullptr),
71  m_depth(0)
72 {
73 }
74 
76 template<typename NODE, typename TREE_MODEL>
78 {
79  while (!m_levelNodes.empty())
80  m_levelNodes.pop();
81  m_current = nullptr;
82  m_depth = 0;
83 
84  if (node == nullptr)
85  TREE_MODEL::enqueueChildren(cpp::pusher(m_levelNodes), node);
86  else
87  m_levelNodes.push(node);
88 
89  this->goNext();
90 }
91 
93 template<typename NODE, typename TREE_MODEL>
95 {
96  if (m_levelNodes.empty()) {
97  m_current = nullptr;
98  return;
99  }
100 
101  const NODE* previous = m_current;
102 
103  m_current = m_levelNodes.front();
104  m_levelNodes.pop();
105 
106  if (previous != nullptr
107  && m_current != nullptr
108  && TREE_MODEL::isDeeper(m_current, previous))
109  {
110  ++m_depth;
111  }
112 
113  if (m_current != nullptr)
114  TREE_MODEL::enqueueChildren(cpp::pusher(m_levelNodes), m_current);
115 }
116 
118 template<typename NODE, typename TREE_MODEL>
120 {
121  return m_current == nullptr;
122 }
123 
125 template<typename NODE, typename TREE_MODEL>
127 {
128  return m_current;
129 }
130 
132 template<typename NODE, typename TREE_MODEL>
134 {
135  return m_depth;
136 }
137 
138 } // namespace cpp
unsigned depth() const
Depth of the current tree node.
Definition: tree_bfs_explorer.h:133
Definition: basic_shared_pointer.h:20
void begin(NODE *node=nullptr)
Prepares exploration to start from tree node.
Definition: tree_bfs_explorer.h:77
TreeBfsExplorer()
Definition: tree_bfs_explorer.h:69
void goNext()
Move exploration to the next tree node.
Definition: tree_bfs_explorer.h:94
Definition: tree_bfs_explorer.h:45
bool atEnd() const
Is exploration beyond the last tree node (ended) ?
Definition: tree_bfs_explorer.h:119
NODE * current() const
Current explored tree node.
Definition: tree_bfs_explorer.h:126
push_iterator< CONTAINER > pusher(CONTAINER &x)
Constructs a push_iterator that pushes new elements into x.
Definition: pusher.h:85