-
Notifications
You must be signed in to change notification settings - Fork 0
/
ah-dry.H.old
1472 lines (1212 loc) · 43.8 KB
/
ah-dry.H.old
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ifndef AH_DRY_H
# define AH_DRY_H
# include "ahFunctional.H"
/** Generic traversal of the container through its iterator.
This class implements a conditioned traversal on the container
through its iterator.
It is assumed that `Container` exports its iterator with
typename Container::Iterator
\ingroup Secuencias
*/
template <class Container>
struct GenericTraverse
{
/** Traverse the container via its iterator and performs a conditioned
operation on each item.
`traverse(operation)` instantiates the internal iterator of the
class and traverses each item performing `operation(item)`.
`operation` must have the following signature:
bool operation(const typename Container::Item_Type & item)
If `operation(item)` returns `true` then the iterator is advanced
and the next item processed. Otherwise. the traversal stops.
\param[in] operation to be performed on each item
\return `true` if all the items were visited (`operation` on each
one always returned `true`) or `false` if the traversal was
stoppep because there was a `false` result on an item.
\throw anything that could throw `operation`
*/
template <class Operation>
bool traverse(Operation & operation) noexcept(noexcept(operation))
{
for (typename Container::Iterator it(*static_cast<Container*>(this));
it.has_curr(); it.next())
if (not operation(it.get_curr()))
return false;
return true;
}
/// \overload traverse()
template <class Operation>
bool traverse(Operation & operation) const noexcept(noexcept(operation))
{
return const_cast<GenericTraverse*>(this)->traverse<Operation>(operation);
}
/// \overload traverse()
template <class Operation>
bool traverse(Operation && operation) const noexcept(noexcept(operation))
{
return traverse<Operation>(operation);
}
/// \overload traverse()
template <class Operation>
bool traverse(Operation && operation) noexcept(noexcept(operation))
{
return traverse<Operation>(operation);
}
};
template <class Container, class Operation>
bool traverse(const Container & c, Operation & op)
{
return c.traverse(op);
}
template <class Container, class Operation>
bool traverse(const Container & c, Operation && op = Operation())
{
return traverse<Container, Operation>(c, op);
}
/** Common sequential searching methods on containers.
This classs implements common sequential searching on containers.
\note Take in account that any of these searches takes \f$O(n)\f$ of
complexity for the worst case and that this is independent of type
of container. For example, a hash table or a binary search tree,
exports its own search that is much more faster that any of these
primitives. As an advice: do not use this method inside loops. If
you find yourself in this situation, then consider to revise your
design and to use another search method. Use these primitives very
few times, for algorithms whose complexity is by far greater that
\f$O(n)\f$ and when you cannot index with an adequate data
structure.
\warning Be very careful about the fact that many of these primitives
return pointers or references to container's data. In many cases,
alteration of that data will corrupt the internal state of
container.
\ingroup Secuencias
*/
template <class Container, typename Type>
class LocateFunctions
{
Container * me() noexcept { return static_cast<Container*>(this); }
const Container * const_me() const noexcept
{
return static_cast<const Container*>(this);
}
LocateFunctions<Container, Type> * base() const
{
return const_cast<LocateFunctions*>(this);
}
public:
/// Return an properly initialized iterator positioned at the first
/// item on the container
auto get_it() const
{
auto ret = typename Container::Iterator(*const_me());
return ret;
}
/// Return an properly initialized iterator positioned at the `pos`
/// item on the container
auto get_it(size_t pos) const
{
auto ret = typename Container::Iterator(*const_me());
for (size_t i = 0; i < pos; ++i)
ret.next();
return ret;
}
/// \overload get_it()
auto get_itor() const { return get_it(); }
/** Return the n-th item of container.
The notion of ordinal depends of type of container. On list,
probably will be the insertion order. On binary search trees will
be the nth smaller item. On hash tables will be pseudo random.
\warning Frequent use of this method will definitively degrade the
performance. Try not to use this method inside loops. In general,
if you falls in this situation, then consider your design and to
use an faster approach.
\param[in] n the nth item to find
\return a valid reference to the item into the container.
\throw out_of_range if n is greater or equal that the size of
container.
*/
Type & nth(const size_t n)
{
Type * ptr = nullptr;
size_t i = 0;
me()->traverse([&ptr, &i, &n] (Type & item)
{
if (i++ < n)
return true;
ptr = &item;
return false;
});
if (i != n + 1)
throw std::out_of_range("index out of range");
return *ptr;
}
/// \overload nth()
const Type & nth(const size_t n) const
{
return base()->nth(n);
}
/** Find a pointer to an item in the container according to a
searching criteria.
`find_ptr(operation)` traverses the container and on each item
perform `operation(item)`. If the result of `operation` is `true`,
then the traversal is stopped and a pointer to the current item
(which mathes `operation`) is returned.
`operation` must have the following signature:
bool operation(const typename Container::Item_Type & item)
\warning Frequent use of this method will definitively degrade the
performance. Try not to use this method inside loops. In general,
if you falls in thie situation, the consider your design and to
use an faster approach.
\param[in] `operation` to be performed on each item for matching a
searching criteria.
\return a valid pointer to an item if this was found or `nullptr`
otherwise.
*/
template <class Operation>
Type * find_ptr(Operation & operation) noexcept(noexcept(operation))
{
Type * ptr = nullptr;
me()->traverse([&ptr,&operation] (Type & item)
{
if (operation(item))
{
ptr = &item;
return false;
}
return true;
});
return ptr;
}
/// \overload find_ptr()
template <class Operation>
const Type * find_ptr(Operation & operation) const
noexcept(noexcept(operation))
{
return base()->find_ptr(operation);
}
/// \overload find_ptr()
template <class Operation>
const Type * find_ptr(Operation && operation) const
noexcept(noexcept(operation))
{
return find_ptr<Operation>(operation);
}
/// \overload find_ptr()
template <class Operation>
Type * find_ptr(Operation && operation) noexcept(noexcept(operation))
{
return find_ptr(operation);
}
/** Find the position of an item in the container according to a
searching criteria.
`find_index(operation)` traverses the container and on each item
perform `operation(item)`. If the result of `operation` is `true`,
then the traversal is stopped and the position of the current item
(which mathes `operation`) is returned.
`operation` must have the following signature:
bool operation(const typename Container::Item_Type & item)
\warning Frequent use of this method will definitively degrade the
performance. Try not to use this method inside loops. In general,
if you falls in this situation, the consider your design and
use a faster approach.
\param[in] `operation` to be performed on each item for matching a
searching criteria.
\return the last seen position. If the item is not found, then the
number of items is returned.
*/
template <class Operation>
size_t find_index(Operation & operation) const noexcept(noexcept(operation))
{
size_t i = 0;
const_me()->traverse([&i,&operation] (Type & item)
{
if (operation(item))
return false;
++i;
return true;
});
return i;
}
/// \overload find_index()
template <class Operation>
size_t find_index(Operation && operation) const noexcept(noexcept(operation))
{
return find_index<Operation>(operation);
}
/** Safe sequential searching of an item matching a criteria.
`find_item(operation)` traverses the container and on each item
perform `operation(item)`. If the result of `operation` is `true`,
then the traversal is stopped and duple containg a copy of found
item is returned.
The method is said safe because returns a copy of item.
`operation` must have the following signature:
bool operation(const typename Container::Item_Type & item)
\param[in] `operation` to be used as searching criteria
\return a duple tuple<bool, Type>. The first field indicates if
the item was found and the second contains a copy of found
item. If no item is found, then the first field is `false` and the
second is the result of default constructor on the type stored in
the container.
*/
template <class Operation>
std::tuple<bool, Type> find_item(Operation & operation)
noexcept(noexcept(operation))
{
using TT = std::tuple<bool, Type>;
auto ptr = find_ptr(operation);
return ptr ? TT(true, *ptr) : TT(false, Type());
}
/// \overload find_item(Operation & operation)
template <class Operation>
std::tuple<bool, Type> find_item(Operation & operation) const
noexcept(noexcept(operation))
{
using TT = std::tuple<bool, Type>;
auto ptr = find_ptr(operation);
return ptr ? TT(true, *ptr) : TT(false, Type());
}
/// \overload find_item(Operation & operation)
template <class Operation>
std::tuple<bool, Type> find_item(Operation && operation)
noexcept(noexcept(operation))
{
return find_item(operation);
}
/// \overload find_item(Operation & operation)
template <class Operation>
std::tuple<bool, Type> find_item(Operation && operation) const
noexcept(noexcept(operation))
{
return find_item(operation);
}
};
/** Special constructors common to `Aleph-w` (\f$\aleph_\omega\f$)
containers.
Basically, the constructors of this class append a sequence of items.
\warning Note that these constructors require that the class is
correctly initialized, what not only very often is not the case, but
it could be impossible in most cases. The most part of situations,
the derived class must be initialized before to start to append new
items. But this is not the case during this construction. So, don't
use this class unless that you are absolutely sure that the derived
class has been initialized.
\ingroup Secuencias
*/
template <class Container, typename T>
struct SpecialCtors
{
SpecialCtors() {}
SpecialCtors(const SpecialCtors&) {}
SpecialCtors(SpecialCtors&&) {}
SpecialCtors & operator = (const SpecialCtors&) { return *this; }
SpecialCtors & operator = (SpecialCtors&&) { return *this; }
/// Build the container by inserting all item of list `l`
SpecialCtors(const DynList<T> & l)
{
l.for_each([this] (const T & item)
{
static_cast<Container*>(this)->append(item);
});
}
template <class It>
SpecialCtors(It b, It e)
{
for (It it = b; it != e; ++it)
static_cast<Container*>(this)->append(*it);
}
SpecialCtors(std::initializer_list<T> l)
{
for (const auto & item : l)
static_cast<Container*>(this)->append(item);
}
};
/** Common methods to the `Aleph-w` (\f$\aleph_\omega\f$) containers.
This class contains many and practice methods that are common to any
`Aleph-w` (\f$\aleph_\omega\f$) container.
\ingroup Secuencias
*/
template <class Container, typename T>
class FunctionalMethods
{
Container * me() { return static_cast<Container*>(this); }
FunctionalMethods<Container, T> * base() const noexcept
{
return const_cast<FunctionalMethods<Container, T>*>(this);
}
const Container * const_me() const noexcept
{
return static_cast<const Container*>(this);
}
public:
/** Appends a new element into the container by constructing it
in-place with the given args.
`emplace(args)` tries to match a contructor `T(args)`. If this
exists, then this is constructed in-place and directely forwarded
to the method `append() of container. If all on the container and
`T` is adequately done, then the object is constructed once time,
successively forwarded and at its target place in the container is
moved, avoiding thus innecesary copies.
\note The semantic of append depends of container. In general,
this has some sense for lists and arrays and it means insertion at
the end of sequence. On other type of container `append()` is
equivalent to `insert()`.
\param[in] args variadic arguments list
\throw bad_alloc if there is no enough memory
*/
template <typename ...Args>
void emplace(Args && ... args)
{
me()->append(T(args...));
}
/// \overload emplace(Args && ... args)
template <typename ...Args>
void emplace_end(Args && ... args)
{
me()->append(T(args...));
}
/** Insert a new element into the container by constructing it
in-place with the given args.
`emplace_ins(args)` tries to match a contructor `T(args)`. If this
exists, then this is constructed in-place and directely forwarded
to the method `insert() of container. If all on the container and
`T` is adequately done, then the object is constructed once time,
successively forwarded and finally, at its target place in the
container, is moved, avoiding thus innecesary copies.
\note The semantic of `insert()` depends of container. In general,
this has some sense for lists and arrays and it means insertion at
the begining of sequence. On other type of container `append()`
is equivalent to `insert()`.
\param[in] args variadic arguments list
\throw bad_alloc if there is no enough memory
*/
template <typename ...Args>
void emplace_ins(Args && ... args)
{
me()->insert(T(args...));
}
private:
void nninsert(size_t&) {}
void nnappend(size_t&) {}
template <typename ... Args>
void nninsert(size_t & n, const T & item, Args & ... args)
{
me()->insert(item);
++n;
nninsert(n, args...);
}
template <typename ... Args>
void nnappend(size_t & n, const T & item, Args & ... args)
{
me()->append(item);
++n;
nnappend(n, args...);
}
public:
/** Insert n variadic items
@param[in] args items to be inserted
@return the number of inserted items
*/
template <typename ... Args>
size_t ninsert(Args ... args)
{
size_t n = 0;
nninsert(n, args...);
return n;
}
/** Append n variadic items
@param[in] args items to be appended
@return the number of appended items
*/
template <typename ... Args>
size_t nappend(Args ... args)
{
size_t n = 0;
nnappend(n, args...);
return n;
}
/** Traverse all the container and performs an operation on each
element.
`for_each(operation)` traverses the container and on each element
`item` is performed `operation(item)`.
`operation` must have the following signature:
void operation(const T & item)
Overloadings of this method allow that that the signature can be
lightly different; for example, remove the reference or the
`const`.
\param[in] `operation` to be done on each element.
\return an reference to `this`
\throw anything that can throw `operation`
*/
template <class Operation>
void for_each(Operation & operation) noexcept(noexcept(operation))
{
me()->traverse([&operation] (const T & item)
{
operation(item);
return true;
});
}
/// \overload for_each(Operation & operation)
template <class Operation>
void for_each(Operation & operation) const noexcept(noexcept(operation))
{
base()->for_each(operation);
}
/// \overload for_each(Operation & operation)
template <class Operation>
void for_each(Operation && operation) const noexcept(noexcept(operation))
{
for_each(operation);
}
/// \overload for_each(Operation & operation)
template <class Operation>
void for_each(Operation && operation) noexcept(noexcept(operation))
{
for_each(operation);
}
/// \overload for_each(Operation & operation)
template <class Operation>
void each(Operation & operation) noexcept(noexcept(operation))
{
for_each(operation);
}
/// \overload for_each(Operation & operation)
template <class Operation>
void each(Operation & operation) const noexcept(noexcept(operation))
{
for_each(operation);
}
/// \overload for_each(Operation & operation)
template <class Operation>
void each(Operation && operation) const noexcept(noexcept(operation))
{
for_each(operation);
}
/// \overload for_each(Operation & operation)
template <class Operation>
void each(Operation && operation) noexcept(noexcept(operation))
{
for_each(operation);
}
/** Traverse all the container and performs a mutable operation on
each element.
`mutable_for_each(operation)` traverses the container and on each
element `item` is performed `operation(item)`.
`operation` could have the following signature:
void operation(T & item)
Be very careful with the fact that this method allows to modify
the elements themselves, what could badly alter the internal
state of container. This would be the case for heaps, binary trees
and hash tables.
\param[in] `operation` to be done on each element.
\return an reference to `this`
\throw anything that can throw `operation`
*/
template <class Operation>
void each(size_t pos, size_t slice, Operation & operation) const
{
auto it = const_me()->get_it(pos);
for (size_t i = pos; true; i += slice)
{
operation(it.get_curr());
for (size_t k = 0; k < slice; ++k)
{
it.next();
if (not it.has_curr())
return;
}
}
}
/// \overload for_each(Operation & operation)
template <class Operation>
void each(size_t pos, size_t slice, Operation && operation) const
{
each(pos, slice, operation);
}
template <class Operation>
void mutable_for_each(Operation & operation) noexcept(noexcept(operation))
{
me()->traverse([&operation] (T & item)
{
operation(item);
return true;
});
}
/// \overload mutable_for_each(Operation & operation)
template <class Operation>
void mutable_for_each(Operation && operation) noexcept(noexcept(operation))
{
mutable_for_each(operation);
}
/** Check if all the elements of container satisfy a condition.
`all(operation)` checks if for each element `item` of container
`operation(item)` returns `true`.
This method has complexity \f$O(n)\f$ in average and worst case.
\param[in] operation to be used as condition
\return `true` if all the elements satisfy the criteria: `false`
otherwise.
\throw anything that could throw `operation`
*/
template <class Operation>
bool all(Operation & operation) const noexcept(noexcept(operation))
{
return const_me()->traverse(operation);
}
/// \overload all(Operation & operation)
template <class Operation>
bool all(Operation && operation) const noexcept(noexcept(operation))
{
return all(operation);
}
/** Test for existence in the container of an element satisfying a
criteria.
`exists(op)` returns `true` if it exists any element `item` in container
for which `op(item)` return `true`.
This method has complexity \f$O(n)\f$ in average and worst case.
\param[in] op operation for testing existence
\return `true` if it exists an item for which `op` return true;
`false` otherwise.
\throw anything that could throw `op`
*/
template <class Operation>
bool exists(Operation & op) const noexcept(noexcept(op))
{
return not const_me()->
traverse([&op] (const T & i) { return not op(i); });
}
/// \overload exists(Operation & op)
template <class Operation>
bool exists(Operation && op) const noexcept(noexcept(op))
{
return exists(op);
}
/** Map the elements of the container.
`maps(op)` produces a dynamic list resulting of mapping of each
element of container `item` to the result of operation `op(item)`.
`maps()` is a template method which receives as template parameters
the type `__T`, which is the type of target or range of mapping,
and the transforming operation. By default `__T` is the same type
of the elements stored in the container.
`operation` should have the following signature:
__T operation(const T & item)
So, `operation(item)` performs a transformation of `item` towards
the type `__T`.
If `__T == `T`, which is common and by default, then you could
specify a mapping without need of template specification. For
example, if the container has interger values, the a mapping of
item multiplied by 4 could be very simply written as follows:
c.maps([] (int item) { return 4*i; });
In contrast, if the range type is different than the domain type,
then it is necessary to specify the `template` keyword in the
method call. For example, if the range is `double` and you want to
return the elements divided by 4, the could do as follows:
c.template maps<double>([] (int item) { return 1.0*item/4; });
\param[in] op operation to be perfomed in order to do the
transformation on an `item`
\return a `DynList<__T> object containing the mapped items. The
order of resulting list is the same than the order of visit of the
iterator for the container.
\throw anything tnat could throw `op` or `bad_alloc` if there is
no enough memory
*/
template <typename __T = T, class Operation = Dft_Map_Op<T, __T>>
DynList<__T> maps(Operation & op) const
{
DynList<__T> ret_val;
const_me()->for_each([&ret_val, &op] (const T & item)
{
ret_val.append(op(item));
});
return ret_val;
}
/// \overload map(Operation & op)
template <typename __T = T, class Operation = Dft_Map_Op<__T, __T>>
DynList<__T> maps(Operation && op) const { return maps<__T, Operation>(op); }
/** Conditional mapping of the elements of the container.
`maps_if(prop, op)` traverses each item of container, on each
item it tests the proposition `prop`. If this last is true, then
the item is mapped through the function `op(item)`.
\param[in] op operation to be perfomed in order to do the
transformation on an `item`
\param[in] prop a lambda returning a `bool` which perform the
logical test.
\return a `DynList<__T> object containing the mapped items. The
order of resulting list is the same than the order of visit of the
iterator for the container.
\throw anything that could throw `op` or `bad_alloc` if there is
no enough memory
*/
template <typename __T = T, class Prop, class Operation>
DynList<__T> maps_if(Prop & prop, Operation & op) const
{
DynList<__T> ret_val;
const_me()->for_each([&ret_val, &prop, &op] (const T & item)
{
if (prop(item))
ret_val.append(op(item));
});
return ret_val;
}
/// \overload map(Prop & prop, Operation & op)
template <typename __T = T, class Prop, class Operation>
DynList<__T> maps_if(Prop & prop, Operation && op) const
{
return maps<__T, Prop, Operation>(prop, op);
}
DynList<T> to_dynlist() const
{
return maps([] (auto & item) { return item; });
}
/** Fold the elements of the container to a specific result.
`foldl(init, op)` set an internal variable `acc` of type `__T` to
`init` value. Then it traverses the container and on each `item`
it performs:
acc = op(acc, op(acc, item);
So `acc` serves as a sort of accumulator.
`op` should have the following signature:
__T op(__T acc, const T & item);
Since `foldl` is overloaded with several operation structures,
there is a certain flexibility with the parameter qualifiers. You
could, for example, to declare `acc` and/or `item` by value.
The method is a template. The first template parameter `__T`
specifies the final folded type. By default, this type is `T` (the
type of elements stored in the container). The second parameter is
the operation. If the folded type is the same than `T` (the type
of item stored), the you can simply write a `foldl()`. For
example, if the container stores integer, in order to determine
the maximum of all elements you could do:
c.foldl(std::numeric_limits<int>::min(), [] (int acc, int item)
{
return std::min(acc, item);
});
When the folded type is different than `T`, then you must specify
the folded type as template parameter. For example, if you want to
compute the sum of inversed elements, the you could do it as
follows:
c.template foldl<double>(0, [] (double acc, int item)
{
return acu + 1.0/item;
});
\param[in] init initial value of folded value (or accumulator).
\param[in] op operation to be performed on each item and used for
folding.
\return the final folded computation.
\throw anything that could throw `op`
*/
template <typename __T = T, class Op = Dft_Fold_Op<__T, T>>
__T foldl(const __T & init, Op & op) const noexcept(noexcept(op))
{
__T ret_val = init;
const_me()->for_each([&ret_val, &op] (const T & item)
{
ret_val = op(ret_val, item);
});
return ret_val;
}
/// \overload foldl(const __T & init, Op & op)
template <typename __T = T, class Op = Dft_Fold_Op<__T, T>>
__T foldl(const __T & init, Op && op = Op()) const noexcept(noexcept(op))
{
return foldl(init, op);
}
/** Simplified version of foldl() where the folded type is the same
type of elements stored in the container.
@see foldl(const __T & init, Op & op)
*/
template <class Operation>
T fold(const T & init, Operation & operation) const
noexcept(noexcept(operation))
{
auto ret_val = init;
const_me()->for_each([&ret_val, &operation] (const T & item)
{
ret_val = operation(ret_val, item);
});
return ret_val;
}
/// \overload fold(const T & init, Operation & operation)
template <class Operation>
T fold(const T & init, Operation && operation) const
noexcept(noexcept(operation))
{
return fold(init, operation);
}
/** Filter the elements of a container according to a matching
criteria.
This method builds a dynamic list with copies of items of
container matching a criteria defined by `operation`, which
should have the following signature:
bool operation(const T & item)
If `operation` return `true` then `item` matches the criteria;
otherwise, `operation` must return `false`.
For example, if the container has integer, the the following
code snippet would return a list containing the items greater
than 100:
c.filter([] (auto item) { return item > 100; });
\param[in] operation defining the flter criteria
\return a `DynList<T>` with the matched elements.
\throw anything that could throw `operation` or `bad_alloc` if
there is no enough memory
*/
template <class Operation>
DynList<T> filter(Operation & operation) const
{
DynList<T> ret_val;
const_me()->for_each([&ret_val, &operation] (const T & item)
{
if (operation(item))
ret_val.append(item);
});
return ret_val;
}
/// \overload filter(Operation & operation)
template <class Operation>
DynList<T> filter(Operation && operation) const
{
return filter(operation);
}
/** Filter the elements of a container according to a matching
criteria an return pointer to the matched items in the container.
This method builds a dynamic list with stores pointers to the
items of matching a criteria defined by `operation`, which
should have the followgin signature:
bool operation(const T & item)
If `operation` return `true` then `item` matches the criteria;
otherwise, `operation` must return `false`.
For example, if the container has integer, the the following
code snippet would return a list containing the items greater
than 100:
c.ptr_filter([] (auto item) { return item > 100; });
\param[in] operation defining the flter criteria
\return a `DynList<const T*>` with the pointers to the matched elements.
\throw anything that could throw `operation` or `bad_alloc` if
there is no enough memory
*/
template <class Operation>
DynList<const T*> ptr_filter(Operation & operation) const
{
DynList<const T*> ret_val;
const_me()->for_each([&ret_val, &operation] (const T & item)
{
if (operation(item))
ret_val.append(&item);
});
return ret_val;
}
template <class Operation>
DynList<const T*> ptr_filter(Operation && operation) const
{
return ptr_filter(operation);
}
/** Filter the elements of a container according to a matching
criteria and determine its positions respect to the traversal
of container.
`pfilter(operation)` is very similar to `filter()`, but instead
of building a list of filtered elements, it builds a list of
pairs with form `(item, pos)`, where `item` is a copy of
filtered element and `pos` is its position respect to the
traversal order. The position is relative to the container type.
The pair is defined with a tuple:
std::tuple<T, size_t>
\param[in] operation that defines the filter criteria
\return a DynList
\throw bad_alloc if there is no enough memory
\see filter(Operation & operation)
*/
template <class Operation>
DynList<std::tuple<T, size_t>> pfilter(Operation & operation) const
{