This is for C#: I have a very large IEnumerable<T> and I need to perform a check that would look like this on an Array:
for (int i = 0; i < Array.Size; i++)
{
for (int j = i; j < Array.Size; j++)
{
Check(Array[j])
}
// Once the following operation is done, I don't care about n <= i elements
DoStuff(Array[i]);
}
This is ~(n * n / 2) operations
The obvious solution is to use ToArray() and do what I just described, but that would mean copying to an array an doing the full loop once too many (n + n * n / 2).
I'm curious, is there a way to save the iteration index in order to "fork" the foreach loop. It would look like:
foreach (var elem in MyEnumerable)
{
var forked = fork(MyEnumerable);
foreach (var elem2 in forked)
{
Check(elem2);
}
DoStuff(elem);
}
I could also have some counter and replace my hypothetical fork function with MyEnumerable.Skip(counter), but that again is not ideal since (I assume) in the back ground Skip is actually iterating so it would be (n * n) operations.
Thank you for you help!