-
Notifications
You must be signed in to change notification settings - Fork 47
/
main.cpp
247 lines (171 loc) · 7.46 KB
/
main.cpp
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
/*
Copyright 2013 Adobe
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
/**************************************************************************************************/
#include <adobe/config.hpp>
#include <adobe/algorithm.hpp>
#include <adobe/selection.hpp>
#include <iostream>
#include <iterator>
#include <sstream>
#include <utility>
#include <vector>
/**************************************************************************************************/
namespace adobe {
/**************************************************************************************************/
template <typename ForwardIterator>
std::pair<ForwardIterator, ForwardIterator>
shift_range(ForwardIterator first, ForwardIterator last, ForwardIterator range_first,
ForwardIterator range_last, ForwardIterator p) {
if (within_range(first, range_first, range_last, p))
return std::pair<ForwardIterator, ForwardIterator>(range_first, range_last);
else if (!within_range(first, range_last, last, p))
return shift_range(first, last, p, range_first, range_last);
return adobe::rotate(range_first, range_last, p);
}
/**************************************************************************************************/
} // namespace adobe
/**************************************************************************************************/
namespace {
/**************************************************************************************************/
template <typename ForwardRange>
void print_selection(const ForwardRange& range, const adobe::selection_t& selection) {
typedef typename ForwardRange::const_iterator set_const_iterator;
typedef typename adobe::selection_t::const_iterator selection_const_iterator;
set_const_iterator iter(boost::begin(range));
set_const_iterator last(boost::end(range));
selection_const_iterator s_iter(boost::begin(selection));
selection_const_iterator s_last(boost::end(selection));
bool inside(selection.start_selected());
std::stringstream selection_output;
while (iter != last) {
if (s_iter != s_last && iter == boost::next(boost::begin(range), *s_iter)) {
inside = !inside;
++s_iter;
}
std::stringstream foo;
foo << *iter << ' ';
selection_output.fill(inside ? '*' : ' ');
selection_output.width(static_cast<std::streamsize>(foo.str().size()));
selection_output << (inside ? '*' : ' ');
std::cout << foo.str().c_str();
++iter;
}
std::cout << std::endl << selection_output.str().c_str() << std::endl;
std::cout << adobe::size(selection) << " boundaries, " << adobe::size(selection, range)
<< " items: [ ";
selection_copy(selection, range,
std::ostream_iterator<typename ForwardRange::value_type>(std::cout, " "));
std::cout << ']' << std::endl;
}
/**************************************************************************************************/
typedef std::vector<int> set_type;
typedef set_type::iterator iterator;
typedef adobe::selection_t selection_type;
typedef selection_type::const_iterator selection_iterator;
/**************************************************************************************************/
template <typename IteratorCategory>
void do_split_selection(const adobe::selection_t& selection, std::size_t p,
IteratorCategory iterator_category) {
std::pair<selection_iterator, adobe::selection_t::difference_type> result =
adobe::selection_find_boundary(selection, p, iterator_category);
std::cout << p << " selection boundary " << static_cast<int>(result.second) << "; ";
if (result.first == boost::end(selection))
std::cout << "returned selection split is (end)";
else
std::cout << "returned selection split is " << *result.first;
std::cout << std::endl;
}
/**************************************************************************************************/
void test1() {
set_type set(20);
adobe::iota(set.begin(), set.end(), 0);
selection_type selection;
selection.push_back(2);
selection.push_back(5);
selection.push_back(9);
selection.push_back(12);
selection.push_back(14);
selection.push_back(17);
print_selection(set, selection);
std::cout << std::endl;
std::cout << "forward case:" << std::endl;
for (std::size_t p(0), last(set.size()); p != last; ++p)
do_split_selection(selection, p, std::forward_iterator_tag());
std::cout << "random_access case:" << std::endl;
for (std::size_t p(0), last(set.size()); p != last; ++p)
do_split_selection(selection, p, std::random_access_iterator_tag());
}
/**************************************************************************************************/
void test2() {
set_type set(20);
adobe::iota(set.begin(), set.end(), 0);
selection_type selection;
selection.push_back(2);
selection.push_back(5);
selection.push_back(9);
selection.push_back(12);
selection.push_back(14);
selection.push_back(17);
print_selection(set, selection);
std::cout << std::endl;
set_type::iterator middle(selection_stable_partition(selection, set));
selection_type selection2;
selection2.push_back(
static_cast<selection_type::size_type>(std::distance(set.begin(), middle)));
print_selection(set, selection2);
std::cout << std::endl;
}
/**************************************************************************************************/
void test3() {
for (int i(0); i != 21; ++i) {
set_type set(20);
adobe::iota(set.begin(), set.end(), 0);
selection_type selection;
selection.push_back(2);
selection.push_back(5);
selection.push_back(9);
selection.push_back(12);
selection.push_back(14);
selection.push_back(17);
std::pair<iterator, iterator> p_range =
adobe::selection_stable_partition_about(selection, set, i);
selection_type new_selection;
new_selection.push_back(std::distance(set.begin(), p_range.first));
new_selection.push_back(std::distance(set.begin(), p_range.second));
print_selection(set, new_selection);
std::cout << std::endl;
}
}
/**************************************************************************************************/
void test4() {
set_type set(20);
adobe::iota(set.begin(), set.end(), 0);
selection_type selection;
selection.push_back(2);
selection.push_back(5);
selection.push_back(9);
selection.push_back(12);
selection.push_back(14);
selection.push_back(17);
adobe::selection_partition_copy(selection, set, std::ostream_iterator<int>(std::cout, "b "),
std::ostream_iterator<int>(std::cout, "g "));
std::cout << std::endl;
}
/**************************************************************************************************/
} // namespace
/**************************************************************************************************/
int main() {
std::cout << "test1:" << std::endl;
test1();
std::cout << "test2:" << std::endl;
test2();
std::cout << "test3:" << std::endl;
test3();
std::cout << "test4:" << std::endl;
test4();
return 0;
}
/**************************************************************************************************/