An arbitrary generator for producing sequential results on-the-fly. More...
#include <generator.hpp>
Classes | |
| struct | countedState |
| A specialized state for generator::enumerate() More... | |
Public Member Functions | |
| generator (const S &initial, std::function< const std::optional< T >(S &)> lambda) | |
| Constructor with an initial state. | |
| generatorIter< T, S > | begin () const noexcept override |
| Begin iterator (start of the range) | |
| generatorIter< T, S > | end () const noexcept override |
| End iterator (end of the range) | |
| virtual std::optional< T > | next () |
| Get the next item from the generator. | |
| long | count () |
| Consume and discard all items from the generator, getting only the number of items generated. | |
| array< T > | collect () |
| Concatenate all generator elements into an array. | |
| long | consume () |
| array< T > | take (int count) |
| Take a certain number of items from the generator. | |
| template<typename U > | |
| generator< U, S > | map (std::function< U(const T &)> mapLambda) noexcept |
| Applies a transformation function to each item that comes out of the generator. | |
| generator | filter (std::function< bool(const T &)> filterLambda) noexcept |
| Filters the generatred items based on a predicate and returns a new generator that yields only the items that satisfy the predicate. | |
| T | reduce (std::function< T(const T &, const T &)> reduceLambda, const T &defaultValue={}) |
| Reduces the generator to a single value by applying a binary operation cumulatively to all yielded values. | |
| generator & | forEach (std::function< void(const T &)> newLambda) noexcept |
| Binds a function to run each time an item comes out of the generator. | |
| generator< T, countedState > | skip (long count) noexcept |
| Skips a certain number of items from the generator. | |
| generator< T, countedState > | limit (long count) noexcept |
| Limits the number of items that the generator will std::optional. | |
| template<typename U , typename S2 > | |
| generator< std::pair< T, U >, generator< U, S2 > > | pair (generator< U, S2 > &other) noexcept |
| Pair items from this generator with those of another generator. | |
| generator< T, std::pair< bool, generator & > > | zip (generator &other) noexcept |
| Zip this generator with another generator. | |
| generator< std::pair< long, T >, std::pair< long, generator< T, S > > > | enumerate () noexcept |
| Enumerate the items in this generator. | |
| generator< T, std::pair< generator, std::optional< T > > > | diff (generator &other) noexcept |
| List the items in this generator which differ from another generator. | |
| generator< array< T >, generator > | chunk (long chunkSize) noexcept |
| Get chunks of items from the generator. | |
| template<typename U = T, typename std::enable_if< is_iterator< U >::value, int >::type = 0> | |
| auto | flatten () noexcept |
| Break up a chunked generator into its constituent generated items. | |
| generator< std::pair< T, std::optional< T > >, std::pair< std::optional< T >, generator > > | peek () noexcept |
| Allow peeking at the next item in the generator as items are generated. | |
| template<typename U > | |
| generator< T, std::pair< generator, bool > > | chain (generator< T, U > &other) noexcept |
| Chains two generators together. | |
| template<typename U > | |
| generator< T, std::pair< std::pair< generator, bool >, generator< T, U > > > | chain (generator< T, U > &&other) noexcept |
| Chains two generators together. | |
| generator | until (std::function< bool(T)> predicate) noexcept |
| End the generator when the given predicate returns true. | |
| generator | until (const T &sentinel) noexcept |
| End the generator when the given value is yielded from the generator. | |
| template<typename U > | |
| generator< T, std::pair< generator, bool > > | operator+ (generator< T, U > &other) noexcept |
| template<typename U > | |
| generator< T, std::pair< std::pair< generator, bool >, generator< T, U > > > | operator+ (generator< T, U > &&other) noexcept |
| template<typename U > | |
| generator< U, S > | operator| (std::function< U(T)> mapLambda) noexcept |
| template<typename U > | |
| generator< U, S > | operator| (U(*mapLambda)(T)) noexcept |
| generator | operator&& (std::function< T(const T &)> filterLambda) noexcept |
| T | operator>> (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. | |
| generator | operator!= (std::function< bool(T)> predicate) |
| generator | operator!= (const T &sentinel) |
| generator< long, generator< long, long >::countedState > | skip (long count) noexcept |
| Template specialization for the skip function with generators that only output longs. | |
Protected Attributes | |
| S | state |
| The state data of this generator. | |
| std::function< const std::optional< T >(S &)> | lambda |
| The function that gets run every time an item is generated. | |
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.
| T | The type of data that the generator returns. |
| S | The state data for the generator. |
|
inline |
Constructor with an initial state.
| initial | The initial value of the generator's state. |
| lambda | The generator function. |
|
inlineoverridevirtualnoexcept |
Begin iterator (start of the range)
Implements z::core::iterable< generatorIter< T, S > >.
|
inlinenoexcept |
Chains two generators together.
This function takes two generators and chains them together, so that multiple generators can be used as a single generator. That is, the items from this are generated first, and once this generator is exhausted, items from the other generator are generated until exhaustion.
| U | The state type of the other generator. |
| other | The generator to chain after this generator. |
|
inlinenoexcept |
Chains two generators together.
This function takes two generators and chains them together, so that multiple generators can be used as a single generator. That is, the items from this are generated first, and once this generator is exhausted, items from the other generator are generated until exhaustion.
| U | The state type of the other generator. |
| other | The generator to chain after this generator. |
|
inlinenoexcept |
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.
| chunkSize | The size of each chunk. |
Concatenate all generator elements into an array.
Consume and discard all items from the generator, getting only the number of items generated.
Consume and discard all items from the generator, getting only the number of items generated.
|
inlinenoexcept |
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".
| other | The other generator to compare against. |
|
inlineoverridevirtualnoexcept |
End iterator (end of the range)
Implements z::core::iterable< generatorIter< T, S > >.
|
inlinenoexcept |
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.
|
inlinenoexcept |
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.
| filterLambda | A function that takes a constant reference to an item of type T and returns a boolean indicating whether the item should be yielded. |
|
inlinenoexcept |
Break up a chunked generator into its constituent generated items.
|
inlinenoexcept |
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.
| newLambda | A function that takes a constant reference to an element of type T and returns nothing. |
|
inlinenoexcept |
Limits the number of items that the generator will std::optional.
| count | The maximum number of items to std::optional. |
|
inlinenoexcept |
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.
| U | The type of items that this new generator yields. |
| mapLambda | A function that takes a constant reference to an element of type T and returns an element of type U. |
|
inlinevirtual |
Get the next item from the generator.
If there are no more items, the optional object will not contain anything.
End the generator when the given value is yielded from the generator.
| sentinel | The value to check for. |
End the generator when the given predicate returns true.
| predicate | A function or lambda that returns true if the generator should stop yielding items, or false if it should continue. |
|
inlinenoexcept |
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.
| filterLambda | A function that takes a constant reference to an item of type T and returns a boolean indicating whether the item should be yielded. |
|
inlinenoexcept |
Chains two generators together.
This function takes two generators and chains them together, so that multiple generators can be used as a single generator. That is, the items from this are generated first, and once this generator is exhausted, items from the other generator are generated until exhaustion.
| U | The state type of the other generator. |
| other | The generator to chain after this generator. |
|
inlinenoexcept |
Chains two generators together.
This function takes two generators and chains them together, so that multiple generators can be used as a single generator. That is, the items from this are generated first, and once this generator is exhausted, items from the other generator are generated until exhaustion.
| U | The state type of the other generator. |
| other | The generator to chain after this generator. |
|
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't yield anything, a default value (created via the default {} constructor) is returned.
| reduceLambda | A function that takes two elements of type T and returns their combined result of type T. |
|
inlinenoexcept |
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.
| U | The type of items that this new generator yields. |
| mapLambda | A function that takes a constant reference to an element of type T and returns an element of type U. |
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.
| U | The type of items that this new generator yields. |
| mapLambda | A function that takes a constant reference to an element of type T and returns an element of type U. |
|
inlinenoexcept |
Pair items from this generator with those of 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.
| other | The other generator to pair with. |
|
inlinenoexcept |
Allow peeking at the next item in the generator as items are generated.
The generator that this function produces will yield std::pairs containing (1) the current value, and (2) a yield object containing the next value, if any. It is up to the programmer to properly check that the yield has a value.
|
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't yield anything, the provided default value is returned.
| reduceLambda | A function that takes two elements of type T and returns their combined result of type T. |
| defaultValue | The value to return if the array is empty. |
|
inlinenoexcept |
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.
| count | The number of items to skip. |
|
noexcept |
Template specialization for the skip function with generators that only output longs.
| count | The number of items to skip. |
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.
| count | The number of items to take. |
End the generator when the given value is yielded from the generator.
| sentinel | The value to check for. |
End the generator when the given predicate returns true.
| predicate | A function or lambda that returns true if the generator should stop yielding items, or false if it should continue. |
|
inlinenoexcept |
Zip this generator with another generator.
This function combines two generators into a single generator that yields items from each generator in an alternating pattern. If one generator runs out of items, then only items from the other generator will be yielded.
| other | The other generator to zip with. |