我已经读了this question篇关于为什么不可能的文章,但还没有找到解决问题的方法.
我想从.NET HashSet<T>
检索一个项目.我正在寻找一种具有此签名的方法:
/// <summary>
/// Determines if this set contains an item equal to <paramref name="item"/>,
/// according to the comparison mechanism that was used when the set was created.
/// The set is not changed. If the set does contain an item equal to
/// <paramref name="item"/>, then the item from the set is returned.
/// </summary>
bool TryGetItem<T>(T item, out T foundItem);
使用这种方法在集合中搜索一个项将是O(1).从HashSet<T>
中检索项目的唯一方法是枚举O(n)的所有项目.
我还没有找到解决这个问题的方法,除了自己制作HashSet<T>
或使用Dictionary<K, V>
.还有其他 idea 吗?
Note: