@@ -10,7 +10,7 @@ which would indicate what mood I'm in.
10
10
This is a very great song by [ Franz Schubert] [ schubert ] but it also
11
11
perfectly reflects what will go on with the course in how I feel.
12
12
The song is called [ "The Organ Grinder"] [ winter-journey ] (Der Leiermann).
13
- The singer is [ Dietrich fischer-dieskau ] [ dietrich ] maybe the greatest
13
+ The singer is [ Dietrich Fischer-Dieskau ] [ dietrich ] maybe the greatest
14
14
leader, or art song singer of the last 50, 60, or 70 years.
15
15
He started singing in the late forties.
16
16
Let us spend a couple of minutes and listen to it... ([ Video here] [ organ-grinder ] )
@@ -263,25 +263,23 @@ That's the invariant on which we rely.
263
263
264
264
### Rotate for bidirectional iterators
265
265
266
- Once we find where it goes, how do we make room for it?
266
+ Once we find where it goes (the element to insert) , how do we make room for it?
267
267
We "rotate" to the right by one.
268
- If it was a bidirectional iterator there is a beautiful algorithm.
269
- Copy is the wrong thing, because it will overwrite everything
270
- with the same value.
271
- What we want is copying from the back.
268
+ If it is a bidirectional iterator there is a beautiful algorithm, copying from the back.
272
269
The algorithm is called: [ ` std::copy_backward ` ] [ cpp-copy-back ] .
273
270
274
271
template <typename I>
275
272
// I is BidirectionalIterator
276
273
void rotate_right_by_one(I first, I last, std::bidirectional_iterator_tag) {
277
274
typedef typename std::iterator_traits<I>::value_type T;
278
- T butlast = last;
275
+ I butlast = last;
279
276
--butlast;
280
277
T x = *butlast;
281
278
std::copy_backward(first, butlast, last);
282
279
*first = x;
283
280
}
284
281
282
+ Note that forward copy is the wrong thing, because it will overwrite everything with the same value (namely the first value).
285
283
286
284
### Rotate for forward iterators
287
285
@@ -349,16 +347,16 @@ It might not be the fastest, but it is going to be much more elegant.
349
347
void rotate_right_by_one(I first, I last, std::forward_iterator_tag) {
350
348
if (first == last) return;
351
349
I current = first;
352
- while (++current != last) std::swap(first, current);
350
+ while (++current != last) std::swap(* first, * current);
353
351
}
354
352
355
353
Let's write a dispatch for both versions,
356
354
it will compile to no code[ ^ concepts-in-standard-soon ] .
357
355
358
356
template <typename I>
359
357
inline
360
- void rotate_right_by_one(I first, I butlast, I last) {
361
- rotate_right_by_one(first, butlast, last, typename std::iterator_traits<I>::iterator_category());
358
+ void rotate_right_by_one(I first, I last) {
359
+ rotate_right_by_one(first, last, typename std::iterator_traits<I>::iterator_category());
362
360
}
363
361
364
362
0 commit comments