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
1 changed files with 7 additions and 9 deletions

View File

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