Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1779640

Browse files
Merge pull request #41 from justinmeiners/9-rearrangement
Rearrange theory and application in chapter 9.
2 parents f5f6646 + 375ad27 commit 1779640

File tree

3 files changed

+101
-101
lines changed

3 files changed

+101
-101
lines changed

‎09_iterators.html‎

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ <h2>History of iterators</h2>
140140
and for all.</p>
141141

142142
<p>Let me tell you a little about them.
143-
Anybody who programs in C++ is forced to use vector
143+
Anybody who programs in C++ is forced to use <code>std::vector</code>
144144
and those have iterators.
145145
But, there are lots of people who do not quite understand what they are,
146146
partially because iterators are called iterators.
@@ -157,12 +157,8 @@ <h2>History of iterators</h2>
157157
But, many people at <a href="https://en.wikipedia.org/wiki/Carnegie_Mellon_University">CMU</a> got tenure because of it.
158158
It has some interesting ideas, including the idea of a generator.
159159
For those of you who know <a href="https://en.wikipedia.org/wiki/Python_(programming_language)">Python</a>, it is like an iterator in Python<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>.
160-
Barbara Liskov said, wouldn&rsquo;t it be nice to write:</p>
161-
162-
<pre><code>for x in thing
163-
</code></pre>
164-
165-
<p>and iterators allow you to do that<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup>.
160+
Barbara Liskov said, &ldquo;wouldn&rsquo;t it be nice to write something like: <code>for x in thing</code>&rdquo;.
161+
Iterators allow you to do that<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup>.
166162
It is like a generator in Alphard.
167163
It is a procedure which returns multiple values, one by one.
168164
It&rsquo;s a procedural abstraction.
@@ -173,12 +169,12 @@ <h2>History of iterators</h2>
173169
<p>At the same time, I was working on how to do algorithms and I
174170
introduced the notion of position.
175171
A better name is coordinate, the name which Paul and I use
176-
in our book &ldquo;Elements of Programming&rdquo;<supid="fnref:3"><ahref="#fn:3" rel="footnote">3</a></sup>.
172+
in our book &ldquo;Elements of Programming&rdquo;.
177173
A coordinate is some way of indicating where in the data structure you are.
178174
It is not a control structure, it&rsquo;s just the pointer into a data structure,
179175
or generalized notion of a coordinate.
180176
It is something which allows me to navigate through the data structure in a
181-
natural way.</p>
177+
natural way<supid="fnref:3"><ahref="#fn:3" rel="footnote">3</a></sup>.</p>
182178

183179
<p>Eventually I started talking to C++ guys and showed them coordinates
184180
and they said, &ldquo;we call it iterator&rdquo;.
@@ -209,8 +205,8 @@ <h2>History of iterators</h2>
209205
Maybe I was stupid,
210206
but I wasn&rsquo;t ignorant.</p>
211207

212-
<a name="List-pool-iterators"></a>
213-
<h2>List pool iterators</h2>
208+
<a name="Affiliated-types-for-iterators"></a>
209+
<h2>Affiliated types for iterators</h2>
214210

215211
<p>We always need to distinguish between how we type
216212
code in C++ and what the notion is behind it.
@@ -224,18 +220,18 @@ <h2>List pool iterators</h2>
224220
gives me a pointer type, I want a way to find out what type
225221
it points to.</p>
226222

227-
<p>To do this we need this notion of <em>type functions</em> which accept one type
223+
<p>To do this we need this notion of <strong>type functions</strong> which accept one type
228224
and return a different type.
229225
This problem of needing to obtain affiliated types is not specific to C and C++.
230226
It appears in Java and Python as well.
231227
In spite of Python&rsquo;s <a href="https://en.wikipedia.org/wiki/Duck_typing">duck typing</a> there&rsquo;s <em>still</em> a connection
232228
between types, even if they are duck types.</p>
233229

