Aleph-w  1.9
General library for algorithms and data structures
FunctionalMethods< Container, T > Class Template Reference

#include <ah-dry.H>

Public Member Functions

template<typename ... Args>
void emplace (Args &&... args)
 
template<typename ... Args>
void emplace_end (Args &&... args)
 
template<typename ... Args>
void emplace_ins (Args &&... args)
 
template<typename ... Args>
size_t ninsert (Args ... args)
 
template<typename ... Args>
size_t nappend (Args ... args)
 
template<class Operation >
void for_each (Operation &operation) noexcept(noexcept(operation))
 
template<class Operation >
void for_each (Operation &operation) const noexcept(noexcept(operation))
 
template<class Operation >
void for_each (Operation &&operation) const noexcept(noexcept(operation))
 
template<class Operation >
void for_each (Operation &&operation) noexcept(noexcept(operation))
 
template<class Operation >
void each (Operation &operation) noexcept(noexcept(operation))
 
template<class Operation >
void each (Operation &operation) const noexcept(noexcept(operation))
 
template<class Operation >
void each (Operation &&operation) const noexcept(noexcept(operation))
 
template<class Operation >
void each (Operation &&operation) noexcept(noexcept(operation))
 
template<class Operation >
void each (size_t pos, size_t slice, Operation &operation) const
 
template<class Operation >
void each (size_t pos, size_t slice, Operation &&operation) const
 
template<class Operation >
void mutable_for_each (Operation &operation) noexcept(noexcept(operation))
 
template<class Operation >
void mutable_for_each (Operation &&operation) noexcept(noexcept(operation))
 
template<class Operation >
bool all (Operation &operation) const noexcept(noexcept(operation))
 
template<class Operation >
bool all (Operation &&operation) const noexcept(noexcept(operation))
 
template<class Operation >
bool exists (Operation &op) const noexcept(noexcept(op))
 
template<class Operation >
bool exists (Operation &&op) const noexcept(noexcept(op))
 
template<typename __T = T, class Operation = Dft_Map_Op<T, __T>>
DynList< __T > maps (Operation &op) const
 
template<typename __T = T, class Operation = Dft_Map_Op<__T, __T>>
DynList< __T > maps (Operation &&op) const
 
template<typename __T = T, class Prop , class Operation >
DynList< __T > maps_if (Prop prop, Operation &op) const
 
template<typename __T = T, class Prop , class Operation >
DynList< __T > maps_if (Prop prop, Operation &&op) const
 
DynList< T > to_dynlist () const
 
template<typename __T = T, class Op = Dft_Fold_Op<__T, T>>
__T foldl (const __T &init, Op &op) const noexcept(noexcept(op))
 
template<typename __T = T, class Op = Dft_Fold_Op<__T, T>>
__T foldl (const __T &init, Op &&op=Op()) const noexcept(noexcept(op))
 
template<class Operation >
fold (const T &init, Operation &operation) const noexcept(noexcept(operation))
 
template<class Operation >
fold (const T &init, Operation &&operation) const noexcept(noexcept(operation))
 
template<class Operation >
DynList< T > filter (Operation &operation) const
 
template<class Operation >
DynList< T > filter (Operation &&operation) const
 
template<class Operation >
DynList< const T * > ptr_filter (Operation &operation) const
 
template<class Operation >
DynList< const T * > ptr_filter (Operation &&operation) const
 
template<class Operation >
DynList< std::tuple< T, size_t > > pfilter (Operation &operation) const
 
template<class Operation >
DynList< std::tuple< T, size_t > > pfilter (Operation &&operation) const
 
template<class Operation >
std::pair< DynList< T >, DynList< T > > partition (Operation &op) const
 
template<class Operation >
std::pair< DynList< T >, DynList< T > > partition (Operation &&op) const
 
template<class Operation >
std::pair< DynList< T >, DynList< T > > partition (size_t n) const
 
template<class Operation >
std::tuple< DynList< T >, DynList< T > > tpartition (Operation &op) const
 
template<class Operation >
std::tuple< DynList< T >, DynList< T > > tpartition (Operation &&op) const
 
size_t length () const noexcept
 
DynList< T > rev () const
 
DynList< T > take (const size_t n) const
 
DynList< T > take (size_t i, size_t j, size_t step=1) const
 
DynList< T > drop (const size_t n) const
 
