Attempt to optimise memory by employing an IEnumerable.

This commit is contained in:
Starbeamrainbowlabs 2018-01-28 23:45:43 +00:00
parent 42d8797d02
commit b98f331fb1
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Policy;
using System.Linq;
namespace SBRL.Algorithms.LSystem
{
@ -49,7 +48,7 @@ namespace SBRL.Algorithms.LSystem
// Find all the current positions
foreach(Rule rule in rules)
{
List<int> positions = AllIndexesOf(CurrentGeneration, rule.Find);
IEnumerable<int> positions = AllIndexesOf(CurrentGeneration, rule.Find);
foreach (int pos in positions)
rulePositions.Add(new KeyValuePair<int, Rule>(pos, rule));
}
@ -79,15 +78,14 @@ namespace SBRL.Algorithms.LSystem
/// <param name="str"></param>
/// <param name="value"></param>
/// <returns></returns>
private List<int> AllIndexesOf(string str, string value) {
private IEnumerable<int> AllIndexesOf(string str, string value) {
if (String.IsNullOrEmpty(value))
throw new ArgumentException("the string to find may not be empty", "value");
List<int> indexes = new List<int>();
for (int index = 0;; index += value.Length) {
throw new ArgumentException("the string to find may not be empty", nameof(value));
for (int index = 0; ; index += value.Length) {
index = str.IndexOf(value, index);
if (index == -1)
return indexes;
indexes.Add(index);
break;
yield return index;
}
}