234-
<p>So we need this notion of <em>type functions</em>,
230+
<p>So we need this notion of type functions,
235231
but C doesn&rsquo;t let us do this,
236232
and neither does C++.
237233
Instead of type functions we are going to solve
238-
this problem for iterators by using typedefs.</p>
234+
this problem for iterators by using <code>typedef</code>s.</p>
239235

240236
<p>For an iterator, there are <a href="https://en.cppreference.com/w/cpp/iterator/iterator_traits">5 types</a> that we always need to define.
241237
Three of them are primary, two are secondary.
@@ -257,32 +253,12 @@ <h2>List pool iterators</h2>
257253

258254
<p> In C++ (without concepts) we use tag types to designate the iterator categories.
259255
Every iterator uses a tag type to denote which theory it supports.
260-
The tag lets you do compile time dispatch<sup id="fnref:5"><a href="#fn:5" rel="footnote">5</a></sup>.</p>
261-
262-
<p> What category is the iterator for <code>list_pool</code> from last chapter?
263-
We need <code>ForwardIterator</code>, because there is no way in a singly linked list
264-
to go backwards.</p></li>
256+
The tag lets you do compile time dispatch<sup id="fnref:5"><a href="#fn:5" rel="footnote">5</a></sup>.</p></li>
265257
</ol>
266258

267259

268-
<p>Let&rsquo;s define these types in the <code>list_pool</code> iterator.</p>
269-
270-
<pre><code>#include &lt;iterator&gt;
271-
272-
// class list_pool {
273-
// ...
274-
275-
struct iterator {
276-
typedef list_pool::value_type value_type;
277-
typedef list_pool::list_type difference_type;
278-
typedef std::forward_iterator_tag iterator_category;
279-
};
280-
281-
// };
282-
</code></pre>
283-
284-
<a name="Iterator-reference-types"></a>
285-
<h3>Iterator reference types</h3>
260+
<a name="Historical-artifacts"></a>
261+
<h3>Historical artifacts</h3>
286262

287263
<p>The fourth and fifth types to be defined are required only for historical reasons.
288264
Microsoft said they were going to vote against STL unless it accommodated
@@ -317,15 +293,33 @@ <h3>Iterator reference types</h3>
317293
<p>&emsp;4. <code>reference</code>: the type of a reference to the value. <br/>
318294
&emsp;5. <code>pointer</code>: the type of a pointer to the value.</p>
319295

320-
<p>Here is our implementation:</p>
321-
322-
<pre><code>typedef value_type&amp; reference;
323-
typedef value_type* pointer;
324-
</code></pre>
325-
326296
<p>It&rsquo;s not particularly harmful, but it obfuscates things
327297
and it provides &ldquo;language experts&rdquo; with steady employment.</p>
328298

299+
<a name="List-pool-iterators"></a>
300+
<h2>List pool iterators</h2>
301+
302+
<p>Let&rsquo;s define these types in the <code>list_pool</code> iterator.
303+
What category is the iterator for <code>list_pool</code> from last chapter?
304+
We need <code>ForwardIterator</code>, because there is no way in a singly linked list
305+
to go backwards.</p>
306+
307+
<pre><code>#include &lt;iterator&gt;
308+
309+
// class list_pool {
310+
// ...
311+
312+
struct iterator {
313+
typedef list_pool::value_type value_type;
314+
typedef list_pool::list_type difference_type;
315+
typedef std::forward_iterator_tag iterator_category;
316+
typedef value_type&amp; reference;
317+
typedef value_type* pointer;
318+
};
319+
320+
// };
321+
</code></pre>
322+
329323
<a name="Constructors"></a>
330324
<h3>Constructors</h3>
331325

@@ -528,7 +522,7 @@ <h3>Everything on a computer is totally ordered</h3>
528522
but it establishes a total ordering.
529523
A total ordering does not have to be topologically induced by the traversal.</p>
530524

