DynConcurrentList.cs

Dynamic Concurrent Generic List

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
[DebuggerDisplay("Count = {Count}")]
[Serializable]
public class DynConcurrentList<T> : MonitorActionFunc, IEnumerable<T>
{
    private          T[]        _array;
    private volatile MiniSet<T> _mSet;
    public volatile  int        Count;
    public DynConcurrentList() : this(101)
    {
    }
    public DynConcurrentList(IEnumerable<T> collection)
    {
        var enumerable = collection as T[] ?? collection.ToArray();
        Count  = enumerable.Length;
        _array = new T[Count];
        Array.Copy(enumerable, 0, _array, 0, Count);
    }
    public DynConcurrentList(int cap)
    {
        _array = new T[cap];
        Count  = 0;
    }
    public int Capacity
    {
        get
        {
            var tmpThis = this;
            return tmpThis.Lock(tmpThis, () =>
            {
                return _array.Length;
            });
        }
        set
        {
            var tmpThis = this;
            tmpThis.Lock(tmpThis, () =>
            {
                Array.Resize(ref _array, value);
            });
        }
    }
    public T this[int index]
    {
        get
        {
            var array   = _array;
            var idx     = index;
            var tmpThis = this;
            return tmpThis.Lock(tmpThis, () =>
            {
                if (idx > array.Length)
                    throw new Exception("Error: Index out of range.");
                return array[idx];
            });
        }
        set
        {
            var tmpThis = this;
            var idx     = index;
            tmpThis.Lock(tmpThis, () =>
            {
                if (idx >= _array.Length)
                    Array.Resize(ref _array, _array.Length + _array.Length / 4 * 3);
                _array[idx] = value;
            });
        }
    }
    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        return GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
    public void Add(T item)
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            if (Count >= _array.Length)
                Array.Resize(ref _array, _array.Length + _array.Length / 4 * 3);
            _array[Count] = item;
            Interlocked.Increment(ref Count);
        });
    }
    public void AddRange(IEnumerable<T> col)
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            var enumerable = col as T[] ?? col.ToArray();
            for (var i = 0; i < enumerable.Length; ++i)
            {
                if (Count >= _array.Length)
                    Array.Resize(ref _array, _array.Length + _array.Length / 4 * 3);
                _array[Count] = enumerable[i];
                Interlocked.Increment(ref Count);
            }
        });
    }
    public void Clear()
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            Array.Clear(_array, 0, _array.Length);
            Count = 0;
        });
    }
    public void Clean()
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            var newtArray = new T[Count];
            Array.Copy(_array, 0, newtArray, 0, Count);
            _array = newtArray;
        });
    }
    public bool Contains(T item, IEqualityComparer<T> comparer = null)
    {
        if (comparer == null)
            comparer = EqualityComparer<T>.Default;
        var tmpThis = this;
        return tmpThis.Lock(tmpThis, () =>
        {
            if (_mSet != null && _mSet.Count != Count)
            {
                _mSet = new MiniSet<T>(_array, comparer);
            }
            else
            {
                _mSet = new MiniSet<T>(Count, comparer);
                Clean();
                _mSet.AddRange(_array);
            }
            return _mSet.Contains(item);
        });
    }
    public void CopyTo(int index, T[] array, int arrayIndex, int count)
    {
        if (Capacity - index < count)
            throw new Exception("Collection too small.");
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            Array.Copy(_array, index, array, arrayIndex, count);
        });
    }
    public void ForEach(Action<T> action)
    {
        if (action == null)
            throw new Exception("Action cannot be null.");
        for (var index = 0; index < Count; ++index)
            action(_array[index]);
    }
    public void ParallelForEach(Action<T> action)
    {
        _array.AsParallel().ForAll(i =>
        {
            if (action == null)
                throw new Exception("Action cannot be null.");
            action(i);
        });
    }
    public int IndexOf(T item, int index)
    {
        var tmpThis = this;
        return tmpThis.Lock(tmpThis, () =>
        {
            if (index > Count)
                throw new Exception("Index out of range.");
            return Array.IndexOf(_array, item, index, Count - index);
        });
    }
    public void RemoveAt(int index)
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            if ((uint) index >= (uint) Count)
                throw new Exception("Index out of range.");
            Interlocked.Decrement(ref Count);
            if (index < Count)
                Array.Copy(_array, index + 1, _array, index, Count - index);
            _array[Count] = default;
        });
    }
    public void Reverse()
    {
        Reverse(0, Count);
    }
    public void Reverse(int index, int count)
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            if (index < 0)
                throw new Exception("Index out of range.");
            if (count < 0)
                throw new Exception("Count out of range.");
            if (Count - index < count)
                throw new Exception("Invalid offset.");
            Clean();
            Array.Reverse(_array, index, count);
        });
    }
    public void Sort()
    {
        Sort(0, Count, null);
    }
    public void Sort(IComparer<T> comparer)
    {
        Sort(0, Count, comparer);
    }
    public void Sort(int index, int count, IComparer<T> comparer)
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            if (index < 0)
                throw new Exception("Index out of range.");
            if (count < 0)
                throw new Exception("Count out of range.");
            if (Count - index < count)
                throw new Exception("Invalid offset.");
            Array.Sort(_array, index, count, comparer);
        });
    }
    public IEnumerator<T> GetEnumerator()
    {
        var tmpThis = this;
        return tmpThis.Lock(tmpThis, () =>
        {
            return GetEnum();
        });
    }
    public T[] ToArray()
    {
        Array.Resize(ref _array, Count);
        return _array;
    }
    private IEnumerator<T> GetEnum()
    {
        var array = _array;
        var count = Count;
        for (var i = 0; i < count; i++)
            yield return array[i];
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *