-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch25.ixx
371 lines (316 loc) · 8.98 KB
/
ch25.ixx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
export module ch25;
import directed_graph;
import directed_graph.to_dot;
import std;
export namespace ch25 {
namespace exercises {
namespace ex1 {
namespace myanswer {
using namespace std;
template <forward_iterator ForwardIterator,
output_iterator<iter_reference_t<ForwardIterator>> OutputIterator,
indirect_unary_predicate<ForwardIterator> Predicate,
invocable<iter_reference_t<ForwardIterator>> Transform>
OutputIterator tranform_if(ForwardIterator first, ForwardIterator last,
OutputIterator dest, Predicate pred, Transform func) {
while (first != last) {
if (std::invoke(pred, *first)) {
*dest = std::invoke(func, *first);
}
else {
*dest = *first;
}
++first;
++dest;
}
return dest;
}
void test() {
array<int, 20> arr;
iota(arr.begin(), arr.end(), 1);
vector<int> result;
auto odd{ [](int i) { return i % 2 != 0; } };
auto multiply_two{ [](int i) { return i * 2; } };
tranform_if(cbegin(arr), cend(arr), back_inserter(result), odd, multiply_two);
for (int i : result) { print("{} ", i); }
println("");
}
}
}
namespace ex2 {
namespace myanswer {
using namespace std;
template <ranges::input_range Range>
void generate_fibonacci(Range&& range)
{
int prev{ -1 };
int val{ 1 };
generate(std::ranges::begin(range), std::ranges::end(range),
[&prev, &val] {
auto temp{ val };
val = prev + val;
prev = temp;
return val;
});
}
void test() {
vector<int> vi(12);
generate_fibonacci(vi);
println("fibonacci");
for (const auto& i : vi) { print("{} ", i); }
}
}
}
namespace ex3 {
namespace myanswer {
using namespace ProCpp;
using namespace std;
namespace ProCpp
{
// Force all code to be compiled for testing.
template class directed_graph<string>;
}
void test()
{
directed_graph graph{ 11,22,33 };
graph.insert({ 44,55 });
vector moreNodes{ 66,77 };
graph.insert_range(moreNodes);
// Insert some edges.
graph.insert_edge(11, 33);
graph.insert_edge(22, 33);
graph.insert_edge(22, 44);
graph.insert_edge(22, 55);
graph.insert_edge(33, 44);
graph.insert_edge(44, 55);
// Try to insert a duplicate, and use structured bindings for the result.
auto [iter22, inserted] = graph.insert(22);
if (!inserted) { println("Duplicate element."); }
// Print nodes using a for loop and iterators.
for (auto iter{ graph.cbegin() }; iter != graph.cend(); ++iter)
{
print("{} ", *iter);
}
println("");
// Print nodes using a for loop and iterators retrieved with the non-member
// functions cbegin() and cend().
for (auto iter{ cbegin(graph) }; iter != cend(graph); ++iter)
{
print("{} ", *iter);
}
println("");
// Print nodes using a range-based for loop.
for (auto& node : graph)
{
print("{} ", node);
}
println("");
// Search a node using the find() Standard Library algorithm.
auto result{ find(begin(graph), end(graph), 22) };
if (result != end(graph))
{
println("Node 22 found.");
}
else
{
println("Node 22 NOT found.");
}
// ch25 ex3
{
println("ch25 ex3 test:");
auto result{ graph.find(22) };
if (result != end(graph))
{
println("Node 22 found.");
}
else
{
println("Node 22 NOT found.");
}
auto result2{ graph.find(8975) };
if (result2 != end(graph))
{
println("Node 8975 found.");
}
else
{
println("Node 8975 NOT found.");
}
}
// ch25 ex4
{
println("ch25 ex4 test:");
auto result{ graph.contains(22) };
println("Node 22 is{} found.", result ? "" : " not");
auto result2{ graph.contains(8975) };
println("Node 8975 is{} found.", result2 ? "" : " not");
}
// Count all nodes with values > 22.
auto count{ count_if(begin(graph), end(graph),
[](const auto& node) { return node > 22; }) };
println("{} nodes > 22", count);
// Use the iterator-based erase() member function in combination with find().
graph.erase(find(begin(graph), end(graph), 44));
// Print nodes in reverse order.
for (auto iter{ graph.rbegin() }; iter != graph.rend(); ++iter)
{
print("{} ", *iter);
}
println("");
// Test adjacency lists.
{
print("Adjacency list for node 22: ");
auto nodesAdjacentTo22{ graph.nodes_adjacent_to(22) };
if (!nodesAdjacentTo22.has_value())
{
println("Value 22 not found.");
}
else
{
for (const auto& node : *nodesAdjacentTo22)
{
print("{} ", node);
}
}
println("");
}
// Remove an edge.
graph.erase_edge(22, 44);
// Remove a node.
graph.erase(44);
graph.erase(find(begin(graph), end(graph), 44));
{
// Test begin()/end() non-const
print("Nodes: ");
for (auto iter{ graph.begin() }; iter != graph.end(); ++iter)
{
print("{} ", *iter);
}
println("");
}
{
// Test begin()/end() const
print("Nodes: ");
const auto& cgraph = graph;
for (auto iter{ cgraph.begin() }; iter != cgraph.end(); ++iter)
{
print("{} ", *iter);
}
println("");
}
{
// Test cbegin()/cend()
print("Nodes: ");
for (auto iter{ graph.cbegin() }; iter != graph.cend(); ++iter)
{
print("{} ", *iter);
}
println("");
}
{
// Test rbegin()/rend()
print("Nodes: ");
for (auto iter{ graph.rbegin() }; iter != graph.rend(); ++iter)
{
print("{} ", *iter);
}
println("");
}
println("{}", to_dot(graph, "Graph1"));
// Query information of a graph.
println("Size: {}", graph.size());
println("MaxSize: {}", graph.max_size());
println("IsEmpty?: {}", graph.empty());
// Test swapping graphs.
directed_graph<int> otherGraph;
swap(graph, otherGraph);
println("After swapping:");
println("Size: {}", graph.size());
println("MaxSize: {}", graph.max_size());
println("IsEmpty?: {}", graph.empty());
// Test copying graphs.
directed_graph<int> graphCopy{ otherGraph };
println("otherGraph == graphCopy? {}", (otherGraph == graphCopy));
graphCopy.erase(find(begin(graphCopy), end(graphCopy), 33));
println("otherGraph == graphCopy? {}", (otherGraph == graphCopy));
// Find a node using find(), and erase the node.
result = find(begin(graphCopy), end(graphCopy), 22);
if (result != end(graphCopy))
{
println("Node 22 found.");
auto next{ graphCopy.erase(result) };
if (next != end(graphCopy))
{
println("Next after erasing 22: {}", *next);
}
}
else
{
println("Node 22 NOT found.");
}
// Erase a range of nodes.
result = find(begin(graphCopy), end(graphCopy), 55);
auto result2{ find(begin(graphCopy), end(graphCopy), 33) };
auto next = graphCopy.erase(result, result2);
if (next != end(graphCopy))
{
println("Next after erasing range [55,33): {}", *next);
}
println("{}", to_dot(graphCopy, "Graph1"));
// Clear the graph.
graph.clear();
// Test equality
directed_graph<int> graph1;
graph1.insert(11);
graph1.insert(22);
graph1.insert(33);
graph1.insert_edge(11, 22);
graph1.insert_edge(11, 33);
graph1.insert_edge(22, 33);
directed_graph<int> graph2;
graph2.insert(22);
graph2.insert(11);
graph2.insert(33);
graph2.insert_edge(22, 33);
graph2.insert_edge(11, 22);
graph2.insert_edge(11, 33);
println("{}", to_dot(graph1, "graph1"));
println("{}", to_dot(graph2, "graph2"));
println("are equal: {}", (graph1 == graph2));
// Test assignment of graphs.
directed_graph<int> graph3;
graph3 = graph2;
println("{}", to_dot(graph3, "graph3"));
// Test a graph with strings.
directed_graph<string> graphStrings;
graphStrings.insert("String 1");
string str{ "String 2" };
graphStrings.insert(str);
graphStrings.insert("String 3");
graphStrings.insert("String 4");
graphStrings.insert_edge("String 1", str);
graphStrings.insert_edge("String 2", str);
graphStrings.insert_edge("String 3", str);
println("{}", to_dot(graphStrings, "String Graph"));
{
// Test rbegin()/rend()
print("Nodes: ");
for (auto iter{ graphStrings.rbegin() }; iter != graphStrings.rend(); ++iter)
{
print("{} ", *iter);
}
println("");
}
auto result3{ find(begin(graphStrings), end(graphStrings), "String 3") };
if (result3 != end(graphStrings)) {
println("Node String 3 found.");
}
else
{
println("Node String 3 NOT found.");
}
}
}
}
}
}