531-
<p><strong>Exercise:</strong> extend the iterator to linked iterator
525+
<p><strong>Exercise:</strong> Extend the iterator to linked iterator
532526
so we can assign the next on a node.
533527
Specifically we want to be able to modify the successor of
534528
an iterator.</p>
@@ -576,9 +570,13 @@ <h2>Code</h2>
576570
<li id="fn:2">
577571
See this <a href="http://web.mit.edu/ghudson/info/iterators">brief description of CLU iterators</a>.<a href="#fnref:2" rev="footnote">&#8617;</a></li>
578572
<li id="fn:3">
579-
See chapter 7 of &ldquo;Elements of Programming&rdquo; on coordinate structures.<a href="#fnref:3" rev="footnote">&#8617;</a></li>
573+
See chapter 7 of &ldquo;Elements of Programming&rdquo; on coordinate structures.
574+
An interesting discussion on the general idea of &ldquo;coordinatisation&rdquo;
575+
is found in chapter 1 of &ldquo;Basic Notions of Algebra&rdquo; by Shafarevich.<a href="#fnref:3" rev="footnote">&#8617;</a></li>
580576
<li id="fn:4">
581-
<a href="https://www.boost.org/">Boost</a> is a popular collection of C++ libraries generally accepted as the next tool to reach for beyond the standard library.
577+
<a href="https://www.boost.org/">Boost</a> is a popular collection of C++ libraries, covering a wide range of uses,
578+
generally accepted as the next tool to reach for beyond the standard library.
579+
Many standard library features, such as smart pointers, were initially developed in Boost.
582580
Alex speaks positively of some parts (see <a href="http://stepanovpapers.com/siekforeword.pdf">his foreword</a> for &ldquo;The Boost Graph Library&rdquo;), but others he is more critical of.<a href="#fnref:4" rev="footnote">&#8617;</a></li>
583581
<li id="fn:5">
584582
Some algorithms can be implemented more efficiently for certain

‎09_iterators.md‎

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ other algorithms and learn to write iterators right once
1010
and for all.
1111

1212
Let me tell you a little about them.
13-
Anybody who programs in C++ is forced to use vector
13+
Anybody who programs in C++ is forced to use `std::vector`
1414
and those have iterators.
1515
But, there are lots of people who do not quite understand what they are,
1616
partially because iterators are called iterators.
@@ -27,11 +27,8 @@ It's a mythical language. It was never implemented.
2727
But, many people at [CMU][cmu] got tenure because of it.
2828
It has some interesting ideas, including the idea of a generator.
2929
For those of you who know [Python][python], it is like an iterator in Python[^python-iterator].
30-
Barbara Liskov said, wouldn't it be nice to write:
31-
32-
for x in thing
33-
34-
and iterators allow you to do that[^clu-iterators].
30+
Barbara Liskov said, "wouldn't it be nice to write something like: `for x in thing`".
31+
Iterators allow you to do that[^clu-iterators].
3532
It is like a generator in Alphard.
3633
It is a procedure which returns multiple values, one by one.
3734
It's a procedural abstraction.
@@ -42,12 +39,12 @@ It was a generalization of a control structure.
4239
At the same time, I was working on how to do algorithms and I
4340
introduced the notion of position.
4441
A better name is coordinate, the name which Paul and I use
45-
in our book "Elements of Programming"[^eop].
42+
in our book "Elements of Programming".
4643
A coordinate is some way of indicating where in the data structure you are.
4744
It is not a control structure, it's just the pointer into a data structure,
4845
or generalized notion of a coordinate.
4946
It is something which allows me to navigate through the data structure in a
50-
natural way.
47+
natural way[^coordinate-references].
5148

5249
Eventually I started talking to C++ guys and showed them coordinates
5350
and they said, "we call it iterator".
@@ -78,9 +75,13 @@ It wasn't out of ignorance.
7875
Maybe I was stupid,
7976
but I wasn't ignorant.
8077

81-
[^eop]: See chapter 7 of "Elements of Programming" on coordinate structures.
78+
[^coordinate-references]: See chapter 7 of "Elements of Programming" on coordinate structures.
79+
An interesting discussion on the general idea of "coordinatisation"
80+
is found in chapter 1 of "Basic Notions of Algebra" by Shafarevich.
8281

