summaryrefslogtreecommitdiff
path: root/reverse-containers.cpp
blob: 39cdb0393f9ef53111ade4bf7ebf37f7961d9ecf (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <array>
#include <unistd.h>
#include <cstring>
#define CONTAINER_SIZE 1048576
char *g_argv = nullptr;
inline void usageOUT() {
	std::cerr << g_argv << " [-1][-2][-3][-4][-5][-6][-7]" << std::endl;
}

std::vector<char> &reverse_vector_reverse_iterator(std::vector<char> &vc) {

	auto size_vc = vc.size();
	std::vector<char> tmp_vc(size_vc);
	auto vc_rbeg = vc.rbegin();
	auto vc_rend = vc.rend();
	for(auto &a : tmp_vc) {
		if(vc_rbeg == vc_rend) {
			break;
		}
		a = *vc_rbeg++;
	}

	vc = tmp_vc;
	return vc;
}

std::vector<char> &reverse_vector_no_tmp_iterators(std::vector<char> &vc) {

	auto r_beg = vc.rbegin();
	auto beg = vc.begin();
	for(decltype(vc.size()) a = 0 ; a != (vc.size() / 2) ; ++a, ++beg, ++r_beg) {
		char tmp = *beg;
		*beg = *r_beg;
		*r_beg = tmp;
	}
	
	return  vc;
}

std::vector<char> &reverse_vector_construct_swap(std::vector<char> &vc) {

	std::vector tmp_vc(vc.crbegin(), vc.crend());
	vc.swap(tmp_vc);
	return vc;
}

std::vector<char> &reverse_vector_subscript(std::vector<char> &vc) {

	auto imax = vc.size()-1;
	for(decltype(vc.size()) i = 0 ; i != (vc.size() / 2) ; ++i, --imax) {
		char tmp = vc[i];
		vc[i] = vc[imax];
		vc[imax] = tmp;
	}
	
	return vc;
}

std::deque<char> &reverse_deque_no_tmp_iterators(std::deque<char> &dc) {

	auto rbeg = dc.rbegin();
	auto beg = dc.begin();
	for(decltype(dc.size()) i = 0 ; i != (dc.size() / 2) ; ++i, ++beg, ++rbeg) {
		char tmp = *beg;
		*beg = *rbeg;
		*rbeg = tmp;
	}
	
	return dc;
}

std::deque<char> &reverse_deque_construct_swap(std::deque<char> &dc) {

	std::deque<char> tmp_dc(dc.crbegin(), dc.crend());
	dc.swap(tmp_dc);
	return dc;
}

char *reverse_cstring(const char *cstr, char cstr2[CONTAINER_SIZE]) {

	auto max_len = std::strlen(cstr) -1;
	for(size_t i = 0 ; cstr[i] != '\0' && i != CONTAINER_SIZE ; ++i, --max_len) {
		cstr2[i] = cstr[max_len];
	}
	
	return cstr2;
}

std::string &reverse_string_construct_swap(std::string &sc) {

	std::string tmp_sc(sc.crbegin(), sc.crend());
	sc.swap(tmp_sc);
	return sc;
}

std::string &reverse_string_no_tmp_iterators(std::string &sc) {

	auto rbeg = sc.rbegin();
	auto beg = sc.begin();
	for(decltype(sc.size()) i = 0 ; i != (sc.size() / 2); ++i, ++beg, ++rbeg) {
		char tmp = *beg;
		*beg = *rbeg;
		*rbeg = tmp;
	}
	
	return sc;
}

std::list<char> &reverse_list(std::list<char> &lc) {

	auto beg = lc.begin();
	auto crbeg = lc.crbegin();
	for(decltype(lc.size()) i = 0 ; i != (lc.size() / 2) ; ++i, ++crbeg) {
		lc.insert(beg, *crbeg);
	}

	lc.erase(++beg, lc.cend());
	return lc;
}

std::array<char, CONTAINER_SIZE> &reverse_array(std::array<char, CONTAINER_SIZE> &ac) {
	// This function is a little weird, not sure if i like it...
	std::array<char, CONTAINER_SIZE> tmp_ac;
	auto crbeg = ac.crbegin();
	auto begin = tmp_ac.begin();
	for( ; begin != tmp_ac.cend() ; ++crbeg, ++begin) {
		*begin = *crbeg;
	}

	ac.swap(tmp_ac);
	return ac;
}

void print_container(std::array<char, CONTAINER_SIZE> &ac) {

	for(auto &b : ac) {
		std::cout << b << " ";
	}
	
	std::cout << std::endl;
}

void print_container(std::string &sc) {

	std::cout << sc << std::endl;
}

void print_container(const char *ap) {

	std::cout << ap;	
	std::cout << std::endl;
}

void print_container(std::vector<char> &vc) {

	for(auto &b : vc) {
		std::cout << b << " ";
	}
	
	std::cout << std::endl;
}

void print_container(std::list<char> &lc) {

	for(auto &b : lc) {
		std::cout << b << " ";
	}
	
	std::cout << std::endl;
}


void print_container(std::deque<char> &dc) {

	for(auto &b : dc) {
		std::cout << b << " ";
	}
	
	std::cout << std::endl;
}

int main(int argc, char **argv) {

	g_argv = argv[0];
	if(argc < 2 || argc > 2) {
		usageOUT();
		return EXIT_FAILURE;
	}
	
	int opt;
	int one = 0;
	int two = 0;
	int three = 0;
	int four = 0;
	int five = 0;
	int six = 0;
	int seven = 0;
	int eight = 0;
	int nine = 0;
	int zero = 0;
	int a_letter = 0;
    while ((opt = getopt(argc, argv, "1234567890a")) != -1) {
        switch (opt) {
        case '1':

			one = 1;

			break;
        case '2':

			two = 1;

			break;
		case '3':

			three = 1;

			break;
		case '4':

			four = 1;

			break;
		case '5':

			five = 1;

			break;
		case '6':

			six = 1;

			break;
		case '7':

			seven = 1;

			break;
		case '8':

			eight = 1;
			
			break;
		case '9':

			nine = 1;

			break;
		case '0':

			zero = 1;
			
			break;
		case 'a':

			a_letter = 1;
			
			break;
		}
	}

	if((one + two + three + four + five + six + seven + eight + nine + zero + a_letter) > 1) {
		usageOUT();
		return EXIT_FAILURE;
	}

	std::array<char, CONTAINER_SIZE> input_array = {'a', 'b'};
	std::string input_str = "ab";
	const char *input_cstr = "ab";
	std::list<char> input_list = {'a', 'b'};
	std::vector<char> input_vec = {'a', 'b'};
	std::deque<char> input_deq = {'a', 'b'};
 	if(one == 1) {
		reverse_vector_reverse_iterator(input_vec);
		print_container(input_vec);
		std::cout << "reverse_vector_reverse_iterator" << std::endl;
	}

	if(two == 1) {
		reverse_vector_no_tmp_iterators(input_vec);
		print_container(input_vec);
		std::cout << "reverse_vector_no_tmp_iterators" << std::endl;
	}

	if(three == 1) {
		reverse_vector_construct_swap(input_vec);		
		print_container(input_vec);
		std::cout << "reverse_vector_construct_swap" << std::endl;
	}

	if(four == 1) {
		reverse_vector_subscript(input_vec);
		print_container(input_vec);
		std::cout << "reverse_vector_subscript" << std::endl;
	}

	if(five == 1) {
		reverse_deque_no_tmp_iterators(input_deq);
		print_container(input_deq);
		std::cout << "reverse_deque_no_tmp_iterators" << std::endl;
	}

	if(six == 1) {
		reverse_deque_construct_swap(input_deq);
		print_container(input_deq);
		std::cout << "reverse_deque_construct_swap" << std::endl;
	}

	if(seven == 1) {
		char cstr2[CONTAINER_SIZE];
		cstr2[0] = '\0';
		reverse_cstring(input_cstr, cstr2);
		print_container(cstr2);
		std::cout << "reverse_cstring" << std::endl;
	}

	if(eight == 1) {
		reverse_string_construct_swap(input_str);
		print_container(input_str);
		std::cout << "reverse_string_construct_swap" << std::endl;
	}

	if(nine == 1) {
		reverse_string_no_tmp_iterators(input_str);
		print_container(input_str);
		std::cout << "reverse_string_no_tmp_iterators" << std::endl;
	}

	if(zero == 1) {
		reverse_list(input_list);
		print_container(input_list);
		std::cout << "reverse_list" << std::endl;
	}

	if(a_letter == 1) {
		reverse_array(input_array);
		print_container(input_array);
		std::cout << "" << std::endl;
	}
	
	return 0;
}