BACKGROUND:
I created a generic TimeSeries<T> class that should implement the IEnumerable<IEnumerable<T>> interface:
public interface ITimeSeries<T> : IEnumerable<IEnumerable<T>>
{
T[,] DataMatrix { get; }
DateTime[] DateTime { get; }
string[] Variables { get; }
}
public class TimeSeries<T> : ITimeSeries<T>
{
public IEnumerator<IEnumerable<T>> GetEnumerator()
{...}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
The TimeSeries<T> class is ‘internally’ implemented as a matrix data structure with each column representing a different variable and each row an observation. There is also an additional DateTime[] array that represents the time axis.
As a start I would like to be able to iterate through the TimeSeries<T> variable by variable with a foreach loop and also through all observations for a specific variable (with an inner foreach loop), but the actual reason for the IEnumerable interface implementation is to get all the features provided by LINQ through the IEnumerable interface, provided that I can somehow ensure that the DateTime association of each observation stays intact.
So for the question:
How should I go about to implement the GetEnumerator method to achieve this?
Simplest way is just to loop through the rows and return the values of each row. I’m assuming you’re storing in row-major order where the first dimension is the row and the second is the column. Just flip r and c below for column-major order:
If you want to avoid making a copy, you could implement it as either a
ListofLists or anArrayofArrays (aT[][]instead of aT[,]).