views:

51

answers:

1

Basically I have a collection of objects each implement a member of Type IValueCollection

public interface IValueCollection : IEnumerable<decimal>
{
    decimal this[int index] { get; set; }
}

MeasurementCollection.Values is of type IValueCollection.

With the logic below I want to pivot a collection of IValueCollection and wrote the extension method below.

 public static IEnumerable<IValueCollection> PivotValues(this MeasurementCollection items)
    {
        if(items.IsQuantized())
        {
            int s = (int)items.First().Template.Frequency; //
            int c = items.Count;
            for (int n = 0; n < s; n++)
            {
                IValueCollection v = new MeasurementValueCollection(c);
                for (int m = 0; m < c; m++)
                {
                    v[m] = items.ElementAt(m).Values[n];
                }
                yield return v;
            }
        }
    }

should do {{1,2,3}{4,5,6}{7,8,9}} results in {{1,4,7},{2,5,8},{3,6,9}} However I think there is some nicer, slimmer and more readable expression to do this can somebody point me in the right direction?

edit info about underlying classes

    interface IValueCollection : IEnumerable<decimal>

    class MeasurementCollection : ICollection<IMeasurement>

    interface IMeasurement 
    {
        IMeasurementTemplate Template { get; }        
        ......
    }

    interface IMeasurementTemplate
    {
        .....
        MeasurementFrequency Frequency { get; }    
    }
+2  A: 

I would, personally, force the evaluation of your collection in advance, and use it as an array. Right now, each time you call ElementAt, you're going to evaluate the IEnumerable<T> again, causing a lot of repeated searching.

By forcing it to evaluate to an array in advance, you simplify the entire process. Something like:

public static IEnumerable<IValueCollection> PivotValues(this MeasurementCollection items)
{
    if(items.IsQuantized())
    {
        int elementLength = (int)items.First().Template.Frequency;
        var itemArray = items.ToArray();
        for (int n = 0; n < itemArray.Length; n++)
        {
            IValueCollection v = new MeasurementValueCollection(elementLength);
            for (int m = 0; m < elementLength; m++)
            {
                v[m] = itemArray[m].Values[n];
            }
            yield return v;
        }
    }
    else
        yield break; // Handle the case where IsQuantized() returns false...
}

If you control MeasurementValueCollection, I would add a constructor which takes an IEnumerable<decimal> as input, as well. You could then do:

public static IEnumerable<IValueCollection> PivotValues(this MeasurementCollection items)
{
    if(items.IsQuantized())
    {
        var elements = Enumerable.Range(0, (int)items.First().Template.Frequency);
        var itemArray = items.ToArray();

        foreach(var element in elements)
            yield return new MeasurementValueCollection(
                                 itemArray.Select( 
                                   (item,index) => itemArray[index].Value[element]
                                 )
                             );
    }
    else
        yield break; // Handle the case where IsQuantized() returns false...
}
Reed Copsey