Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

list_templates.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/Array.h>
36 #include <functional>
37 
38 namespace ogdf {
39 
47 template<typename CONTAINER>
48 inline void safeForEach(CONTAINER &container, std::function<void(typename CONTAINER::value_type)> func) {
49  for (auto it = container.begin(); it != container.end();) {
50  auto prev = it++;
51  func(*prev);
52  }
53 }
54 
61 template<typename CONTAINER>
62 inline bool safeTestForEach(CONTAINER &container, std::function<bool(typename CONTAINER::value_type)> func) {
63  for (auto it = container.begin(); it != container.end();) {
64  auto prev = it++;
65  if (!func(*prev)) {
66  return false;
67  }
68  }
69  return true;
70 }
71 
72 // sorts list L using quicksort
73 template<class LIST>
74 void quicksortTemplate(LIST &L)
75 {
77  quicksortTemplate(L, comparer);
78 }
79 
80 
81 // sorts list L using quicksort and compare element comp
82 template<class LIST, class COMPARER>
83 void quicksortTemplate(LIST &L, const COMPARER &comp)
84 {
85  const int n = L.size();
87 
88  int i = 0;
89  for (const typename LIST::value_type &x : L)
90  A[i++] = x;
91 
92  A.quicksort(comp);
93 
94  i = 0;
95  for (typename LIST::value_type &x : L)
96  x = A[i++];
97 }
98 
99 namespace internal {
100 
106 template<typename CONTAINER, typename TYPE, typename ITERATOR>
108  CONTAINER &container,
109  std::function<bool(const TYPE &)> includeElement) {
110  int nElements = 0;
111 
112  for(const auto &e : container) {
113  nElements += includeElement(e) ? 1 : 0;
114  }
115 
116  ITERATOR result = container.end();
117 
118  if(nElements > 0) {
119  int chosenElement = randomNumber(1, nElements);
120  int elemCounter = 0;
121 
122  for (ITERATOR it = container.begin(); result == container.end(); it++) {
123  if(includeElement(*it)) {
124  elemCounter++;
125 
126  if(elemCounter == chosenElement) {
127  result = it;
128  }
129  }
130  }
131  }
132 
133  return result;
134 };
135 
141 template<typename CONTAINER, typename TYPE, typename ITERATOR>
143  CONTAINER &container,
144  std::function<bool(const TYPE &)> includeElement,
145  int size) {
146  Array<ITERATOR> other(size);
147 
148  int i = 0;
149  for (ITERATOR it = container.begin(); it != container.end(); it++) {
150  other[i] = it;
151  i++;
152  }
153 
154  other.permute();
155 
156  ITERATOR result = container.end();
157 
158  for (auto it : other) {
159  if (includeElement(*it)) {
160  result = it;
161  break;
162  }
163  }
164 
165  return result;
166 };
167 
168 
187 template<typename CONTAINER, typename TYPE, typename ITERATOR>
189  CONTAINER &container,
190  std::function<bool(const TYPE &)> includeElement,
191  bool isFastTest) {
192  ITERATOR result = container.begin();
193  int size = container.size();
194 
195  if (size > 0) {
196  // let's try to pick *any* element
197  int index = randomNumber(0, size - 1);
198 
199  for (int i = 0; i < index; i++) {
200  result++;
201  }
202 
203  // the initially chosen element is not feasible?
204  if (!includeElement(*result)) {
205  if(isFastTest) {
206  result = chooseIteratorByFastTest<CONTAINER, TYPE, ITERATOR>(container, includeElement);
207  } else {
208  result = chooseIteratorBySlowTest<CONTAINER, TYPE, ITERATOR>(container, includeElement, size);
209  }
210  }
211  }
212 
213  return result;
214 }
215 
216 
217 }
218 
220 template<typename CONTAINER, typename TYPE>
221 typename CONTAINER::iterator chooseIteratorFrom(
222  CONTAINER &container,
223  std::function<bool(const TYPE&)> includeElement = [](const TYPE&) { return true; },
224  bool isFastTest = true) {
226  <CONTAINER, TYPE, typename CONTAINER::iterator>(container, includeElement, isFastTest);
227 }
228 
230 template<typename CONTAINER, typename TYPE>
231 typename CONTAINER::const_iterator chooseIteratorFrom(
232  const CONTAINER &container,
233  std::function<bool(const TYPE&)> includeElement = [](const TYPE&) { return true; },
234  bool isFastTest = true) {
236  <const CONTAINER, TYPE, typename CONTAINER::const_iterator>(container, includeElement, isFastTest);
237 }
238 
239 }
ITERATOR chooseIteratorByFastTest(CONTAINER &container, std::function< bool(const TYPE &)> includeElement)
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:200
void quicksortTemplate(LIST &L)
bool safeTestForEach(CONTAINER &container, std::function< bool(typename CONTAINER::value_type)> func)
Like ogdf::safeForEach() but aborts if func returns false.
int randomNumber(int low, int high)
Returns random integer between low and high (including).
ITERATOR chooseIteratorFrom(CONTAINER &container, std::function< bool(const TYPE &)> includeElement, bool isFastTest)
Returns an iterator to a random element in the container.
Declaration and implementation of Array class and Array algorithms.
void permute(INDEX l, INDEX r)
Randomly permutes the subarray with index set [l..r].
Definition: Array.h:503
Standard comparer (valid as a static comparer).
Definition: comparer.h:58
ITERATOR chooseIteratorBySlowTest(CONTAINER &container, std::function< bool(const TYPE &)> includeElement, int size)
void quicksort()
Sorts array using Quicksort.
Definition: Array.h:613
void safeForEach(CONTAINER &container, std::function< void(typename CONTAINER::value_type)> func)
Calls (possibly destructive) func for each element of container.
The namespace for all OGDF objects.