Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
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
45namespace ogdf::pc_tree {
46class PCTree;
47struct PCNodeChildrenIterable;
48struct PCNodeNeighborsIterable;
49
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
61public:
62 using iterator_category = std::forward_iterator_tag;
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
76
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
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
131template<bool dfs, bool reverse = false>
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
140public:
141 // iterator traits
142 using iterator_category = std::input_iterator_tag;
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
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) {
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}
A node in a PC-tree that is either a P-node, C-node or leaf.
Basic declarations, included by all source files.
Class for the representation of nodes.
Definition Graph_d.h:241
A DFS or BFS through a PCTree.
typename std::conditional< dfs, std::vector< PCNode * >, std::deque< PCNode * > >::type container_type
bool operator!=(const FilteringPCTreeWalk &rhs) const
FilteringPCTreeWalk(const PCTree &T, PCNode *start, std::function< bool(PCNode *)> visit=return_true, std::function< bool(PCNode *)> descend_from=return_true)
bool operator==(const FilteringPCTreeWalk &rhs) const
FilteringPCTreeWalk end() const
std::function< bool(PCNode *)> m_visit
FilteringPCTreeWalk & operator++()
Increment operator (prefix, returns result).
std::input_iterator_tag iterator_category
std::function< bool(PCNode *)> m_descend
A node in a PC-tree that is either a P-node, C-node or leaf.
Definition PCNode.h:62
size_t getDegree() const
Definition PCNode.h:458
bool isParentOf(const PCNode *other) const
Definition PCNode.h:315
std::forward_iterator_tag iterator_category
bool operator==(const PCNodeIterator &rhs) const
bool operator!=(const PCNodeIterator &rhs) const
PCNodeIterator operator++(int)
Increment operator (postfix, returns previous value).
PCNodeIterator & operator++()
Increment operator (prefix, returns result).
PCNodeIterator(PCNode *node, PCNode *pred, PCNode *curr)
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
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
Reverse< T > reverse(T &container)
Provides iterators for container to make it easily iterable in reverse.
Definition Reverse.h:74
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition config.h:206
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
PCNodeIterator begin() const noexcept
PCNodeNeighborsIterable(PCNode *node, PCNode *first=nullptr)
PCNodeIterator begin() const noexcept