Skip to content

Commit 7a53fb3

Browse files
Contract handling improvements.
Some added extensions. Mostly cosmetic changes.
1 parent 0ca9b9e commit 7a53fb3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+4089
-4149
lines changed

source/CollectionWrapper.cs

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,51 @@
11
using System.Collections.Generic;
22

3-
namespace Open.Collections
3+
namespace Open.Collections;
4+
5+
public class CollectionWrapper<T, TCollection> : ReadOnlyCollectionWrapper<T, TCollection>, ICollection<T>
6+
where TCollection : class, ICollection<T>
47
{
5-
public class CollectionWrapper<T, TCollection> : ReadOnlyCollectionWrapper<T, TCollection>, ICollection<T>
6-
where TCollection : class, ICollection<T>
8+
protected CollectionWrapper(TCollection source) : base(source)
9+
{
10+
}
11+
12+
#region Implementation of ICollection<T>
13+
14+
/// <inheritdoc />
15+
public virtual void Add(T item) => InternalSource.Add(item);
16+
17+
/// <inheritdoc cref="ICollection&lt;T&gt;" />
18+
/// <param name="item1">First item to add.</param>
19+
/// <param name="item2">Additional item to add.</param>
20+
/// <param name="items">Extended param items to add.</param>
21+
public virtual void Add(T item1, T item2, params T[] items)
722
{
8-
protected CollectionWrapper(TCollection source) : base(source)
9-
{
10-
}
11-
12-
#region Implementation of ICollection<T>
13-
14-
/// <inheritdoc />
15-
public virtual void Add(T item) => InternalSource.Add(item);
16-
17-
/// <inheritdoc cref="ICollection&lt;T&gt;" />
18-
/// <param name="item1">First item to add.</param>
19-
/// <param name="item2">Additional item to add.</param>
20-
/// <param name="items">Extended param items to add.</param>
21-
public virtual void Add(T item1, T item2, params T[] items)
22-
{
23-
InternalSource.Add(item1);
24-
InternalSource.Add(item2);
25-
foreach (var i in items)
26-
InternalSource.Add(i);
27-
}
28-
29-
30-
/// <summary>
31-
/// Adds mutliple items to the collection.
32-
/// It's important to avoid locking for too long so an array is used to add multiple items.
33-
/// An enumerable is potentially slow as it may be yielding to a process.
34-
/// </summary>
35-
/// <param name="items">The items to add.</param>
36-
public virtual void Add(T[] items)
37-
{
38-
foreach (var i in items)
39-
InternalSource.Add(i);
40-
}
41-
42-
/// <inheritdoc />
43-
public virtual void Clear() => InternalSource.Clear();
44-
45-
/// <inheritdoc />
46-
public virtual bool Remove(T item) => InternalSource.Remove(item);
47-
48-
/// <inheritdoc />
49-
public override bool IsReadOnly
50-
=> InternalSource.IsReadOnly;
51-
#endregion
23+
InternalSource.Add(item1);
24+
InternalSource.Add(item2);
25+
foreach (var i in items)
26+
InternalSource.Add(i);
5227
}
28+
29+
/// <summary>
30+
/// Adds mutliple items to the collection.
31+
/// It's important to avoid locking for too long so an array is used to add multiple items.
32+
/// An enumerable is potentially slow as it may be yielding to a process.
33+
/// </summary>
34+
/// <param name="items">The items to add.</param>
35+
public virtual void Add(T[] items)
36+
{
37+
foreach (var i in items)
38+
InternalSource.Add(i);
39+
}
40+
41+
/// <inheritdoc />
42+
public virtual void Clear() => InternalSource.Clear();
43+
44+
/// <inheritdoc />
45+
public virtual bool Remove(T item) => InternalSource.Remove(item);
46+
47+
/// <inheritdoc />
48+
public override bool IsReadOnly
49+
=> InternalSource.IsReadOnly;
50+
#endregion
5351
}

source/ConcurrentHashSet.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System.Collections.Concurrent;
22
using System.Collections.Generic;
33

