Table of Contents

Class Generator

Namespace
Gamelogic.Extensions.Algorithms
Assembly
Gamelogic.Extensions.dll

Contains static methods for creating generators, and extension methods to create generators from existing ones.

[Version(2, 0, 0)]
public static class Generator
Inheritance
Generator
Inherited Members

Remarks

A generator is a class that generates data on demand. In this implementation, generators implement the IGenerator interface, and through an extension method supports a Next method that returns an element every time it is called.

Generators are infinite.

Many methods make methods from existing generators. The existing generators are always cloned, so no derived generator will call the Next<TSource>(IGenerator<TSource>) or MoveNext() methods of an existing generator. Clones are always returned in the restarted state.

Although Generators have almost the same interface as IEnumerator, there are some differences. Generators are always in a "valid" state, so calls to Current will always return a valid result, there is no "before first" and "after last" states.

You can also implement your own generators. There are two ways to do this. The first is to simply use the static methods provided; they allow you to make new generators by manipulating existing ones.

The second way is appropriate for more complicated situations, and that is to define your own class that implements the IGenerator<TResult> interface.

Methods

Aggregate<TSource>(IGenerator<TSource>, Func<TSource, TSource, TSource>)

Makes a generator that generates a running aggregate of the source generator.

Aggregate<TSource>(IGenerator<TSource>, Func<TSource, TSource, TSource>, TSource)

Makes a generator that generates a running aggregate of the source generator.

Aggregate<TSource, TResult>(IGenerator<TSource>, Func<TResult, TSource, TResult>, TResult)

Makes a generator that generates a running aggregate of the source generator.

Apply<TSource>(IGenerator<TSource>, Action<TSource>)

Makes a generator that applies a function on the elements it generates.

Average(IGenerator<int>)

Makes a generator the will generate the average of elements generated by another generator.

Average(IGenerator<float>)

Makes a generator the will generate the average of elements generated by another generator.

Cast<TResult>(IGenerator)

Makes a generator that will generate elements by casting the elements of a source generator.

ChooseUniformRandom<TSource>(params IGenerator<TSource>[])

Chooses the from the given generators selected uniform randomly.

ChooseUniformRandom<TSource>(IList<IGenerator<TSource>>)

Makes a generator that selects a random generator from a given element to generate an element from.

ChooseUniformRandom<TSource>(IList<IGenerator<TSource>>, int)

Chooses the random.

ChooseUniformRandom<TSource>(IList<TSource>)

Make a generator that randomly generates elements from a list.

ChooseUniformRandom<TSource>(IList<TSource>, int)

Make a generator that randomly generates elements from a list. Can be seeded.

Choose<TSource>(IList<IGenerator<TSource>>, IGenerator<int>)

Makes a generator that uses an index generator to choose a generator to generate an element from.

Choose<TSource>(IList<TSource>, IGenerator<int>)

Makes a generator that chooses elements from a list using an index generator.

ClosedSawTooth(int)

Makes a generator that produces evenly spaced floats from 0 to 1, both limits included, and repeats the result.

Combine<TSource, TResult>(IEnumerable<IGenerator<TSource>>, Func<IList<TSource>, TResult>)

Makes a generator that combines the elements of specified generators.

Combine<T1, T2, TResult>(IGenerator<T1>, IGenerator<T2>, Func<T1, T2, TResult>)

Combines the specified generators by applying a result selector function to the elements of each generator.

Combine<T1, T2, T3, TResult>(IGenerator<T1>, IGenerator<T2>, IGenerator<T3>, Func<T1, T2, T3, TResult>)

Combines the specified generators by applying a result selector function to the elements of each generator.

Combine<T1, T2, T3, T4, TResult>(IGenerator<T1>, IGenerator<T2>, IGenerator<T3>, IGenerator<T4>, Func<T1, T2, T3, T4, TResult>)

Combines the specified generators by applying a result selector function to the elements of each generator.

Constant<TSource>(TSource)

Makes a generator that generates a the same item each time.

Count(int)

Makes a generator that generates consecutive integers starting from zero up to a limit, and repeats the cycle.