void mutable_drop (size_t n)
 

Detailed Description

template<class Container, typename T>
class FunctionalMethods< Container, T >

Common methods to the Aleph-w ( $\aleph_\omega$) containers.

This class contains many and practice methods that are common to any Aleph-w ( $\aleph_\omega$) container.

Member Function Documentation

◆ all() [1/2]

template<class Container , typename T >
template<class Operation >
bool FunctionalMethods< Container, T >::all ( Operation &  operation) const
inlinenoexcept

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 $O(n)$ in average and worst case.

Parameters
[in]operationto be used as condition
Returns
true if all the elements satisfy the criteria: false otherwise.
Exceptions
anythingthat could throw operation

◆ all() [2/2]

template<class Container , typename T >
template<class Operation >
bool FunctionalMethods< Container, T >::all ( Operation &&  operation) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ drop()

template<class Container , typename T >
DynList<T> FunctionalMethods< Container, T >::drop ( const size_t  n) const
inline

Drop the first n elements seen in the container during its traversal.

The complexity of this method is $O(N)$ where N always is the number of elements of container.

Returns
A DynList<T> having the remainder $N - n$ elements according to traversal order.
Exceptions
bad_allocif there is no enough memory or out_of_range if n is greater or equal than N (the number of elements in the container).

◆ each() [1/6]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::each ( Operation &  operation)
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ each() [2/6]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::each ( Operation &  operation) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ each() [3/6]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::each ( Operation &&  operation) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ each() [4/6]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::each ( Operation &&  operation)
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ each() [5/6]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::each ( size_t  pos,
size_t  slice,
Operation &  operation 
) const
inline

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.

Parameters
[in]<tt>operation</tt>to be done on each element.
Returns
an reference to this
Exceptions
anythingthat can throw operation

◆ each() [6/6]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::each ( size_t  pos,
size_t  slice,
Operation &&  operation 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ emplace()

template<class Container , typename T >
template<typename ... Args>
void FunctionalMethods< Container, T >::emplace ( Args &&...  args)
inline

Appends a new element into the container by constructing it in-place with the given args.

emplace(args) tries to match a constructor T(args). If this exists, then this is constructed in-place and directly 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 unnecessary 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().
Parameters
[in]argsvariadic arguments list
Exceptions
bad_allocif there is no enough memory

◆ emplace_end()

template<class Container , typename T >
template<typename ... Args>
void FunctionalMethods< Container, T >::emplace_end ( Args &&...  args)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ emplace_ins()

template<class Container , typename T >
template<typename ... Args>
void FunctionalMethods< Container, T >::emplace_ins ( Args &&...  args)
inline

Insert a new element into the container by constructing it in-place with the given args.

emplace_ins(args) tries to match a constructor T(args). If this exists, then this is constructed in-place and directly 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 unnecessary 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 beginning of sequence. On other type of container append() is equivalent to insert().
Parameters
[in]argsvariadic arguments list
Exceptions
bad_allocif there is no enough memory

◆ exists() [1/2]

template<class Container , typename T >
template<class Operation >
bool FunctionalMethods< Container, T >::exists ( Operation &  op) const
inlinenoexcept

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 $O(n)$ in average and worst case.

Parameters
[in]opoperation for testing existence
Returns
true if it exists an item for which op return true; false otherwise.
Exceptions
anythingthat could throw op
+ Here is the call graph for this function:

◆ exists() [2/2]

template<class Container , typename T >
template<class Operation >
bool FunctionalMethods< Container, T >::exists ( Operation &&  op) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ filter() [1/2]

template<class Container , typename T >
template<class Operation >
DynList<T> FunctionalMethods< Container, T >::filter ( Operation &  operation) const
inline

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; });
Parameters
[in]operationdefining the flter criteria
Returns
a DynList<T> with the matched elements.
Exceptions
anythingthat could throw operation or bad_alloc if there is no enough memory

◆ filter() [2/2]

template<class Container , typename T >
template<class Operation >
DynList<T> FunctionalMethods< Container, T >::filter ( Operation &&  operation) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ fold() [1/2]

template<class Container , typename T >
template<class Operation >
T FunctionalMethods< Container, T >::fold ( const T &  init,
Operation &  operation 
) const
inlinenoexcept

Simplified version of foldl() where the folded type is the same type of elements stored in the container.

See also
foldl(const __T & init, Op & op)

