Skip to content

Commit 4edcd9a

Browse files
authored
Merge branch 'master' into fix/typo-in-core
2 parents 17fb515 + 15d36b7 commit 4edcd9a

File tree

9 files changed

+72
-86
lines changed

9 files changed

+72
-86
lines changed

src/Neo/IO/Caching/Cache.cs src/Neo.IO/Caching/Cache.cs

+23-33
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,21 @@
1717

1818
namespace Neo.IO.Caching
1919
{
20-
internal abstract class Cache<TKey, TValue> : ICollection<TValue>, IDisposable
20+
internal abstract class Cache<TKey, TValue>
21+
(int max_capacity, IEqualityComparer<TKey>? comparer = null) : ICollection<TValue>, IDisposable
22+
where TKey : notnull
2123
{
2224
protected class CacheItem
25+
(TKey key, TValue value)
2326
{
24-
public readonly TKey Key;
25-
public readonly TValue Value;
26-
public readonly DateTime Time;
27-
28-
public CacheItem(TKey key, TValue value)
29-
{
30-
Key = key;
31-
Value = value;
32-
Time = TimeProvider.Current.UtcNow;
33-
}
27+
public readonly TKey Key = key;
28+
public readonly TValue Value = value;
29+
public readonly DateTime Time = DateTime.UtcNow;
3430
}
3531

3632
protected readonly ReaderWriterLockSlim RwSyncRootLock = new(LockRecursionPolicy.SupportsRecursion);
37-
protected readonly Dictionary<TKey, CacheItem> InnerDictionary;
38-
private readonly int max_capacity;
33+
protected readonly Dictionary<TKey, CacheItem> InnerDictionary = new Dictionary<TKey, CacheItem>(comparer);
34+
private readonly int _max_capacity = max_capacity;
3935

4036
public TValue this[TKey key]
4137
{
@@ -44,7 +40,7 @@ public TValue this[TKey key]
4440
RwSyncRootLock.EnterReadLock();
4541
try
4642
{
47-
if (!InnerDictionary.TryGetValue(key, out CacheItem item)) throw new KeyNotFoundException();
43+
if (!InnerDictionary.TryGetValue(key, out CacheItem? item)) throw new KeyNotFoundException();
4844
OnAccess(item);
4945
return item.Value;
5046
}
@@ -73,15 +69,9 @@ public int Count
7369

7470
public bool IsReadOnly => false;
7571

76-
public Cache(int max_capacity, IEqualityComparer<TKey> comparer = null)
77-
{
78-
this.max_capacity = max_capacity;
79-
InnerDictionary = new Dictionary<TKey, CacheItem>(comparer);
80-
}
81-
8272
public void Add(TValue item)
8373
{
84-
TKey key = GetKeyForItem(item);
74+
var key = GetKeyForItem(item);
8575
RwSyncRootLock.EnterWriteLock();
8676
try
8777
{
@@ -95,16 +85,16 @@ public void Add(TValue item)
9585

9686
private void AddInternal(TKey key, TValue item)
9787
{
98-
if (InnerDictionary.TryGetValue(key, out CacheItem cacheItem))
88+
if (InnerDictionary.TryGetValue(key, out CacheItem? cacheItem))
9989
{
10090
OnAccess(cacheItem);
10191
}
10292
else
10393
{
104-
if (InnerDictionary.Count >= max_capacity)
94+
if (InnerDictionary.Count >= _max_capacity)
10595
{
10696
//TODO: Perform a performance test on the PLINQ query to determine which algorithm is better here (parallel or not)
107-
foreach (CacheItem item_del in InnerDictionary.Values.AsParallel().OrderBy(p => p.Time).Take(InnerDictionary.Count - max_capacity + 1))
97+
foreach (var item_del in InnerDictionary.Values.AsParallel().OrderBy(p => p.Time).Take(InnerDictionary.Count - _max_capacity + 1))
10898
{
10999
RemoveInternal(item_del);
110100
}
@@ -118,9 +108,9 @@ public void AddRange(IEnumerable<TValue> items)
118108
RwSyncRootLock.EnterWriteLock();
119109
try
120110
{
121-
foreach (TValue item in items)
111+
foreach (var item in items)
122112
{
123-
TKey key = GetKeyForItem(item);
113+
var key = GetKeyForItem(item);
124114
AddInternal(key, item);
125115
}
126116
}
@@ -135,7 +125,7 @@ public void Clear()
135125
RwSyncRootLock.EnterWriteLock();
136126
try
137127
{
138-
foreach (CacheItem item_del in InnerDictionary.Values.ToArray())
128+
foreach (var item_del in InnerDictionary.Values.ToArray())
139129
{
140130
RemoveInternal(item_del);
141131
}
@@ -151,7 +141,7 @@ public bool Contains(TKey key)
151141
RwSyncRootLock.EnterReadLock();
152142
try
153143
{
154-
if (!InnerDictionary.TryGetValue(key, out CacheItem cacheItem)) return false;
144+
if (!InnerDictionary.TryGetValue(key, out CacheItem? cacheItem)) return false;
155145
OnAccess(cacheItem);
156146
return true;
157147
}
@@ -171,7 +161,7 @@ public void CopyTo(TValue[] array, int arrayIndex)
171161
if (array == null) throw new ArgumentNullException();
172162
if (arrayIndex < 0) throw new ArgumentOutOfRangeException();
173163
if (arrayIndex + InnerDictionary.Count > array.Length) throw new ArgumentException();
174-
foreach (TValue item in this)
164+
foreach (var item in this)
175165
{
176166
array[arrayIndex++] = item;
177167
}
@@ -188,7 +178,7 @@ public IEnumerator<TValue> GetEnumerator()
188178
RwSyncRootLock.EnterReadLock();
189179
try
190180
{
191-
foreach (TValue item in InnerDictionary.Values.Select(p => p.Value))
181+
foreach (var item in InnerDictionary.Values.Select(p => p.Value))
192182
{
193183
yield return item;
194184
}
@@ -211,7 +201,7 @@ public bool Remove(TKey key)
211201
RwSyncRootLock.EnterWriteLock();
212202
try
213203
{
214-
if (!InnerDictionary.TryGetValue(key, out CacheItem cacheItem)) return false;
204+
if (!InnerDictionary.TryGetValue(key, out CacheItem? cacheItem)) return false;
215205
RemoveInternal(cacheItem);
216206
return true;
217207
}
@@ -242,7 +232,7 @@ public bool TryGet(TKey key, out TValue item)
242232
RwSyncRootLock.EnterReadLock();
243233
try
244234
{
245-
if (InnerDictionary.TryGetValue(key, out CacheItem cacheItem))
235+
if (InnerDictionary.TryGetValue(key, out CacheItem? cacheItem))
246236
{
247237
OnAccess(cacheItem);
248238
item = cacheItem.Value;
@@ -253,7 +243,7 @@ public bool TryGet(TKey key, out TValue item)
253243
{
254244
RwSyncRootLock.ExitReadLock();
255245
}
256-
item = default;
246+
item = default!;
257247
return false;
258248
}
259249
}

src/Neo/IO/Caching/FIFOCache.cs src/Neo.IO/Caching/FIFOCache.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313

1414
namespace Neo.IO.Caching
1515
{
16-
internal abstract class FIFOCache<TKey, TValue> : Cache<TKey, TValue>
16+
internal abstract class FIFOCache<TKey, TValue>
17+
(int max_capacity, IEqualityComparer<TKey>? comparer = null) : Cache<TKey, TValue>(max_capacity, comparer)
18+
where TKey : notnull
1719
{
18-
public FIFOCache(int max_capacity, IEqualityComparer<TKey> comparer = null)
19-
: base(max_capacity, comparer)
20-
{
21-
}
22-
2320
protected override void OnAccess(CacheItem item)
2421
{
2522
}

src/Neo/IO/Caching/HashSetCache.cs src/Neo.IO/Caching/HashSetCache.cs

+18-18
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ class HashSetCache<T> : IReadOnlyCollection<T> where T : IEquatable<T>
2020
/// <summary>
2121
/// Sets where the Hashes are stored
2222
/// </summary>
23-
private readonly LinkedList<HashSet<T>> sets = new();
23+
private readonly LinkedList<HashSet<T>> _sets = new();
2424

2525
/// <summary>
26-
/// Maximum capacity of each bucket inside each HashSet of <see cref="sets"/>.
26+
/// Maximum capacity of each bucket inside each HashSet of <see cref="_sets"/>.
2727
/// </summary>
28-
private readonly int bucketCapacity;
28+
private readonly int _bucketCapacity;
2929

3030
/// <summary>
3131
/// Maximum number of buckets for the LinkedList, meaning its maximum cardinality.
3232
/// </summary>
33-
private readonly int maxBucketCount;
33+
private readonly int _maxBucketCount;
3434

3535
/// <summary>
3636
/// Entry count
@@ -43,32 +43,32 @@ public HashSetCache(int bucketCapacity, int maxBucketCount = 10)
4343
if (maxBucketCount <= 0) throw new ArgumentOutOfRangeException($"{nameof(maxBucketCount)} should be greater than 0");
4444

4545
Count = 0;
46-
this.bucketCapacity = bucketCapacity;
47-
this.maxBucketCount = maxBucketCount;
48-
sets.AddFirst(new HashSet<T>());
46+
_bucketCapacity = bucketCapacity;
47+
_maxBucketCount = maxBucketCount;
48+
_sets.AddFirst([]);
4949
}
5050

5151
public bool Add(T item)
5252
{
5353
if (Contains(item)) return false;
5454
Count++;
55-
if (sets.First.Value.Count < bucketCapacity) return sets.First.Value.Add(item);
55+
if (_sets.First?.Value.Count < _bucketCapacity) return _sets.First.Value.Add(item);
5656
var newSet = new HashSet<T>
5757
{
5858
item
5959
};
60-
sets.AddFirst(newSet);
61-
if (sets.Count > maxBucketCount)
60+
_sets.AddFirst(newSet);
61+
if (_sets.Count > _maxBucketCount)
6262
{
63-
Count -= sets.Last.Value.Count;
64-
sets.RemoveLast();
63+
Count -= _sets.Last?.Value.Count ?? 0;
64+
_sets.RemoveLast();
6565
}
6666
return true;
6767
}
6868

6969
public bool Contains(T item)
7070
{
71-
foreach (var set in sets)
71+
foreach (var set in _sets)
7272
{
7373
if (set.Contains(item)) return true;
7474
}
@@ -77,17 +77,17 @@ public bool Contains(T item)
7777

7878
public void ExceptWith(IEnumerable<T> items)
7979
{
80-
List<HashSet<T>> removeList = null;
80+
List<HashSet<T>> removeList = default!;
8181
foreach (var item in items)
8282
{
83-
foreach (var set in sets)
83+
foreach (var set in _sets)
8484
{
8585
if (set.Remove(item))
8686
{
8787
Count--;
8888
if (set.Count == 0)
8989
{
90-
removeList ??= new List<HashSet<T>>();
90+
removeList ??= [];
9191
removeList.Add(set);
9292
}
9393
break;
@@ -97,13 +97,13 @@ public void ExceptWith(IEnumerable<T> items)
9797
if (removeList == null) return;
9898
foreach (var set in removeList)
9999
{
100-
sets.Remove(set);
100+
_sets.Remove(set);
101101
}
102102
}
103103

104104
public IEnumerator<T> GetEnumerator()
105105
{
106-
foreach (var set in sets)
106+
foreach (var set in _sets)
107107
{
108108
foreach (var item in set)
109109
{

src/Neo/IO/Caching/IndexedQueue.cs src/Neo.IO/Caching/IndexedQueue.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ public void Enqueue(T item)
8989
{
9090
if (_array.Length == _count)
9191
{
92-
int newSize = _array.Length * GrowthFactor;
92+
var newSize = _array.Length * GrowthFactor;
9393
if (_head == 0)
9494
{
9595
Array.Resize(ref _array, newSize);
9696
}
9797
else
9898
{
99-
T[] buffer = new T[newSize];
99+
var buffer = new T[newSize];
100100
Array.Copy(_array, _head, buffer, 0, _array.Length - _head);
101101
Array.Copy(_array, 0, buffer, _array.Length - _head, _head);
102102
_array = buffer;
@@ -127,7 +127,7 @@ public bool TryPeek(out T item)
127127
{
128128
if (_count == 0)
129129
{
130-
item = default;
130+
item = default!;
131131
return false;
132132
}
133133
else
@@ -145,7 +145,7 @@ public T Dequeue()
145145
{
146146
if (_count == 0)
147147
throw new InvalidOperationException("The queue is empty");
148-
T result = _array[_head];
148+
var result = _array[_head];
149149
++_head;
150150
_head %= _array.Length;
151151
--_count;
@@ -161,7 +161,7 @@ public bool TryDequeue(out T item)
161161
{
162162
if (_count == 0)
163163
{
164-
item = default;
164+
item = default!;
165165
return false;
166166
}
167167
else
@@ -194,7 +194,7 @@ public void TrimExcess()
194194
}
195195
else if (_array.Length * TrimThreshold >= _count)
196196
{
197-
T[] arr = new T[_count];
197+
var arr = new T[_count];
198198
CopyTo(arr, 0);
199199
_array = arr;
200200
_head = 0;
@@ -228,14 +228,14 @@ public void CopyTo(T[] array, int arrayIndex)
228228
/// <returns>An array containing the queue's items</returns>
229229
public T[] ToArray()
230230
{
231-
T[] result = new T[_count];
231+
var result = new T[_count];
232232
CopyTo(result, 0);
233233
return result;
234234
}
235235

236236
public IEnumerator<T> GetEnumerator()
237237
{
238-
for (int i = 0; i < _count; i++)
238+
for (var i = 0; i < _count; i++)
239239
yield return _array[(_head + i) % _array.Length];
240240
}
241241

0 commit comments

Comments
 (0)