Set collection order

I need a collection that behaves like Set and preserves the insertion order of the element.

Is there one or should I implement it myself?

What will be the best implementation?

+5
source share
5 answers

This does not exist in .NET, but you can emulate it using the extension method Listand DistinctLINQ, which should preserve the basic order List.

+2
source

Will OrderedDictionarydo what you want?

(, , , ) ( ), , . - , null true, , .

:

public class OrderedSet : ICollection, IEnumerable
{
    OrderedDictionary dict = new OrderedDictionary();
    public void Add(object member)
    {
        dict.Add(member, null);
    }
    public void Clear()
    {
        dict.Clear();
    }
    public void CopyTo(Array array, int index)
    {
        for (int i = 0; i < Count; i++)
            array[i + index] = dict[i];
    }
    public bool Contains(object member)
    {
        return dict.Contains(member);
    }
    public void Insert(int index, object member)
    {
        dict.Insert(index, member, null);
    }
    public void Remove(object member)
    {
        dict.Remove(member);
    }
    public void RemoveAt(int index)
    {
        dict.RemoveAt(index);
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return dict.Keys.GetEnumerator();
    }

    public int Count { get { return dict.Count; } }
    public ICollection Members { get { return dict.Keys; } }
    bool ICollection.IsSynchronized { get { return dict.IsSynchronized; } }
    object ICollection.SyncRoot { get { return dict.SyncRoot; } }
}
+2

:

public class InsertionOrderSet<T> : KeyedCollection<T,T>
{
    protected override T GetKeyForItem(T item)
    {
        return item;
    }
}

. .Add(T) ArgumentException s, , , HashSet<T>, false.

+1

inCountryList = ArrayList(); , , , countrySet = LinkedHashSet (inCountryList);

LinkedHashSet , .

0

I understand that this is an old post, but I recently needed something similar, and thought that this implementation could help if someone wants to create a common sequence in which order items are stored (and also allows you to insert before and after any subject). I'm sure someone has more efficient ways to do this, but it does the trick.

public class Sequence<T> : ICollection<T>
{
    private readonly SortedList<long, T> _baseList;

    public Sequence()
    {
        this._baseList = new SortedList<long, T>();
    }

    public IEnumerator<T> GetEnumerator()
    {
        return this._baseList.Values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

    public void Add(T item)
    {
        this._baseList.Add(this._baseList.Count(), item);
    }

    public void AddAfter(T item, T newItem)
    {
        var currentIndex = this._baseList.IndexOfValue(item);
        if (currentIndex == _baseList.Count())
        {
            this.Add(newItem);
        }
        else
        {
            var itemsToMove = new SortedList<long, T>();
            var total = Count;
            for (var i = currentIndex + 1; i < total; i++)
            {
                itemsToMove.Add(i, _baseList[i]);
                _baseList.Remove(i);
            }

            this.Add(newItem);
            foreach (var itemToMove in itemsToMove)
            {
                this.Add(itemToMove.Value);
            }
        }
    }

    public void AddBefore(T item, T newItem)
    {
        var currentIndex = this._baseList.IndexOfValue(item);
        var itemsToMove = new SortedList<long, T>();
        var total = Count;
        for (var i = currentIndex; i < total; i++)
        {
            itemsToMove.Add(i, this._baseList[i]);
            _baseList.Remove(i);
        }

        this.Add(newItem);
        foreach (var itemToMove in itemsToMove.Values)
        {
            this.Add(itemToMove);
        }
    }

    public void Clear()
    {
        this._baseList.Clear();
    }

    public bool Contains(T item)
    {
        return this._baseList.ContainsValue(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        this._baseList.Values.CopyTo(array, arrayIndex);
    }

    public bool Remove(T item)
    {
        try
        {
            this._baseList.RemoveAt(this._baseList.IndexOfValue(item));
            return true;
        }
        catch
        {
            return false;
        }
    }

    public int Count
    {
        get
        {
            return this._baseList.Count();
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }
}
0
source

All Articles