83-
[^boost]: [Boost][boost] is a popular collection of C++ libraries generally accepted as the next tool to reach for beyond the standard library.
82+
[^boost]: [Boost][boost] is a popular collection of C++ libraries, covering a wide range of uses,
83+
generally accepted as the next tool to reach for beyond the standard library.
84+
Many standard library features, such as smart pointers, were initially developed in Boost.
8485
Alex speaks positively of some parts (see [his foreword][alex-graph-foreword] for "The Boost Graph Library"), but others he is more critical of.
8586

8687

@@ -125,7 +126,8 @@ but I wasn't ignorant.
125126
[snyder]: https://dblp.org/pid/04/4444.html
126127
[alex-graph-foreword]: http://stepanovpapers.com/siekforeword.pdf
127128

128-
## List pool iterators
129+
130+
## Affiliated types for iterators
129131

130132
We always need to distinguish between how we type
131133
code in C++ and what the notion is behind it.
@@ -139,18 +141,18 @@ way to obtain `int` from `int*`. That is, if somebody
139141
gives me a pointer type, I want a way to find out what type
140142
it points to.
141143

142-
To do this we need this notion of *type functions* which accept one type
144+
To do this we need this notion of **type functions** which accept one type
143145
and return a different type.
144146
This problem of needing to obtain affiliated types is not specific to C and C++.
145147
It appears in Java and Python as well.
146148
In spite of Python's [duck typing][duck] there's *still* a connection
147149
between types, even if they are duck types.
148150

149-
So we need this notion of *type functions*,
151+
So we need this notion of type functions,
150152
but C doesn't let us do this,
151153
and neither does C++.
152154
Instead of type functions we are going to solve
153-
this problem for iterators by using typedefs.
155+
this problem for iterators by using `typedef`s.
154156

155157
For an iterator, there are [5 types][cpp-iterator-traits] that we always need to define.
156158
Three of them are primary, two are secondary.
@@ -175,40 +177,7 @@ Let's start with the primary:
175177
Every iterator uses a tag type to denote which theory it supports.
176178
The tag lets you do compile time dispatch[^compile-time-dispatch].
177179

178-
What category is the iterator for `list_pool` from last chapter?
179-
We need `ForwardIterator`, because there is no way in a singly linked list
180-
to go backwards.
181-
182-
Let's define these types in the `list_pool` iterator.
183-
184-
#include <iterator>
185-
186-
// class list_pool {
187-
// ...
188-
189-
struct iterator {
190-
typedef list_pool::value_type value_type;
191-
typedef list_pool::list_type difference_type;
192-
typedef std::forward_iterator_tag iterator_category;
193-
};
194-
195-
// };
196-
197-
198-
[^compile-time-dispatch]: Some algorithms can be implemented more efficiently for certain
199-
iterator categories. For example [`std::distance`][std-distance] can be
200-
implemented as a constant time algorithm for `RandomAccessIterators` but
201-
only a linear time algorithm for other iterator categories. The
202-
`iterator_category` tag allows the appropriate algorithm to be selected at
203-
compile time. This technique is known as [tag disptach][tag-dispatch].
204-
205-
[duck]: https://en.wikipedia.org/wiki/Duck_typing
206-
[ptrdiff]: https://en.cppreference.com/w/c/types/ptrdiff_t
207-
[cpp-iterator-traits]: https://en.cppreference.com/w/cpp/iterator/iterator_traits
208-
[std-distance]: https://en.cppreference.com/w/cpp/iterator/distance
209-
[tag-dispatch]: https://quuxplusone.github.io/blog/2021/06/07/tag-dispatch-and-concept-overloading
210-
211-
### Iterator reference types
180+
### Historical artifacts
212181

213182
The fourth and fifth types to be defined are required only for historical reasons.
214183
Microsoft said they were going to vote against STL unless it accommodated
@@ -243,22 +212,51 @@ So now we need to provide it.
243212
&emsp;4. `reference`: the type of a reference to the value. <br/>
244213
&emsp;5. `pointer`: the type of a pointer to the value.
245214

