Browse Source

Attempt to optimise memory by employing an IEnumerable.

master
Starbeamrainbowlabs 5 years ago
parent
commit
b98f331fb1
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
  1. 16
      SimpleTurtle/LSystemEngine.cs

16
SimpleTurtle/LSystemEngine.cs

@ -1,8 +1,7 @@ @@ -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 @@ -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 @@ -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;
}
}

Loading…
Cancel
Save