◆ fold() [2/2]

template<class Container , typename T >
template<class Operation >
T FunctionalMethods< Container, T >::fold ( const T &  init,
Operation &&  operation 
) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ foldl() [1/2]

template<class Container , typename T >
template<typename __T = T, class Op = Dft_Fold_Op<__T, T>>
__T FunctionalMethods< Container, T >::foldl ( const __T &  init,
Op &  op 
) const
inlinenoexcept

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;
  });
Parameters
[in]initinitial value of folded value (or accumulator).
[in]opoperation to be performed on each item and used for folding.
Returns
the final folded computation.
Exceptions
anythingthat could throw op

◆ foldl() [2/2]

template<class Container , typename T >
template<typename __T = T, class Op = Dft_Fold_Op<__T, T>>
__T FunctionalMethods< Container, T >::foldl ( const __T &  init,
Op &&  op = Op() 
) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ for_each() [1/4]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::for_each ( Operation &  operation)
inlinenoexcept

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.

Parameters
[in]<tt>operation</tt>to be done on each element.
Returns
an reference to this
Exceptions
anythingthat can throw operation

◆ for_each() [2/4]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::for_each ( Operation &  operation) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ for_each() [3/4]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::for_each ( Operation &&  operation) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ for_each() [4/4]

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::for_each ( Operation &&  operation)
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ length()

template<class Container , typename T >
size_t FunctionalMethods< Container, T >::length ( ) const
inlinenoexcept

Count the number of elements of a container.

This method counts the number of elements stored in the container.

Note
Take in account that this method computes; it does not retrieve. Consequently it always takes $O(n)$. However, for many containers this number is already stored and retrievable in $O(1)$ through the methos size()
Returns
the number of elements stored in the container.

◆ maps() [1/2]

template<class Container , typename T >
template<typename __T = T, class Operation = Dft_Map_Op<T, __T>>
DynList<__T> FunctionalMethods< Container, T >::maps ( Operation &  op) const
inline

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 integer 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; });
Parameters
[in]opoperation to be performed in order to do the transformation on an item
Returns
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.
Exceptions
anythingthat could throw op or bad_alloc if there is no enough memory

◆ maps() [2/2]

template<class Container , typename T >
template<typename __T = T, class Operation = Dft_Map_Op<__T, __T>>
DynList<__T> FunctionalMethods< Container, T >::maps ( Operation &&  op) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ maps_if() [1/2]

template<class Container , typename T >
template<typename __T = T, class Prop , class Operation >
DynList<__T> FunctionalMethods< Container, T >::maps_if ( Prop  prop,
Operation &  op 
) const
inline

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).

Parameters
[in]opoperation to be perfomed in order to do the transformation on an item.
[in]propa lambda returning a bool which perform the logical test.
Returns
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.
Exceptions
anythingthat could throw op or bad_alloc if there is no enough memory

◆ maps_if() [2/2]

