using System; using System.Collections.Generic; using System.Linq; using Vision.Core.Utils; namespace Vision.Core.Collections { public class FastList : List { private readonly object _lock = new object(); public new volatile int Count; public int UpperBound => Count - 1; public FastList() { } public FastList(IEnumerable Source) { lock (_lock) { if (Source == null) return; AddRange(Source); } } public int GetUpperBound() { return Count <= 0 ? -1 : Count - 1; } public FastList Filter(Func Predicate) { lock (_lock) return new FastList(this.Where(Predicate)); } public T First(Func predicate) { lock (_lock) return Filter(predicate)[0]; } public FastList Filter(Func Predicate) { lock (_lock) return new FastList(this.Where(Predicate).Cast()); } public new T this[int index] { get { lock (_lock) return Count < index + 1 ? default(T) : base[index]; } set { lock (_lock) base[index] = value; } } public new void Add(T value) { lock (_lock) { base.Add(value); ++Count; } } public void Add(params T[] values) { lock (_lock) { foreach (T obj in values) Add(obj); } } public void AddSafe(T value) { lock (_lock) { if (Contains(value)) return; Add(value); ++Count; } } public new bool Remove(T value) { lock (_lock) { if (!base.Remove(value)) return false; --Count; return true; } } public new void AddRange(IEnumerable collection) { lock (_lock) { T[] array = collection.ToArray(); base.AddRange(array); Count += array.Length; } } public new void Clear() { lock (_lock) { base.Clear(); Count = 0; } } public FastList Shuffle() { int count = Count; while (count > 1) { --count; int index = MathUtils.Random(count + 1); T obj = this[index]; this[index] = this[count]; this[count] = obj; } return this; } public FastList Take(int count) { return new FastList(this.Take(count)); } public void Copy(out FastList destination) { destination = new FastList(this); } } }