Skip to content

Commit

Permalink
capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Aug 20, 2024
1 parent b0f2a50 commit d7fac36
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/ObservableCollections/ObservableDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace ObservableCollections
{
Expand All @@ -13,12 +14,27 @@ public sealed partial class ObservableDictionary<TKey, TValue> : IDictionary<TKe
readonly Dictionary<TKey, TValue> dictionary;
public object SyncRoot { get; } = new object();

public ObservableDictionary(IEqualityComparer<TKey>? comparer = null)
public ObservableDictionary()
{
this.dictionary = new Dictionary<TKey, TValue>();
}

public ObservableDictionary(IEqualityComparer<TKey>? comparer)
{
this.dictionary = new Dictionary<TKey, TValue>(comparer: comparer);
}

public ObservableDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey>? comparer = null)
public ObservableDictionary(int capacity, IEqualityComparer<TKey>? comparer)
{
this.dictionary = new Dictionary<TKey, TValue>(capacity, comparer: comparer);
}

public ObservableDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection)
: this(collection, null)
{
}

public ObservableDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey>? comparer)
{
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
this.dictionary = new Dictionary<TKey, TValue>(collection: collection, comparer: comparer);
Expand Down
23 changes: 19 additions & 4 deletions src/ObservableCollections/ObservableHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,36 @@ public sealed partial class ObservableHashSet<T> : IReadOnlySet<T>, IReadOnlyCol
readonly HashSet<T> set;
public object SyncRoot { get; } = new object();

public ObservableHashSet(IEqualityComparer<T>? comparer = null)
public ObservableHashSet()
{
this.set = new HashSet<T>();
}

public ObservableHashSet(IEqualityComparer<T>? comparer)
{
this.set = new HashSet<T>(comparer: comparer);
}

#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER

public ObservableHashSet(int capacity, IEqualityComparer<T>? comparer = null)
public ObservableHashSet(int capacity)
{
this.set = new HashSet<T>(capacity: capacity);
}

public ObservableHashSet(int capacity, IEqualityComparer<T>? comparer)
{
this.set = new HashSet<T>(capacity: capacity, comparer: comparer);
}

#endif

public ObservableHashSet(IEnumerable<T> collection, IEqualityComparer<T>? comparer = null)
public ObservableHashSet(IEnumerable<T> collection)
{
this.set = new HashSet<T>(collection: collection);
}

public ObservableHashSet(IEnumerable<T> collection, IEqualityComparer<T>? comparer)
{
this.set = new HashSet<T>(collection: collection, comparer: comparer);
}
Expand Down Expand Up @@ -185,7 +200,7 @@ public void Clear()

public bool TryGetValue(T equalValue, [MaybeNullWhen(false)] out T actualValue)
{
lock(SyncRoot)
lock (SyncRoot)
{
return set.TryGetValue(equalValue, out actualValue);
}
Expand Down

0 comments on commit d7fac36

Please sign in to comment.