2014-12-05 23 views
2

我在尋找.NET類,這將基本上是:任何現有的.Net有序集?

  • 確保項目在它獨特的(像一個HashSet的)
  • 確保當我們一一列舉,我們就得到了比同階項目我們插入它們(如列表)

是否有一個現有的.Net類來做到這一點?

我知道HashSet(不保證順序),SortedSet(關於內容的順序),但它們不符合我的需要。我沒有任何其他需求(如StackQueue)。

我目前的選擇是有一個List<>並在添加和刪除數據前使用Contains(...)

+0

你試過'Dictionary',特別是'SortedDictionary'?這將是一種黑客,但會滿足您的需求 – 2014-12-05 08:27:38

+0

@VsevolodGoloviznin是的,但我沒有真正有任何指定的關鍵(我不確定一個'字典'保證枚舉的任何順序。 – J4N 2014-12-05 08:28:40

+0

您將插入密鑰(這將是你的值列表),值可以是隨機的任何東西哦,我的意思不是'SortedDictionary',而是'OrderedDictionary' – 2014-12-05 08:41:19

回答

0

你說得對。 HashSet不保留廣告訂單。

Stackoverflow: HashSet that preserves ordering by achitaka-san 它使用字典來查找項目和LinkedList以保持順序。所有三個插入,刪除和查找工作仍在O(1)中。

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

另一種實現:

@Codeproject: HashSet that Preserves Insertion Order or .NET Implementation of LinkedHashSet

+0

是的,我可以想象有很多自定義實現有這種行爲,我的問題是更多要知道是否有現有的官方集合,可以做到這一點。 – J4N 2014-12-05 08:34:56

+0

我不知道任何官方的實施。 – SteMa 2014-12-05 08:35:45

0

您可以使用OrderedDictionary,文檔可以發現here

您將在字典中使用值從當前List密鑰和你可以留下一些隨機性的價值。

OrderedDictionary myOrderedDictionary = new OrderedDictionary(); 
myOrderedDictionary.Add(1, "smth"); 
myOrderedDictionary.Add(2, "smth"); 

foreach (DictionaryEntry v in myOrderedDictionary) 
{ 
    int youValue = (int)v.Key; 
} 

這裏唯一的缺陷是,這個字典不使用泛型,你必須從自己object投。

+0

英文鏈接:http://msdn.microsoft.com/en-us/library/system.collections.specialized .ordereddictionary%28v = vs.110%29.aspx – 2014-12-05 08:49:11

+0

@MthetheWWatson謝謝,修正了鏈接:) – 2014-12-05 08:52:21

+0

ew,not generic :( – nawfal 2017-12-07 14:00:04

相關問題