-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
489 lines (481 loc) · 25.9 KB
/
index.html
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Iterator pattern in STL</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/4.1.2/reveal.css" />
<link rel="stylesheet" href="night.css">
<link rel="stylesheet" href="tuning.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/4.1.2/plugin/highlight/monokai.min.css"
integrity="sha512-z8wQkuDRFwCBfoj7KOiu1MECaRVoXx6rZQWL21x0BsVVH7JkqCp1Otf39qve6CrCycOOL5o9vgfII5Smds23rg=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css"
integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Titillium+Web:200,300&subset=latin-ext" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Fira+Code" rel="stylesheet">
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<section>
<h2>The Iterator Pattern</h2>
<img src="img/presentation-logo.drawio.png" alt="">
<h3>in Standard Template Library</h3>
</section>
<section>
<h2>Scope & goals</h2>
<ul class="loose">
<li>Why was STL designed the way it was?</li>
<li class="fragment">C++ iterators walkthrough</li>
<li class="fragment">Custom iterators</li>
<li class="fragment">Some code modernization techniques</li>
<li class="fragment">The next big step in the STL evolution</li>
</ul>
</section>
</section>
<section>
<section>
<h2>STL's design principles</h2>
<hr>
<p>
<div class="small">Alexander Stepanov <a href="https://www.youtube.com/watch?v=COuHLky7E2Q">youtube.com/watch?v=COuHLky7E2Q</a></div>
<div class="small">Bjarne Stroustroup <a href="https://www.stroustrup.com/rules.pdf">stroustrup.com/rules.pdf</a></div>
<div class="small">Jonathan Boccara <a href="https://www.fluentcpp.com/2017/04/18/the-design-of-the-stl/">fluentcpp.com/2017/04/18/the-design-of-the-stl</a></div>
</p>
<hr>
<div>
<h3 class="fragment">Separation of algorithms from the data structures</h3>
<h3 class="fragment">Generality and Specialization</h3>
</div>
</section>
</section>
<section>
<section>
<h3>What's an iterator?</h3>
<ul class="loose">
<li>A container-traversal tool</li>
<li class="fragment">An object (with a specific API)</li>
<li class="fragment">Sort of a pointer to container's elements</li>
</ul>
</section>
<section>
<h3>What's so special about iterators?</h3>
<ul>
<li><span class="accent">Separation</span> of algorithms from the data structures</li>
<li class="fragment"><span class="accent">Universal traversal</span> method over a range of elements</li>
<li class="fragment">A single <span class="fira">std::find()</span> for all data structures instead of separate overloads for vector, map, set, deque,...</li>
<li class="fragment"><span class="accent">Loose coupling</span> between algorithms and containers</li>
<li class="fragment">Open-Closed Principle - extension of container's capabilities by new <span class="accent">generic</span> algorithms, not by modification of the containers themselves</li>
</ul>
</section>
<section>
<h3>Iterators-based approach</h3>
<ul>
<li>Iterators-based approach makes the code look simpler</li>
<li class="fragment">Works for empty containers, single value or any number of elements</li>
<li class="fragment">No need to handle corner cases separately</li>
<li class="fragment">Enables writing generic and very simple code</li>
<li class="fragment">It can even handle past-the-end operations without a special check</li>
</ul>
</section>
<section>
<img src="img/unified-reduce.png" alt="">
</section>
<section>
<img src="img/generic-reduce.png" class="w80">
</section>
<section>
<img src="img/remove-element.png" alt="">
</section>
<section>
<h3>Why iterators instead of containers?</h3>
<img src="img/std-sort-iterators.png" alt="">
<ul>
<li class="fragment">Flexibility</li>
<li class="fragment">Working with a subset of a container (range)</li>
<li class="fragment">Iterator adaptors & their composability</li>
<li class="fragment">Fewer algorithm overloads (find & reverse find)</li>
</ul>
</section>
<section>
<h2>Pitfall</h2>
<img src="img/iters-to-different-containers.png" alt="">
</section>
</section>
<section>
<section>
<h2>Pointer semantics</h2>
<h3>of a C++ iterator</h3>
</section>
<section>
<h2>Iterator dereference</h2>
<img src="img/iterator-dereference.png">
</section>
<section>
<h3>Dereferencing a <span class="accent">past-the-end</span> iterator is undefined behavior</h3>
<img src="img/past-the-end.png" alt="">
<a href="https://en.cppreference.com/w/cpp/container/vector/end">en.cppreference.com/w/cpp/container/vector/end</a>
</section>
<section>
<h2>Custom iterator dereference</h2>
<img src="img/iterator-dereference-struct.png" class="w80">
</section>
<section>
<h2>Iterator's <span class="fira">operator->()</span></h2>
<img src="img/iterator-arrow.png" alt="">
</section>
<section>
<h3>Custom iterator's <span class="fira">operator->()</span></h3>
<img src="img/iterator-arrow-struct.png" class="w80"><br>
<a class="small" href="https://en.cppreference.com/w/cpp/memory/addressof">why std::addressof()?</a>
</section>
<section>
<h3>Iterator incrementation/decrementation</h3>
<img src="img/iterator-increment-decrement.png" class="w80">
</section>
<section>
<h3>Custom iterator implementation</h3>
<img src="img/iterator-incrementation.png" class="w80">
</section>
<section>
<h3>Pointer semantics</h3>
<h5>makes STL algorithms applicable to containers but also to old-school arrays</h5>
<img src="img/pointer-semantics.png" alt="">
</section>
<section>
<h2>A C++ raw pointer</h2>
<p>meets all requirements of a</p>
<span class="fira accent big">RandomAccessIterator</span>
</section>
</section>
<section>
<section><h2>Iterator categories</h2></section>
<section>
<h2>Named requirements</h2>
<q class="accent">Written expectations about C++ types and objects</q>
<p class="fragment">Used in the C++ standard to state those expectations</p>
<p class="fragment">One requirement can be used to define another</p>
<p class="fragment">Usually paired with some helpers that express those expectations in the code</p>
<a href="https://en.cppreference.com/w/cpp/named_req" class="fragment">en.cppreference.com/w/cpp/named_req</a>
</section>
<section>
<h3><q>DefaultConstructible</q></h3>
<img src="img/default_constructible.png" class="w80">
</section>
<section>
<h3><q>Predicate</q></h3>
<img src="img/predicate.png" class="w90">
</section>
<section>
<h2>Iterator categories</h2>
<img src="img/iterator-categories.png" alt="">
</section>
<section>
<h2><span class="fira">LegacyIterator</span></h2>
<p>The base set of requirements for all iterators</p>
<ul class="fragment">
<li><i>CopyConstructible</i></li>
<li><i>CopyAssignable</i></li>
<li><i>Destructible</i></li>
<li><i>Swappable</i> - <span class="fira">std::swap, std::iter_swap</span></li>
<li><span class="fira accent">std::iterator_traits<Iterator></span></li>
<li><span class="fira accent">*iter</span> - dereferenceable (unspecified return value)</li>
<li><span class="fira accent">++iter</span> - defined and returns <span class="fira">Iterator&</span></li>
</ul>
</section>
<section>
<h3><span class="fira">std::iterator_traits<I></span></h3>
<img src="img/iterator-traits.png" alt="">
</section>
<section>
<h2><span class="fira">LegacyInputIterator</span></h2>
<ul>
<li><i>EqualityComparable</i></li>
<li class="fragment"><span class="fira accent">i != j</span> - valid expression</li>
<li class="fragment"><span class="fira accent">*i</span> returns <span class="fira accent">reference</span> from <span class="fira accent">iterator_traits</span></li>
<li class="fragment"><span class="fira accent">reference</span> might be a proxy object</li>
<li class="fragment"><span class="fira accent">operator->()</span> is defined</li>
<li class="fragment"><span class="fira accent">*it++</span> returns <span class="fira accent">value_type</span></li>
<li class="fragment">intended for single-pass algorithms - <span class="fira accent">std::find, std::count</span></li>
</ul>
</section>
<section>
<img src="img/input-iterator-single-pass.png">
</section>
<section>
<h3>Input iterators be like</h3>
<img src="https://www.meme-arsenal.com/memes/bda3710e8c0f496156f70ee6b76a6b36.jpg" class="w70">
<p class="src small">src: meme-arsenal.com/memes/bda3710e8c0f496156f70ee6b76a6b36.jpg</p>
</section>
<section>
<h3><span class="fira">LegacyForwardIterator</span></h3>
<ul>
<li><i>DefaultConstructible</i></li>
<li class="fragment"><span class="fira accent">std::iterator_traits<Iter>::reference</span> defined as <span class="fira"><span class="accent">T&</span> or <span class="accent">const T&</span></span></li>
<li class="fragment"><span class="fira accent">i++</span> returns <span class="fira accent">Iterator</span> (not <span class="fira accent">Iterator&</span> )</li>
<li class="fragment"><span class="fira accent">*i++</span> returns <span class="fira accent">reference</span></li>
<li class="fragment">Provides <i>multipass guarantee</i></li>
</ul>
</section>
<section>
<h3>Multipass guarantee</h3>
<img src="img/multipass-guarantee.png" alt="">
</section>
<section>
<h3>Forward iterators be like</h3>
<img src="http://www.carbide-red.com/comp/double_drive.jpg" alt="">
<p class="src small">src: carbide-red.com/comp/double_drive.jpg</p>
</section>
<section>
<h3><span class="fira">LegacyBidirectionalIterator</span></h3>
<ul>
<li><span class="fira accent">--i</span> returns <span class="fira accent">Iterator&</span></li>
<li class="fragment"><span class="fira accent">i--</span> return value convertible to <span class="fira accent">const Iterator&</span></li>
<li class="fragment"><span class="fira accent">*i--</span> returns <span class="fira accent">reference</span></li>
</ul>
<br><br>
<p class="fragment">Used by <span class="fira accent">std::map</span> and <span class="fira accent">std::set</span></p>
<p class="fragment">But also <span class="fira accent">std::reverse_copy</span> and <span class="fira accent">std::random_shuffle</span></p>
</section>
<section>
<h3><span class="fira">LegacyRandomAccessIterator</span></h3>
<ul>
<li>both <span class="fira accent">{i += n}, {i -= n}</span> return <span class="fira accent">Iterator&</span></li>
<li class="fragment">The complexity of the expressions above is <span class="fira accent">O(1)</span></li>
<li class="fragment">both <span class="fira accent">{i + n}, {i - n}</span> return <span class="fira accent">Iterator</span></li>
<li class="fragment"><span class="fira accent">i - j</span> returns <span class="fira accent">difference_type</span></li>
<li class="fragment"><span class="fira accent">i[n]</span> returns <span class="fira accent">reference</span></li>
<li class="fragment">all comparison operators are defined</li>
</ul>
<br><br>
<p class="fragment">Used by <span class="fira accent">std::vector</span></p>
</section>
</section>
<section>
<section>
<h2>Iterator tags</h2>
<img src="img/iterator-tags.png" alt=""><br><br>
<img src="img/custom-iterator-category.png" class="fragment">
</section>
<section>
<h2>Tag dispatch</h2>
<img src="img/advance-tag-dispatch.png" alt="">
</section>
<section>
<h3><span class="fira">std::advance() with <span class="accent">input_iterator_tag</span></span></h3>
<img src="img/advance-input-iterator.png" alt="">
</section>
<section>
<h3><span class="fira">std::advance() with <span class="accent">bidirectional_iterator_tag</span></span></h3>
<img src="img/advance-bidirectional-iterator.png" alt="">
</section>
<section>
<h3><span class="fira">std::advance() with <span class="accent">random_access_iterator_tag</span></span></h3>
<img src="img/advance-random-access-iterator.png" alt="">
</section>
<section>
<h3>Iterator Concepts</h3>
<img src="img/iterator-concepts.png" class="w80">
</section>
<section>
<h3>ForwardIterator concept</h3>
<img src="img/fwd-iter-concept.png" class="w80">
</section>
</section>
<section>
<section>
<h3>Iterator related special functions</h3>
<ul>
<li>advance</li>
<li>next</li>
<li>distance</li>
<li>begin & end</li>
</ul>
</section>
<section>
<h3 class="fira"><span class="fira">std::advance</span></h3>
</section>
<section>
<h3><span class="fira">std::next</span></h3>
<img src="img/std-next.png" alt="">
</section>
<section>
<h3 class="fira"><span class="fira">std::distance</span></h3>
<img src="img/std-distance.png" alt="">
</section>
<section>
<h3><span class="fira">std::begin()</span> vs <span class="fira">obj.begin()</span></h3>
<img src="img/begin-end-error.png" class="w70">
</section>
<section>
<h3>ADL</h3>
<img src="img/begin-end-ok.png" class="w80">
</section>
<section>
<h3>The curious case of <span class="fira accent">std::vector<bool></span></h3>
<img src="img/vector-of-bool.png" class="w80">
</section>
<section>
<h3><span class="fira accent">std::basic_string</span> to the rescue</h3>
<img src="img/string-of-bool.png" alt="">
</section>
</section>
<section>
<section>
<h2>Iterator adaptors</h2>
</section>
<section>
<h2>The decorator pattern</h2>
<a href="https://refactoring.guru/design-patterns/decorator">refactoring.guru/design-patterns/decorator</a><br><br>
<ul class="loose">
<li class="fragment">Preserves the interface</li>
<li class="fragment">Composable</li>
</ul>
</section>
<section>
<h3>C++ iterator adaptors</h3>
</section>
<section>
<h3>Simple printout</h3>
<img src="img/adaptors-print-simple.png" alt="">
</section>
<section>
<h3>Squared printout</h3>
<img src="img/adaptors-print-squared.png" alt="">
</section>
<section>
<h3><span class="fira">struct SquareIterAdaptor</span></h3>
<img src="img/square-iter-adaptor.png" alt="">
</section>
<section>
<h3>Squared printout with for-each</h3>
<img src="img/adaptors-print-squared2.png" class="w80">
</section>
<section>
<h3><span class="fira">struct SquareElements</span></h3>
<img src="img/square-elements-struct.png" alt="">
</section>
<section>
<h2>Composition of adaptors</h2>
</section>
<section>
<h3>Squared printout in reverse</h3>
<img src="img/adaptors-print-squared3.png" alt="">
</section>
<section>
<h3><span class="fira">struct ReverseOrder</span></h3>
<img src="img/reverse-order-struct.png" alt="">
</section>
</section>
<section>
<section>
<h3><span class="fira accent">#include <ranges></span></h3>
<img src="img/ranges-basic.png" class="w80">
</section>
<section>
<h3>The next level - range adaptors</h3>
<img src="img/ranges-pipeline.png">
</section>
<section>
<h3>More about ranges</h3>
<a href="https://hannes.hauswedell.net/post/2019/11/30/range_intro/">hannes.hauswedell.net/post/2019/11/30/range_intro</a><br><br>
<a href="https://www.modernescpp.com/index.php/c-20-the-ranges-library">modernescpp.com/index.php/c-20-the-ranges-library</a><br>
<p class="small">C++20 Ranges in Practice - Tristan Brindle</p>
<a href="https://www.youtube.com/watch?v=d_E-VLyUnzc">youtube.com/watch?v=d_E-VLyUnzc</a><br>
<p class="small">From STL to Ranges: Using Ranges Effectively - Jeff Garland</p>
<a href="https://www.youtube.com/watch?v=vJ290qlAbbw">youtube.com/watch?v=vJ290qlAbbw</a><br><br>
<a href="https://github.com/ericniebler/range-v3">github.com/ericniebler/range-v3</a>
</section>
</section>
<section>
<section>
<h2 class="fira">std::span</h2>
</section>
<section>
<h3>Legacy approach</h3>
<img src="img/non-span.png" class="w80">
</section>
<section>
<h3><span class="fira">std::span</span> approach</h3>
<img src="img/span-reduce.png" class="w80">
</section>
<section>
<h3><span class="fira">std::span</span> approach #2</h3>
<img src="img/span-reduce-2.png" alt="">
</section>
</section>
<section>
<section>
<h2>MaxPool</h2>
<img src="img/max_pool_animation.gif" alt="">
</section>
<section>
<h3>MaxPool algorithm</h3>
<ul class="loose">
<li>For each output tensor element...</li>
<li>Calculate the kernel position (top-left corner)</li>
<li>Iterate over the kernel elements row-by-row</li>
<li>Select the element with the maximum value</li>
<li>Requires transformation of an element's 2D coordinate to an address in the flat tensor</li>
</ul>
</section>
<section>
<h3>Kernel position calculation</h3>
<img src="img/kernel-pos-calculation.png" alt="">
</section>
<section>
<h3>Kernel position calculation</h3>
<h4>(with padding)</h4>
<img src="img/kernel-pos-calculation-padding.png" alt="">
</section>
<section>
<h3>Kernel position calculation</h3>
<h4>(with padding, strides and dilations)</h4>
<img src="img/kernel-pos-calculation-strides-dilations.png" alt="">
</section>
<section>
<h2>MaxPool</h2>
<img src="img/max_pool_animation.gif" alt="">
<h4 class="fragment">(ugly) code time</h4>
</section>
<section>
<img src="img/no-raw-loops.png" alt="">
<h2 class="fragment"><span class="fira accent">std::max_element</span></h2>
</section>
<section>
<img src="img/max-pool-kernel.png" alt="">
</section>
<section>
<img src="img/max-pool-kernel-iterator.png" alt="">
</section>
<section>
<h2><span class="fira accent">Kernel::end()</span></h2>
<img src="img/kernel-iterator-end.png" alt="">
</section>
<section>
<h3>MaxPool</h3>
<img src="img/mp-transform.png" alt="">
<h3 class="fragment accent"><span class="fira">std::transform std::max_element</span></h3>
</section>
</section>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/4.1.2/reveal.min.js"
integrity="sha512-K7P1+dtPriNNHlE4aJr+JKx1X6R0wvy24QBqL2CxaHc4XdkQjrH2t2FCrgoxZGMh6s1TgigNLEdrWa6NJra6Zg=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/4.1.2/plugin/highlight/highlight.min.js"
integrity="sha512-NVdqCvsrfhXUYzGTEGGBFf5jxheVVcglfdvO8r+WdG0BKMrRYvaiCGIC0S+wf4hkE3Xq43gTd1k3Mwl/UNgdvw=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
Reveal.initialize({
controls: false,
slideNumber: 'c/t',
transition: 'zoom',
plugins: [RevealHighlight]
});
</script>
</body>
</html>