matador::stream< T > Class Template Reference

#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
 
streamoperator= (const stream &x)=default
 
streamoperator= (stream &&x) noexcept=default
 
 stream (std::shared_ptr< detail::stream_element_processor< T > > processor)
 
streamoperator= (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 >
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)
 

Detailed Description

template<class T>
class matador::stream< T >

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.

Template Parameters
TType of the stream elements

Member Typedef Documentation

◆ iterator

template<class T >
typedef detail::stream_element_processor_iterator<T> matador::stream< T >::iterator

Shortcut to stream iterator type

Constructor & Destructor Documentation

◆ stream() [1/4]

template<class T >
matador::stream< T >::stream ( )
default

Default constructor

◆ stream() [2/4]

template<class T >
matador::stream< T >::stream ( const stream< T > &  x)
default

Copys the steam from given stream x

Parameters
xStream to copy from

◆ stream() [3/4]

template<class T >
matador::stream< T >::stream ( stream< T > &&  x)
defaultnoexcept

Copy move stream from given stream x

Parameters
xStream to copy move from

◆ stream() [4/4]

template<class T >
matador::stream< T >::stream ( std::shared_ptr< detail::stream_element_processor< T > >  processor)
explicit

Initialize a new stream with the given stream processor

Parameters
processorThe initial stream processor

Member Function Documentation

◆ all()

template<class T >
template<typename Predicate >
bool matador::stream< T >::all ( Predicate &&  pred)

Returns true if all elements of the stream matches the given predicate.

Template Parameters
PredicateType of the predicate
Parameters
predCondition to check
Returns
True if all elements matches

◆ any()

template<class T >
template<typename Predicate >
bool matador::stream< T >::any ( Predicate &&  pred)

Returns true if any element of the stream matches the given predicate.

Template Parameters
PredicateType of the predicate
Parameters
predCondition to check
Returns
True if any element matches

◆ at()

template<class T >
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

Parameters
indexPosition of the requested element
Returns
An optional possibly containing the value.

◆ begin()

template<class T >
stream< T >::iterator matador::stream< T >::begin ( )

Returns the begin iterator of the stream

Returns
Begin iterator of the stream

◆ collect() [1/2]

template<class T >
template<template< class ... > class C, class Allocator >
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>

Template Parameters
CType of the container
AllocatorAllocator type
Returns
The container containing all elements

◆ collect() [2/2]

template<class T >
template<template< class ... > class C, class Key , class Value , typename KeyFunc , typename ValueFunc >
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.

Template Parameters
CType of the container
KeyKey type of the associative container
ValueValue type of the associative container
Parameters
keyFuncThe function returning the key
valueFuncThe function returning the value
Returns
An associative container containing all transformed elements

◆ concat()

template<class T >
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.

Parameters
otherThe stream to concatenate
Returns
The enhanced stream

◆ count() [1/2]

template<class T >
std::size_t matador::stream< T >::count ( )

Returns the number of elements in the stream

Returns
The number of elements

◆ count() [2/2]

template<class T >
template<typename Predicate >
std::size_t matador::stream< T >::count ( Predicate &&  pred)

Returns the number of elements matching the given predicate.

Template Parameters
PredicateType of the predicate
Parameters
predCondition to check
Returns
Number of matching elements

◆ end()

template<class T >
stream< T >::iterator matador::stream< T >::end ( )

Returns the end iterator of the stream

Returns
End iterator of the stream

◆ every()

template<class T >
stream< T > & matador::stream< T >::every ( std::size_t  count)

Returns every nth elements of the stream identified by the given count

Parameters
countIdentifies which elements are valid
Returns
The enhanced stream

◆ filter()

template<class T >
template<typename Predicate >
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

Template Parameters
PredicateType of the predicate
Parameters
predCondition to check
Returns
The enhanced stream

◆ first()

template<class T >
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.

Returns
An optional possibly containing the first element.

◆ flatmap()

template<class T >
template<typename Predicate , typename R >
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

Template Parameters
PredicateType of the predicate
RReturn type of the predicate and type of the new stream
Parameters
predPredicate to apply on every stream element
Returns
New stream created of the result of given predicate

◆ for_each()

template<class T >
template<typename Predicate >
void matador::stream< T >::for_each ( Predicate &&  pred)

Iterates over all stream elements and applies the given predicate to each element.

Template Parameters
PredicateType of predicate
Parameters
predFunction to apply

