From d7fac361bda51044a87516061570cf3cdf0b7b48 Mon Sep 17 00:00:00 2001 From: neuecc Date: Tue, 20 Aug 2024 17:08:27 +0900 Subject: [PATCH] capacity --- .../ObservableDictionary.cs | 20 ++++++++++++++-- .../ObservableHashSet.cs | 23 +++++++++++++++---- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/ObservableCollections/ObservableDictionary.cs b/src/ObservableCollections/ObservableDictionary.cs index e1c3d27..118fc87 100644 --- a/src/ObservableCollections/ObservableDictionary.cs +++ b/src/ObservableCollections/ObservableDictionary.cs @@ -3,6 +3,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Linq; namespace ObservableCollections { @@ -13,12 +14,27 @@ namespace ObservableCollections readonly Dictionary dictionary; public object SyncRoot { get; } = new object(); - public ObservableDictionary(IEqualityComparer? comparer = null) + public ObservableDictionary() + { + this.dictionary = new Dictionary(); + } + + public ObservableDictionary(IEqualityComparer? comparer) { this.dictionary = new Dictionary(comparer: comparer); } - public ObservableDictionary(IEnumerable> collection, IEqualityComparer? comparer = null) + public ObservableDictionary(int capacity, IEqualityComparer? comparer) + { + this.dictionary = new Dictionary(capacity, comparer: comparer); + } + + public ObservableDictionary(IEnumerable> collection) + : this(collection, null) + { + } + + public ObservableDictionary(IEnumerable> collection, IEqualityComparer? comparer) { #if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER this.dictionary = new Dictionary(collection: collection, comparer: comparer); diff --git a/src/ObservableCollections/ObservableHashSet.cs b/src/ObservableCollections/ObservableHashSet.cs index 08076cc..0b95615 100644 --- a/src/ObservableCollections/ObservableHashSet.cs +++ b/src/ObservableCollections/ObservableHashSet.cs @@ -14,21 +14,36 @@ namespace ObservableCollections readonly HashSet set; public object SyncRoot { get; } = new object(); - public ObservableHashSet(IEqualityComparer? comparer = null) + public ObservableHashSet() + { + this.set = new HashSet(); + } + + public ObservableHashSet(IEqualityComparer? comparer) { this.set = new HashSet(comparer: comparer); } #if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER - public ObservableHashSet(int capacity, IEqualityComparer? comparer = null) + public ObservableHashSet(int capacity) + { + this.set = new HashSet(capacity: capacity); + } + + public ObservableHashSet(int capacity, IEqualityComparer? comparer) { this.set = new HashSet(capacity: capacity, comparer: comparer); } #endif - public ObservableHashSet(IEnumerable collection, IEqualityComparer? comparer = null) + public ObservableHashSet(IEnumerable collection) + { + this.set = new HashSet(collection: collection); + } + + public ObservableHashSet(IEnumerable collection, IEqualityComparer? comparer) { this.set = new HashSet(collection: collection, comparer: comparer); } @@ -185,7 +200,7 @@ namespace ObservableCollections public bool TryGetValue(T equalValue, [MaybeNullWhen(false)] out T actualValue) { - lock(SyncRoot) + lock (SyncRoot) { return set.TryGetValue(equalValue, out actualValue); }