Add extra constructors also to other public collections

This commit is contained in:
Archi
2024-02-21 13:18:23 +01:00
parent f63dbffee3
commit 482576f16d
3 changed files with 41 additions and 6 deletions

View File

@@ -24,6 +24,7 @@ using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
namespace ArchiSteamFarm.Collections;
@@ -37,6 +38,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlySet<T>, ISet<T> where T : no
private readonly ConcurrentDictionary<T, bool> BackingCollection;
[JsonConstructor]
public ConcurrentHashSet() => BackingCollection = new ConcurrentDictionary<T, bool>();
public ConcurrentHashSet(IEnumerable<T> collection) {

View File

@@ -22,6 +22,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using Nito.AsyncEx;
@@ -31,9 +32,7 @@ public sealed class ConcurrentList<T> : IList<T>, IReadOnlyList<T> where T : not
[PublicAPI]
public event EventHandler? OnModified;
public bool IsReadOnly => false;
internal int Count {
public int Count {
get {
using (Lock.ReaderLock()) {
return BackingCollection.Count;
@@ -41,7 +40,9 @@ public sealed class ConcurrentList<T> : IList<T>, IReadOnlyList<T> where T : not
}
}
private readonly List<T> BackingCollection = [];
public bool IsReadOnly => false;
private readonly List<T> BackingCollection;
private readonly AsyncReaderWriterLock Lock = new();
int ICollection<T>.Count => Count;
@@ -65,6 +66,15 @@ public sealed class ConcurrentList<T> : IList<T>, IReadOnlyList<T> where T : not
}
}
[JsonConstructor]
public ConcurrentList() => BackingCollection = [];
public ConcurrentList(IEnumerable<T> collection) {
ArgumentNullException.ThrowIfNull(collection);
BackingCollection = [..collection];
}
public void Add(T item) {
ArgumentNullException.ThrowIfNull(item);
@@ -147,7 +157,7 @@ public sealed class ConcurrentList<T> : IList<T>, IReadOnlyList<T> where T : not
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
internal void ReplaceWith(IEnumerable<T> collection) {
public void ReplaceWith(IEnumerable<T> collection) {
ArgumentNullException.ThrowIfNull(collection);
using (Lock.WriterLock()) {

View File

@@ -23,6 +23,7 @@ using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
namespace ArchiSteamFarm.Collections;
@@ -41,7 +42,7 @@ public sealed class ObservableConcurrentDictionary<TKey, TValue> : IDictionary<T
[PublicAPI]
public ICollection<TKey> Keys => BackingDictionary.Keys;
private readonly ConcurrentDictionary<TKey, TValue> BackingDictionary = new();
private readonly ConcurrentDictionary<TKey, TValue> BackingDictionary;
int ICollection<KeyValuePair<TKey, TValue>>.Count => BackingDictionary.Count;
int IReadOnlyCollection<KeyValuePair<TKey, TValue>>.Count => BackingDictionary.Count;
@@ -65,6 +66,28 @@ public sealed class ObservableConcurrentDictionary<TKey, TValue> : IDictionary<T
}
}
[JsonConstructor]
public ObservableConcurrentDictionary() => BackingDictionary = new ConcurrentDictionary<TKey, TValue>();
public ObservableConcurrentDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection) {
ArgumentNullException.ThrowIfNull(collection);
BackingDictionary = new ConcurrentDictionary<TKey, TValue>(collection);
}
public ObservableConcurrentDictionary(IEqualityComparer<TKey> comparer) {
ArgumentNullException.ThrowIfNull(comparer);
BackingDictionary = new ConcurrentDictionary<TKey, TValue>(comparer);
}
public ObservableConcurrentDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer) {
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(comparer);
BackingDictionary = new ConcurrentDictionary<TKey, TValue>(collection, comparer);
}
public void Add(KeyValuePair<TKey, TValue> item) {
(TKey key, TValue value) = item;