246-
Here is our implementation:
247-
248-
typedef value_type& reference;
249-
typedef value_type* pointer;
250-
251215
It's not particularly harmful, but it obfuscates things
252216
and it provides "language experts" with steady employment.
253217

254218
[^ms-dos-pointers]: See ["A look back at memory models in 16-bit MS-DOS"][far-ptr-article]
255219
for a brief overview of these various pointer types.
256220
The more general concept behind them is [memory segmentation][memory-segmentation].
257221

222+
[^compile-time-dispatch]: Some algorithms can be implemented more efficiently for certain
223+
iterator categories. For example [`std::distance`][std-distance] can be
224+
implemented as a constant time algorithm for `RandomAccessIterators` but
225+
only a linear time algorithm for other iterator categories. The
226+
`iterator_category` tag allows the appropriate algorithm to be selected at
227+
compile time. This technique is known as [tag disptach][tag-dispatch].
228+
229+
[duck]: https://en.wikipedia.org/wiki/Duck_typing
230+
[ptrdiff]: https://en.cppreference.com/w/c/types/ptrdiff_t
231+
[cpp-iterator-traits]: https://en.cppreference.com/w/cpp/iterator/iterator_traits
232+
[std-distance]: https://en.cppreference.com/w/cpp/iterator/distance
233+
[tag-dispatch]: https://quuxplusone.github.io/blog/2021/06/07/tag-dispatch-and-concept-overloading
258234
[lvalue]: https://en.wikipedia.org/wiki/Value_(computer_science)#lrvalue
259235
[far-ptr-article]: https://devblogs.microsoft.com/oldnewthing/20200728-00/?p=104012
260236
[memory-segmentation]: https://en.wikipedia.org/wiki/Memory_segmentation
261237

238+
## List pool iterators
239+
240+
Let's define these types in the `list_pool` iterator.
241+
What category is the iterator for `list_pool` from last chapter?
242+
We need `ForwardIterator`, because there is no way in a singly linked list
243+
to go backwards.
244+
245+
#include <iterator>
246+
247+
// class list_pool {
248+
// ...
249+
250+
struct iterator {
251+
typedef list_pool::value_type value_type;
252+
typedef list_pool::list_type difference_type;
253+
typedef std::forward_iterator_tag iterator_category;
254+
typedef value_type& reference;
255+
typedef value_type* pointer;
256+
};
257+
258+
// };
259+
262260
### Constructors
263261

264262
Let's write constructors for our iterator:
@@ -472,7 +470,7 @@ A total ordering does not have to be topologically induced by the traversal.
472470

473471
[lex]: https://en.wikipedia.org/wiki/Lexicographic_order
474472

475-
**Exercise:** extend the iterator to linked iterator
473+
**Exercise:** Extend the iterator to linked iterator
476474
so we can assign the next on a node.
477475
Specifically we want to be able to modify the successor of
478476
an iterator.

‎index.html‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,13 @@
330330
<li><a href="09_iterators.html#L9.-Iterators">9. Iterators</a>
331331
<ul>
332332
<li><a href="09_iterators.html#History-of-iterators">History of iterators</a></li>
333+
<li><a href="09_iterators.html#Affiliated-types-for-iterators">Affiliated types for iterators</a>
334+
<ul>
335+
<li><a href="09_iterators.html#Historical-artifacts">Historical artifacts</a></li>
336+
</ul>
337+
</li>
333338
<li><a href="09_iterators.html#List-pool-iterators">List pool iterators</a>
334339
<ul>
335-
<li><a href="09_iterators.html#Iterator-reference-types">Iterator reference types</a></li>
336340
<li><a href="09_iterators.html#Constructors">Constructors</a></li>
337341
<li><a href="09_iterators.html#Dereference">Dereference</a></li>
338342
<li><a href="09_iterators.html#Pre-2d-increment-2c--post-2d-increment">Pre-increment, post-increment</a></li>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /