Путь вперед лежит в сочетании ответов Марка и Филиппа. В идеальных условиях я бы отредактировал один из их постов, но, похоже, мои правки были отклонены.
Во всяком случае, я взял DelimitedArray, с которым связался Марк, и изменил в нем несколько вещей:
Конструктор изменен на:
public DelimitedArray(T[] array, int offset, int count, bool throwErrors = false)
{
this.array = array;
this.offset = offset;
this.count = count;
this.throwErrors = throwErrors;
}
Ссылка на индекс изменена на:
public T this[int index]
{
get
{
int idx = this.offset + index;
if (idx > this.Count - 1 || idx < 0)
{
if (throwErrors == true)
throw new IndexOutOfRangeException("Index '" + idx + "' was outside the bounds of the array.");
return default(T);
}
return this.array[idx];
}
}
Затем я применил это к использованию цикла Филиппа. Это становится:
for (var start = 0; start < words.Length - 2; start++) // at least one word
{
for (var end = start + 1; end < words.Length - 1; end++)
{
var segment = new DelimitedArray<string>(words, start, end - start);
lemma = string.Join(" ", segment.GetEnumerator()); // get the word/phrase to test
result = this.DoTheTest(lemma);
if (result > 0)
{
// Add the new result
ret = ret + result;
// Move the start sentinel up, mindful of the +1 that will happen at the end of the loop
start = start + segment.Count - 1;
// And instantly finish the end sentinel; we're done here.
end = words.Length;
}
}
}
Если бы я мог принять более одного ответа, я бы отметил оба их ответа, но поскольку оба они неполные, мне придется принять свой, когда я смогу сделать это завтра.