Pivoting a collection of arrays

微笑、不失礼 提交于 2019-12-10 10:49:01

问题


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; }    
}

回答1:


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...
}


来源:https://stackoverflow.com/questions/3687708/pivoting-a-collection-of-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!