-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
328 lines (245 loc) · 11.9 KB
/
main.cpp
File metadata and controls
328 lines (245 loc) · 11.9 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
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
#define NDEBUG
#include <iostream>
#include "headers/simple_strategy.h"
#include "headers/extensible_simple_strategy.h"
#include "headers/extensible_realoc_strategy.h"
#include "headers/Custom_allocator.h"
#include "headers/list_struct.h"
#include <map>
static const int SZ = 10; // size of allocator
int MyFactorial(int n){
int res = 1;
for(int i = 2; i <= n; ++i){
res *= i;
}
return res;
}
template<typename T>
using allocatorSimple = Custom_allocator<T, SZ, simple_strategy>;
template<typename T>
using allocatorExtendSimple = Custom_allocator<T, SZ, ext_simple_strategy>;
template<typename T>
using allocatorReallocate = Custom_allocator<T, SZ, ext_realloc_strategy>;
template<typename T>
using LLi = LinkedList<T, allocatorExtendSimple<T>>;
int main(int, char**){
auto print_head = [](const std::string& str){
std::size_t length = str.length() < 80 ? 40 - str.length()/2 : 0;
std::string filler(length, '_');
std::cout << filler << str << filler << std::endl;
};
[[maybe_unused]] auto print_addr = [](const auto& cont){
std::cout << "Head address: " << cont.Address_of_Head() << ". Head data address: " << cont.Address_of_Head_Data() << std::endl;
};
[[maybe_unused]] auto print_addr_all = [](auto& cont){
for(auto it = cont.begin(); it != cont.end(); ++it)
std::cout << "Value = " << *it << (*it < 10'000'000 ? "\t" : "") << "Data address: " << it.pointer_to_node() << std::endl;
};
print_head("_");
print_head("MAIN_TASK");
print_head("_");
//-----------------------------------------------------------------------------------------------------------------------------------------
auto MapFactSTL = std::map<int, int>{};
print_head("std::allocator+std::map");
for(int i = 0; i < SZ; i++)
MapFactSTL.try_emplace(i, MyFactorial(i));
for (const auto & [i, v] : MapFactSTL)
std::cout << i << " " << v << std::endl;
//------------------------------------------------------------------------------------------------------------------------------------
auto MapFactCustom =
std::map<int,
int,
std::less<int>,
allocatorSimple<std::pair<const int, int>>>{};
print_head("Custom_simple_allocator+std::map");
for(int i = 0; i < SZ; i++){
try {
MapFactCustom.try_emplace(i, MyFactorial(i));
}
catch(const std::exception& e) {
std::cout << "Erorr: " << e.what() << " has occurred on the " << i << "th element while trying for allocate." << std::endl;
}
}
for(const auto & [i, v] : MapFactCustom)
std::cout << i << " " << v << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("CustomList+std::allocator");
LinkedList<int> Third;
for (int i = 0; i < SZ; i++){
Third.Add(i);
}
for(auto it = Third.begin(); it != Third.end(); ++it)
std::cout << *it << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("CustomList+Custom_simple_allocator");
LinkedList<int, allocatorSimple<int>> llist_CustAlloc;
for (int i = 0; i < SZ; i++) {
try {
llist_CustAlloc.Add(i);
}
catch(const std::exception& e) {
std::cout << "Erorr: " << e.what() << " has occurred on the " << i << "th element while trying for allocate." << std::endl;
}
}
for(const auto& it : llist_CustAlloc)
std::cout << it << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("CustomList+Custom_simple_allocator(here are trying to allocate 12 elements)");
LinkedList<int, allocatorSimple<int>> llist_CustAlloc_12;
for (int i = 0; i < SZ+2; i++) {
int fact = MyFactorial(i);
try {
llist_CustAlloc_12.Add(fact);
}
catch(const std::exception& e) {
std::cout << "Erorr: " << e.what() << " has occurred on the " << i << "th element while trying for allocate." << std::endl;
}
}
for(const auto& it : llist_CustAlloc_12)
std::cout << it << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("_");
print_head("EXTAND_TASK");
print_head("_");
//-----------------------------------------------------------------------------------------------------------------------------------------
auto MapFactCustomExt =
std::map<int,
int,
std::less<int>,
allocatorExtendSimple<std::pair<const int, int>>
>{};
print_head("Custom_extensible_simple_allocator+std::map");
for(int i = 0; i < SZ+4; i++){
try {
MapFactCustomExt.try_emplace(i, i);
}
catch(const std::exception& e) {
std::cout << "Erorr: " << e.what() << " has occurred on the " << i << "th element while trying for allocate." << std::endl;
}
}
for(const auto & [i, v] : MapFactCustomExt)
std::cout << i << " " << v << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
auto MapFactCustomDeal =
std::map<int,
int,
std::less<int>,
allocatorReallocate<std::pair<const int, int>>
>{};
print_head("Custom_extensible_realloc_allocator+std::map");
for(int i = 0; i < SZ; i++){
try {
MapFactCustomDeal.try_emplace(i, MyFactorial(i));
}
catch(const std::exception& e) {
std::cout << "Erorr: " << e.what() << " has occurred on the " << i << "th element while trying for allocate." << std::endl;
}
}
for(const auto & [i, v] : MapFactCustomDeal)
std::cout << i << " " << v << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("CustomList+Custom_ext_simple_allocator");
LinkedList<int, allocatorExtendSimple<int>> llist_CustExtAlloc;
for (int i = 0; i < SZ + 2; i++)
llist_CustExtAlloc.Add(MyFactorial(i));
for(const auto& it : llist_CustExtAlloc)
std::cout << it << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
/*
print_head("Copy_ctor_of_LinkedList");
LinkedList<int, allocatorExtendSimple<int>> llist_1, llist_4;
for (int i = 0; i < SZ; i++){
int fct = MyFactorial(i);
llist_1.Add(fct);
llist_4.Add(fct);
}
for(const auto& it : llist_1)
std::cout << it << std::endl;
print_head("Copy_with_the_same_allocators");
auto llist_2 = llist_1;
for(const auto& it : llist_2)
std::cout << it << std::endl;
print_head("Copy_with_different_allocators");
LinkedList<int> llist_3 = llist_4;
for(const auto& it : llist_3)
std::cout << it << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------
{
print_head("Move_ctor_LinkedList");
LLi<bunch<int>> llist_1;
for (int i = 0; i < SZ; i++){
bunch<int> bnc{i, MyFactorial(i)};
llist_1.Add(bnc);
}
DEBUG_MODE(print_addr(llist_1));
for(const auto& [f, s] : llist_1)
std::cout << f << ' ' << s << std::endl;
print_head("Move_with_the_same_allocators");
auto llist_2 = std::move(llist_1);
DEBUG_MODE(print_addr(llist_2));
for(const auto& [f, s] : llist_2)
std::cout << f << ' ' << s << std::endl;
print_head("Move_with_different_allocators");
LinkedList<bunch<int>> llist_3;
for (int i = 0; i < SZ; i++){
bunch<int> bnc{i, MyFactorial(i)};
llist_3.Add(bnc);
}
DEBUG_MODE(print_addr(llist_3));
for(const auto& [f, s] : llist_3)
std::cout << f << ' ' << s << std::endl;
LinkedList<bunch<int>, allocatorExtendSimple<bunch<int>>> llist_4 = std::move(llist_3);
DEBUG_MODE(print_addr(llist_4));
for(const auto& [f, s] : llist_4)
std::cout << f << ' ' << s << std::endl;
std::cout << llist_4.front().f << ' ' << llist_4.empty() << std::endl;
llist_4.clear();
std::cout << llist_4.empty() << std::endl;
}
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("checking_iterators_LinkedList");
LinkedList<int, allocatorReallocate<int>> llist_30;
for (int i = 0; i < SZ + 1; i++){
int fct = MyFactorial(i);
llist_30.Add(fct);
}
print_addr_all(llist_30);
//-----------------------------------------------------------------------------------------------------------------------------------------
std::cout << "replace zero element." << std::endl;
auto it_l30 = std::begin(llist_30);
*it_l30 = 222;
std::cout << "zero: " << *it_l30 << std::endl;
std::cout << "element after \"before_begin\"." << std::endl;
auto it_bef_beg = llist_30.before_begin();
std::cout << "zero: " << *++it_bef_beg << std::endl;
++it_l30;
std::cout << "element after twice oparation ++." << std::endl;
std::cout << "second: " << *++it_l30 << std::endl;
std::cout << "element after twice oparation std::next" << std::endl;
std::cout << "third: " << *std::next(std::next(it_l30)) << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("initial_data_of_LinkedList");
print_addr_all(llist_30);
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("remove_two_elements");
std::cout << "element with value: " << it_l30.pointer_to_node()->next->data << " will be erased by address: " << it_l30.pointer_to_node()->next << std::endl;
it_l30 = llist_30.erase_after(it_l30); //must return an iterator to the element after the deleted
std::cout << "element with value: " << it_l30.pointer_to_node()->next->data << " will be erased by address: " << it_l30.pointer_to_node()->next << std::endl;
llist_30.erase_after(it_l30);
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("data_after_erasing_from_LinkedList");
print_addr_all(llist_30);
llist_30.Add(1515); //Make sure that the addresses of the new elements match the addresses of the earlier erased ones.
llist_30.Add(2222); //This confirms that an allocator with the ext_realloc_strategy is reusing memory.
//-----------------------------------------------------------------------------------------------------------------------------------------
print_head("data_after_inserting_to_LinkedList");
print_addr_all(llist_30);
std::cout << std::endl << "Make sure that the addresses of the new elements match the addresses of the earlier erased ones." << std::endl;
std::cout << "This confirms that an allocator with the ext_realloc_strategy is reusing memory." << std::endl;
//-----------------------------------------------------------------------------------------------------------------------------------------
[[maybe_unused]] char a;
a = getchar();
*/
return 0;
}