Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

graph_iterators.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
35 
36 #include <iosfwd>
37 #include <iterator>
38 #include <type_traits>
39 
40 namespace ogdf {
41 
42 namespace internal {
43 
44 template<class GraphObjectPtr, bool isReverse, bool isConst>
46 
47 template<class GraphObjectPtr>
49 template<class GraphObjectPtr>
51 
52 template<class GraphObjectPtr, bool isReverse, bool isConst>
53 class GraphIteratorBase {
54  friend class GraphIteratorBase<GraphObjectPtr, !isReverse, isConst>;
56 
57  GraphObjectPtr m_ptr;
58 
59 public:
60  using difference_type = std::ptrdiff_t;
62  using iterator_category = std::bidirectional_iterator_tag;
63  using pointer = value_type*;
65 
66  GraphIteratorBase() : m_ptr(nullptr) { }
67 
68  GraphIteratorBase(GraphObjectPtr ptr) : m_ptr(ptr) { }
69 
70  template<bool isArgReverse>
72  : m_ptr(it.m_ptr) { }
73 
74  bool operator==(const T& other) const { return m_ptr == other.m_ptr; }
75 
76  bool operator!=(const T& other) const { return m_ptr != other.m_ptr; }
77 
78  const GraphObjectPtr& operator*() const { return m_ptr; }
79 
82  OGDF_ASSERT(m_ptr != nullptr);
83  m_ptr = isReverse ? m_ptr->pred() : m_ptr->succ();
84  return *this;
85  }
86 
88  T operator++(int) {
89  OGDF_ASSERT(m_ptr != nullptr);
90  GraphObjectPtr ptr = m_ptr;
91  m_ptr = isReverse ? m_ptr->pred() : m_ptr->succ();
92  return ptr;
93  }
94 
97  OGDF_ASSERT(m_ptr != nullptr);
98  m_ptr = isReverse ? m_ptr->succ() : m_ptr->pred();
99  return *this;
100  }
101 
103  T operator--(int) {
104  OGDF_ASSERT(m_ptr != nullptr);
105  GraphObjectPtr ptr = m_ptr;
106  m_ptr = isReverse ? m_ptr->succ() : m_ptr->pred();
107  return ptr;
108  }
109 };
110 
111 template<class ArrayType, bool isConst>
113 
114 template<class ArrayType>
116 template<class ArrayType>
118 
119 template<class ArrayType, bool isConst>
121  friend class GraphArrayIteratorBase<ArrayType, true>;
122 
123 public:
125  using key_type = typename ArrayType::key_type;
127  using value_type = typename std::conditional<isConst, const typename ArrayType::value_type,
128  typename ArrayType::value_type>::type;
131 
133  GraphArrayIteratorBase() : m_key(nullptr), m_array(nullptr) { }
134 
137 
141  : GraphArrayIteratorBase(iter.m_key, iter.m_array) { }
142 
144  // gcc9 complains since it cannot match the templated constructor above (-Werror=deprecated-copy).
146  : GraphArrayIteratorBase(iter.m_key, iter.m_array) { }
147 
151  m_key = iter.m_key;
152  m_array = iter.m_array;
153  return *this;
154  }
155 
157  key_type key() const { return m_key; }
158 
160  value_type& value() const { return (*m_array)[m_key]; }
161 
163  value_type& operator*() const { return (*m_array)[m_key]; }
164 
167  return m_key == iter.m_key && m_array == iter.m_array;
168  }
169 
172  return !operator==(iter);
173  }
174 
177  OGDF_ASSERT(m_key != nullptr);
178  m_key = ArrayType::findSuccKey(m_key);
179  return *this;
180  }
181 
185  m_key = ArrayType::findSuccKey(m_key);
186  return iter;
187  }
188 
191  OGDF_ASSERT(m_key != nullptr);
192  m_key = ArrayType::findPredKey(m_key);
193  return *this;
194  }
195 
199  m_key = ArrayType::findPredKey(m_key);
200  return iter;
201  }
202 
203 private:
206 };
207 
208 template<typename It>
209 class SimpleRange {
211  It m_end;
212 
213 public:
214  SimpleRange(It&& begin, It&& end) : m_begin(std::move(begin)), m_end(std::move(end)) { }
215 
216  SimpleRange(const It& begin, const It& end) : m_begin(begin), m_end(end) { }
217 
218  SimpleRange() = default;
219 
220  const It& begin() const { return m_begin; }
221 
222  const It& end() const { return m_end; }
223 
224  It& begin() { return m_begin; }
225 
226  It& end() { return m_end; }
227 };
228 
229 }
230 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::internal::GraphIteratorBase::operator--
T & operator--()
Decrement operator (prefix).
Definition: graph_iterators.h:96
ogdf::internal::GraphIteratorBase::iterator_category
std::bidirectional_iterator_tag iterator_category
Definition: graph_iterators.h:62
ogdf::internal::GraphArrayIteratorBase::operator++
GraphArrayIteratorBase< ArrayType, isConst > operator++(int)
Increment operator (postfix).
Definition: graph_iterators.h:183
ogdf::internal::GraphArrayIteratorBase::operator++
GraphArrayIteratorBase< ArrayType, isConst > & operator++()
Increment operator (prefix).
Definition: graph_iterators.h:176
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::internal::GraphIteratorBase
Definition: graph_iterators.h:45
ogdf::internal::GraphArrayIteratorBase::key
key_type key() const
Index in m_array.
Definition: graph_iterators.h:157
ogdf::internal::GraphArrayIteratorBase::operator*
value_type & operator*() const
Value of m_array at index m_key.
Definition: graph_iterators.h:163
ogdf::internal::SimpleRange::end
It & end()
Definition: graph_iterators.h:226
ogdf::internal::GraphIteratorBase::GraphIteratorBase
GraphIteratorBase(GraphIteratorBase< GraphObjectPtr, isArgReverse, isConst > &it)
Definition: graph_iterators.h:71
ogdf::internal::GraphIteratorBase::m_ptr
GraphObjectPtr m_ptr
Definition: graph_iterators.h:57
ogdf::internal::GraphIteratorBase::operator!=
bool operator!=(const T &other) const
Definition: graph_iterators.h:76
ogdf::internal::SimpleRange::SimpleRange
SimpleRange(const It &begin, const It &end)
Definition: graph_iterators.h:216
ogdf::internal::GraphArrayIteratorBase::GraphArrayIteratorBase
GraphArrayIteratorBase(const GraphArrayIteratorBase< ArrayType, isConst > &iter)
Copy constructor.
Definition: graph_iterators.h:145
ogdf::internal::GraphIteratorBase::reference
value_type & reference
Definition: graph_iterators.h:64
ogdf::internal::SimpleRange::begin
const It & begin() const
Definition: graph_iterators.h:220
ogdf::internal::GraphArrayIteratorBase::array_pointer_type
typename std::conditional< isConst, const ArrayType *, ArrayType * >::type array_pointer_type
Type of the array.
Definition: graph_iterators.h:130
ogdf::internal::GraphArrayIteratorBase
Definition: graph_iterators.h:112
ogdf::internal::GraphArrayIteratorBase::operator=
GraphArrayIteratorBase< ArrayType, isConst > & operator=(const GraphArrayIteratorBase< ArrayType, isConst > &iter)
Copy assignment operator.
Definition: graph_iterators.h:149
ogdf::internal::GraphArrayIteratorBase::m_key
key_type m_key
Index in m_array.
Definition: graph_iterators.h:204
ogdf::internal::SimpleRange::SimpleRange
SimpleRange()=default
ogdf::internal::GraphArrayIteratorBase::operator--
GraphArrayIteratorBase< ArrayType, isConst > operator--(int)
Decrement operator (postfix).
Definition: graph_iterators.h:197
ogdf::internal::GraphArrayIteratorBase::m_array
array_pointer_type m_array
The array.
Definition: graph_iterators.h:205
backward::Color::type
type
Definition: backward.hpp:1716
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:243
ogdf::internal::GraphArrayIteratorBase::GraphArrayIteratorBase
GraphArrayIteratorBase(const GraphArrayIteratorBase< ArrayType, isArgConst > &iter)
Constructor.
Definition: graph_iterators.h:140
ogdf::internal::GraphIteratorBase::GraphIteratorBase
GraphIteratorBase()
Definition: graph_iterators.h:66
ogdf::internal::GraphIteratorBase::value_type
typename std::conditional< isConst, const GraphObjectPtr, GraphObjectPtr >::type value_type
Definition: graph_iterators.h:61
ogdf::internal::GraphArrayIteratorBase::GraphArrayIteratorBase
GraphArrayIteratorBase(key_type key, array_pointer_type a)
Constructor.
Definition: graph_iterators.h:136
ogdf::internal::GraphArrayIteratorBase::GraphArrayIteratorBase
GraphArrayIteratorBase()
Constructor.
Definition: graph_iterators.h:133
ogdf::internal::GraphArrayIteratorBase::operator--
GraphArrayIteratorBase< ArrayType, isConst > & operator--()
Decrement operator (prefix).
Definition: graph_iterators.h:190
ogdf::internal::SimpleRange::end
const It & end() const
Definition: graph_iterators.h:222
ogdf::internal::SimpleRange::SimpleRange
SimpleRange(It &&begin, It &&end)
Definition: graph_iterators.h:214
ogdf::internal::GraphArrayIteratorBase::operator!=
bool operator!=(const GraphArrayIteratorBase< ArrayType, isConst > &iter) const
Inequality operator.
Definition: graph_iterators.h:171
ogdf::internal::GraphArrayIteratorBase::value
value_type & value() const
Value of m_array at index m_key.
Definition: graph_iterators.h:160
ogdf::internal::GraphIteratorBase::operator++
T operator++(int)
Increment operator (postfix).
Definition: graph_iterators.h:88
ogdf::internal::SimpleRange::m_begin
It m_begin
Definition: graph_iterators.h:210
ogdf::internal::GraphIteratorBase::operator*
const GraphObjectPtr & operator*() const
Definition: graph_iterators.h:78
ogdf::internal::GraphArrayIteratorBase::operator==
bool operator==(const GraphArrayIteratorBase< ArrayType, isConst > &iter) const
Equality operator.
Definition: graph_iterators.h:166
basic.h
Basic declarations, included by all source files.
std
Definition: GML.h:111
ogdf::internal::GraphArrayIteratorBase::value_type
typename std::conditional< isConst, const typename ArrayType::value_type, typename ArrayType::value_type >::type value_type
Value type of the associated array.
Definition: graph_iterators.h:128
ogdf::internal::GraphArrayIteratorBase::key_type
typename ArrayType::key_type key_type
Index type of the associated array.
Definition: graph_iterators.h:125
ogdf::internal::SimpleRange
Definition: graph_iterators.h:209
ogdf::internal::GraphIteratorBase::difference_type
std::ptrdiff_t difference_type
Definition: graph_iterators.h:60
ogdf::internal::GraphIteratorBase::GraphIteratorBase
GraphIteratorBase(GraphObjectPtr ptr)
Definition: graph_iterators.h:68
ogdf::internal::GraphIteratorBase::operator--
T operator--(int)
Decrement operator (postfix).
Definition: graph_iterators.h:103
ogdf::internal::SimpleRange::begin
It & begin()
Definition: graph_iterators.h:224
ogdf::internal::SimpleRange::m_end
It m_end
Definition: graph_iterators.h:211
ogdf::internal::GraphIteratorBase::operator==
bool operator==(const T &other) const
Definition: graph_iterators.h:74
ogdf::internal::GraphIteratorBase::pointer
value_type * pointer
Definition: graph_iterators.h:63
ogdf::internal::GraphIteratorBase::operator++
T & operator++()
Increment operator (prefix).
Definition: graph_iterators.h:81