C#中:如何实现IOrderedEnumerable< T> [英] C#: How to implement IOrderedEnumerable<T>

查看:112
本文介绍了C#中:如何实现IOrderedEnumerable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现一些不同的算法实践,正好看到我有多么糟糕的是,并获得更好的:P

I want to implement some various algorithms for practice, just to see how bad I really am and to get better :p

不管怎么说,我想我会尝试使用的IEnumerable< T> IOrderedEnumerable< T> 等。净集合类型仅仅是兼容的(所以我写的东西可以更容易地更新版本)。

Anyways, I thought I would try to use IEnumerable<T> and IOrderedEnumerable<T> and other .Net collection types just to be compatible (so that what I write can be used more easily later).

但我不能找到一个方法来返回 IOrderedEnumerable与其中的一个实例; T&GT; 比使用排序依据和ThenBy扩展方法等。所以我想我要创建我自己的类实现此接口。但界面没有引起太大意义,我要诚实。它可能会,但我不知道。

But I can't find a way to return an instance of IOrderedEnumerable<T> other than using the OrderBy and ThenBy extension methods. So I guess I have to create my own class that implements this interface. But the interface doesn't quite make sense to me to be honest. It might, but I'm not sure.

我创建了一个空的类,添加的接口,然后得到ReSharper的加空实现我。它看起来是这样的:

I created an empty class, added the interface and then got ReSharper to add empty implementations for me. It looks like this:

class MyOrderedEnumerable<T> : IOrderedEnumerable<T>
{
    /// <summary>
    /// Performs a subsequent ordering on the elements of an <see cref="T:System.Linq.IOrderedEnumerable`1"/> according to a key.
    /// </summary>
    /// <returns>
    /// An <see cref="T:System.Linq.IOrderedEnumerable`1"/> whose elements are sorted according to a key.
    /// </returns>
    /// <param name="keySelector">The <see cref="T:System.Func`2"/> used to extract the key for each element.</param><param name="comparer">The <see cref="T:System.Collections.Generic.IComparer`1"/> used to compare keys for placement in the returned sequence.</param><param name="descending">true to sort the elements in descending order; false to sort the elements in ascending order.</param><typeparam name="TKey">The type of the key produced by <paramref name="keySelector"/>.</typeparam><filterpriority>2</filterpriority>
    public IOrderedEnumerable<T> CreateOrderedEnumerable<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer, bool descending)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Returns an enumerator that iterates through the collection.
    /// </summary>
    /// <returns>
    /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
    /// </returns>
    /// <filterpriority>1</filterpriority>
    public IEnumerator<T> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Returns an enumerator that iterates through a collection.
    /// </summary>
    /// <returns>
    /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
    /// </returns>
    /// <filterpriority>2</filterpriority>
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

我不明白的是 CreateOrderedEnumerable 方法。它到底是什么意思呢?好吧,我想这当然会创造一个有序的枚举,但如何?难道排序算法本身应该去那里?而且会是什么排序?有项目将在该法没有收集,那么,是这意味着,以获得集合命令?你将如何使用类?难道这意味着实现为例如里面的东西的私人助手类需要排序的东西?因此,而不是一个 MyOrderedEnumerable&LT的; T&GT; :IOrderedEnumerable&LT; T&GT; ,你将有一个 QuickSorter&LT; T&GT; :IOrderedEnumerable&LT; T&GT; 是花了集合在其构造和分类它时CreateOrderedEnumerable方法被称为......但后来会发生什么,如果有人打电话的GetEnumerator并开始枚举该方法已被调用之前?

What I don't understand is the CreateOrderedEnumerable method. What exactly is it meant to do? Well, I guess it of course would create an ordered enumerable, but how? Is the sorting algorithm itself supposed to go in there? And what will it sort? There is no collection of items going in to that method, so where is it meant to get the collection to order? How would you use the class? Is it meant to be implemented as for example a private helper class inside something that needs to sort stuff? So instead of a MyOrderedEnumerable<T> : IOrderedEnumerable<T>, you would have a QuickSorter<T> : IOrderedEnumerable<T> that took a collection in its constructor and sorted it when that CreateOrderedEnumerable method was called... but what would then happen if someone called GetEnumerator and started to enumerate before that method had been called?

任何人都有一个线索吗?

Anyone have a clue?


哈哈,刚发现我问过类似的东西前段时间<一个href="http://stackoverflow.com/questions/829487/c-is-it-possible-to-return-an-iorderedenumerablet">here.但是,这只是如果有可能返回之一。所以我想这个问题是一个答案,我到了那里=)

Haha, just discovered I had asked something similar a while ago here. But that was just about if it was possible to return one. So I guess this question is a response to the one answer I got there =)

推荐答案

我有一个样本实现你可以看看。它不是为工作能力以任何手段,但它应该让你开始。

I have a sample implementation you could look at. It's not designed to be efficient by any means, but it should get you started.

基本上是一个 IOrderedEnumerable&LT; T&GT; 只需要拥有最新的订购的想法,所以它可以创建一个新的。假设你已经有一个的IComparer&LT; T&GT; 你建立一个新的,就像这样:

Basically an IOrderedEnumerable<T> just needs to have an idea of its current ordering, so it can create a new one. Assuming you already have an IComparer<T> you build a new one by saying something like:

int Compare(T first, T second)
{
    if (baseComparer != null)
    {
        int baseResult = baseComparer.Compare(first, second);
        if (baseResult != 0)
        {
            return baseResult;
        }
    }
    TKey firstKey = keySelector(first);
    TKey secondKey = keySelector(second);

    return comparer.Compare(firstKey, secondKey);        
}

所以基本上你创建一个链comparers从最低显著到最显著去的。你也需要把降位在那里,但是这很容易:)

So basically you create a chain of comparers going from the "least significant" up to the "most significant". You also need to put the "descending" bit in there, but that's easy :)

在上面链接样品,三个不同的方面重新psented在三个不同的类别$ P $早已美元的 p $ psent MiscUtil

In the sample linked above, the three different aspects are represented in three different classes already present in MiscUtil:

  • ReverseComparer :反转现有的的IComparer&LT; T&GT; 的结果
  • LinkedComparer :从两个创建一个比较器,一个主站和一个从
  • ProjectionComparer :将基于从原来的项目键投影一个比较器,委托给另一个比较器来比较这些键
  • ReverseComparer: reverses an existing IComparer<T>'s results
  • LinkedComparer: creates one comparer from two, with one master and one slave
  • ProjectionComparer: creates a comparer based on a projection from the original items to keys, delegating to another comparer to compare those keys.

Comparers是很好的串联起来是这样的。

Comparers are great for chaining together like this.

这篇关于C#中:如何实现IOrderedEnumerable&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