◆ join() [1/3]

template<class T >
std::string matador::stream< T >::join ( )

Joins all stream elements into one string.

Returns
All stream elements in one string

◆ join() [2/3]

template<class T >
std::string matador::stream< T >::join ( const std::string &  delimiter)

Joins all stream elements delimited by the given delimiter into one string.

Parameters
delimiterDelimiter for the elements
Returns
All stream elements in one string

◆ join() [3/3]

template<class T >
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.

Parameters
delimiterDelimiter for the elements
prefixPrefix for the string
suffixSuffix for the string
Returns
All stream elements in one string

◆ last()

template<class T >
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.

Returns
An optional possibly containing the last element.

◆ map()

template<class T >
template<typename Predicate , typename R >
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.

Template Parameters
PredicateType of the predicate
RReturn type of the predicate and type of the new stream
Parameters
predPredicate to apply on every stream element
Returns
New stream created of the result of given predicate

◆ max()

template<class T >
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.

Returns
An optional possibly containing the maximum value.

◆ min()

template<class T >
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.

Returns
An optional possibly containing the minimum value.

◆ none()

template<class T >
template<typename Predicate >
bool matador::stream< T >::none ( Predicate &&  pred)

Returns true if none of the elements of the stream matches the given predicate.

Template Parameters
PredicateType of the predicate
Parameters
predCondition to check
Returns
True if none element matches

◆ operator=() [1/3]

template<class T >
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

Parameters
processorStream processor to assign from
Returns
The assigned stream

◆ operator=() [2/3]

template<class T >
stream & matador::stream< T >::operator= ( const stream< T > &  x)
default

Copy assigns stream from given stream x

Parameters
xStream to copy assign from
Returns
The copy assigned stream

◆ operator=() [3/3]

template<class T >
stream & matador::stream< T >::operator= ( stream< T > &&  x)
defaultnoexcept

Assign move stream from given stream x

Parameters
xStream to assign move from
Returns
The assign moved stream

◆ pack_every()

template<class T >
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.

Parameters
packsizeNumber of elements to pack
Returns
Stream of packed elements

◆ peek()

template<class T >
template<typename Predicate >
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.

Template Parameters
PredicateType of the predicate
Parameters
predCondition to check
Returns
The enhanced stream

◆ print_to()

template<class T >
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.

Parameters
outThe ostream to write to
delimThe delimiter for the elements

◆ processor()

template<class T >
std::shared_ptr< detail::stream_element_processor< T > > & matador::stream< T >::processor ( )
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.

Returns
The sentinel stream element processor

◆ reduce() [1/2]

template<class T >
template<typename Accumulator >
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.

Template Parameters
AccumulatorType of the accumulator
Parameters
accuThe accumulator function to be applied
Returns
An optional possibly containing the accumulated value.

◆ reduce() [2/2]

template<class T >
template<typename U , typename Accumulator >
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.

Template Parameters
UType of the return and identity value
AccumulatorType of the accumulator function
Parameters
identityInitial value
accuThe accumulator function to be applied
Returns
The reduced value, at least the given identity value

◆ reduce_idfunc()

template<class T >
template<typename U , typename Accumulator , typename R >
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.

Template Parameters
UType of the identity function
AccumulatorType of the accumulator function
RType of the returned value
Parameters
identity_funFunction to create the initial value
accuThe accumulator function to be applied
Returns
An optional possibly containing the accumulated value.

◆ skip()

template<class T >
stream< T > & matador::stream< T >::skip ( std::size_t  count)

Skip the first number elements of the stream defined by the given count.

Parameters
countNumber of elements to skip
Returns
The enhanced stream

◆ skip_while()

template<class T >
template<typename Predicate >
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.

Template Parameters
PredicateType of the predicate
Parameters
predCondition to check
Returns
The enhanced stream

◆ take()

template<class T >
stream< T > & matador::stream< T >::take ( std::size_t  count)

Take only count elements of the data stream. Than the stream id finished.

Parameters
countNumber of elements to be taken
Returns
The stream enhanced with the take processor.

◆ take_while()

template<class T >
template<typename Predicate >
stream< T > & matador::stream< T >::take_while ( Predicate  pred)

Take only elements of the stream as long as the given predicate if true

Template Parameters
PredicateType of the predicate
Parameters
predCondition to check
Returns
The enhanced stream

The documentation for this class was generated from the following file:
  • matador/utils/stream.hpp