-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch11.cpp
More file actions
110 lines (106 loc) · 4.24 KB
/
ch11.cpp
File metadata and controls
110 lines (106 loc) · 4.24 KB
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
// Copyright (c) 2014 Alexander A. Stepanov and Daniel E. Rose
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without
// fee, provided that the above copyright notice appear in all copies
// and that both that copyright notice and this permission notice
// appear in supporting documentation. The authors make no
// representations about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.
//
// This code accompanies the "fM2GP" book:
//
// From Mathematics to Generic Programming
// by Alexander Stepanov and Daniel E. Rose
// Addison-Wesley Professional, 2015
//
// -------------------------------------------------------------------
// ch11.cpp -- For testing functions from Chapter 11 of fM2GP.
// -------------------------------------------------------------------
#include <iostream>
#include <vector>
#include "ch11.h"
template <InputIterator I>
void print_range(I f, I l) {
while (f != l) std::cout << *f++ << " ";
std::cout << std::endl;
}
int main() {
std::vector<int> v {1, 2, 3, 4, 5, 6, 7};
auto x = begin(v);
auto y = end(v);
std::cout << "Initial sequence:\t\t\t\t\t";
print_range(x, y);
std::cout << "Setting x = begin(v); y = end(v)" << std::endl;
fmgp::swap_ranges(x, x + 3, x + 4);
std::cout << "After swap_ranges(x, x+3, x+4):\t\t\t\t";
print_range(x, y);
fmgp::swap_ranges(x, x + 3, x + 4);
std::cout << "After swap_ranges(x, x+3, x+4):\t\t\t\t";
print_range(x, y);
fmgp::swap_ranges(x, x + 4, x + 4, y);
std::cout << "After swap_ranges(x, x+4, x+4, y):\t\t\t";
print_range(x, y);
fmgp::swap_ranges(x, x + 4, x + 4, y);
std::cout << "After swap_ranges(x, x+4, x+4, y):\t\t\t";
print_range(x, y);
fmgp::swap_ranges_n(x, x + 4, 3);
std::cout << "After swap_ranges(x, x+4, 3):\t\t\t\t";
print_range(x, y);
fmgp::swap_ranges_n(x, x + 4, 3);
std::cout << "After swap_ranges(x, x+4, 3):\t\t\t\t";
print_range(x, y);
auto m = x + 4;
fmgp::gries_mills_rotate(x, m, y);
std::cout << "Setting m = x + 4" << std::endl;
std::cout << "After gries_mills_rotate(x, m, y):\t\t\t";
print_range(x, y);
fmgp::gries_mills_rotate(x, x + 3, y);
std::cout << "After gries_mills_rotate(x, x+3, y):\t\t\t";
print_range(x, y);
auto m1 = fmgp::rotate(x, m, y, std::forward_iterator_tag());
std::cout << "Setting m1 = rotate(x, m, y, forward_iterator_tag())" << std::endl;
std::cout << "After rotate(x, m, y, forward_iterator_tag()):\t\t";
print_range(x, y);
fmgp::rotate(x, m1, y, std::forward_iterator_tag());
std::cout << "After rotate(x, m1, y, forward_iterator_tag()):\t\t";
print_range(x, y);
m1 = fmgp::rotate(x, m, y, std::random_access_iterator_tag());
std::cout << "After rotate(x, m, y, random_access_iterator_tag()):\t";
print_range(x, y);
fmgp::rotate(x, m1, y, std::random_access_iterator_tag());
std::cout << "After rotate(x, m1, y, random_access_iterator_tag()):\t";
print_range(x, y);
fmgp::three_reverse_rotate(x, m, y);
std::cout << "After three_reverse_rotate(x, m, y)\t\t\t";
print_range(x, y);
fmgp::three_reverse_rotate(x, x + 3, y);
std::cout << "After three_reverse_rotate(x, x+3, y)\t\t\t";
print_range(x, y);
m1 = fmgp::rotate(x, m, y, std::bidirectional_iterator_tag());
std::cout << "After rotate(x, m, y, bidirectional_iterator_tag()):\t";
print_range(x, y);
fmgp::rotate(x, m1, y, std::bidirectional_iterator_tag());
std::cout << "After rotate(x, m1, y, bidirectional_iterator_tag()):\t";
print_range(x, y);
fmgp::reverse_n(x, y, 7);
std::cout << "After reverse_n(x, y, 7):\t\t\t\t";
print_range(x, y);
fmgp::reverse_n(x, y, 7);
std::cout << "After reverse_n(x, y, 7):\t\t\t\t";
print_range(x, y);
fmgp::reverse_recursive(x, 7);
std::cout << "After reverse_recursive(x, 7):\t\t\t\t";
print_range(x, y);
fmgp::reverse_recursive(x, 7);
std::cout << "After reverse_recursive(x, 7):\t\t\t\t";
print_range(x, y);
vector<int> buffer(3);
fmgp::reverse_n_adaptive(x, 7, begin(buffer), 3);
std::cout << "After reverse_n_adaptive(x, 7, begin(buffer), 3):\t";
print_range(x, y);
fmgp::reverse_n_adaptive(x, 7, begin(buffer), 3);
std::cout << "After reverse_n_adaptive(x, 7, begin(buffer), 3):\t";
print_range(x, y);
}