using System.Collections.Generic;
namespace Gma.QrCodeNet.Encoding.ReedSolomon;
///
/// Description of GeneratorPolynomial.
///
internal sealed class GeneratorPolynomial
{
///
/// After create GeneratorPolynomial. Keep it as long as possible.
/// Unless QRCode encode is done or no more QRCode need to generate.
///
internal GeneratorPolynomial(GaloisField256 gfield)
{
Gfield = gfield;
CacheGenerator = new List(10)
{
new Polynomial(Gfield, new int[] { 1 })
};
}
private GaloisField256 Gfield { get; }
private List CacheGenerator { get; }
///
/// Get generator by degree. (Largest degree for that generator)
///
/// Generator
internal Polynomial GetGenerator(int degree)
{
if (degree >= CacheGenerator.Count)
{
BuildGenerator(degree);
}
return CacheGenerator[degree];
}
///
/// Build Generator if we cannot find specific degree of generator from cache
///
private void BuildGenerator(int degree)
{
lock (CacheGenerator)
{
int currentCacheLength = CacheGenerator.Count;
if (degree >= currentCacheLength)
{
Polynomial lastGenerator = CacheGenerator[currentCacheLength - 1];
for (int d = currentCacheLength; d <= degree; d++)
{
Polynomial nextGenerator = lastGenerator.Multiply(new Polynomial(Gfield, new int[] { 1, Gfield.Exponent(d - 1) }));
CacheGenerator.Add(nextGenerator);
lastGenerator = nextGenerator;
}
}
}
}
}