#include <stream.hpp>
Public Types | |
typedef detail::stream_element_processor_iterator< T > | iterator |
Public Member Functions | |
stream ()=default | |
stream (const stream &x)=default | |
stream (stream &&x) noexcept=default | |
stream & | operator= (const stream &x)=default |
stream & | operator= (stream &&x) noexcept=default |
stream (std::shared_ptr< detail::stream_element_processor< T > > processor) | |
stream & | operator= (const std::shared_ptr< detail::stream_element_processor< T > > &processor) |
iterator | begin () |
iterator | end () |
stream< T > & | take (std::size_t count) |
template<typename Predicate > | |
stream< T > & | take_while (Predicate pred) |
stream< T > & | skip (std::size_t count) |
template<typename Predicate > | |
stream< T > & | skip_while (Predicate pred) |
stream< T > & | every (std::size_t count) |
template<typename Predicate > | |
stream< T > & | filter (Predicate &&pred) |
template<typename Predicate , typename R = typename std::result_of<Predicate&(T)>::type> | |
stream< R > | map (Predicate &&pred) |
template<typename Predicate , typename R = typename std::result_of<Predicate&(T)>::type::value_type> | |
stream< R > | flatmap (Predicate &&pred) |
template<typename Predicate > | |
stream< T > & | peek (Predicate &&pred) |
stream< T > & | concat (const stream< T > &other) |
stream< std::vector< T > > | pack_every (std::size_t packsize) |
optional< T > | first () |
optional< T > | last () |
optional< T > | min () |
optional< T > | max () |
optional< T > | at (std::size_t index) |
template<typename Predicate > | |
bool | any (Predicate &&pred) |
template<typename Predicate > | |
bool | all (Predicate &&pred) |
template<typename Predicate > | |
bool | none (Predicate &&pred) |
std::size_t | count () |
template<typename Predicate > | |
std::size_t | count (Predicate &&pred) |
template<typename Accumulator > | |
optional< T > | reduce (Accumulator &&accu) |
template<typename U , typename Accumulator > | |
U | reduce (const U &identity, Accumulator &&accu) |
template<typename U , typename Accumulator , typename R = typename std::result_of<U&(T)>::type> | |
optional< R > | reduce_idfunc (const U &identity_fun, Accumulator &&accu) |
void | print_to (std::ostream &out, const char *delim=" ") |
template<template< class ... > class C, class Allocator = std::allocator<T>> | |
C< T, Allocator > | collect () |
template<template< class ... > class C, class Key , class Value , typename KeyFunc , typename ValueFunc > | |
C< Key, Value > | collect (const KeyFunc &key_func, const ValueFunc &value_func) |
template<typename Predicate > | |
void | for_each (Predicate &&pred) |
std::shared_ptr< detail::stream_element_processor< T > > & | processor () |
std::string | join () |
std::string | join (const std::string &delimiter) |
std::string | join (const std::string &delimiter, const std::string &prefix, const std::string &suffix) |
The stream class allows stream like data processing with lazy evaluation.
It provides a couple of stream element processors like "filter", "map" or "skip".
There are also some stream terminators allowing to get a single result based on the evaluated stream like "min", "max" or "any".
Once a steam is completely processed it can't be processed again.
T | Type of the stream elements |
typedef detail::stream_element_processor_iterator<T> matador::stream< T >::iterator |
Shortcut to stream iterator type
|
default |
Default constructor
|
default |
Copys the steam from given stream x
x | Stream to copy from |
|
defaultnoexcept |
Copy move stream from given stream x
x | Stream to copy move from |
|
explicit |
Initialize a new stream with the given stream processor
processor | The initial stream processor |
bool matador::stream< T >::all | ( | Predicate && | pred | ) |
Returns true if all elements of the stream matches the given predicate.
Predicate | Type of the predicate |
pred | Condition to check |
bool matador::stream< T >::any | ( | Predicate && | pred | ) |
Returns true if any element of the stream matches the given predicate.
Predicate | Type of the predicate |
pred | Condition to check |
optional< T > matador::stream< T >::at | ( | std::size_t | index | ) |
Returns an element at the given position index. The value is returned as an optional because the requested position could be invalid
index | Position of the requested element |
stream< T >::iterator matador::stream< T >::begin | ( | ) |
Returns the begin iterator of the stream
C< T, Allocator > matador::stream< T >::collect | ( | ) |
The collect method collects all elements into new container of type C. The containers template parameters are the type T and an allocator type Allocator. The default for the allocator is std::allocator<T>
C | Type of the container |
Allocator | Allocator type |
C< Key, Value > matador::stream< T >::collect | ( | const KeyFunc & | key_func, |
const ValueFunc & | value_func | ||
) |
The collect method collects all elements into new associative container of type C. The containers template parameters are the type Key and Value.
C | Type of the container |
Key | Key type of the associative container |
Value | Value type of the associative container |
keyFunc | The function returning the key |
valueFunc | The function returning the value |
stream< T > & matador::stream< T >::concat | ( | const stream< T > & | other | ) |
Concatenates the current stream with the given stream of the same type. Once the current stream finishes the elements of the next stream are processed seamlessly.
other | The stream to concatenate |
std::size_t matador::stream< T >::count | ( | ) |
Returns the number of elements in the stream
std::size_t matador::stream< T >::count | ( | Predicate && | pred | ) |
Returns the number of elements matching the given predicate.
Predicate | Type of the predicate |
pred | Condition to check |
stream< T >::iterator matador::stream< T >::end | ( | ) |
Returns the end iterator of the stream
stream< T > & matador::stream< T >::every | ( | std::size_t | count | ) |
Returns every nth elements of the stream identified by the given count
count | Identifies which elements are valid |
stream< T > & matador::stream< T >::filter | ( | Predicate && | pred | ) |
Filters the stream by the given predicate. Only if the predicate called with the current element evaluates to true the element is valid
Predicate | Type of the predicate |
pred | Condition to check |
optional< T > matador::stream< T >::first | ( | ) |
Return the first element of the current processed stream. The element is returned as an optional because it can be not available.
stream< R > matador::stream< T >::flatmap | ( | Predicate && | pred | ) |
Flattens a nested container (list, vectors, i.e.) of an element type into one new stream containing all elements of the container
Predicate | Type of the predicate |
R | Return type of the predicate and type of the new stream |
pred | Predicate to apply on every stream element |
void matador::stream< T >::for_each | ( | Predicate && | pred | ) |
Iterates over all stream elements and applies the given predicate to each element.
Predicate | Type of predicate |
pred | Function to apply |
std::string matador::stream< T >::join | ( | ) |
Joins all stream elements into one string.
std::string matador::stream< T >::join | ( | const std::string & | delimiter | ) |
Joins all stream elements delimited by the given delimiter into one string.
delimiter | Delimiter for the elements |
std::string matador::stream< T >::join | ( | const std::string & | delimiter, |
const std::string & | prefix, | ||
const std::string & | suffix | ||
) |
Joins all stream elements delimited by the given delimiter into one string. Additionally a prefix and a suffix is prepended and appended to the string.
delimiter | Delimiter for the elements |
prefix | Prefix for the string |
suffix | Suffix for the string |
optional< T > matador::stream< T >::last | ( | ) |
Return the last element of the current processed stream. The element is returned as an optional because it can be not available.
stream< R > matador::stream< T >::map | ( | Predicate && | pred | ) |
Maps each element of stream to new element creating a new stream of elements. This stream could be of the same type as the origin stream or of a new type.
Predicate | Type of the predicate |
R | Return type of the predicate and type of the new stream |
pred | Predicate to apply on every stream element |
optional< T > matador::stream< T >::max | ( | ) |
Returns the maximum of the stream. This value is calculated using the std::less algorithm. The return value is an optional because the stream could be empty.
optional< T > matador::stream< T >::min | ( | ) |
Returns the minimum of the stream. This value is calculated using the std::less algorithm. The return value is an optional because the stream could be empty.
bool matador::stream< T >::none | ( | Predicate && | pred | ) |
Returns true if none of the elements of the stream matches the given predicate.
Predicate | Type of the predicate |
pred | Condition to check |
stream< T > & matador::stream< T >::operator= | ( | const std::shared_ptr< detail::stream_element_processor< T > > & | processor | ) |
Creates a new stream by assigning a given stream processor
processor | Stream processor to assign from |
|
default |
Copy assigns stream from given stream x
x | Stream to copy assign from |
|
defaultnoexcept |
Assign move stream from given stream x
x | Stream to assign move from |
stream< std::vector< T > > matador::stream< T >::pack_every | ( | std::size_t | packsize | ) |
Pack every n elemets of the stream into a container (vector) and create a ne wstream of it.
packsize | Number of elements to pack |
stream< T > & matador::stream< T >::peek | ( | Predicate && | pred | ) |
Peeks each valid element with the given predicate. This processor can be used to check or debug each stream element.
Predicate | Type of the predicate |
pred | Condition to check |
void matador::stream< T >::print_to | ( | std::ostream< T > & | out, |
const char * | delim = " " |
||
) |
Prints all elements of the stream to the given std::ostream. The elements are separated by the given delimiter string. The default delimiter is a blank.
out | The ostream to write to |
delim | The delimiter for the elements |
|
inline |
Returns the current sentinel processor of the stream This processor represents the last stream processor and contains its successor to ensure a valid processing chain for each element.
optional< T > matador::stream< T >::reduce | ( | Accumulator && | accu | ) |
All elements of the stream are reduced to one element using the given accumulator function. This function takes the current element and the current accumulated value and returns the new accumulated value. The returned value is an optional because the stream could be empty and thus the result.
Accumulator | Type of the accumulator |
accu | The accumulator function to be applied |
U matador::stream< T >::reduce | ( | const U & | identity, |
Accumulator && | accu | ||
) |
All elements of the stream are reduced to one element using the given accumulator function. This function takes the current element and the current accumulated value and returns the new accumulated value. The initial value for this reduction is the given identity value. If the stream is empty, at least this value is returned.
U | Type of the return and identity value |
Accumulator | Type of the accumulator function |
identity | Initial value |
accu | The accumulator function to be applied |
optional< R > matador::stream< T >::reduce_idfunc | ( | const U & | identity_fun, |
Accumulator && | accu | ||
) |
All elements of the stream are reduced to one element using the given accumulator function. This function takes the current element and the current accumulated value and returns the new accumulated value. To create the initial value the given identity function is used. The returned value is an optional because the stream could be empty and thus the result.
U | Type of the identity function |
Accumulator | Type of the accumulator function |
R | Type of the returned value |
identity_fun | Function to create the initial value |
accu | The accumulator function to be applied |
stream< T > & matador::stream< T >::skip | ( | std::size_t | count | ) |
Skip the first number elements of the stream defined by the given count.
count | Number of elements to skip |
stream< T > & matador::stream< T >::skip_while | ( | Predicate | pred | ) |
Skip elements of the stream while given predicate is true. Once the predicate gets false the capacity elements are valid.
Predicate | Type of the predicate |
pred | Condition to check |
stream< T > & matador::stream< T >::take | ( | std::size_t | count | ) |
Take only count elements of the data stream. Than the stream id finished.
count | Number of elements to be taken |
stream< T > & matador::stream< T >::take_while | ( | Predicate | pred | ) |
Take only elements of the stream as long as the given predicate if true
Predicate | Type of the predicate |
pred | Condition to check |