u5e
UnicodeTextC++Library
filter.hpp
1 #ifndef INCLUDED_U5E_FILTER
2 #define INCLUDED_U5E_FILTER
3 
4 namespace u5e {
5 
6  /**
7  * \brief Walks an input iterator through a filter
8  *
9  * This will go from the begin to the end of the input iterator and
10  * will execute the filter function once for every input element.
11  *
12  * Unlike std::transform, the filter function does not return the
13  * output element, but it receives the output object and will do
14  * whatever makes sense with the output object.
15  *
16  * That means that the type of filter will define what type of
17  * object can be used as output. The filter function itself will not
18  * touch the output object, but simply forward it to the operator
19  * function.
20  *
21  * The operator function returns an int that is meant to indicate
22  * how much output was produced. The filter function will accumulate
23  * those values and return the sum.
24  *
25  * The filter is not required to produce a constant number of
26  * outputs for each input. The function can be produce many outputs
27  * or even none at all during the processing of each element.
28  *
29  * The value type for input and output is not required to be the
30  * same. The input type is resolved by the value_type member type of
31  * the input iterator type.
32  *
33  * \tparam InputIteratorType the type of the input iterator
34  * \tparam OutputType the type of the output iterator
35  * \tparam Functor the callback function type called for each element
36  *
37  * \param input_from starting position for the input iterator
38  * \param input_to end position for the input iterator
39  * \param output output container sent to the operator function
40  * \param operation function that takes the element, the output
41  * container and returns the number of outputted elements
42  */
43  template <typename InputIteratorType, typename OutputType,
44  typename Functor >
45  inline int
46  filter(InputIteratorType input_from, InputIteratorType input_to,
47  OutputType& output, Functor operation) {
48  int counter = 0;
49  while (input_from != input_to) {
50  counter += operation(*input_from, output);
51  input_from++;
52  }
53  return counter;
54  }
55 
56 }
57 
58 #endif
main u5e namespace
int filter(InputIteratorType input_from, InputIteratorType input_to, OutputType &output, Functor operation)
Walks an input iterator through a filter.
Definition: filter.hpp:46