Changeset 14834 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data
- Timestamp:
- 04/10/17 00:27:31 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Pool/ManagedPoolProvider.cs
r14777 r14834 5 5 using System.Collections.Concurrent; 6 6 using System.IO; 7 using System. Linq;7 using System.Reflection; 8 8 using System.Runtime.Serialization.Formatters.Binary; 9 9 … … 18 18 private byte[] dummyPartition; 19 19 20 private static readonly FieldInfo InternalListArrayProperty = typeof(List<T[]>).GetField( 21 "_items", 22 BindingFlags.NonPublic | BindingFlags.Instance); 23 20 24 private readonly Func<T> factory; 21 25 … … 29 33 this.factory = factory; 30 34 31 managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool <T>(this), MaxParitionCount);35 managedPools = new ObjectPool<IManagedPool<T>>(() => new ManagedPool(this)); 32 36 } 33 37 public int InstanceCount { get { return partitions.Count * PartitionSize; } } … … 46 50 } 47 51 48 public void ReleasePartitions( params T[][] partition) {49 if (partitions.Count < = MaxParitionCount && partition.Length > 0)50 partitions.PushRange( partition);52 public void ReleasePartitions(List<T[]> releasedPartitions) { 53 if (partitions.Count < MaxParitionCount) 54 partitions.PushRange((T[][])InternalListArrayProperty.GetValue(releasedPartitions), 0, releasedPartitions.Count); 51 55 } 52 56 … … 71 75 } 72 76 73 private class ManagedPool <T> : IManagedPool<T> where T : class, IPooledObject{77 private class ManagedPool : IManagedPool<T> { 74 78 private readonly ManagedPoolProvider<T> provider; 75 private readonly IList<T[]> partitions = new List<T[]>();79 private readonly List<T[]> partitions = new List<T[]>(); 76 80 private T[] currentPartition; 77 81 private int entryIndex; … … 82 86 } 83 87 84 public T Get(bool reset = true) {88 public T Get(bool resetEntry = true) { 85 89 if (entryIndex == provider.PartitionSize) { 86 90 currentPartition = provider.GetPartition(); … … 90 94 91 95 var entry = currentPartition[entryIndex++]; 92 93 if (reset) entry.Reset(); 94 96 if (resetEntry) entry.Reset(); 95 97 return entry; 96 98 } 97 99 98 100 public void Dispose() { 99 provider.ReleasePartitions(partitions.ToArray()); 100 partitions.Clear(); 101 entryIndex = provider.PartitionSize; 101 if (partitions.Count > 0) { 102 provider.ReleasePartitions(partitions); 103 partitions.Clear(); 104 currentPartition = null; 105 entryIndex = provider.PartitionSize; 106 } 107 102 108 provider.managedPools.Free(this); 103 109 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Data/Tree/TreeExtensions.cs
r14777 r14834 27 27 } 28 28 29 public static IEnumerable<TreeNode<T>> Depth First<T>(this TreeNode<T> node) {29 public static IEnumerable<TreeNode<T>> DepthLast<T>(this TreeNode<T> node) { 30 30 foreach (var child in node.Children) { 31 31 if (child.Children.Count > 0) { 32 foreach (var subChild in child.Depth First()) {32 foreach (var subChild in child.DepthLast()) { 33 33 yield return subChild; 34 34 } … … 37 37 } 38 38 39 public static IEnumerable<T> Depth First<T>(this TreeNode<T> node, Func<IEnumerable<T>, T> resolveParent) {39 public static IEnumerable<T> DepthLast<T>(this TreeNode<T> node, Func<IEnumerable<T>, T> resolveParent) { 40 40 foreach (var child in node.Children) { 41 41 if (child.Children.Count > 0) { 42 var subExpressions = child.Depth First(resolveParent);42 var subExpressions = child.DepthLast(resolveParent); 43 43 yield return resolveParent(subExpressions); 44 44 } else yield return child.Value; … … 47 47 48 48 public static PushProgram ToPushProgram(this TreeNode<Expression> tree, Func<Expression, bool> condition = null) { 49 var expressions = tree.Depth First(e => ResolveProgram(e, condition));49 var expressions = tree.DepthLast(e => ResolveProgram(e, condition)); 50 50 51 51 return ResolveProgram(expressions, condition);
Note: See TracChangeset
for help on using the changeset viewer.