template<class Container , typename T >
template<typename __T = T, class Prop , class Operation >
DynList<__T> FunctionalMethods< Container, T >::maps_if ( Prop  prop,
Operation &&  op 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ mutable_drop()

template<class Container , typename T >
void FunctionalMethods< Container, T >::mutable_drop ( size_t  n)
inline

Drop the first n elements seen from container.

The complexity of this method is $O(N)$ where N always is the number of elements of container.

Exceptions
out_of_rangeif n is greater or equal than N (the number of elements in the container).

◆ mutable_for_each()

template<class Container , typename T >
template<class Operation >
void FunctionalMethods< Container, T >::mutable_for_each ( Operation &&  operation)
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ nappend()

template<class Container , typename T >
template<typename ... Args>
size_t FunctionalMethods< Container, T >::nappend ( Args ...  args)
inline

Append n variadic items

Parameters
[in]argsitems to be appended
Returns
the number of appended items

◆ ninsert()

template<class Container , typename T >
template<typename ... Args>
size_t FunctionalMethods< Container, T >::ninsert ( Args ...  args)
inline

Insert n variadic items

Parameters
[in]argsitems to be inserted
Returns
the number of inserted items

◆ partition() [1/3]

template<class Container , typename T >
template<class Operation >
std::pair<DynList<T>, DynList<T> > FunctionalMethods< Container, T >::partition ( Operation &  op) const
inline

Exclusive partition of container according to a filter criteria.

partition(op) traverses the container and filters its elements according to the filter criteria defined by op. The filtered elements are copied to a first list and the not filtered ones to a second list. When all the container is traversed, a pair containing these lists is returned.

The op requirements are the same than for filter().

Parameters
[in]opoperation instrumenting the filter criteria
Returns
a std::pair<DynList<T>, DynList<T>>.firstcontains the filtered elements andsecondthe non-filtered ones. \throw anything that could throw op orbad_alloc` if there is no enough memory
See also
filter()

◆ partition() [2/3]

template<class Container , typename T >
template<class Operation >
std::pair<DynList<T>, DynList<T> > FunctionalMethods< Container, T >::partition ( Operation &&  op) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ partition() [3/3]

template<class Container , typename T >
template<class Operation >
std::pair<DynList<T>, DynList<T> > FunctionalMethods< Container, T >::partition ( size_t  n) const
inline

Exclusive partition of container in the nth item

partition(n) traverses the container and produces a pair of lists. The first one contains the first n elements and the second one the this->size() - n remaining elements.

Parameters
[in]nthe first n items of the first list
Exceptions
anythingthat could throw op or bad_alloc if there is no enough memory

◆ pfilter() [1/2]

template<class Container , typename T >
template<class Operation >
DynList<std::tuple<T, size_t> > FunctionalMethods< Container, T >::pfilter ( Operation &  operation) const
inline

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>
Parameters
[in]operationthat defines the filter criteria
Returns
a DynList
Exceptions
bad_allocif there is no enough memory
See also
filter(Operation & operation)

◆ pfilter() [2/2]

template<class Container , typename T >
template<class Operation >
DynList<std::tuple<T, size_t> > FunctionalMethods< Container, T >::pfilter ( Operation &&  operation) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ ptr_filter()

template<class Container , typename T >
template<class Operation >
DynList<const T*> FunctionalMethods< Container, T >::ptr_filter ( Operation &  operation) const
inline

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; });
Parameters
[in]operationdefining the flter criteria
Returns
a DynList<const T*> with the pointers to the matched elements.
Exceptions
anythingthat could throw operation or bad_alloc if there is no enough memory

◆ rev()

template<class Container , typename T >
DynList<T> FunctionalMethods< Container, T >::rev ( ) const
inline

Return a list with the elements of container in reverse order respect to its traversal order.

Returns
a DynList<T> inversely ordered accordirg to the traversal order.
Exceptions
bad_allocif there is no enough memory

◆ take() [1/2]

template<class Container , typename T >
DynList<T> FunctionalMethods< Container, T >::take ( const size_t  n) const
inline

Return a list with the first n elements seen in the container during its traversal.

The complexity of this method is $O(n)$ where n can be less than the number of elements of container.

Returns
A DynList<T> having the first n elements according to its traversal order.
Exceptions
bad_allocif there is no enough memory or out_of_range if n is greater or equal than the number of elements in the container.

◆ take() [2/2]

template<class Container , typename T >
DynList<T> FunctionalMethods< Container, T >::take ( size_t  i,
size_t  j,
size_t  step = 1 
) const
inline

Return a list with elements seen in the container between i and j position respect to its traversal.

The complexity of this method is $O(n)$ where n can be less than the number of elements of container.

Returns
A DynList<T> having the first n elements according to its traversal order.
Exceptions
bad_allocif there is no enough memory or out_of_range if n is greater or equal than the number of elements in the container.

◆ tpartition() [1/2]

template<class Container , typename T >
template<class Operation >
std::tuple<DynList<T>, DynList<T> > FunctionalMethods< Container, T >::tpartition ( Operation &  op) const
inline

Exclusive partition of container according to a filter criteria.

This methos has exactly the same semantic than partition(Operation & op), excepts than instead of returning a std::pair it returns a std::tuple.

Parameters
[in]opoperation instrumenting the filter criteria
Returns
a std::tuple<DynList<T>, DynList<T>>.firstcontains the filteres elements andsecondthe non-filtered ones. \throw anything that could throw op orbad_alloc` if there is no enough memory
See also
partition(Operation & op)

◆ tpartition() [2/2]

template<class Container , typename T >
template<class Operation >
std::tuple<DynList<T>, DynList<T> > FunctionalMethods< Container, T >::tpartition ( Operation &&  op) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.


The documentation for this class was generated from the following file:

Leandro Rabindranath León