4-
namespace Open.Collections
4+
namespace Open.Collections;
5+
6+
public class ConcurrentHashSet<T> : DictionaryToHashSetWrapper<T>
57
{
6-
public class ConcurrentHashSet<T> : DictionaryToHashSetWrapper<T>
8+
public ConcurrentHashSet(IEnumerable<T>? intialValues = null)
9+
: base(new ConcurrentDictionary<T, bool>())
710
{
8-
public ConcurrentHashSet(IEnumerable<T>? intialValues = null)
9-
: base(new ConcurrentDictionary<T, bool>())
10-
{
11-
if (intialValues is not null)
12-
UnionWith(intialValues);
13-
}
11+
if (intialValues is not null)
12+
UnionWith(intialValues);
1413
}
1514
}

source/DictionaryToHashSetWrapper.cs

Lines changed: 82 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,117 +2,116 @@
22
using System.Collections;
33
using System.Collections.Generic;
44

5-
namespace Open.Collections
5+
namespace Open.Collections;
6+
7+
public class DictionaryToHashSetWrapper<T> : ISet<T>
68
{
7-
public class DictionaryToHashSetWrapper<T> : ISet<T>
8-
{
9-
protected readonly IDictionary<T, bool> InternalSource;
9+
protected readonly IDictionary<T, bool> InternalSource;
1010

11-
// ReSharper disable once MemberCanBeProtected.Global
12-
public DictionaryToHashSetWrapper(IDictionary<T, bool> source) => InternalSource = source;
11+
// ReSharper disable once MemberCanBeProtected.Global
12+
public DictionaryToHashSetWrapper(IDictionary<T, bool> source) => InternalSource = source;
1313

14-
/// <inheritdoc />
15-
public int Count
16-
=> InternalSource.Count;
14+
/// <inheritdoc />
15+
public int Count
16+
=> InternalSource.Count;
1717

18-
/// <inheritdoc />
19-
public bool IsReadOnly
20-
=> InternalSource.IsReadOnly;
18+
/// <inheritdoc />
19+
public bool IsReadOnly
20+
=> InternalSource.IsReadOnly;
2121

22-
/// <inheritdoc />
23-
public virtual bool Add(T item)
22+
/// <inheritdoc />
23+
public virtual bool Add(T item)
24+
{
25+
if (InternalSource.ContainsKey(item))
26+
return false;
27+
28+
try
29+
{
30+
InternalSource.Add(item, true);
31+
}
32+
catch
2433
{
25-
if (InternalSource.ContainsKey(item))
26-
return false;
27-
28-
try
29-
{
30-
InternalSource.Add(item, true);
31-
}
32-
catch
33-
{
34-
return false;
35-
}
36-
return true;
34+
return false;
3735
}
36+
return true;
37+
}
3838

39-
/// <inheritdoc />
40-
public bool Remove(T item) => InternalSource.Remove(item);
39+
/// <inheritdoc />
40+
public bool Remove(T item) => InternalSource.Remove(item);
4141

42-
/// <inheritdoc />
43-
public void Clear() => InternalSource.Clear();
42+
/// <inheritdoc />
43+
public void Clear() => InternalSource.Clear();
4444

45-
/// <inheritdoc />
46-
public bool Contains(T item) => InternalSource.ContainsKey(item);
45+
/// <inheritdoc />
46+
public bool Contains(T item) => InternalSource.ContainsKey(item);
4747

48-
/// <inheritdoc />
49-
public void CopyTo(T[] array, int arrayIndex) => InternalSource.Keys.CopyTo(array, arrayIndex);
48+
/// <inheritdoc />
49+
public void CopyTo(T[] array, int arrayIndex) => InternalSource.Keys.CopyTo(array, arrayIndex);
5050

51-
/// <inheritdoc cref="ReadOnlyCollectionWrapper{T, TCollection}.CopyTo(Span{T})"/>
52-
public virtual Span<T> CopyTo(Span<T> span) => InternalSource.Keys.CopyToSpan(span);
51+
/// <inheritdoc cref="ReadOnlyCollectionWrapper{T, TCollection}.CopyTo(Span{T})"/>
52+
public virtual Span<T> CopyTo(Span<T> span) => InternalSource.Keys.CopyToSpan(span);
5353

54-
/// <summary>
55-
/// Returns a copy of the underlying keys.
56-
/// </summary>
57-
public HashSet<T> ToHashSet() => new(InternalSource.Keys);
54+
/// <summary>
55+
/// Returns a copy of the underlying keys.
56+
/// </summary>
57+
public HashSet<T> ToHashSet() => new(InternalSource.Keys);
5858

59-
/// <inheritdoc />
60-
public IEnumerator<T> GetEnumerator() => InternalSource.Keys.GetEnumerator();
59+
/// <inheritdoc />
60+
public IEnumerator<T> GetEnumerator() => InternalSource.Keys.GetEnumerator();
6161

62-
/// <inheritdoc />
63-
public void ExceptWith(IEnumerable<T> other)
64-
{
65-
foreach (var e in other) Remove(e);
66-
}
62+
/// <inheritdoc />
63+
public void ExceptWith(IEnumerable<T> other)
64+
{
65+
foreach (var e in other) Remove(e);
66+
}
6767

68-
/// <inheritdoc />
69-
public void IntersectWith(IEnumerable<T> other)
68+
/// <inheritdoc />
69+
public void IntersectWith(IEnumerable<T> other)
70+
{
71+
foreach (var e in other)
7072
{
71-
foreach (var e in other)
72-
{
73-
if (!InternalSource.ContainsKey(e))
74-
Remove(e);
75-
}
73+
if (!InternalSource.ContainsKey(e))
74+
Remove(e);
7675
}
76+
}
7777

78-
/// <inheritdoc />
79-
public bool IsProperSubsetOf(IEnumerable<T> other) => ToHashSet().IsProperSubsetOf(other);
78+
/// <inheritdoc />
79+
public bool IsProperSubsetOf(IEnumerable<T> other) => ToHashSet().IsProperSubsetOf(other);
8080

81-
/// <inheritdoc />
82-
public bool IsProperSupersetOf(IEnumerable<T> other) => ToHashSet().IsProperSupersetOf(other);
81+
/// <inheritdoc />
82+
public bool IsProperSupersetOf(IEnumerable<T> other) => ToHashSet().IsProperSupersetOf(other);
8383

84-
/// <inheritdoc />
85-
public bool IsSubsetOf(IEnumerable<T> other) => ToHashSet().IsSubsetOf(other);
84+
/// <inheritdoc />
85+
public bool IsSubsetOf(IEnumerable<T> other) => ToHashSet().IsSubsetOf(other);
8686

87-
/// <inheritdoc />
88-
public bool IsSupersetOf(IEnumerable<T> other) => ToHashSet().IsSupersetOf(other);
87+
/// <inheritdoc />
88+
public bool IsSupersetOf(IEnumerable<T> other) => ToHashSet().IsSupersetOf(other);
8989

90-
/// <inheritdoc />
91-
public bool Overlaps(IEnumerable<T> other) => ToHashSet().Overlaps(other);
90+
/// <inheritdoc />
91+
public bool Overlaps(IEnumerable<T> other) => ToHashSet().Overlaps(other);
9292

93-
/// <inheritdoc />
94-
public bool SetEquals(IEnumerable<T> other) => ToHashSet().SetEquals(other);
93+
/// <inheritdoc />
94+
public bool SetEquals(IEnumerable<T> other) => ToHashSet().SetEquals(other);
9595

96-
/// <inheritdoc />
97-
public void SymmetricExceptWith(IEnumerable<T> other)
96+
/// <inheritdoc />
97+
public void SymmetricExceptWith(IEnumerable<T> other)
98+
{
99+
foreach (var e in other)
98100
{
99-
foreach (var e in other)
100-
{
101-
if (InternalSource.ContainsKey(e))
102-
Add(e);
103-
else
104-
Remove(e);
105-
}
101+
if (InternalSource.ContainsKey(e))
102+
Add(e);
103+
else
104+
Remove(e);
106105
}
106+
}
107107

108-
/// <inheritdoc />
109-
public void UnionWith(IEnumerable<T> other)
110-
{
111-
foreach (var e in other) Add(e);
112-
}
108+
/// <inheritdoc />
109+
public void UnionWith(IEnumerable<T> other)
110+
{
111+
foreach (var e in other) Add(e);
112+
}
113113

114-
void ICollection<T>.Add(T item) => Add(item);
114+
void ICollection<T>.Add(T item) => Add(item);
115115

116-
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
117-
}
116+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
118117
}

0 commit comments

Comments
 (0)