あなたはOrderedDictionary
を探しているようです。
唯一の欠点は、ジェネリックをサポートしていないことですが、簡単にラッパーを作成したり、必要な型に値をキャストすることができます。
EDIT:コメントの中に参照さ@Blorgbeardとして、あなたはあなたの正確な要件を満たしている(そして正確にLinkedList<T>
とHashSet<T>
でこれを実装)another answerで「OrderedSet」の実装を見つけることができる、と役に立つかもしれません君は。ここで
は(クレジットは、元の作者に行く、ではない私)
public class OrderedSet<T> : ICollection<T>
{
private readonly IDictionary<T, LinkedListNode<T>> m_Dictionary;
private readonly LinkedList<T> m_LinkedList;
public OrderedSet()
: this(EqualityComparer<T>.Default)
{
}
public OrderedSet(IEqualityComparer<T> comparer)
{
m_Dictionary = new Dictionary<T, LinkedListNode<T>>(comparer);
m_LinkedList = new LinkedList<T>();
}
public int Count
{
get { return m_Dictionary.Count; }
}
public virtual bool IsReadOnly
{
get { return m_Dictionary.IsReadOnly; }
}
void ICollection<T>.Add(T item)
{
Add(item);
}
public bool Add(T item)
{
if (m_Dictionary.ContainsKey(item)) return false;
LinkedListNode<T> node = m_LinkedList.AddLast(item);
m_Dictionary.Add(item, node);
return true;
}
public void Clear()
{
m_LinkedList.Clear();
m_Dictionary.Clear();
}
public bool Remove(T item)
{
LinkedListNode<T> node;
bool found = m_Dictionary.TryGetValue(item, out node);
if (!found) return false;
m_Dictionary.Remove(item);
m_LinkedList.Remove(node);
return true;
}
public IEnumerator<T> GetEnumerator()
{
return m_LinkedList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool Contains(T item)
{
return m_Dictionary.ContainsKey(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
m_LinkedList.CopyTo(array, arrayIndex);
}
}
'HashTable'は何でしょうか?彼らの実装ですか – DiskJunky
あなたの提案された実装では、O(1)ではなく、O(n)個のアイテムが削除されます。 – Servy
[OrderedDictionary](https://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx)はオプションです。 – Blorgbeard