Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

graph_iterators.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <functional>
35 #include <ogdf/basic/basic.h>
36 #include <ogdf/basic/Reverse.h>
37 
38 namespace ogdf {
39 
40 namespace internal {
41 
42 template<class GraphObjectPtr, bool isReverse> class GraphIteratorBase;
43 template<class GraphObjectPtr> using GraphIterator = GraphIteratorBase<GraphObjectPtr,false>;
44 template<class GraphObjectPtr> using GraphReverseIterator = GraphIteratorBase<GraphObjectPtr,true>;
45 
46 template<class GraphObjectPtr, bool isReverse>
47 class GraphIteratorBase {
48  friend class GraphIteratorBase<GraphObjectPtr, !isReverse>;
49 
50  GraphObjectPtr m_ptr;
51 
52 public:
53  GraphIteratorBase() : m_ptr(nullptr) { }
54  GraphIteratorBase(GraphObjectPtr ptr) : m_ptr(ptr) { }
55 
56  template<bool isArgReverse>
58 
60  return m_ptr == other.m_ptr;
61  }
62 
64  return m_ptr != other.m_ptr;
65  }
66 
67  GraphObjectPtr operator*() {
68  return m_ptr;
69  }
70 
73  OGDF_ASSERT(m_ptr != nullptr);
74  m_ptr = isReverse ? m_ptr->pred() : m_ptr->succ();
75  return *this;
76  }
77 
80  OGDF_ASSERT(m_ptr != nullptr);
81  GraphObjectPtr ptr = m_ptr;
82  m_ptr = isReverse ? m_ptr->pred() : m_ptr->succ();
83  return ptr;
84  }
85 
88  OGDF_ASSERT(m_ptr != nullptr);
89  m_ptr = isReverse ? m_ptr->succ() : m_ptr->pred();
90  return *this;
91  }
92 
95  OGDF_ASSERT(m_ptr != nullptr);
96  GraphObjectPtr ptr = m_ptr;
97  m_ptr = isReverse ? m_ptr->succ() : m_ptr->pred();
98  return ptr;
99  }
100 };
101 
102 template<class ArrayType, bool isConst> class GraphArrayIteratorBase;
105 
106 template<class ArrayType, bool isConst> class GraphArrayIteratorBase {
107  friend class GraphArrayIteratorBase<ArrayType, true>;
108 
109 public:
111  using key_type = typename ArrayType::key_type;
116 
119  : m_key(nullptr), m_array(nullptr) {}
120 
123  : m_key(key), m_array(a) { }
124 
128  : GraphArrayIteratorBase(iter.m_key, iter.m_array) { }
129 
131  // gcc9 complains since it cannot match the templated constructor above (-Werror=deprecated-copy).
133  : GraphArrayIteratorBase(iter.m_key, iter.m_array) { }
134 
137  m_key = iter.m_key;
138  m_array = iter.m_array;
139  return *this;
140  }
141 
143  key_type key() const { return m_key; }
144 
146  value_type &value() const { return (*m_array)[m_key]; }
147 
149  value_type &operator*() const { return (*m_array)[m_key]; }
150 
153  return m_key == iter.m_key && m_array == iter.m_array;
154  }
155 
158  return !operator==(iter);
159  }
160 
163  OGDF_ASSERT(m_key != nullptr);
164  m_key = ArrayType::findSuccKey(m_key);
165  return *this;
166  }
167 
171  m_key = ArrayType::findSuccKey(m_key);
172  return iter;
173  }
174 
177  OGDF_ASSERT(m_key != nullptr);
178  m_key = ArrayType::findPredKey(m_key);
179  return *this;
180  }
181 
185  m_key = ArrayType::findPredKey(m_key);
186  return iter;
187  }
188 
189 private:
192 };
193 
194 }
195 }
key_type key() const
Index in m_array.
key_type m_key
Index in m_array.
GraphArrayIteratorBase< ArrayType, isConst > & operator++()
Increment operator (prefix).
GraphIteratorBase(GraphIteratorBase< GraphObjectPtr, isArgReverse > &it)
bool operator!=(const GraphIteratorBase< GraphObjectPtr, isReverse > &other) const
GraphArrayIteratorBase< ArrayType, isConst > operator++(int)
Increment operator (postfix).
GraphIteratorBase< GraphObjectPtr, isReverse > operator--(int)
Decrement operator (postfix).
value_type & value() const
Value of m_array at index m_key.
array_pointer_type m_array
The array.
Implementation of the Reverse class for the reverse iteration of containers.
bool operator!=(const GraphArrayIteratorBase< ArrayType, isConst > &iter) const
Inequality operator.
typename std::conditional< isConst, const ArrayType *, ArrayType * >::type array_pointer_type
Type of the array.
GraphIteratorBase< GraphObjectPtr, isReverse > & operator--()
Decrement operator (prefix).
GraphArrayIteratorBase(key_type key, array_pointer_type a)
Constructor.
value_type & operator*() const
Value of m_array at index m_key.
typename ArrayType::key_type key_type
Index type of the associated array.
GraphArrayIteratorBase< ArrayType, isConst > & operator--()
Decrement operator (prefix).
bool operator==(const GraphIteratorBase< GraphObjectPtr, isReverse > &other) const
GraphIteratorBase< GraphObjectPtr, isReverse > operator++(int)
Increment operator (postfix).
GraphArrayIteratorBase(const GraphArrayIteratorBase< ArrayType, isArgConst > &iter)
Constructor.
Basic declarations, included by all source files.
GraphIteratorBase(GraphObjectPtr ptr)
typename std::conditional< isConst, const typename ArrayType::value_type, typename ArrayType::value_type >::type value_type
Value type of the associated array.
bool operator==(const GraphArrayIteratorBase< ArrayType, isConst > &iter) const
Equality operator.
GraphArrayIteratorBase< ArrayType, isConst > operator--(int)
Decrement operator (postfix).
The namespace for all OGDF objects.
GraphArrayIteratorBase< ArrayType, isConst > & operator=(const GraphArrayIteratorBase< ArrayType, isConst > &iter)
Copy assignment operator.
GraphArrayIteratorBase(const GraphArrayIteratorBase< ArrayType, isConst > &iter)
Copy constructor.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
GraphIteratorBase< GraphObjectPtr, isReverse > & operator++()
Increment operator (prefix).