Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

PCTreeIterators.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
36 
37 #include <deque>
38 #include <functional>
39 #include <iosfwd>
40 #include <iterator>
41 #include <type_traits>
42 #include <utility>
43 #include <vector>
44 
45 namespace ogdf::pc_tree {
46 class PCTree;
47 struct PCNodeChildrenIterable;
48 struct PCNodeNeighborsIterable;
49 
51  friend struct PCNodeChildrenIterable;
52  friend struct PCNodeNeighborsIterable;
53 
54  PCNode* m_node = nullptr;
55  PCNode* m_pred = nullptr;
56  PCNode* m_curr = nullptr;
57 
59  : m_node(node), m_pred(pred), m_curr(curr) { }
60 
61 public:
62  using iterator_category = std::forward_iterator_tag;
63  using value_type = PCNode;
64  using pointer = PCNode*;
65  using reference = PCNode&;
66  using difference_type = std::ptrdiff_t;
67 
68  PCNodeIterator() = default;
69 
70  PCNode& operator->() const { return *m_curr; }
71 
72  PCNode* operator*() const { return m_curr; }
73 
75  PCNodeIterator& operator++();
76 
78  PCNodeIterator operator++(int);
79 
80  bool operator==(const PCNodeIterator& rhs) const {
81  return m_node == rhs.m_node && m_pred == rhs.m_pred && m_curr == rhs.m_curr;
82  }
83 
84  bool operator!=(const PCNodeIterator& rhs) const { return !(rhs == *this); }
85 
86  PCNode* nodeOf() const { return m_node; }
87 
88  bool isParent();
89 };
90 
92  PCNode* const m_node;
93 
94  explicit PCNodeChildrenIterable(PCNode* node) : m_node(node) { }
95 
96  PCNodeIterator begin() const noexcept;
97 
98  PCNodeIterator end() const noexcept;
99 
100  unsigned long count() const;
101 };
102 
104  PCNode* const m_node;
105  PCNode* const m_first;
106 
107  explicit PCNodeNeighborsIterable(PCNode* node, PCNode* first = nullptr)
108  : m_node(node)
109  , m_first(first != nullptr
110  ? first
111  : (node->m_child1 != nullptr ? node->m_child1 : node->getParent())) {
112  if (this->m_first == nullptr) {
113  OGDF_ASSERT(this->m_node->getDegree() == 0);
114  } else {
115  OGDF_ASSERT(this->m_node->isParentOf(this->m_first)
116  || this->m_first->isParentOf(this->m_node));
117  }
118  }
119 
120  PCNodeIterator begin() const noexcept;
121 
122  PCNodeIterator end() const noexcept;
123 
124  unsigned long count() const;
125 };
126 
131 template<bool dfs, bool reverse = false>
133  using container_type =
134  typename std::conditional<dfs, std::vector<PCNode*>, std::deque<PCNode*>>::type;
135 
137  std::function<bool(PCNode*)> m_visit;
138  std::function<bool(PCNode*)> m_descend;
139 
140 public:
141  // iterator traits
142  using iterator_category = std::input_iterator_tag;
143  using value_type = PCNode*;
144  using difference_type = std::ptrdiff_t;
145  using pointer = PCNode**;
146  using reference = PCNode*&;
147 
148  static bool return_true([[maybe_unused]] PCNode* n) { return true; }
149 
150  explicit FilteringPCTreeWalk() = default;
151 
152  explicit FilteringPCTreeWalk([[maybe_unused]] const PCTree& T, PCNode* start,
153  std::function<bool(PCNode*)> visit = return_true,
154  std::function<bool(PCNode*)> descend_from = return_true)
155  : m_pending({start}), m_visit(std::move(visit)), m_descend(std::move(descend_from)) {
156  if (!m_pending.empty() && !m_visit(top())) {
157  next();
158  }
159  }
160 
161  bool operator==(const FilteringPCTreeWalk& rhs) const { return m_pending == rhs.m_pending; }
162 
163  bool operator!=(const FilteringPCTreeWalk& rhs) const { return m_pending != rhs.m_pending; }
164 
165  FilteringPCTreeWalk& begin() { return *this; }
166 
168 
169  PCNode* top() {
170  OGDF_ASSERT(!m_pending.empty());
171  if constexpr (dfs) {
172  return m_pending.back();
173  } else {
174  return m_pending.front();
175  }
176  }
177 
178  PCNode* operator*() { return top(); }
179 
182  next();
183  return *this;
184  }
185 
187  OGDF_DEPRECATED("Calling FilteringPCTreeWalk++ will copy the array of pending nodes")
188 
189  FilteringPCTreeWalk operator++(int) {
190  FilteringPCTreeWalk before = *this;
191  next();
192  return before;
193  }
194 
195  void next() {
196  do {
197  OGDF_ASSERT(!m_pending.empty());
198  PCNode* node = top();
199  if constexpr (dfs) {
200  m_pending.pop_back();
201  } else {
202  m_pending.pop_front();
203  }
204  if (m_descend(node)) {
205  std::copy(node->children().begin(), node->children().end(),
206  std::back_inserter(m_pending));
207  if constexpr (reverse) {
208  std::reverse(m_pending.end() - node->getChildCount(), m_pending.end());
209  }
210  }
211  } while (!m_pending.empty() && !m_visit(top()));
212  }
213 
214  explicit operator bool() const { return valid(); }
215 
216  bool valid() const { return !m_pending.empty(); }
217 
218  void append(PCNode* a) { m_pending.push(a); }
219 
220  int pendingCount() const { return m_pending.size(); }
221 };
222 
225 }
ogdf::pc_tree::FilteringPCTreeWalk::container_type
typename std::conditional< dfs, std::vector< PCNode * >, std::deque< PCNode * > >::type container_type
Definition: PCTreeIterators.h:134
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::pc_tree::PCNodeNeighborsIterable
Definition: PCTreeIterators.h:103
ogdf::pc_tree::FilteringPCTreeWalk::FilteringPCTreeWalk
FilteringPCTreeWalk([[maybe_unused]] const PCTree &T, PCNode *start, std::function< bool(PCNode *)> visit=return_true, std::function< bool(PCNode *)> descend_from=return_true)
Definition: PCTreeIterators.h:152
ogdf::pc_tree::FilteringPCTreeWalk::next
void next()
Definition: PCTreeIterators.h:195
ogdf::pc_tree::PCNodeIterator::operator*
PCNode * operator*() const
Definition: PCTreeIterators.h:72
ogdf::pc_tree::PCNode::isParentOf
bool isParentOf(const PCNode *other) const
Definition: PCNode.h:315
ogdf::pc_tree::PCNodeIterator::m_node
PCNode * m_node
Definition: PCTreeIterators.h:54
OGDF_DEPRECATED
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition: config.h:120
ogdf::begin
HypergraphRegistry< HypernodeElement >::iterator begin(const HypergraphRegistry< HypernodeElement > &self)
ogdf::pc_tree
Definition: NodePCRotation.h:47
ogdf::pc_tree::FilteringPCTreeWalk::difference_type
std::ptrdiff_t difference_type
Definition: PCTreeIterators.h:144
ogdf::pc_tree::FilteringPCTreeWalk::pendingCount
int pendingCount() const
Definition: PCTreeIterators.h:220
ogdf::pc_tree::PCTree
A PC-tree represents a set of cyclic orders of its leaves by labeling its inner nodes as either P- or...
Definition: PCTree.h:118
ogdf::pc_tree::PCNodeChildrenIterable
Definition: PCTreeIterators.h:91
ogdf::pc_tree::PCNodeIterator::m_curr
PCNode * m_curr
Definition: PCTreeIterators.h:56
ogdf::pc_tree::PCNodeIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: PCTreeIterators.h:62
ogdf::pc_tree::PCNodeChildrenIterable::PCNodeChildrenIterable
PCNodeChildrenIterable(PCNode *node)
Definition: PCTreeIterators.h:94
ogdf::pc_tree::FilteringPCTreeWalk::end
FilteringPCTreeWalk end() const
Definition: PCTreeIterators.h:167
ogdf::pc_tree::PCNodeIterator::operator->
PCNode & operator->() const
Definition: PCTreeIterators.h:70
ogdf::pc_tree::PCNodeNeighborsIterable::m_node
PCNode *const m_node
Definition: PCTreeIterators.h:104
ogdf::Direction::before
@ before
ogdf::pc_tree::PCNodeIterator::operator!=
bool operator!=(const PCNodeIterator &rhs) const
Definition: PCTreeIterators.h:84
ogdf::pc_tree::PCNodeIterator
Definition: PCTreeIterators.h:50
ogdf::pc_tree::PCNodeChildrenIterable::m_node
PCNode *const m_node
Definition: PCTreeIterators.h:92
ogdf::reverse
Reverse< T > reverse(T &container)
Provides iterators for container to make it easily iterable in reverse.
Definition: Reverse.h:74
ogdf::pc_tree::FilteringPCTreeWalk::iterator_category
std::input_iterator_tag iterator_category
Definition: PCTreeIterators.h:142
ogdf::pc_tree::FilteringPCTreeWalk
A DFS or BFS through a PCTree.
Definition: PCTreeIterators.h:132
ogdf::pc_tree::FilteringPCTreeWalk::begin
FilteringPCTreeWalk & begin()
Definition: PCTreeIterators.h:165
Minisat::Internal::copy
static void copy(const T &from, T &to)
Definition: Alg.h:61
ogdf::pc_tree::PCNodeIterator::operator==
bool operator==(const PCNodeIterator &rhs) const
Definition: PCTreeIterators.h:80
ogdf::pc_tree::FilteringPCTreeWalk::operator*
PCNode * operator*()
Definition: PCTreeIterators.h:178
ogdf::pc_tree::PCNodeNeighborsIterable::m_first
PCNode *const m_first
Definition: PCTreeIterators.h:105
ogdf::pc_tree::FilteringPCTreeWalk::return_true
static bool return_true([[maybe_unused]] PCNode *n)
Definition: PCTreeIterators.h:148
backward::Color::type
type
Definition: backward.hpp:1716
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:243
ogdf::pc_tree::FilteringPCTreeWalk::top
PCNode * top()
Definition: PCTreeIterators.h:169
ogdf::pc_tree::FilteringPCTreeWalk::append
void append(PCNode *a)
Definition: PCTreeIterators.h:218
ogdf::pc_tree::FilteringPCTreeWalk::m_pending
container_type m_pending
Definition: PCTreeIterators.h:136
ogdf::pc_tree::FilteringPCTreeWalk::m_descend
std::function< bool(PCNode *)> m_descend
Definition: PCTreeIterators.h:138
ogdf::pc_tree::PCNode::getDegree
size_t getDegree() const
Definition: PCNode.h:458
ogdf::pc_tree::FilteringPCTreeWalk::valid
bool valid() const
Definition: PCTreeIterators.h:216
ogdf::pc_tree::PCNodeIterator::nodeOf
PCNode * nodeOf() const
Definition: PCTreeIterators.h:86
ogdf::pc_tree::FilteringPCTreeWalk::operator==
bool operator==(const FilteringPCTreeWalk &rhs) const
Definition: PCTreeIterators.h:161
ogdf::pc_tree::FilteringPCTreeWalk::m_visit
std::function< bool(PCNode *)> m_visit
Definition: PCTreeIterators.h:137
ogdf::pc_tree::PCNode
A node in a PC-tree that is either a P-node, C-node or leaf.
Definition: PCNode.h:62
PCNode.h
A node in a PC-tree that is either a P-node, C-node or leaf.
basic.h
Basic declarations, included by all source files.
ogdf::end
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
ogdf::pc_tree::PCNodeIterator::difference_type
std::ptrdiff_t difference_type
Definition: PCTreeIterators.h:66
ogdf::pc_tree::PCNodeNeighborsIterable::PCNodeNeighborsIterable
PCNodeNeighborsIterable(PCNode *node, PCNode *first=nullptr)
Definition: PCTreeIterators.h:107
ogdf::pc_tree::FilteringPCTreeWalk::operator!=
bool operator!=(const FilteringPCTreeWalk &rhs) const
Definition: PCTreeIterators.h:163
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::pc_tree::PCNodeIterator::PCNodeIterator
PCNodeIterator(PCNode *node, PCNode *pred, PCNode *curr)
Definition: PCTreeIterators.h:58
ogdf::pc_tree::PCNodeIterator::m_pred
PCNode * m_pred
Definition: PCTreeIterators.h:55
ogdf::pc_tree::FilteringPCTreeWalk::operator++
FilteringPCTreeWalk & operator++()
Increment operator (prefix, returns result).
Definition: PCTreeIterators.h:181