Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

FilteringBFS.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
35 #include <ogdf/basic/GraphList.h>
36 #include <ogdf/basic/Queue.h>
37 #include <ogdf/basic/SList.h>
38 #include <ogdf/basic/basic.h>
41 
42 #include <functional>
43 #include <initializer_list>
44 #include <iosfwd>
45 #include <iterator>
46 
47 namespace ogdf {
48 
49 class FilteringBFSIterator;
50 
56 // see also FilteringPCTreeDFS/BFS
60  std::function<bool(adjEntry)> m_visit;
61  std::function<bool(node)> m_descend;
62 
63 public:
64  template<typename T>
65  static bool return_true(T t) {
66  return true;
67  }
68 
69  explicit FilteringBFS() = default;
70 
73 
74  template<typename Container>
75  explicit FilteringBFS(const Graph& G, Container& nodes,
76  const std::function<bool(adjEntry)>& visit = return_true<adjEntry>,
77  const std::function<bool(node)>& descend_from = return_true<node>)
78  : m_pending(), m_visited(G, false), m_visit(visit), m_descend(descend_from) {
79  for (node n : nodes) {
80  m_pending.append(n);
81  }
82  }
83 
84  explicit FilteringBFS(const Graph& G, std::initializer_list<node> nodes,
85  const std::function<bool(adjEntry)>& visit = return_true<adjEntry>,
86  const std::function<bool(node)>& descend_from = return_true<node>)
87  : m_pending(nodes), m_visited(G, false), m_visit(visit), m_descend(descend_from) { }
88 
89  bool operator==(const FilteringBFS& rhs) const {
90  return m_pending.getList() == rhs.m_pending.getList();
91  }
92 
93  bool operator!=(const FilteringBFS& rhs) const {
94  return m_pending.getList() != rhs.m_pending.getList();
95  }
96 
98 
100 
101  void next() {
102  OGDF_ASSERT(!m_pending.empty());
103  node n = m_pending.pop();
104  OGDF_ASSERT(!m_visited[n]);
105  m_visited[n] = true;
106  if (m_descend(n)) {
107  for (adjEntry adj : n->adjEntries) {
108  node twin = adj->twinNode();
109  if (!m_visited[twin] && m_visit(adj)) {
110  m_pending.append(twin);
111  }
112  }
113  }
114  while (!m_pending.empty() && m_visited[m_pending.top()]) {
115  m_pending.pop();
116  }
117  }
118 
120  OGDF_ASSERT(!m_pending.empty());
121  return m_pending.top();
122  }
123 
124  operator bool() const { return valid(); }
125 
126  bool valid() const { return !m_pending.empty(); }
127 
128  void append(node n) {
129  m_visited[n] = false;
130  m_pending.append(n);
131  }
132 
133  bool hasVisited(node n) const { return m_visited[n]; }
134 
135  bool willVisitTarget(adjEntry adj) const { return m_visit(adj); }
136 
137  bool willDescendFrom(node n) const { return m_descend(n); }
138 
139  void setVisitFilter(const std::function<bool(adjEntry)>& mVisit) { m_visit = mVisit; }
140 
141  void setDescendFilter(const std::function<bool(node)>& mDescend) { m_descend = mDescend; }
142 
143  int pendingCount() const { return m_pending.size(); }
144 };
145 
148 
149 public:
150  // iterator traits
151  using iterator_category = std::input_iterator_tag;
152  using value_type = node;
153  using difference_type = std::ptrdiff_t;
154  using pointer = node*;
155  using reference = node&;
156 
157  explicit FilteringBFSIterator() : m_bfs(nullptr) { }
158 
159  explicit FilteringBFSIterator(FilteringBFS* bfs) : m_bfs(bfs) { }
160 
161  bool operator==(const FilteringBFSIterator& rhs) const {
162  if (m_bfs) {
163  if (rhs.m_bfs) {
164  return m_bfs == rhs.m_bfs;
165  } else {
166  return !m_bfs->valid();
167  }
168  } else {
169  if (rhs.m_bfs) {
170  return !rhs.m_bfs->valid();
171  } else {
172  return true;
173  }
174  }
175  }
176 
177  bool operator!=(const FilteringBFSIterator& rhs) const { return !(*this == rhs); }
178 
180  OGDF_ASSERT(m_bfs != nullptr);
181  return m_bfs->current();
182  }
183 
185  OGDF_ASSERT(m_bfs != nullptr);
186  m_bfs->next();
187  return *this;
188  }
189 };
190 
192 
194 
195 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::FilteringBFS::hasVisited
bool hasVisited(node n) const
Definition: FilteringBFS.h:133
ogdf::FilteringBFSIterator::operator++
FilteringBFSIterator & operator++()
Definition: FilteringBFS.h:184
ogdf::Queue::append
iterator append(const E &x)
Adds x at the end of queue.
Definition: Queue.h:324
ogdf::FilteringBFSIterator::operator*
node operator*()
Definition: FilteringBFS.h:179
Graph.h
Includes declaration of graph class.
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::FilteringBFSIterator::FilteringBFSIterator
FilteringBFSIterator(FilteringBFS *bfs)
Definition: FilteringBFS.h:159
copy_move.h
Utility macros for declaring copy and move constructors and assignment operations.
ogdf::FilteringBFS
An iterator-based BFS through a Graph.
Definition: FilteringBFS.h:57
ogdf::begin
HypergraphRegistry< HypernodeElement >::iterator begin(const HypergraphRegistry< HypernodeElement > &self)
OGDF_DEFAULT_MOVE
#define OGDF_DEFAULT_MOVE(cls)
Explicitly provides default move construction and assignment for class cls.
Definition: copy_move.h:52
ogdf::FilteringBFS::willVisitTarget
bool willVisitTarget(adjEntry adj) const
Definition: FilteringBFS.h:135
ogdf::FilteringBFS::willDescendFrom
bool willDescendFrom(node n) const
Definition: FilteringBFS.h:137
ogdf::Queue::top
const_reference top() const
Returns a reference to the front element.
Definition: Queue.h:249
Queue.h
Declaration and implementation of list-based queues (classes QueuePure<E> and Queue<E>).
ogdf::FilteringBFS::operator!=
bool operator!=(const FilteringBFS &rhs) const
Definition: FilteringBFS.h:93
ogdf::FilteringBFSIterator::m_bfs
FilteringBFS * m_bfs
Definition: FilteringBFS.h:147
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
ogdf::FilteringBFS::begin
FilteringBFSIterator begin()
Definition: FilteringBFS.h:191
ogdf::FilteringBFS::m_pending
Queue< node > m_pending
Definition: FilteringBFS.h:58
ogdf::FilteringBFS::end
FilteringBFSIterator end()
Definition: FilteringBFS.h:193
ogdf::FilteringBFS::m_descend
std::function< bool(node)> m_descend
Definition: FilteringBFS.h:61
ogdf::FilteringBFS::setVisitFilter
void setVisitFilter(const std::function< bool(adjEntry)> &mVisit)
Definition: FilteringBFS.h:139
ogdf::FilteringBFS::current
node current()
Definition: FilteringBFS.h:119
ogdf::FilteringBFS::FilteringBFS
FilteringBFS(const Graph &G, Container &nodes, const std::function< bool(adjEntry)> &visit=return_true< adjEntry >, const std::function< bool(node)> &descend_from=return_true< node >)
Definition: FilteringBFS.h:75
ogdf::FilteringBFS::append
void append(node n)
Definition: FilteringBFS.h:128
ogdf::FilteringBFS::operator==
bool operator==(const FilteringBFS &rhs) const
Definition: FilteringBFS.h:89
ogdf::Queue::pop
E pop()
Removes front element and returns it.
Definition: Queue.h:336
ogdf::FilteringBFSIterator::operator==
bool operator==(const FilteringBFSIterator &rhs) const
Definition: FilteringBFS.h:161
SList.h
Declaration of singly linked lists and iterators.
ogdf::Queue::size
int size() const
Returns the number of elements in the queue.
Definition: Queue.h:246
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::FilteringBFSIterator
Definition: FilteringBFS.h:146
ogdf::FilteringBFS::FilteringBFS
FilteringBFS(const Graph &G, std::initializer_list< node > nodes, const std::function< bool(adjEntry)> &visit=return_true< adjEntry >, const std::function< bool(node)> &descend_from=return_true< node >)
Definition: FilteringBFS.h:84
ogdf::node
NodeElement * node
The type of nodes.
Definition: Graph_d.h:70
ogdf::FilteringBFSIterator::iterator_category
std::input_iterator_tag iterator_category
Definition: FilteringBFS.h:151
ogdf::FilteringBFS::valid
bool valid() const
Definition: FilteringBFS.h:126
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::Queue
The parameterized class Queue<E> implements list-based queues.
Definition: Queue.h:205
ogdf::Graph
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:869
ogdf::FilteringBFS::next
void next()
Definition: FilteringBFS.h:101
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:175
graph_iterators.h
Decralation of graph iterators.
ogdf::Queue::empty
bool empty() const
Returns true iff the queue is empty.
Definition: Queue.h:243
basic.h
Basic declarations, included by all source files.
ogdf::end
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)
ogdf::FilteringBFS::m_visited
NodeArray< bool > m_visited
Definition: FilteringBFS.h:59
ogdf::FilteringBFSIterator::FilteringBFSIterator
FilteringBFSIterator()
Definition: FilteringBFS.h:157
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
ogdf::FilteringBFS::return_true
static bool return_true(T t)
Definition: FilteringBFS.h:65
ogdf::FilteringBFS::setDescendFilter
void setDescendFilter(const std::function< bool(node)> &mDescend)
Definition: FilteringBFS.h:141
ogdf::FilteringBFSIterator::operator!=
bool operator!=(const FilteringBFSIterator &rhs) const
Definition: FilteringBFS.h:177
OGDF_DEFAULT_COPY
#define OGDF_DEFAULT_COPY(cls)
Explicitly provides default copy construction and assignment for class cls.
Definition: copy_move.h:47
ogdf::FilteringBFSIterator::difference_type
std::ptrdiff_t difference_type
Definition: FilteringBFS.h:153
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::FilteringBFS::pendingCount
int pendingCount() const
Definition: FilteringBFS.h:143
ogdf::Queue::getList
const SList< E > & getList() const
Conversion to const SList.
Definition: Queue.h:314
ogdf::FilteringBFS::m_visit
std::function< bool(adjEntry)> m_visit
Definition: FilteringBFS.h:60