namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter { using System; using HeuristicLab.Core; using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; public class PushInterpreterPool { private readonly ObjectPool pool; public readonly ManagedPoolProvider PushProgramPoolProvider; public readonly ManagedPoolProvider> ExpressionListPoolProvider; public PushInterpreterPool(IReadOnlyPushConfiguration config = null) : this(Environment.ProcessorCount * 4, 1024, null, config) { } public PushInterpreterPool(int size, int poolPartitionSize, int? maxPartitionCount = null, IReadOnlyPushConfiguration config = null) { PushConfiguration = config ?? new PushConfiguration(); PushProgramPoolProvider = new ManagedPoolProvider(poolPartitionSize, () => new PushProgram(), maxPartitionCount); ExpressionListPoolProvider = new ManagedPoolProvider>(poolPartitionSize, () => new PooledList(), maxPartitionCount); pool = new ObjectPool(CreateInterpreter, size); } private PooledPushInterpreter CreateInterpreter() { var poolContainer = new InterpreterPoolContainer(PushProgramPoolProvider, ExpressionListPoolProvider); return new PooledPushInterpreter(this, PushConfiguration, poolContainer); } public IReadOnlyPushConfiguration PushConfiguration { get; private set; } public PooledPushInterpreter Create(IRandom random = null) { var interpreter = pool.Allocate(); interpreter.Reset(random); return interpreter; } public void Free(PooledPushInterpreter interpreter) { interpreter.ClearStacks(); pool.Free(interpreter); } public void Clear() { PushProgramPoolProvider.Clear(); ExpressionListPoolProvider.Clear(); pool.Clear(); } } }