From de002c1adc15189c4ca2d53e1255c39a1a37603a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Ko=C5=BEelj?= Date: Tue, 17 Jan 2023 15:22:31 +0100 Subject: [PATCH] Add ConcurrentList implementation --- .../Concurrent/ConcurrentList.cs | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/Connected.Collections/Concurrent/ConcurrentList.cs diff --git a/src/Connected.Collections/Concurrent/ConcurrentList.cs b/src/Connected.Collections/Concurrent/ConcurrentList.cs new file mode 100644 index 0000000..484d045 --- /dev/null +++ b/src/Connected.Collections/Concurrent/ConcurrentList.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Connected.Collections.Concurrent; +public class ConcurrentList : IList, IEnumerable +{ + private readonly List _values = new(); + + public T this[int index] + { + get + { + lock (_values) + { + return _values[index]; + } + } + set + { + lock (_values) + { + _values[index] = value; + } + } + } + + public int Count + { + get + { + lock (_values) + { + return _values.Count; + } + } + } + + public bool IsReadOnly => false; + + public void Add(T item) + { + + lock (_values) + { + _values.Add(item); + } + } + + public void Clear() + { + lock (_values) + { + _values.Clear(); + } + } + + public bool Contains(T item) + { + lock (_values) + { + return _values.Contains(item); + } + } + + public void CopyTo(T[] array, int arrayIndex) + { + lock (_values) + { + foreach (var value in _values) + { + if (array.Length >= arrayIndex) + { + array.SetValue(value, arrayIndex++); + } + else + { + array.Append(value); + } + } + } + } + + public IEnumerator GetEnumerator() + { + lock (_values) + { + return _values.ToImmutableList(false).GetEnumerator(); + } + } + + public int IndexOf(T item) + { + lock (_values) + { + return _values.IndexOf(item); + } + } + + public void Insert(int index, T item) + { + lock (_values) + { + _values.Insert(index, item); + } + } + + public bool Remove(T item) + { + lock (_values) + { + return _values.Remove(item); + } + } + + public void RemoveAt(int index) + { + lock (_values) + { + _values.RemoveAt(index); + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + lock (_values) + { + return _values.ToImmutableList(false).GetEnumerator(); + } + } +}