diff --git a/ArchiSteamFarm/Collections/ConcurrentHashSet.cs b/ArchiSteamFarm/Collections/ConcurrentHashSet.cs index 1570805e3..cd22fc396 100644 --- a/ArchiSteamFarm/Collections/ConcurrentHashSet.cs +++ b/ArchiSteamFarm/Collections/ConcurrentHashSet.cs @@ -22,7 +22,6 @@ using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Linq; namespace ArchiSteamFarm.Collections { @@ -34,10 +33,7 @@ namespace ArchiSteamFarm.Collections { public bool Add(T item) => BackingCollection.TryAdd(item, true); public void Clear() => BackingCollection.Clear(); - - [SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")] public bool Contains(T item) => BackingCollection.ContainsKey(item); - public void CopyTo(T[] array, int arrayIndex) => BackingCollection.Keys.CopyTo(array, arrayIndex); public void ExceptWith(IEnumerable other) { @@ -49,48 +45,55 @@ namespace ArchiSteamFarm.Collections { public IEnumerator GetEnumerator() => BackingCollection.Keys.GetEnumerator(); public void IntersectWith(IEnumerable other) { - ICollection collection = other as ICollection ?? other.ToList(); - foreach (T item in this.Where(item => !collection.Contains(item))) { + ISet otherSet = other as ISet ?? other.ToHashSet(); + + foreach (T item in this.Where(item => !otherSet.Contains(item))) { Remove(item); } } public bool IsProperSubsetOf(IEnumerable other) { - ICollection collection = other as ICollection ?? other.ToList(); - return (collection.Count != Count) && IsSubsetOf(collection); + ISet otherSet = other as ISet ?? other.ToHashSet(); + return (otherSet.Count > Count) && IsSubsetOf(otherSet); } public bool IsProperSupersetOf(IEnumerable other) { - ICollection collection = other as ICollection ?? other.ToList(); - return (collection.Count != Count) && IsSupersetOf(collection); + ISet otherSet = other as ISet ?? other.ToHashSet(); + return (otherSet.Count < Count) && IsSupersetOf(otherSet); } public bool IsSubsetOf(IEnumerable other) { - ICollection collection = other as ICollection ?? other.ToList(); - return this.AsParallel().All(collection.Contains); + ISet otherSet = other as ISet ?? other.ToHashSet(); + return this.All(otherSet.Contains); } - public bool IsSupersetOf(IEnumerable other) => other.AsParallel().All(Contains); - public bool Overlaps(IEnumerable other) => other.AsParallel().Any(Contains); + public bool IsSupersetOf(IEnumerable other) { + ISet otherSet = other as ISet ?? other.ToHashSet(); + return otherSet.All(Contains); + } + + public bool Overlaps(IEnumerable other) { + ISet otherSet = other as ISet ?? other.ToHashSet(); + return otherSet.Any(Contains); + } - [SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")] public bool Remove(T item) => BackingCollection.TryRemove(item, out _); public bool SetEquals(IEnumerable other) { - ICollection collection = other as ICollection ?? other.ToList(); - return (collection.Count == Count) && collection.AsParallel().All(Contains); + ISet otherSet = other as ISet ?? other.ToHashSet(); + return (otherSet.Count == Count) && otherSet.All(Contains); } public void SymmetricExceptWith(IEnumerable other) { - ICollection collection = other as ICollection ?? other.ToList(); + ISet otherSet = other as ISet ?? other.ToHashSet(); HashSet removed = new HashSet(); - foreach (T item in collection.Where(Contains)) { + foreach (T item in otherSet.Where(Contains)) { removed.Add(item); Remove(item); } - foreach (T item in collection.Where(item => !removed.Contains(item))) { + foreach (T item in otherSet.Where(item => !removed.Contains(item))) { Add(item); } } @@ -101,9 +104,7 @@ namespace ArchiSteamFarm.Collections { } } - [SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")] void ICollection.Add(T item) => Add(item); - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); // We use Count() and not Any() because we must ensure full loop pass @@ -123,6 +124,7 @@ namespace ArchiSteamFarm.Collections { internal void ReplaceWith(IEnumerable other) { BackingCollection.Clear(); + foreach (T item in other) { BackingCollection[item] = true; }