-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathList.cpp
More file actions
454 lines (379 loc) · 8.31 KB
/
List.cpp
File metadata and controls
454 lines (379 loc) · 8.31 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <iostream>
#include <string>
#include <functional>
#include <memory>
#include <algorithm>
#include <vector>
#include <intrin.h>
using namespace std;
template <class T>
class CList
{
struct CItem
{
T Value;
shared_ptr<CItem> Tail;
CItem()
{
}
CItem(T AValue, shared_ptr<CItem> ATail) : Value(AValue), Tail(ATail)
{
}
};
shared_ptr<CItem> Head;
CList(shared_ptr<CItem> Ptr) : Head(Ptr)
{
}
public:
CList()
{
}
explicit CList(T Value)
{
Head = make_shared<CItem>();
Head->Value = Value;
}
explicit CList(initializer_list<T> L)
{
for (auto it = rbegin(L); it != rend(L); it++)
{
Head = make_shared<CItem>(*it, Head);
}
}
explicit CList(T Value, CList Tail)
{
Head = make_shared<CItem>();
Head->Value = Value;
Head->Tail = Tail.Head;
}
CList PushFront(T Value) const
{
return CList(make_shared<CItem>(Value, Head));
}
bool ForEach(function<bool(T, size_t)> f) const
{
shared_ptr<CItem> Ptr = Head;
size_t i = 0;
while (Ptr)
{
if (!f(Ptr->Value, i))
return false;
i++;
Ptr = Ptr->Tail;
}
return true;
}
bool ForEach(function<bool(T)> f) const
{
shared_ptr<CItem> Ptr = Head;
while (Ptr)
{
if (!f(Ptr->Value))
return false;
Ptr = Ptr->Tail;
}
return true;
}
bool IsEmpty() const
{
return !Head;
}
CList Tail() const
{
return CList(Head->Tail);
}
template <class U, class F>
CList<U> Map(F f) const
{
static_assert(is_convertible<F, function<U(T)> >::value, "U(T) required");
if (IsEmpty())
{
return CList<U>();
}
return CList<U>(f(Head->Value), Tail().Map<U>(f));
}
template<class F>
CList Filter1(F f) const
{
if (IsEmpty())
{
return *this;
}
if (f(Head->Value))
{
return CList(Head->Value, Tail().Filter(f));
}
else
{
return Tail().Filter(f);
}
}
template<class F>
CList Filter(F f) const
{
shared_ptr<CItem> Ptr = Head;
shared_ptr<CItem> QueueStart;
shared_ptr<CItem> QueueEnd;
shared_ptr<CItem> New;
while (Ptr)
{
if (f(Ptr->Value))
{
New = make_shared<CItem>();
New->Value = Ptr->Value;
if (QueueStart) QueueEnd->Tail = New; else QueueStart = New;
QueueEnd = New;
}
Ptr = Ptr->Tail;
}
return CList(QueueStart);
}
template <class U>
U FoldR(function<U(T, U)> f, U Start) const
{
if (IsEmpty())
{
return Start;
}
return f(Head->Value, Tail().FoldR(f, Start));
}
template <class U>
U FoldL(function<U(U, T)> f, U Start) const
{
if (IsEmpty())
{
return Start;
}
U Current = f(Start, Head->Value);
return Tail().FoldL(f, Current);
}
template <class U>
U FoldL(function<U(T, T)> f) const
{
if (IsEmpty())
{
return T();
}
return Tail().FoldL<U>(f, Head->Value);
}
template <class U>
T FoldR(function < U(T, T)> f)
{
if (!Head)
{
return T();
}
if (!Head->Tail)
return Head->Value;
return f(Head->Value, Tail().FoldR<U>(f));
}
CList Concat(const CList Right) const
{
if (IsEmpty())
{
return Right;
}
return CList(Head->Value, Tail().Concat(Right));
}
CList operator+(const CList& Right)
{
return Concat(Right);
}
CList Reverse() const
{
return FoldL<CList>([](CList A, T B) {
return CList(B, A);
}, CList());
}
string ToString() const
{
return Map<string>([](T Value) {
return to_string(Value);
}).FoldL<string>([](string A, string B) {
return A + ", " + B;
});
}
CList<CList> Group() const
{
if (IsEmpty())
{
return CList<CList>();
}
T Current = Head->Value;
CList First = Filter([Current](T Value) {
return Value == Current;
});
CList Second = Filter([Current](T Value) {
return Value != Current;
});
if (Second.IsEmpty())
{
return CList<CList>(First);
}
return CList<CList>(First) + Second.Group();
}
size_t Count() const
{
if (IsEmpty())
{
return 0;
}
return (1 + Tail().Count());
}
template <class C>
static CList FromCollection(const C& V)
{
shared_ptr<CItem> Item;
for (auto it = rbegin(V); it != rend(V); it++)
{
Item = make_shared<CItem>(*it, Item);
}
return CList(Item);
}
vector<T> ToVector() const
{
vector<T> Result;
shared_ptr<CItem> Ptr = Head;
while (Ptr)
{
Result.push_back(Ptr->Value);
Ptr = Ptr->Tail;
}
return Result;
}
CList Sort(function<bool(T, T)> F)
{
vector<T> Temp = ToVector();
sort(Temp.begin(), Temp.end(), F);
return FromCollection(Temp);
}
pair<CList, CList> TakeWhile(function<bool(T)> f)
{
CList Current = *this;
CList Result;
while (true)
{
if (Current.IsEmpty())
break;
if (!f(Current.Head->Value))
break;
Result = CList(Current.Head->Value, Result);
Current = Current.Tail();
}
return make_pair(Result.Reverse(), Current);
}
CList<CList> Split(T Separator)
{
CList<CList> Result;
CList Current = *this;
while (true)
{
pair<CList, CList> P = Current.TakeWhile([Separator](T Value) {
return Value != Separator;
});
//printf("%d, %s\r\n", (int)P.first.Count(), P.first.ToString().c_str());
Result = CList<CList>(P.first, Result);
Current = P.second;
if (Current.IsEmpty())
{
break;
}
/*
Remove front separator.
*/
Current = Current.Tail();
}
return Result.Reverse();
}
T Front() const
{
return Head->Value;
}
};
int main()
{
CList<int> List({ 2, 1, 0 });
CList<int> List2(3, List);
CList<string> List3({ "2ê", "1ð", "ûû0" });
printf("Sum %d\r\n", List2.FoldR<int>([](int A, int B) -> int {
return A + B;
}, 0));
printf("Sum %d\r\n", List2.FoldL<int>([](int A, int B) -> int {
return A + B;
}, 0));
printf("Sum %d\r\n", List2.FoldL<int>([](int A, int B) -> int {
return A + B;
}));
printf("Sum %d\r\n", List2.FoldR<int>([](int A, int B) -> int {
return A + B;
}));
List2.Filter([](int Value) {
return Value != 2;
}).
ForEach([](int Value, size_t Index) {
printf("%d, %d\n", Value, (int)Index);
return true; }
);
List2.Map<string>([](int Value) {
return to_string(Value);
}).ForEach([](string Value) {
printf("%s\n", Value.c_str());
return true;
});
string Str = CList<int>({ 1,2,3 })
.Concat(CList<int>({ 4,5,6 }))
.Map<string>([](int Value) {
return to_string(Value);
})
.FoldL<string>([](string A, string B) {
return A + B;
}, "");
printf("%s\r\n", Str.c_str());
string Str1 = (CList<int>({ 1,2,3 }) + CList<int>({ 4,5,6 }))
.Map<string>([](int Value) {
return to_string(Value);
})
.FoldL<string>([](string A, string B) {
return A + B;
}, "");
printf("%s\r\n", Str1.c_str());
printf("%s\r\n", CList<int>({ 1,2,3 }).ToString().c_str());
printf("%s\r\n", CList<int>({ 1,2,3 }).Reverse().ToString().c_str());
printf("\r\nGroup test:\r\n");
CList<int> GList({ 1,2,3,2,2,2,1,3,4,4,4 });
printf("%d\r\n", (int)GList.Count());
GList.Group().ForEach([](CList<int> Value) {
printf("Item: %s\r\n", Value.ToString().c_str());
return true;
});
printf("\r\nSort test:\r\n");
printf("%s\r\n", CList<int>({ 1,2,10,3,4,1 }).Sort([](int A, int B) {return A < B; }).ToString().c_str());
printf("\r\nSplit Test:\r\n");
string Text = "to be or not to be, or or or";
CList<char> TextList = CList<char>::FromCollection(Text);
TextList.Split(' ').ForEach([](CList<char> Item) {
printf("%s\r\n",
Item.FoldL<string>([](string P, char C) {
return P + C;
}, "").c_str());
return true;
});
/*
Parsing file count of words functional.
Ïîäñ÷åò ÷èñëà ñëîâ â ñòðîêå è âûâîä ñëîâ è èõ ÷àñòîòû ñî ñíèæåíèåì ÷àñòîòû.
*/
CList<char>::FromCollection(Text).Split(' ')
.Map<string>([](CList<char> V) {
return V.FoldL<string>([](string P, char C) {
return P + C;
}, "");
}).Group().Map<pair<string, size_t> >([](CList<string> V) {
return make_pair(V.Front(), V.Count());
}).Sort([](pair<string, size_t> A, pair<string, size_t> B) {
return A.second > B.second;
})
.ForEach([](pair<string, size_t> P) {
printf("%s: %d\r\n", P.first.c_str(), (int)P.second);
return true;
});
}