Dither(IGenerator<float>, int, IEnumerable<float>)

Takes the source generator's output, and generate a dithered sequence of integers in the range 0 to levels - 1. Uses error diffusion.

FrequencyRandomInt(IEnumerable<float>)

Generates random integers at relative frequencies provided.

FrequencyRandomInt(IEnumerable<float>, int)

Generates random integers at relative frequencies provided.

FromFunc<TResult>(Func<TResult>)

Makes a new generator from a generator function.

GaussianRandomFloat(float, float)

Makes a generator that generates floats with a Gaussian distribution.

Group<TSource>(IGenerator<TSource>, IGenerator<int>)

Makes a generator that generates groups of items from the source generator.

Group<TSource>(IGenerator<TSource>, int)

Makes a generator that returns groups of elements from the source generator.

Interleave<TSource>(IGenerator<TSource>, params IGenerator<TSource>[])

Makes a generator that interleaves the elements of the specified generators.

Interleave<TSource>(IList<IGenerator<TSource>>)

Makes a generator that interleaves the elements of the specified generators.

InterpolateDither<TSource>(IGenerator<TSource>, IGenerator<int>)

Interpolates a sequence, but applies dithering.

Interpolate<TSource>(IGenerator<TSource>, IGenerator<int>, Func<TSource, TSource, float, TSource>)

Makes a generator that interpolates between values of a given generator.

Interpolate<TSource>(IGenerator<TSource>, int, Func<TSource, TSource, float, TSource>)

Makes a generator that interpolates between values of a given generator.

Iterate<TSource>(IEnumerable<TSource>, Func<IList<TSource>, TSource>)

Makes a generator that uses an iterator function to generate elements.

Iterate<TSource>(TSource, Func<TSource, TSource>)

Makes a generator that returns iterations of the specified initial element.

Iterate<TSource>(TSource, TSource, Func<TSource, TSource, TSource>)

Makes a generator that uses an iterator function to generate elements.

Iterate<TSource>(TSource, TSource, TSource, Func<TSource, TSource, TSource, TSource>)

Makes a generator that uses an iterator function to generate elements.

Iterate<TSource>(TSource, TSource, TSource, TSource, Func<TSource, TSource, TSource, TSource, TSource>)

Makes a generator that uses an iterator function to generate elements.

Iterate<TSource, TSource2>(TSource, IGenerator<TSource2>, Func<TSource, TSource2, TSource>)

Applies a function on the last element to produce the next element. The function takes twp parameters; the second parameter is supplied by a generator.

Log<TSource>(IGenerator<TSource>)

A generator that generates the elements of the source generator, but sends the generated element to the Unity Console.

Log<TSource>(IGenerator<TSource>, Action<TSource>)

A generator that generates the elements of the source generator, but applies a log function to each element as it is generated.

MarkovRandomInt(float[,])

Generates a Markov chain of integers from a transition table.

MarkovRandomInt(float[,], int)

Generates a Markov chain of integers from a transition table.

MarkovRandomIntStartsWith(float[,], int)

Makes a Markov generator that starts with the given value.

MarkovRandomIntStartsWith(float[,], int, int)

Makes a Markov generator that starts with the given value.

MoveNext<TSource>(IGenerator<TSource>, int)

Moves the generator by a specified amount forward.

NextWhile<TSource>(IGenerator<TSource>, Func<TSource, bool>)

Generates the elements from the generator while the predicate applied to elements hold an return them in an enumerable. After calling this method, the next element returned by Next (or the current value of Current) will not satisfy the predicate.

Next<TSource>(IGenerator<TSource>)

Returns the next element of the specified generator.

Next<TSource>(IGenerator<TSource>, int)

Returns a list of the next n items from the generator.

OfType<TResult>(IGenerator)

Makes a generator that will generate elements of a source generator that is of the given type.

OpenSawTooth(IGenerator<int>)

Makes a generator that produces evenly spaced floats from 0 (included) to 1 (excluded), and repeats the result (but with the number of samples each time given by a generator).

OpenSawTooth(int)

Makes a generator that produces evenly spaced floats from 0 (included) to 1 (excluded), and repeats the result.

