我正在为min heap编写一个泛型类,我希望能够在TKey和T上堆积。
interface IHeap<T, TKey> where TKey : IComparable<TKey> { void Insert(T x); T Delete(); T Top(); } public class MinHeap<T, TKey> : IHeap<T, TKey> where TKey : IComparable<TKey> { public MinHeap(int capacity) : this(capacity, x => x) // <---- compilation error here { } public MinHeap(int capacity, Func<T, TKey> keySelector) : this(capacity, keySelector, Comparer<TKey>.Default) { } public MinHeap(int capacity, Func<T, TKey> keySelector, IComparer<TKey> comparer) { // ... } // ... }我得到x => x这些编译错误:
Cannot convert lambda expression to delegate type 'System.Func<T,TKey>' because some of the return types in the block are not implicitly convertible to the delegate return type. Cannot implicitly convert type 'T' to 'TKey'我如何实现这一目标并且只有一个班级?
更新:
我希望能够做两件事:
// 1 var minheap = new MinHeap<Person, int>(10, x => x.Age); // 2 var minheap = new MinHeap<int>(10); // instead of var minheap = new MinHeap<int, int>(10, x => x);I'm writing a generic class for min heap where I want to be able to heapify on TKey as well as T.
interface IHeap<T, TKey> where TKey : IComparable<TKey> { void Insert(T x); T Delete(); T Top(); } public class MinHeap<T, TKey> : IHeap<T, TKey> where TKey : IComparable<TKey> { public MinHeap(int capacity) : this(capacity, x => x) // <---- compilation error here { } public MinHeap(int capacity, Func<T, TKey> keySelector) : this(capacity, keySelector, Comparer<TKey>.Default) { } public MinHeap(int capacity, Func<T, TKey> keySelector, IComparer<TKey> comparer) { // ... } // ... }I get these compilation errors for x => x:
Cannot convert lambda expression to delegate type 'System.Func<T,TKey>' because some of the return types in the block are not implicitly convertible to the delegate return type. Cannot implicitly convert type 'T' to 'TKey'How do I achieve this and just have one class?
Update:
I want to be able to do two things:
// 1 var minheap = new MinHeap<Person, int>(10, x => x.Age); // 2 var minheap = new MinHeap<int>(10); // instead of var minheap = new MinHeap<int, int>(10, x => x);最满意答案
MinHeap<T,TKey>可以使用与约束匹配的任何泛型类型参数进行实例化。
这意味着,例如,您可以拥有MinHeap<string,int> 。 在这种情况下,您将尝试将lambda x => x分配给Func<string,int> ,这不起作用,因为它是一个Func<string,string> 。
我不认为有一种明智的方法来实现你想要的东西,因为没有一个好的候选者可以将一种任意类型转换为另一种任意类型的“默认”方式,这就是你所需要的。
您可以做的是删除此构造函数并添加一个静态构造函数,该构造函数可用于T和TKey类型相同的情况:
public static class MinHeap { public static MinHeap<T,T> Create<T>(int capacity) where T : IComparable<T> { return new MinHeap<T,T>(capacity, x => x); } }但如果这还不足以满足您的需求,那么只需删除构造函数并接受人们将不得不处理传递lambda的问题。
MinHeap<T,TKey> can be instantiated with any generic type parameters matching the constraints.
That means for example, you could have a MinHeap<string,int>. In that case, you'd be trying to assign lambda x => x to a Func<string,int> which wouldn't work, since it's a Func<string,string>.
I don't think there is a sensible way to achieve what you want, as there's no good candidate for a "default" way to convert one arbitrary type to another arbitrary type, which is what you'd need.
What you could do is remove this constructor and add a static constructor which could be used for the cases when the T and TKey are the same type:
public static class MinHeap { public static MinHeap<T,T> Create<T>(int capacity) where T : IComparable<T> { return new MinHeap<T,T>(capacity, x => x); } }But if this isn't enough for your needs, then just remove the constructor and accept that people will have to deal with passing a lambda in themselves.
更多推荐
发布评论