libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
z::core::generator< T, S > Class Template Reference

An arbitrary generator for producing sequential results on-the-fly. More...

#include <generator.hpp>

Inheritance diagram for z::core::generator< T, S >:
z::core::iterable< generatorIter< T, S > >

Public Member Functions

 generator (const S &initial, std::function< const yield< T >(S &)> lambda)
 Constructor with an initial state.
 
generatorIter< T, Sbegin () const noexcept override
 Begin iterator (start of the range)
 
generatorIter< T, Send () const noexcept override
 End iterator (end of the range)
 
yield< Tnext ()
 Get the next item from the generator.
 
long count ()
 Get the total count of items that will be generated.
 
array< Tcollect ()
 Concatenate all generator elements into an array.
 
long consume ()
 Consume and discard all items from the generator.
 
array< Ttake (int count)
 Take a certain number of items from the generator.
 
template<typename U >
generator< U, Smap (std::function< U(const T &)> mapLambda)
 Applies a transformation function to each item that comes out of the generator.
 
generator filter (std::function< T(const T &)> filterLambda)
 Filters the generatred items based on a predicate and returns a new generator that yields only the items that satisfy the predicate.
 
T reduce (const T &defaultValue, std::function< T(const T &, const T &)> reduceLambda)
 Reduces the generator to a single value by applying a binary operation cumulatively to all yielded values.
 
generatorforEach (std::function< void(const T &)> newLambda)
 Binds a function to run each time an item comes out of the generator.
 
generator< T, countedState > skip (long count)
 Skips a certain number of items from the generator.
 
generator< T, countedState > limit (long count)
 Limits the number of items that the generator will yield.
 
template<typename U , typename S2 >
generator< std::pair< T, U >, generator< U, S2 > > zip (generator< U, S2 > &other)
 Zip this generator with another generator.
 
generator< std::pair< long, T >, std::pair< long, generator< T, S > > > enumerate ()
 Enumerate the items in this generator.
 
generator< T, std::pair< generator, yield< T > > > diff (generator &other)
 List the items in this generator which differ from another generator.
 
generator< array< T >, generatorchunk (long chunkSize)
 Get chunks of items from the generator.
 
generator< long, generator< long, long >::countedState > skip (long count)
 Template specialization for the skip function with generators that only output longs.
 
- Public Member Functions inherited from z::core::iterable< generatorIter< T, S > >
virtual ~iterable () noexcept
 Virtual destructor.
 

Detailed Description

template<typename T, typename S>
class z::core::generator< T, S >

An arbitrary generator for producing sequential results on-the-fly.

This class encapsulates generator functionality with any state data the generator may require. Some ideal uses for this class include looping over large sets of data that can't all be loaded at runtime, or loading data in batches while still outputting only a small bit at a time.

Template Parameters
TThe type of data that the generator returns.
SThe state data for the generator.

Constructor & Destructor Documentation

◆ generator()

template<typename T , typename S >
z::core::generator< T, S >::generator ( const S initial,
std::function< const yield< T >(S &)>  lambda 
)
inline

Constructor with an initial state.

Parameters
initialThe initial value of the generator's state.
lambdaThe generator function.

Member Function Documentation

◆ begin()

template<typename T , typename S >
generatorIter< T, S > z::core::generator< T, S >::begin ( ) const
inlineoverridevirtualnoexcept

Begin iterator (start of the range)

Returns
An iterator that will give the first value in the generator.

Implements z::core::iterable< generatorIter< T, S > >.

◆ chunk()

template<typename T , typename S >
generator< array< T >, generator > z::core::generator< T, S >::chunk ( long  chunkSize)
inline

Get chunks of items from the generator.

This function will yield chunks of items from the generator, where each chunk is an array of items of at most the specified size. If the generator runs out of items, the last chunk may contain fewer items.

Parameters
chunkSizeThe size of each chunk.
Returns
A new generator that yields arrays of items, each of at most the specified size.

◆ collect()

template<typename T , typename S >
array< T > z::core::generator< T, S >::collect ( )
inline

Concatenate all generator elements into an array.

Note
This function will consume the generator, and it will not be able to be used again.
Returns
An array containing all elements from the generator.

◆ consume()

template<typename T , typename S >
long z::core::generator< T, S >::consume ( )
inline

Consume and discard all items from the generator.

Note
This function will consume the generator, and it will not be able to be used again.
Returns
The number of items that were generated.

◆ count()

template<typename T , typename S >
long z::core::generator< T, S >::count ( )
inline

Get the total count of items that will be generated.

Warning
This function will consume the generator, and it will not be able to be used again.
Returns
The number of items that were generated.

◆ diff()

template<typename T , typename S >
generator< T, std::pair< generator, yield< T > > > z::core::generator< T, S >::diff ( generator< T, S > &  other)
inline

List the items in this generator which differ from another generator.

This function is only useful for generators that yield items of the same type. (if the generators yielded different types, ALL items would be considered different!)

As an example of how this works, if you have two generators that yield strings, generator1 yields "apple", "banana", "cherry", "melon", and generator2 yields "banana", "cherry", "date", "fig", then calling generator1.diff(generator2) will yield "apple" and "melon".

