nswart
nswart

Reputation: 173

How to Implement GetEnumerator method for class that implements IEnumerable<IEnumerable<T>>

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?

Upvotes: 3

Views: 3445

Answers (2)

nswart
nswart

Reputation: 173

I've decided to add a struct which wraps each value and adds the DateTime stamp and Variable name properties and the GetEnumerator method yields a column of wrapped values:

    public struct TSItem<T>
    {
        public string Variable { get; private set; }
        public DateTime Date { get; private set; }
        public T Value { get; private set; }
    }

    public IEnumerator<IEnumerable<TSItem<T>>> GetEnumerator()
    {
        int rows = DataMatrix.GetLength(0);
        int cols = DataMatrix.GetLength(1);

        for (int c = 0; c < cols; c++)
        {
            var col = new List<TSItem<T>>();

            for (int r = 0; r < rows; c++)
            {
                col.Add(new TSItem<T>(this.Variables[c], this.DateTime[r], 
                                DataMatrix[r, c]));
            }
            yield return col;
        }
    }

This is probably not the most performant solution, but achieves what I want in terms of LINQ queries.

Upvotes: 0

D Stanley
D Stanley

Reputation: 152491

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:

public IEnumerator<IEnumerable<T>> GetEnumerator()
{
    int rows = DataMatrix.GetLength(0);
    int cols = DataMatrix.GetLength(1);

     for(int r = 0; r < rows; r++)
     {
        T[] row = new T[cols]();

        for(int c = 0; c < cols; c++)
        {
            row[c] = DataMatrix[r,c];
        }
        yield return row;
    }
}

If you want to avoid making a copy, you could implement it as either a List of Lists or an Array of Arrays (a T[][] instead of a T[,]).

Upvotes: 2

Related Questions