You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 05_swap.html
+2-2
Original file line number
Diff line number
Diff line change
@@ -143,7 +143,7 @@ <h3>Three tests of a language’s ability to write components</h3>
143
143
<p>So you say, “Alex, why don’t we use a new language?”
144
144
Go try implementing these three program in your favorite language.
145
145
Do them in a general way.
146
-
If they’re at least relatively efficient, that is, they are not slower than specific things written in the language, then let us talk.
146
+
If they’re at least relatively efficient, that is, they are not slower than specific things written in the language, then let us talk.
147
147
If you cannot do it, let us stick with C++.
148
148
I’m just explaining the reasoning behind my choice of C++.</p>
149
149
@@ -396,7 +396,7 @@ <h2>Code</h2>
396
396
397
397
<ol>
398
398
<li><code>G</code> contains an identity element <code>e</code> in <code>G</code> such that <code>e * x = x * e = x</code> for all <code>x</code> in <code>G</code>.</li>
399
-
<li>The operation <code>*</code> is associative. So <code>((x * y) * z) = (x * (y * z))</code> for all <code>x, y, z</code> in <code>G</code>.</li>
399
+
<li>The operation <code>*</code> is associative. So <code>((x * y) * z) = (x * (y * z))</code> for all <code>x, y, z</code> in <code>G</code>.</li>
400
400
<li>Every element <code>x</code> in <code>G</code> has an inverse element <code>y</code> such that x * y = y * x = e.</li>
Copy file name to clipboardExpand all lines: 10_binary_counter.html
+11-11
Original file line number
Diff line number
Diff line change
@@ -228,7 +228,7 @@ <h2>Binary counting and reduction</h2>
228
228
But, they want to publish papers themselves and not tell you the general mechanism.</p>
229
229
230
230
<p>Let us assume we have elements of type <code>T</code> that need to be paired or combined in some way,
231
-
whether with <code>min</code>, <code>+</code>, <code>merge</code>, or any other assocative operation on <code>T</code>.
231
+
whether with <code>min</code>, <code>+</code>, <code>merge</code>, or any other associative operation on <code>T</code>.
232
232
What we can do is create an array called a “counter”.</p>
233
233
234
234
<pre><code> index: 0 1 ... 31
@@ -275,12 +275,12 @@ <h2>Binary counting and reduction</h2>
275
275
276
276
<p>What if the index <code>1</code> slot was non-zero, after comparing <code>x</code> and <code>y</code>?
277
277
Then the guy there already won one game.
278
-
So, we must <strong>carry propogate</strong><supid="fnref:10"><ahref="#fn:10" rel="footnote">10</a></sup>.
278
+
So, we must <strong>carry propagate</strong><supid="fnref:10"><ahref="#fn:10" rel="footnote">10</a></sup>.
279
279
Repeat the same process all the way up the counter, until we find a slot which is zero.
280
280
What if the counter is full, and has no zero slots?
281
281
That’s called an <strong>overflow</strong>.</p>
282
282
283
-
<p>We borrow terminology from binary integer arithemtic because
283
+
<p>We borrow terminology from binary integer arithmetic because
284
284
our counter works just like a binary integer counting up:</p>
285
285
286
286
<pre><code>0 0 0
@@ -293,7 +293,7 @@ <h2>Binary counting and reduction</h2>
293
293
1 1 1
294
294
</code></pre>
295
295
296
-
<p>But instead of 0 and 1 in each slot or “bit” we have arbitary elements that are combined with an associative operation.</p>
296
+
<p>But instead of 0 and 1 in each slot or “bit” we have arbitrary elements that are combined with an associative operation.</p>
297
297
298
298
<aname="Handling-overflow"></a>
299
299
<h3>Handling overflow</h3>
@@ -469,8 +469,8 @@ <h3>Counter storage</h3>
469
469
470
470
<p><strong>Exercise:</strong> Rewrite <code>min_element</code> using this code (just <code>min_element</code>, don’t worry about second best).</p>
471
471
472
-
<p><strong>Exercise:</strong>If you want to implement <ahref="https://en.wikipedia.org/wiki/Merge_sort">merge sort</a> you can use exactly the same device, since merge is associative. The idea with merge sort, is you only want to merge lists if they are roughly the same length and this helps you do it (see Chapter 12).
473
-
Write the associtative binary operation <code>merge</code> which can combine two sorted arrays into a sorted array.</p>
472
+
<p><strong>Exercise:</strong> If you want to implement <ahref="https://en.wikipedia.org/wiki/Merge_sort">merge sort</a> you can use exactly the same device, since merge is associative. The idea with merge sort, is you only want to merge lists if they are roughly the same length and this helps you do it (see Chapter 12).
473
+
Write the associative binary operation <code>merge</code> which can combine two sorted arrays into a sorted array.</p>
474
474
475
475
<p><strong>Exercise:</strong> When we become grownups we learn about advanced data structures, such as <ahref="https://en.wikipedia.org/wiki/Binomial_heap">binomial forest</a>. They use the same idea. Learn about this data structure and try to figure out where
476
476
the counter could be used.</p>
@@ -535,7 +535,7 @@ <h2>Code</h2>
535
535
See the definition of “in-place memory usage” at the end of the chapter.<ahref="#fnref:6" rev="footnote">↩</a></li>
536
536
<liid="fn:7">
537
537
<p>A binary function <code>f</code> is <ahref="https://en.wikipedia.org/wiki/Associative_property">associative</a>
538
-
if the the following holds for all <code>a, b, c</code> in its domain:</p>
538
+
if the following holds for all <code>a, b, c</code> in its domain:</p>
539
539
540
540
<pre><code>f(f(a, b), c)) = f(a, f(b, c))
541
541
</code></pre>
@@ -576,7 +576,7 @@ <h2>Code</h2>
576
576
* * *
577
577
</code></pre>
578
578
579
-
<p>Or as <ahref="https://en.wikipedia.org/wiki/Peter_Gustav_Lejeune_Dirichlet)">Dirchlet</a> put it: “Whether you arrange soldiers in rows or columns, you still have the same number of soldiers”.</p>
579
+
<p>Or as <ahref="https://en.wikipedia.org/wiki/Peter_Gustav_Lejeune_Dirichlet)">Dirichlet</a> put it: “Whether you arrange soldiers in rows or columns, you still have the same number of soldiers”.</p>
580
580
581
581
<p>An example of an operation which is not commutative is string concatenation.</p>
582
582
@@ -590,7 +590,7 @@ <h2>Code</h2>
590
590
We distinguish between the left and right argument.<ahref="#fnref:9" rev="footnote">↩</a></li>
591
591
<liid="fn:10">
592
592
<p>The terms <strong>carry</strong> and <strong>overflow</strong>
593
-
are closely associated the implementation of binary counting
593
+
are closely associated with the implementation of binary counting
594
594
or addition as an electrical circuit, called an <ahref="https://en.wikipedia.org/wiki/Adder_(electronics)">adder</a>.</p>
595
595
596
596
<p>A single bit adder has two inputs <code>a</code> and <code>b</code> for
@@ -611,7 +611,7 @@ <h2>Code</h2>
611
611
</code></pre>
612
612
613
613
<p>This adds a single digit, but we want to add entire numbers.
614
-
To, do so we add an additional input <code>l</code> for carried digits
614
+
To do so we add an additional input <code>l</code> for carried digits
615
615
from lower place values.</p>
616
616
617
617
<pre><code>a b l c s
@@ -640,7 +640,7 @@ <h2>Code</h2>
640
640
<ahref="https://signalvnoise.com/posts/2710-designed-by-apple-in-california">Designed in Cupertino</a>, assembled in China.
641
641
So let’s try to assemble our machine in Palo Alto.<ahref="#fnref:11" rev="footnote">↩</a></li>
642
642
<liid="fn:12">
643
-
<p>Floating point arithemetic can involve many tricky details,
643
+
<p>Floating point arithmetic can involve many tricky details,
644
644
but the basic issue Alex is referring to is straightforward.
645
645
We often use scientific notation to write larger numbers as a decimal to a power,
646
646
when they would otherwise be very long to write out.
0 commit comments