Class Generator
- Namespace
- Gamelogic.Extensions.Algorithms
- Assembly
- Assembly-CSharp.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.
public static IGenerator<TSource> Aggregate<TSource>(this IGenerator<TSource> generator, Func<TSource, TSource, TSource> aggregator)
Parameters
generatorIGenerator<TSource>The source generator.
aggregatorFunc<TSource, TSource, TSource>The aggregator.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements of the source generator.
Remarks
This overload uses the first item as the first element to generate.
The resulting generator will generate the following sequence:
result0 = generator[0]
result1 = aggregator(result0, generator[1])
result2 = aggregator(result1, generator[2])
Exceptions
- ArgumentNullException
generatoris null.- ArgumentNullException
aggregatoris null.
Aggregate<TSource>(IGenerator<TSource>, Func<TSource, TSource, TSource>, TSource)
Makes a generator that generates a running aggregate of the source generator.
public static IGenerator<TSource> Aggregate<TSource>(this IGenerator<TSource> generator, Func<TSource, TSource, TSource> aggregator, TSource initialValue)
Parameters
generatorIGenerator<TSource>The source generator.
aggregatorFunc<TSource, TSource, TSource>The aggregator.
initialValueTSourceThe initial value.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements of the source generator.
Remarks
The resulting generator will generate the following sequence:
result0 = aggregator(initialValue, generator[0])
result1 = aggregator(result0, generator[1])
result2 = aggregator(result1, generator[2])
Aggregate<TSource, TResult>(IGenerator<TSource>, Func<TResult, TSource, TResult>, TResult)
Makes a generator that generates a running aggregate of the source generator.
public static IGenerator<TResult> Aggregate<TSource, TResult>(this IGenerator<TSource> generator, Func<TResult, TSource, TResult> aggregator, TResult initialValue)
Parameters
generatorIGenerator<TSource>The source generator.
aggregatorFunc<TResult, TSource, TResult>The aggregator function.
initialValueTResultThe initial value.
Returns
- IGenerator<TResult>
A new generator.
Type Parameters
TSourceThe type of elements of the source generator.
TResultThe type of elements to generate.
Examples
The following will generate the maximum element generated by source so far:
var generator = source.Aggregate((x, y) = Max(x, y));
For example, if source generates 0 1 2 0 1 2..., then generator will generate 0 1 2 2 2 2...
Remarks
The resulting generator will generate the following sequence:
result0 = aggregator(initialValue, generator[0])
result1 = aggregator(result0, generator[1])
result2 = aggregator(result1, generator[2])
Apply<TSource>(IGenerator<TSource>, Action<TSource>)
Makes a generator that applies a function on the elements it generates.
public static IGenerator<TSource> Apply<TSource>(this IGenerator<TSource> generator, Action<TSource> action)
Parameters
generatorIGenerator<TSource>The source generator.
actionAction<TSource>The action to apply to generated elements.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of the elements to generate.
Examples
The following will generate a generator that generates 0 1 2 3 0 1 2 ... and print the values to the console.
var generator = Generator.Count(4).Apply((x) => {Debug.Log(x);});
The following will generate a monster, and sets its properties based on the player's current level:
var monsterGenerator = Generator
.Constant(monsterTemplate)
.Apply(m => m.SetProperties(GetPlayerLevel());
Exceptions
- ArgumentNullException
generatoris $(null).- ArgumentNullException
actionis $(null).
Average(IGenerator<int>)
Makes a generator the will generate the average of elements generated by another generator.
public static IGenerator<int> Average(this IGenerator<int> generator)
Parameters
generatorIGenerator<int>The generator.
Returns
- IGenerator<int>
A new generator.
Examples
The following generator will generate 4, 4, 4, 4, ....
var generator = Generator.Constant(4).Average()
Exceptions
- ArgumentNullException
sourceis null
Average(IGenerator<float>)
Makes a generator the will generate the average of elements generated by another generator.
public static IGenerator<float> Average(this IGenerator<float> generator)
Parameters
generatorIGenerator<float>The generator.
Returns
- IGenerator<float>
A new generator.
Examples
The following generator will generate 0, 0.5f, 0.33f, 0.5f, 0.4f, ...
var generator = Generator.Count(2).Average()
Exceptions
- ArgumentNullException
sourceis null
Cast<TResult>(IGenerator)
Makes a generator that will generate elements by casting the elements of a source generator.
public static IGenerator<TResult> Cast<TResult>(this IGenerator generator)
Parameters
generatorIGeneratorThe source generator.
Returns
- IGenerator<TResult>
A new generator.
Type Parameters
TResultThe type of elements of the source generator.
ChooseUniformRandom<TSource>(params IGenerator<TSource>[])
Chooses the from the given generators selected uniform randomly.
[Obsolete("Use an appropriate overload instead.")]
public static IGenerator<TSource> ChooseUniformRandom<TSource>(params IGenerator<TSource>[] list)
Parameters
listIGenerator<TSource>[]
Returns
- IGenerator<TSource>
IGenerator<TSource>.
Type Parameters
TSource
ChooseUniformRandom<TSource>(IList<IGenerator<TSource>>)
Makes a generator that selects a random generator from a given element to generate an element from.
public static IGenerator<TSource> ChooseUniformRandom<TSource>(IList<IGenerator<TSource>> list)
Parameters
listIList<IGenerator<TSource>>The list of generators to choose from.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements to generate.
Exceptions
- ArgumentNullException
listis $(null).- ArgumentException
listis empty or has null elements.
ChooseUniformRandom<TSource>(IList<IGenerator<TSource>>, int)
Chooses the random.
public static IGenerator<TSource> ChooseUniformRandom<TSource>(IList<IGenerator<TSource>> list, int seed)
Parameters
listIList<IGenerator<TSource>>The list.
seedintThe seed.
Returns
- IGenerator<TSource>
IGenerator<TSource>.
Type Parameters
TSourceThe type of the t source.
Exceptions
- ArgumentNullException
listis $(null).- ArgumentException
listis empty or has null elements.
ChooseUniformRandom<TSource>(IList<TSource>)
Make a generator that randomly generates elements from a list.
public static IGenerator<TSource> ChooseUniformRandom<TSource>(IList<TSource> list)
Parameters
listIList<TSource>The source list.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements to generate.
Exceptions
- ArgumentNullException
listis null- ArgumentException
listis empty.
ChooseUniformRandom<TSource>(IList<TSource>, int)
Make a generator that randomly generates elements from a list. Can be seeded.
public static IGenerator<TSource> ChooseUniformRandom<TSource>(IList<TSource> list, int seed)
Parameters
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements to generate.
Exceptions
- ArgumentNullException
listis null- ArgumentException
listis empty.
Choose<TSource>(IList<IGenerator<TSource>>, IGenerator<int>)
Makes a generator that uses an index generator to choose a generator to generate an element from.
public static IGenerator<TSource> Choose<TSource>(IList<IGenerator<TSource>> generators, IGenerator<int> indexGenerator)
Parameters
generatorsIList<IGenerator<TSource>>The source generators.
indexGeneratorIGenerator<int>The index generator.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of element to generate.
Examples
The following generator will interleave the given generators:
//generators is a IEnumerable of generators
var generator = Generator.Choose(generators, Generator.Count(generators.Count());
Exceptions
- ArgumentNullException
generators or indexGenerator
- ArgumentException
Cannot contain null elements;generators
Choose<TSource>(IList<TSource>, IGenerator<int>)
Makes a generator that chooses elements from a list using an index generator.
public static IGenerator<TSource> Choose<TSource>(IList<TSource> source, IGenerator<int> indexGenerator)
Parameters
sourceIList<TSource>The source list.
indexGeneratorIGenerator<int>The index generator.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements to generate.
Examples
The following will generate "cat" "dog" "cat" "dog":
var generator = Generator.Choose(new[] {"cat", "dog"}, Generator.Count(2));
Exceptions
- ArgumentNullException
list or indexGenerator
- ArgumentException
List must have at least one element;list
ClosedSawTooth(int)
Makes a generator that produces evenly spaced floats from 0 to 1, both limits included, and repeats the result.
public static IGenerator<float> ClosedSawTooth(int sampleCount)
Parameters
sampleCountintThe number of samples per cycle.
Returns
- IGenerator<float>
A new generator.
Exceptions
- ArgumentOutOfRangeException
sampleCountis not bigger than 1.
Combine<TSource, TResult>(IEnumerable<IGenerator<TSource>>, Func<IList<TSource>, TResult>)
Makes a generator that combines the elements of specified generators.
public static IGenerator<TResult> Combine<TSource, TResult>(IEnumerable<IGenerator<TSource>> generators, Func<IList<TSource>, TResult> resultSelector)
Parameters
generatorsIEnumerable<IGenerator<TSource>>The generators.
resultSelectorFunc<IList<TSource>, TResult>The result selector.
Returns
- IGenerator<TResult>
A new generator.
Type Parameters
TSourceThe type of elements of the source generators.
TResultThe type of elements this generator will generate.
Exceptions
- ArgumentNullException
generators
- ArgumentException
Cannot contain null elements;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.
public static IGenerator<TResult> Combine<T1, T2, TResult>(IGenerator<T1> generator1, IGenerator<T2> generator2, Func<T1, T2, TResult> resultSelector)
Parameters
generator1IGenerator<T1>The first generator.
generator2IGenerator<T2>The second generator.
resultSelectorFunc<T1, T2, TResult>The result selector function.
Returns
- IGenerator<TResult>
A new generator
Type Parameters
T1The type of elements of the first generator.
T2The type of elements of the second generator.
TResultThe type of the elements of the result generator.
Examples
The following will code will add the elements of two generators to form its elements.
var generator1 = Generator.Count(3); //generates 0 1 2 0 1 2 0 1 2...
var generator2 = Generator.Count(2); //generates 0 1 0 1 0 1 0 1 0...
var generator = Generator.Combine(generator1, generator2, (x, y) => x + y);
//generates 0 2 2 1 1 3 0 2 2...</code></pre>
Exceptions
- ArgumentNullException
generator1 or generator2 or resultSelector
- ArgumentNullException
generator1is null.- ArgumentNullException
generator2is null.- ArgumentNullException
resultSelectoris null.
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.
public static IGenerator<TResult> Combine<T1, T2, T3, TResult>(IGenerator<T1> generator1, IGenerator<T2> generator2, IGenerator<T3> generator3, Func<T1, T2, T3, TResult> resultSelector)
Parameters
generator1IGenerator<T1>The first generator.
generator2IGenerator<T2>The second generator.
generator3IGenerator<T3>The third generator.
resultSelectorFunc<T1, T2, T3, TResult>The result selector function.
Returns
- IGenerator<TResult>
A new generator
Type Parameters
T1The type of elements of the first generator.
T2The type of elements of the second generator.
T3The type of elements of the third generator.
TResultThe type of the elements of the result generator.
Exceptions
- ArgumentNullException
generator1is null.- ArgumentNullException
generator2is null.- ArgumentNullException
generator3is null.- ArgumentNullException
resultSelectoris null.
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.
public static IGenerator<TResult> Combine<T1, T2, T3, T4, TResult>(IGenerator<T1> generator1, IGenerator<T2> generator2, IGenerator<T3> generator3, IGenerator<T4> generator4, Func<T1, T2, T3, T4, TResult> resultSelector)
Parameters
generator1IGenerator<T1>The first generator.
generator2IGenerator<T2>The second generator.
generator3IGenerator<T3>The third generator.
generator4IGenerator<T4>The fourth generator.
resultSelectorFunc<T1, T2, T3, T4, TResult>The result selector function.
Returns
- IGenerator<TResult>
A new generator
Type Parameters
T1The type of elements of the first generator.
T2The type of elements of the second generator.
T3The type of elements of the third generator.
T4The type of elements of the fourth generator.
TResultThe type of the elements of the result generator.
Exceptions
- ArgumentNullException
generator1is null.- ArgumentNullException
generator2is null.- ArgumentNullException
generator3is null.- ArgumentNullException
generator4is null.- ArgumentNullException
resultSelectoris null.
Constant<TSource>(TSource)
Makes a generator that generates a the same item each time.
public static IGenerator<TSource> Constant<TSource>(TSource item)
Parameters
itemTSourceThe item to generate.
Returns
- IGenerator<TSource>
Type Parameters
TSourceThe type of the item to generate.
Count(int)
Makes a generator that generates consecutive integers starting from zero up to a limit, and repeats the cycle.
public static IGenerator<int> Count(int upperLimitExcluded)
Parameters
upperLimitExcludedintThe upper limit (excluded).
Returns
- IGenerator<int>
A new generator
Exceptions
- ArgumentOutOfRangeException
upperLimitExcludedis not positive.
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.
public static IGenerator<int> Dither(this IGenerator<float> source, int levels, IEnumerable<float> errorFactors)
Parameters
sourceIGenerator<float>The source.
levelsintThe levels.
errorFactorsIEnumerable<float>The error factors.
Returns
- IGenerator<int>
IGenerator<System.Int32>.
FrequencyRandomInt(IEnumerable<float>)
Generates random integers at relative frequencies provided.
public static IGenerator<int> FrequencyRandomInt(IEnumerable<float> frequencies)
Parameters
frequenciesIEnumerable<float>The relative frequencies of each integer. If the array of frequencies has three elements, then the integers produced will be 0, 1, 2 (provided all frequencies are positive). All frequencies must be non-negative, and at least one must be positive.
Returns
- IGenerator<int>
A new generator.
FrequencyRandomInt(IEnumerable<float>, int)
Generates random integers at relative frequencies provided.
public static IGenerator<int> FrequencyRandomInt(IEnumerable<float> frequencies, int seed)
Parameters
frequenciesIEnumerable<float>The relative frequencies of each integer. If the array of frequencies has three elements, then the integers produced will be 0, 1, 2 (provided all frequencies are positive). All frequencies must be non-negative, and at least one must be positive.
seedintA seed to use for the random number generator.
Returns
- IGenerator<int>
A new generator.
FromFunc<TResult>(Func<TResult>)
Makes a new generator from a generator function.
public static IGenerator<TResult> FromFunc<TResult>(Func<TResult> generator)
Parameters
generatorFunc<TResult>The generator function that will be called to generate new elements.
Returns
- IGenerator<TResult>
Type Parameters
TResultThe type of elements to generate.
Exceptions
- ArgumentNullException
generator
GaussianRandomFloat(float, float)
Makes a generator that generates floats with a Gaussian distribution.
public static IGenerator<float> GaussianRandomFloat(float mean, float standardDeviation)
Parameters
meanfloatThe mean of the distribution. Values generated will be centered around this point.
standardDeviationfloatThe standard deviation of the distribution. The bigger this value, the flatter the distribution curve will be.
Returns
- IGenerator<float>
A new generator.
Group<TSource>(IGenerator<TSource>, IGenerator<int>)
Makes a generator that generates groups of items from the source generator.
public static IGenerator<IList<TSource>> Group<TSource>(this IGenerator<TSource> generator, IGenerator<int> groupSizeGenerator)
Parameters
generatorIGenerator<TSource>The source generator.
groupSizeGeneratorIGenerator<int>The generator used to determine the size of the groups to return.
Returns
- IGenerator<IList<TSource>>
IGenerator<IEnumerable<TSource>>.
Type Parameters
TSourceThe type of the source generator.
Exceptions
- ArgumentNullException
generator
- ArgumentOutOfRangeException
groupSizeGenerator;Argument must be positive.
Group<TSource>(IGenerator<TSource>, int)
Makes a generator that returns groups of elements from the source generator.
public static IGenerator<IList<TSource>> Group<TSource>(this IGenerator<TSource> generator, int groupSize)
Parameters
generatorIGenerator<TSource>The source generator.
groupSizeintThe size of the groups.
Returns
- IGenerator<IList<TSource>>
IGenerator<IEnumerable<TSource>>.
Type Parameters
TSourceThe type of the source generator.
Remarks
For example, if the source generator generates 0, 1, 2, 3, ... and the group size is 2 then the result generator will return groups (0, 1), (2, 3), ...
Exceptions
- ArgumentNullException
generator
- ArgumentOutOfRangeException
groupSize;Argument must be positive.
Interleave<TSource>(IGenerator<TSource>, params IGenerator<TSource>[])
Makes a generator that interleaves the elements of the specified generators.
public static IGenerator<TSource> Interleave<TSource>(IGenerator<TSource> generator, params IGenerator<TSource>[] generators)
Parameters
generatorIGenerator<TSource>The first generator.
generatorsIGenerator<TSource>[]The other generators.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements to generate.
Exceptions
- ArgumentNullException
generatoris $(null)- ArgumentException
Any of
generatorsis null.
Interleave<TSource>(IList<IGenerator<TSource>>)
Makes a generator that interleaves the elements of the specified generators.
public static IGenerator<TSource> Interleave<TSource>(IList<IGenerator<TSource>> generators)
Parameters
generatorsIList<IGenerator<TSource>>The source generators.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements to generate.
Exceptions
- ArgumentNullException
generatorsis null.- ArgumentException
generatorsis empty.- ArgumentException
Any of
generatorsis null.
InterpolateDither<TSource>(IGenerator<TSource>, IGenerator<int>)
Interpolates a sequence, but applies dithering.
public static IGenerator<TSource> InterpolateDither<TSource>(this IGenerator<TSource> generator, IGenerator<int> sampleCount)
Parameters
generatorIGenerator<TSource>sampleCountIGenerator<int>
Returns
- IGenerator<TSource>
Type Parameters
TSource
Remarks
For example, if the sequence is a binary sequence 1 0 1..., the result is a dithered sequence, something like 1 1 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0
Interpolate<TSource>(IGenerator<TSource>, IGenerator<int>, Func<TSource, TSource, float, TSource>)
Makes a generator that interpolates between values of a given generator.
public static IGenerator<TSource> Interpolate<TSource>(this IGenerator<TSource> generator, IGenerator<int> sampleCount, Func<TSource, TSource, float, TSource> interpolater)
Parameters
generatorIGenerator<TSource>sampleCountIGenerator<int>interpolaterFunc<TSource, TSource, float, TSource>
Returns
- IGenerator<TSource>
Type Parameters
TSource
Interpolate<TSource>(IGenerator<TSource>, int, Func<TSource, TSource, float, TSource>)
Makes a generator that interpolates between values of a given generator.
public static IGenerator<TSource> Interpolate<TSource>(this IGenerator<TSource> generator, int sampleCount, Func<TSource, TSource, float, TSource> interpolater)
Parameters
generatorIGenerator<TSource>The source generator.
sampleCountintThe sample count per cycle.
interpolaterFunc<TSource, TSource, float, TSource>The interpolater function. This function takes three arguments: the first two are the left and right endpoints, and the third is a fraction between 0 and 1. For typical interpolation, the function must return the left endpoint if this fraction is 0, and the right endpoint if the fraction is 1.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements to generate.
Examples
var baseGenerator = Generator.Count(2); //Generates 0 1 0 1 0 1...
var interpolatedGenerator =
baseGenerator.Interpolate(2, (x, y, t) => x*(1 - t) + y*t); //Generates 0 0.5f 1 0.5f 0 0.5f....
Remarks
The expression (x, y, t) => x*(1 - t) + y*t) is standard linear interpolation.
Exceptions
- ArgumentNullException
generatoris $(null).- ArgumentOutOfRangeException
sampleCountnot positive.- ArgumentNullException
interpolateris $(null).
Iterate<TSource>(IEnumerable<TSource>, Func<IList<TSource>, TSource>)
Makes a generator that uses an iterator function to generate elements.
public static IGenerator<TSource> Iterate<TSource>(IEnumerable<TSource> initialElements, Func<IList<TSource>, TSource> iterator)
Parameters
initialElementsIEnumerable<TSource>The initial elements.
iteratorFunc<IList<TSource>, TSource>The iterator function.
Returns
- IGenerator<TSource>
Type Parameters
TSourceThe type of elements to generate.
Remarks
Uses the last n elements to generate the next one, where n is the same number of elements as is provided initially.
Exceptions
- ArgumentNullException
iterator
Iterate<TSource>(TSource, Func<TSource, TSource>)
Makes a generator that returns iterations of the specified initial element.
public static IGenerator<TSource> Iterate<TSource>(TSource initialElement, Func<TSource, TSource> iterator)
Parameters
initialElementTSourceThe initial element.
iteratorFunc<TSource, TSource>The iteration function.
Returns
- IGenerator<TSource>
IGenerator<TSource>.
Type Parameters
TSourceThe type of the source generator.
Remarks
If the iteration function is f and the initial element is x, then
the result generator will generate x, f(x), f(f(x)), f(f(f(x))), ...
Exceptions
- ArgumentNullException
iterator
Iterate<TSource>(TSource, TSource, Func<TSource, TSource, TSource>)
Makes a generator that uses an iterator function to generate elements.
public static IGenerator<TSource> Iterate<TSource>(TSource initialElement0, TSource initialElement1, Func<TSource, TSource, TSource> iterator)
Parameters
initialElement0TSourceThe first element to generate.
initialElement1TSourceThe second element to generate.
iteratorFunc<TSource, TSource, TSource>The iterator function.
Returns
- IGenerator<TSource>
Type Parameters
TSourceThe type of the t source.
Remarks
The iterator function is applied the last two items generated to produce the next one. The first two elements are provided by the caller. For example,
Iterate(0, 1, (x, y) => x + y); make a generator that produces the Fibonacci numbers.
Exceptions
- ArgumentNullException
iterator
Iterate<TSource>(TSource, TSource, TSource, Func<TSource, TSource, TSource, TSource>)
Makes a generator that uses an iterator function to generate elements.
public static IGenerator<TSource> Iterate<TSource>(TSource initialElement0, TSource initialElement1, TSource initialElement2, Func<TSource, TSource, TSource, TSource> iterator)
Parameters
initialElement0TSourceThe first element to generate.
initialElement1TSourceThe second element to generate.
initialElement2TSourceThe third element to generate.
iteratorFunc<TSource, TSource, TSource, TSource>The iterator function.
Returns
- IGenerator<TSource>
Type Parameters
TSourceThe type of the t source.
Remarks
The iterator function is applied the last three items generated to produce the next one. The first three elements are provided by the caller. For example,
Iterate(0, 0, 1, (x, y, z) => x + y + z); make a generator that produces the Tribonacci numbers.
Exceptions
- ArgumentNullException
iterator
Iterate<TSource>(TSource, TSource, TSource, TSource, Func<TSource, TSource, TSource, TSource, TSource>)
Makes a generator that uses an iterator function to generate elements.
public static IGenerator<TSource> Iterate<TSource>(TSource initialElement0, TSource initialElement1, TSource initialElement2, TSource initialElement3, Func<TSource, TSource, TSource, TSource, TSource> iterator)
Parameters
initialElement0TSourceThe first element to generate.
initialElement1TSourceThe second element to generate.
initialElement2TSourceThe third element to generate.
initialElement3TSourceThe fourth element to generate.
iteratorFunc<TSource, TSource, TSource, TSource, TSource>The iterator function.
Returns
- IGenerator<TSource>
Type Parameters
TSourceThe type of the t source.
Remarks
The iterator function is applied the last four items generated to produce the next one. The first four elements are provided by the caller. For example,
Iterate(0, 0, 0, 1, (x, y, z, w) => x + y + w + z); make a generator that produces the Tetranacci numbers.
Exceptions
- ArgumentNullException
iterator
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.
public static IGenerator<TSource> Iterate<TSource, TSource2>(TSource initialElement, IGenerator<TSource2> parm2, Func<TSource, TSource2, TSource> iterator)
Parameters
initialElementTSourceparm2IGenerator<TSource2>iteratorFunc<TSource, TSource2, TSource>
Returns
- IGenerator<TSource>
Type Parameters
TSourceTSource2
Log<TSource>(IGenerator<TSource>)
A generator that generates the elements of the source generator, but sends the generated element to the Unity Console.
public static IGenerator<TSource> Log<TSource>(this IGenerator<TSource> generator)
Parameters
generatorIGenerator<TSource>The base generator.
Returns
- IGenerator<TSource>
Type Parameters
TSourceThe type of the generator's items.
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.
public static IGenerator<TSource> Log<TSource>(this IGenerator<TSource> generator, Action<TSource> log)
Parameters
generatorIGenerator<TSource>The generator.
logAction<TSource>The log.
Returns
- IGenerator<TSource>
IGenerator<TSource>.
Type Parameters
TSourceThe type of the source generator.
Examples
var generator = Generator.Count(4).Log(x => Debug.Log(x.ToString())).Select(x => 2*x);
Remarks
This generator is useful for debugging, to inspect the results of a internal generator. It should not be used for other purposes. (Generators should generally not have side effects).
MarkovRandomInt(float[,])
Generates a Markov chain of integers from a transition table.
public static IGenerator<int> MarkovRandomInt(float[,] transitionTable)
Parameters
transitionTablefloat[,]The transition table to use. The value in the table at [i][j] is the relative probability that i will be followed by j. Note that values in a row need not add to 1, the values are normalized per row. In each row, there must be at least one positive value. All values must be non-negative.
Returns
- IGenerator<int>
A new generator.
MarkovRandomInt(float[,], int)
Generates a Markov chain of integers from a transition table.
public static IGenerator<int> MarkovRandomInt(float[,] transitionTable, int seed)
Parameters
transitionTablefloat[,]The transition table to use. The value in the table at [i][j] is the relative probability that i will be followed by j. Note that values in a row need not add to 1, the values are normalized per row. In each row, there must be at least one positive value. All values must be non-negative.
seedintThe seed to use for the random number generator.
Returns
- IGenerator<int>
A new generator.
MarkovRandomIntStartsWith(float[,], int)
Makes a Markov generator that starts with the given value.
public static IGenerator<int> MarkovRandomIntStartsWith(float[,] transitionTable, int initialValue)
Parameters
Returns
MarkovRandomIntStartsWith(float[,], int, int)
Makes a Markov generator that starts with the given value.
public static IGenerator<int> MarkovRandomIntStartsWith(float[,] transitionTable, int initialValue, int seed)
Parameters
Returns
MoveNext<TSource>(IGenerator<TSource>, int)
Moves the generator by a specified amount forward.
public static void MoveNext<TSource>(this IGenerator<TSource> generator, int count)
Parameters
generatorIGenerator<TSource>The generator.
countintThe number of times to move the generator forward.
Type Parameters
TSourceThe type of the t source.
Exceptions
- ArgumentNullException
generator
- ArgumentOutOfRangeException
count;Argument cannot be smaller than 0.
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.
public static IEnumerable<TSource> NextWhile<TSource>(this IGenerator<TSource> source, Func<TSource, bool> predicate)
Parameters
sourceIGenerator<TSource>The source generator.
predicateFunc<TSource, bool>The predicate.
Returns
- IEnumerable<TSource>
A new enumerator.
Type Parameters
TSourceThe type of the source generator.
Examples
The enumerable in the following will contain the elements 0, 1, 2, 3:
var list = Generator.Count(100).NextWhile(x => x < 4);
Next<TSource>(IGenerator<TSource>)
Returns the next element of the specified generator.
public static TSource Next<TSource>(this IGenerator<TSource> generator)
Parameters
generatorIGenerator<TSource>The generator.
Returns
- TSource
TSource.
Type Parameters
TSourceThe type of the source generator.
Exceptions
- ArgumentNullException
generator
Next<TSource>(IGenerator<TSource>, int)
Returns a list of the next n items from the generator.
public static IEnumerable<TSource> Next<TSource>(this IGenerator<TSource> generator, int count)
Parameters
generatorIGenerator<TSource>The generator.
countintHow many items to return.
Returns
- IEnumerable<TSource>
IEnumerable<TResult>.
Type Parameters
TSourceThe type of the source.
Exceptions
- ArgumentNullException
generator
- ArgumentOutOfRangeException
count;Argument must be positive.
OfType<TResult>(IGenerator)
Makes a generator that will generate elements of a source generator that is of the given type.
public static IGenerator<TResult> OfType<TResult>(this IGenerator generator) where TResult : class
Parameters
generatorIGeneratorThe source generator.
Returns
- IGenerator<TResult>
A new generator.
Type Parameters
TResultThe type of the elements the generator must generate.
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).
public static IGenerator<float> OpenSawTooth(IGenerator<int> sampleCount)
Parameters
sampleCountIGenerator<int>The number of samples per cycle.
Returns
- IGenerator<float>
A new generator.
Exceptions
- ArgumentOutOfRangeException
sampleCountis not positive.
OpenSawTooth(int)
Makes a generator that produces evenly spaced floats from 0 (included) to 1 (excluded), and repeats the result.
public static IGenerator<float> OpenSawTooth(int sampleCount)
Parameters
sampleCountintThe number of samples per cycle.
Returns
- IGenerator<float>
A new generator.
Exceptions
- ArgumentOutOfRangeException
sampleCountis not positive.
Pad<TSource>(IGenerator<TSource>, IEnumerable<TSource>)
Pads the specified generator with elements from a given list.
public static IGenerator<TSource> Pad<TSource>(this IGenerator<TSource> generator, IEnumerable<TSource> padding)
Parameters
generatorIGenerator<TSource>The source generator.
paddingIEnumerable<TSource>The padding.
Returns
- IGenerator<TSource>
IGenerator<TSource>.
Type Parameters
TSourceThe type of elements to generate.
Examples
var paddedGenerator = Generator.Count(4).Pad(new List(){7, 8});
//will generate 7 8 0 1 2 3 0 1 2 3...
Exceptions
- ArgumentNullException
generatoris null.- ArgumentNullException
paddingis null.
Pad<TSource>(IGenerator<TSource>, TSource, int)
Pads the specified generator with a constant element repeated a specified number of times.
public static IGenerator<TSource> Pad<TSource>(this IGenerator<TSource> generator, TSource padding, int padCount)
Parameters
generatorIGenerator<TSource>The source generator.
paddingTSourceThe padding value.
padCountintThe number of values to pad.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements to generate.
Examples
var generator = Generator.Count(4).Pad(0, 3);
//will generate 0 0 0 0 1 2 0 1 2 ...
Poisson(int, int)
A boolean generator that returns true at intervals uniformly distributed between minRadius and maxRadius.
public static IGenerator<bool> Poisson(int minRadius, int maxRadius)
Parameters
Returns
RandomBoolGenerator(float)
Makes a generator that returns random boolean values, true with the specified probability.
public static IGenerator<bool> RandomBoolGenerator(float probability)
Parameters
probabilityfloatThe probability of generating $(true).
Returns
- IGenerator<bool>
A new generator.
Exceptions
- ArgumentException
probabilitydoes not lie between 0 and 1 (inclusive)
RandomBoolGenerator(float, int)
Makes a generator that returns random boolean values, true with the specified probability.
public static IGenerator<bool> RandomBoolGenerator(float probability, int seed)
Parameters
probabilityfloatThe probability of generating $(true).
seedintThe seed to use for the random num ber generator.
Returns
- IGenerator<bool>
A new generator.
Exceptions
- ArgumentException
probabilitydoes not lie between 0 and 1 (inclusive)
RepeatEach<TSource>(IGenerator<TSource>, IGenerator<int>)
Makes a new generator that will repeat each of the given generators elements a number of times.
public static IGenerator<TSource> RepeatEach<TSource>(this IGenerator<TSource> generator, IGenerator<int> repeatCountGenerator)
Parameters
generatorIGenerator<TSource>The source generator.
repeatCountGeneratorIGenerator<int>A generator used to get the number of times each element will be generated.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of the elements of the source generator.
Exceptions
- ArgumentNullException
generator- ArgumentNullException
repeatCountGeneratoris null.
RepeatEach<TSource>(IGenerator<TSource>, int)
Makes a new generator that will repeat each of the given generators elements a number of times.
public static IGenerator<TSource> RepeatEach<TSource>(this IGenerator<TSource> generator, int repeatCount)
Parameters
generatorIGenerator<TSource>The source generator.
repeatCountintThe number of times each element will be generated.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of the elements of the source generator.
Exceptions
- ArgumentNullException
generatoris null- ArgumentOutOfRangeException
repeatCountis not positive.
Repeat<TSource>(IEnumerable<TSource>)
Makes a generator that repeats elements of the given list over and over.
public static IGenerator<TSource> Repeat<TSource>(IEnumerable<TSource> list)
Parameters
listIEnumerable<TSource>The list from which elements are generated.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements in the given list and the type of elements that will be generated.
Exceptions
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).
public static IGenerator<TResult> SelectMany<TSource, TResult>(this IGenerator<TSource> generator, Func<TSource, IEnumerable<TResult>> selector)
Parameters
generatorIGenerator<TSource>The generator.
selectorFunc<TSource, IEnumerable<TResult>>The function that transform elements of the source generator to a list of items.
Returns
- IGenerator<TResult>
IGenerator<TResult>.
Type Parameters
TSourceThe type of the source generator.
TResultThe type of the result generator.
Exceptions
- ArgumentNullException
generator or selector
Select<TResult>(IGenerator<float>, ResponseCurveBase<TResult>)
Makes a new generator by transforming the elements of a given float generator using a response curve.
public static IGenerator<TResult> Select<TResult>(this IGenerator<float> generator, ResponseCurveBase<TResult> selector)
Parameters
generatorIGenerator<float>The source generator.
selectorResponseCurveBase<TResult>The selector function, used to transform floats to elements.
Returns
- IGenerator<TResult>
IGenerator<TResult>.
Type Parameters
TResultThe type of elements this generator will generate.
Exceptions
- ArgumentNullException
generatororselectoris null
Select<TSource, TResult>(IGenerator<TSource>, Func<TSource, TResult>)
Makes a generator which generates items that are transformed, generated from a given generator.
public static IGenerator<TResult> Select<TSource, TResult>(this IGenerator<TSource> generator, Func<TSource, TResult> selector)
Parameters
generatorIGenerator<TSource>The generator.
selectorFunc<TSource, TResult>The function used to transform elements of the source generator.
Returns
- IGenerator<TResult>
IGenerator<TResult>.
Type Parameters
TSourceThe type of the source generator.
TResultThe type of the result generator.
Exceptions
- ArgumentNullException
generator or selector
SkipAndTake<TSource>(IGenerator<TSource>, int, int)
Makes a generator that repeatedly skips over and takes elements from a given generator.
public static IGenerator<TSource> SkipAndTake<TSource>(this IGenerator<TSource> generator, int skipCount, int takeCount)
Parameters
generatorIGenerator<TSource>The source generator.
skipCountintThe number of elements to skip each cycle.
takeCountintThe number of elements to take each cycle.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements of the source generator.
Examples
The following generator will generate 0 1 2 4 5 6 8 9 10...
var generator = Generator
.Count(100)
.TakeAndSkip(3, 1);
Exceptions
- ArgumentNullException
generatoris null.- ArgumentOutOfRangeException
takeCountis not larger than 0.- ArgumentOutOfRangeException
skipCountis negative.
Skip<TSource>(IGenerator<TSource>, int)
Makes a generator that skips over the specified number of elements from the source generator.
public static IGenerator<TSource> Skip<TSource>(this IGenerator<TSource> generator, int count)
Parameters
generatorIGenerator<TSource>The source generator.
countintThe number of elements to skip.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements of the source generator.
Examples
The following generator will generate 3 4 0 1 2 3 4 0 ...
var generator = Generator.Count(5).Skip(3);
Exceptions
- ArgumentNullException
generatoris null.- ArgumentOutOfRangeException
countis negative.
Sum(IGenerator<int>)
Makes a generator that will generate partial sums of a given generator.
public static IGenerator<int> Sum(this IGenerator<int> generator)
Parameters
generatorIGenerator<int>The generator.
Returns
- IGenerator<int>
A new generator.
Examples
The following generator will generate 0, 1, 2, 3, 4, 5...
var generator = Generator.Constant(1).Sum();
Exceptions
- ArgumentNullException
sourceis null
Sum(IGenerator<float>)
Makes a generator that will generate partial sums of a given generator.
public static IGenerator<float> Sum(this IGenerator<float> generator)
Parameters
generatorIGenerator<float>The generator.
Returns
- IGenerator<float>
A new generator.
Examples
The following generator will generate 0f, 1f, 2f, 3f, 4f, 5f...
var generator = Generator.Constant(1f).Sum();
Exceptions
- ArgumentNullException
sourceis null
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.
public static IGenerator<TSource> SwitchAfter<TSource>(this IGenerator<TSource> source, int steps, IGenerator<TSource> newGenerator)
Parameters
sourceIGenerator<TSource>The source generator that will be used before the switch.
stepsintThe number of steps before switching.
newGeneratorIGenerator<TSource>The new generator that will be used after the switch.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of the source generator.
Exceptions
- ArgumentNullException
sourceis null- ArgumentNullException
newGeneratoris null
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.
public static IGenerator<TSource> SwitchWhen<TSource>(this IGenerator<TSource> source, Func<TSource, bool> predicate, IGenerator<TSource> newGenerator)
Parameters
sourceIGenerator<TSource>The source.
predicateFunc<TSource, bool>The predicate.
newGeneratorIGenerator<TSource>The new generator.
Returns
- IGenerator<TSource>
A new generator
Type Parameters
TSourceThe type of the source generator.
Exceptions
- ArgumentNullException
sourceis null- ArgumentNullException
predicateis null- ArgumentNullException
newGeneratoris null
TakeAndSkip<TSource>(IGenerator<TSource>, int, int)
Makes a generator that repeatedly takes and skips over elements from a given generator.
public static IGenerator<TSource> TakeAndSkip<TSource>(this IGenerator<TSource> generator, int takeCount, int skipCount)
Parameters
generatorIGenerator<TSource>The source generator.
takeCountintThe number of elements to take each cycle.
skipCountintThe number of elements to skip each cycle.
Returns
- IGenerator<TSource>
A new generator.
Type Parameters
TSourceThe type of elements of the source generator.
Examples
The following generator will generate 0 1 2 4 5 6 8 9 10...
var generator = Generator
.Count(100)
.TakeAndSkip(3, 1);
Exceptions
- ArgumentNullException
generatoris null.- ArgumentOutOfRangeException
takeCountis not larger than 0.- ArgumentOutOfRangeException
skipCountis negative.
UniformRandomFloat()
Makes a generator hat generates floats uniformly between 0 and 1.
public static IGenerator<float> UniformRandomFloat()
Returns
- IGenerator<float>
A new generator.
UniformRandomFloat(int)
Makes a generator that generates floats uniformly between 0 and 1.
public static IGenerator<float> UniformRandomFloat(int seed)
Parameters
seedint
Returns
- IGenerator<float>
A new generator.
UniformRandomInt(int)
Makes a generator that generates integers uniformly distributed between 0 (included) and the specified limit (excluded).
public static IGenerator<int> UniformRandomInt(int upperLimitExcluded)
Parameters
upperLimitExcludedintThe upper limit (excluded).
Returns
- IGenerator<int>
IGenerator<System.Int32>.
Exceptions
- ArgumentOutOfRangeException
upperLimitExcludedis not positive.
UniformRandomInt(int, int)
Makes a generator hat generates integers uniformly distributed between 0 (included) and the specified limit (excluded).
public static IGenerator<int> UniformRandomInt(int upperLimitExcluded, int seed)
Parameters
upperLimitExcludedintThe upper limit (excluded).
seedintThe seed to use for the random number generator.
Returns
- IGenerator<int>
A new generator.
Exceptions
- ArgumentOutOfRangeException
upperLimitExcludedis not positive.
UniformVector2InCircle(float)
Generates vectors uniformly distributed in a given circle.
public static IGenerator<Vector2> UniformVector2InCircle(float radius)
Parameters
radiusfloat
Returns
UniformVector2InRect(Vector2)
Generates vectors uniformly distributed in a given rectangle.
public static IGenerator<Vector2> UniformVector2InRect(Vector2 dimensions)
Parameters
dimensionsVector2The dimensions of the rectangle.
Returns
WhereWindow<TSource>(IGenerator<TSource>, int, Func<TSource[], bool>)
Only generates an item if the window of the item passes the predicate.
public static IGenerator<TSource> WhereWindow<TSource>(this IGenerator<TSource> generator, int windowSize, Func<TSource[], bool> predicate)
Parameters
generatorIGenerator<TSource>The generator.
windowSizeintSize of the window.
predicateFunc<TSource[], bool>The predicate.
Returns
- IGenerator<TSource>
IGenerator<TSource>.
Type Parameters
TSourceThe type of the source generator.
Remarks
The window of an item is the element and a number of elements after the
element (together, the number of elements is windowSize).
Exceptions
- ArgumentNullException
generator or predicate
- ArgumentOutOfRangeException
windowSize;Argument must be positive.
Where<TSource>(IGenerator<TSource>, IGenerator<bool>)
Makes a generator that will only generate elements that pass the predicate generated by the predicate generator.
public static IGenerator<TSource> Where<TSource>(this IGenerator<TSource> generator, IGenerator<bool> predicateGenerator)
Parameters
generatorIGenerator<TSource>predicateGeneratorIGenerator<bool>
Returns
- IGenerator<TSource>
Type Parameters
TSource
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.
public static IGenerator<T> Where<T>(this IGenerator<T> generator, IGenerator<bool> predicateGenerator, int maxIterations)
Parameters
generatorIGenerator<T>predicateGeneratorIGenerator<bool>maxIterationsint
Returns
- IGenerator<T>
Type Parameters
T
Where<TSource>(IGenerator<TSource>, Func<TSource, bool>)
Makes a generator that will only generate elements that pass the predicate.
public static IGenerator<TSource> Where<TSource>(this IGenerator<TSource> generator, Func<TSource, bool> predicate)
Parameters
generatorIGenerator<TSource>The generator.
predicateFunc<TSource, bool>The predicate.
Returns
- IGenerator<TSource>
IGenerator<TSource>.
Type Parameters
TSourceThe type of the source generator.
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.
public static IGenerator<T> Where<T>(this IGenerator<T> generator, Func<T, bool> predicate, int maxIterations)
Parameters
generatorIGenerator<T>predicateFunc<T, bool>maxIterationsint
Returns
- IGenerator<T>
Type Parameters
T
Window<TSource>(IGenerator<TSource>, int)
Makes a generator that generates a moving window of elements over a given generator.
public static IGenerator<TSource[]> Window<TSource>(this IGenerator<TSource> source, int windowSize)
Parameters
sourceIGenerator<TSource>The source generator.
windowSizeintSize of the window.
Returns
- IGenerator<TSource[]>
A new generator.
Type Parameters
TSourceThe type of the source generator.
Examples
In the following example,
var generator = Generator.Count(4).Window(2);
the generator will generate (0 1) (1 2) (2 3) (3 0) (0 1)... The window of size 2 move one element (of the original generator) at a time.
The following is an implementation of a box blur on the given sequence:
public static IGenerator<float> BoxBlur(IGenerator<float> generator)
{
return generator.Window(3).Select(w => (w[0] + w[1] + w[2])/3f);
}
Exceptions
- ArgumentOutOfRangeException
windowSize;Argument must be positive.