Note
If the other generator runs out of items, the rest of the items from this generator will be yielded, as they are considered different from nothing.
Parameters
otherThe other generator to compare against.
Returns
A new generator that yields only items that are different from the items in the other generator.

◆ end()

template<typename T , typename S >
generatorIter< T, S > z::core::generator< T, S >::end ( ) const
inlineoverridevirtualnoexcept

End iterator (end of the range)

Returns
A dummy iterator indicating the end of the range.

Implements z::core::iterable< generatorIter< T, S > >.

◆ enumerate()

template<typename T , typename S >
generator< std::pair< long, T >, std::pair< long, generator< T, S > > > z::core::generator< T, S >::enumerate ( )
inline

Enumerate the items in this generator.

This function wraps the existing generator in another generator that yields pairs of indices and items. The first item will have index 0, the second item will have index 1, and so on.

Returns
A new generator that yields pairs of indices and items.

◆ filter()

template<typename T , typename S >
generator z::core::generator< T, S >::filter ( std::function< T(const T &)>  filterLambda)
inline

Filters the generatred items based on a predicate and returns a new generator that yields only the items that satisfy the predicate.

This function wraps the existing generator in another generator, and as each item is generated, applies the given lambda function as a predicate, and only yields items that satisfy the predicate.

Parameters
filterLambdaA function that takes a constant reference to an item of type T and returns a boolean indicating whether the item should be yielded.
Returns
A new generator that yields only items that satisfy the predicate.

◆ forEach()

template<typename T , typename S >
generator & z::core::generator< T, S >::forEach ( std::function< void(const T &)>  newLambda)
inline

Binds a function to run each time an item comes out of the generator.

This function wraps the existing generator function in another function, effectively binding extra logic to this generator. each item on-the-fly as it's generated.

Note
This function does not consume the generator directly, it just runs the given function each time an item is generated.
Parameters
newLambdaA function that takes a constant reference to an element of type T and returns nothing.
Returns
A reference to this generator, with the new function bound.

◆ limit()

template<typename T , typename S >
generator< T, countedState > z::core::generator< T, S >::limit ( long  count)
inline

Limits the number of items that the generator will yield.

Parameters
countThe maximum number of items to yield.
Returns
A new generator that yields the given number of items.

◆ map()

template<typename T , typename S >
template<typename U >
generator< U, S > z::core::generator< T, S >::map ( std::function< U(const T &)>  mapLambda)
inline

Applies a transformation function to each item that comes out of the generator.

This function wraps the existing generator in another generator, effectively transforming each item on-the-fly as it's generated.

Template Parameters
UThe type of items that this new generator yields.
Parameters
mapLambdaA function that takes a constant reference to an element of type T and returns an element of type U.
Returns
A new generator that yields the transformed elements.

◆ next()

template<typename T , typename S >
yield< T > z::core::generator< T, S >::next ( )
inline

Get the next item from the generator.

If there are no more items, the .done field of this yield object will be true.

Returns
A yield object containing the next value, if any.

◆ reduce()

template<typename T , typename S >
T z::core::generator< T, S >::reduce ( const T defaultValue,
std::function< T(const T &, const T &)>  reduceLambda 
)
inline

Reduces the generator to a single value by applying a binary operation cumulatively to all yielded values.

This function applies a binary operation (provided as a lambda) to combine all yielded items into a single value. If the generator doesn yield anything, the provided default value is returned.

Note
Especially for long lists of items, this is significantly more memory-efficient than calling collect()and then reduce() on the resulting array, as an intermediate array does not need to be constructed.
Parameters
defaultValueThe value to return if the array is empty.
reduceLambdaA function that takes two elements of type T and returns their combined result of type T.
Returns
The result of the reduction operation.

◆ skip() [1/2]

template<typename T , typename S >
generator< T, countedState > z::core::generator< T, S >::skip ( long  count)
inline

Skips a certain number of items from the generator.

This function will skip a certain number of items from the generator, or all items if there are fewer than the requested count.

Note
This function does not consume the generator directly, it just skips items as they are generated.
Parameters
countThe number of items to skip.
Returns
A new generator that skips the given number of items.

◆ skip() [2/2]

generator< long, generator< long, long >::countedState > z::core::generator< long, long >::skip ( long  count)

Template specialization for the skip function with generators that only output longs.

Parameters
countThe number of items to skip.
Returns
A new generator that skips the given number of items.

◆ take()

template<typename T , typename S >
array< T > z::core::generator< T, S >::take ( int  count)
inline

Take a certain number of items from the generator.

This function will take a certain number of items from the generator, or all items if there are fewer than the requested count.

Parameters
countThe number of items to take.
Returns
An array containing the taken items.

◆ zip()

template<typename T , typename S >
template<typename U , typename S2 >
generator< std::pair< T, U >, generator< U, S2 > > z::core::generator< T, S >::zip ( generator< U, S2 > &  other)
inline

Zip this generator with another generator.

This function combines two generators into a single generator that yields pairs of items from both generators. If one generator runs out of items, the resulting generator will stop yielding items.

Parameters
otherThe other generator to zip with.
Returns
A new generator that yields pairs of items from both generators.

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