Pad<TSource>(IGenerator<TSource>, IEnumerable<TSource>)

Pads the specified generator with elements from a given list.

Pad<TSource>(IGenerator<TSource>, TSource, int)

Pads the specified generator with a constant element repeated a specified number of times.

Poisson(int, int)

A boolean generator that returns true at intervals uniformly distributed between minRadius and maxRadius.

RandomBoolGenerator(float)

Makes a generator that returns random boolean values, true with the specified probability.

RandomBoolGenerator(float, int)

Makes a generator that returns random boolean values, true with the specified probability.

RepeatEach<TSource>(IGenerator<TSource>, IGenerator<int>)

Makes a new generator that will repeat each of the given generators elements a number of times.

RepeatEach<TSource>(IGenerator<TSource>, int)

Makes a new generator that will repeat each of the given generators elements a number of times.

Repeat<TSource>(IEnumerable<TSource>)

Makes a generator that repeats elements of the given list over and over.

SelectMany<TSource, TResult>(IGenerator<TSource>, Func<TSource, IEnumerable<TResult>>)

For each item in the source generator, a list of items is generated, but the items are generated one by one (and not as a list of items).

Select<TResult>(IGenerator<float>, ResponseCurveBase<TResult>)

Makes a new generator by transforming the elements of a given float generator using a response curve.

Select<TSource, TResult>(IGenerator<TSource>, Func<TSource, TResult>)

Makes a generator which generates items that are transformed, generated from a given generator.

SkipAndTake<TSource>(IGenerator<TSource>, int, int)

Makes a generator that repeatedly skips over and takes elements from a given generator.

Skip<TSource>(IGenerator<TSource>, int)

Makes a generator that skips over the specified number of elements from the source generator.

Sum(IGenerator<int>)

Makes a generator that will generate partial sums of a given generator.

Sum(IGenerator<float>)

Makes a generator that will generate partial sums of a given generator.

SwitchAfter<TSource>(IGenerator<TSource>, int, IGenerator<TSource>)

Generates elements from a source generator for the given number of steps, then switches to a second generator.

SwitchWhen<TSource>(IGenerator<TSource>, Func<TSource, bool>, IGenerator<TSource>)

Generates elements from the source generator until a condition is met, then generate elements from a second generator.

TakeAndSkip<TSource>(IGenerator<TSource>, int, int)

Makes a generator that repeatedly takes and skips over elements from a given generator.

UniformRandomFloat()

Makes a generator hat generates floats uniformly between 0 and 1.

UniformRandomFloat(int)

Makes a generator that generates floats uniformly between 0 and 1.

UniformRandomInt(int)

Makes a generator that generates integers uniformly distributed between 0 (included) and the specified limit (excluded).

UniformRandomInt(int, int)

Makes a generator hat generates integers uniformly distributed between 0 (included) and the specified limit (excluded).

UniformVector2InCircle(float)

Generates vectors uniformly distributed in a given circle.

UniformVector2InRect(Vector2)

Generates vectors uniformly distributed in a given rectangle.

WhereWindow<TSource>(IGenerator<TSource>, int, Func<TSource[], bool>)

Only generates an item if the window of the item passes the predicate.

Where<TSource>(IGenerator<TSource>, IGenerator<bool>)

Makes a generator that will only generate elements that pass the predicate generated by the predicate generator.

Where<T>(IGenerator<T>, IGenerator<bool>, int)

Makes a generator that will only generate elements that pass the predicate generated by the predicate generator. If the source elements does not provide elements that pass the predicate for the given number of maximum iterations, an exception is thrown. This is to prevent a stalling the generator forever.

Where<TSource>(IGenerator<TSource>, Func<TSource, bool>)

Makes a generator that will only generate elements that pass the predicate.

Where<T>(IGenerator<T>, Func<T, bool>, int)

Makes a generator that will only generate elements that pass the predicate. If the source elements does not provide elements that pass the predicate for the given number of maximum iterations, an exception is thrown. This is to prevent a stalling the generator forever.

Window<TSource>(IGenerator<TSource>, int)

Makes a generator that generates a moving window of elements over a given generator.