Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
list_templates.h
Go to the documentation of this file.
1
33#pragma once
34
35#include <ogdf/basic/Array.h>
36
37#include <functional>
38
39namespace ogdf {
40
48template<typename CONTAINER>
50 std::function<void(typename CONTAINER::value_type)> func) {
51 for (auto it = container.begin(); it != container.end();) {
52 auto prev = it++;
53 func(*prev);
54 }
55}
56
63template<typename CONTAINER>
65 std::function<bool(typename CONTAINER::value_type)> func) {
66 for (auto it = container.begin(); it != container.end();) {
67 auto prev = it++;
68 if (!func(*prev)) {
69 return false;
70 }
71 }
72 return true;
73}
74
75// sorts list L using quicksort
76template<class LIST>
79 quicksortTemplate(L, comparer);
80}
81
82// sorts list L using quicksort and compare element comp
83template<class LIST, class COMPARER>
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
93 A.quicksort(comp);
94
95 i = 0;
96 for (typename LIST::value_type& x : L) {
97 x = A[i++];
98 }
99}
100
101namespace internal {
102
108template<typename CONTAINER, typename TYPE, typename ITERATOR>
110 std::function<bool(const TYPE&)> includeElement) {
111 int nElements = 0;
112
113 for (const auto& e : container) {
114 nElements += includeElement(e) ? 1 : 0;
115 }
116
117 ITERATOR result = container.end();
118
119 if (nElements > 0) {
121 int elemCounter = 0;
122
123 for (ITERATOR it = container.begin(); result == container.end(); it++) {
124 if (includeElement(*it)) {
125 elemCounter++;
126
127 if (elemCounter == chosenElement) {
128 result = it;
129 }
130 }
131 }
132 }
133
134 return result;
135};
136
142template<typename CONTAINER, typename TYPE, typename ITERATOR>
144 std::function<bool(const TYPE&)> includeElement, int size) {
146
147 int i = 0;
148 for (ITERATOR it = container.begin(); it != container.end(); it++) {
149 other[i] = it;
150 i++;
151 }
152
153 other.permute();
154
155 ITERATOR result = container.end();
156
157 for (auto it : other) {
158 if (includeElement(*it)) {
159 result = it;
160 break;
161 }
162 }
163
164 return result;
165};
166
185template<typename CONTAINER, typename TYPE, typename ITERATOR>
187 bool isFastTest) {
188 ITERATOR result = container.begin();
189 int size = container.size();
190
191 if (size > 0) {
192 // let's try to pick *any* element
193 int index = randomNumber(0, size - 1);
194
195 for (int i = 0; i < index; i++) {
196 result++;
197 }
198
199 // the initially chosen element is not feasible?
200 if (!includeElement(*result)) {
201 if (isFastTest) {
204 } else {
206 includeElement, size);
207 }
208 }
209 }
210
211 return result;
212}
213
214
215}
216
218template<typename CONTAINER, typename TYPE>
219typename CONTAINER::iterator chooseIteratorFrom(
221 std::function<bool(const TYPE&)> includeElement = [](const TYPE&) { return true; },
222 bool isFastTest = true) {
223 return internal::chooseIteratorFrom<CONTAINER, TYPE, typename CONTAINER::iterator>(container,
225}
226
228template<typename CONTAINER, typename TYPE>
229typename CONTAINER::const_iterator chooseIteratorFrom(
230 const CONTAINER& container,
231 std::function<bool(const TYPE&)> includeElement = [](const TYPE&) { return true; },
232 bool isFastTest = true) {
233 return internal::chooseIteratorFrom<const CONTAINER, TYPE, typename CONTAINER::const_iterator>(
235}
236
237}
Declaration and implementation of Array class and Array algorithms.
The parameterized class Array implements dynamic arrays of type E.
Definition Array.h:214
int randomNumber(int low, int high)
Returns random integer between low and high (including).
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
ITERATOR chooseIteratorByFastTest(CONTAINER &container, std::function< bool(const TYPE &)> includeElement)
ITERATOR chooseIteratorFrom(CONTAINER &container, std::function< bool(const TYPE &)> includeElement, bool isFastTest)
Returns an iterator to a random element in the container.
ITERATOR chooseIteratorBySlowTest(CONTAINER &container, std::function< bool(const TYPE &)> includeElement, int size)
The namespace for all OGDF objects.
bool safeTestForEach(CONTAINER &container, std::function< bool(typename CONTAINER::value_type)> func)
Like ogdf::safeForEach() but aborts if func returns false.
void safeForEach(CONTAINER &container, std::function< void(typename CONTAINER::value_type)> func)
Calls (possibly destructive) func for each element of container.
CONTAINER::iterator chooseIteratorFrom(CONTAINER &container, std::function< bool(const TYPE &)> includeElement=[](const TYPE &) { return true;}, bool isFastTest=true)
Returns an iterator to a random element in the container.
void quicksortTemplate(LIST &L)