DynamicLimitedList.cs

Concurrent Non-Concurrent Limited List

using System;
using System.Collections;
using System.Collections.Generic;
[Serializable]
public class DynamicLimitedList<T> : MonitorActionFunc, IEnumerable<T>
{
    private volatile T[] _array;
    private volatile int _count;
    private volatile int _maxSize;
    public DynamicLimitedList(int maxsize)
    {
        _array   = new T[maxsize];
        _maxSize = maxsize;
    }
    public int MaxSize
    {
        get
        {
            var tmpThis = this;
            return tmpThis.Lock(tmpThis, () =>
            {
                return _maxSize;
            });
        }
    }
    public int Count
    {
        get
        {
            var tmpThis = this;
            return tmpThis.Lock(tmpThis, () =>
            {
                return _count;
            });
        }
    }
    public T this[int index]
    {
        get
        {
            var tmpThis = this;
            return tmpThis.Lock(tmpThis, () =>
            {
                if (index > _array.Length - 1)
                    throw new Exception($"Index {index} is out of range {_array.Length - 1}");
                return _array[index];
            });
        }
        set
        {
            var tmpThis = this;
            tmpThis.Lock(tmpThis, () =>
            {
                if (index > _array.Length - 1)
                    throw new Exception($"Index {index} is out of range {_array.Length - 1}");
                if (index < _maxSize)
                {
                    _array[index] = value;
                    _count++;
                    if (_count > _maxSize)
                        _count = _maxSize;
                }
            });
        }
    }
    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        return GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
    public IEnumerator<T> GetEnumerator()
    {
        var tmpThis = this;
        return tmpThis.Lock(tmpThis, () =>
        {
            return GetEnum();
        });
    }
    private IEnumerator<T> GetEnum()
    {
        var array = _array;
        var count = Count;
        for (var i = 0; i < count; i++)
            yield return array[i];
    }
    public void Add(T value)
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            if (_count == _maxSize)
            {
                LeftShift();
                _array[_maxSize - 1] = value;
            }
            else
            {
                _array[_count] = value;
                _count++;
            }
        });
    }
    private void LeftShift()
    {
        var TArray = new T[_maxSize];
        Array.Copy(_array, 1, TArray, 0, _maxSize - 1);
        _array = TArray;
    }
    public void Clear()
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            _array = new T[_maxSize];
            _count = 0;
        });
    }
    public bool Contains(T item)
    {
        var tmpThis = this;
        return tmpThis.Lock(tmpThis, () =>
        {
            var size             = _maxSize;
            var equalityComparer = EqualityComparer<T>.Default;
            while (size-- > 0)
                if (_array[size] != null && equalityComparer.Equals(_array[size], item))
                    return true;
            return false;
        });
    }
    public T[] ToArray()
    {
        var tmpThis = this;
        return tmpThis.Lock(tmpThis, () =>
        {
            var objArray = new T[_maxSize];
            Array.Copy(_array, 0, objArray, 0, _maxSize);
            return objArray;
        });
    }
    public List<T> ToList()
    {
        var tmpThis = this;
        return tmpThis.Lock(tmpThis, () =>
        {
            return new List<T>(_array);
        });
    }
    public HashSet<T> ToHashSet()
    {
        var tmpThis = this;
        return tmpThis.Lock(tmpThis, () =>
        {
            return new HashSet<T>(_array);
        });
    }
    public void CopyTo(T[] array)
    {
        var tmpThis = this;
        tmpThis.Lock(tmpThis, () =>
        {
            Array.Copy(_array, 0, array, 0, _maxSize);
        });
    }
}

Leave a Reply

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