From 1aaafbc979679158640d3dfa6f14461300c6f5c7 Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 7 Jan 2020 15:28:43 +0100 Subject: [PATCH] Vitor suggestions --- src/neo/IO/Caching/HashSetCache.cs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/neo/IO/Caching/HashSetCache.cs b/src/neo/IO/Caching/HashSetCache.cs index f563ee82b4..d9acf927ec 100644 --- a/src/neo/IO/Caching/HashSetCache.cs +++ b/src/neo/IO/Caching/HashSetCache.cs @@ -9,7 +9,7 @@ public class HashSetCache : IReadOnlyCollection where T : IEquatable /// /// Cached count /// - private int _count = -1; + private int _count = 0; /// /// Sets where the Hashes are stored @@ -26,20 +26,8 @@ public class HashSetCache : IReadOnlyCollection where T : IEquatable /// private readonly int maxBucketCount; - public int Count - { - get - { - if (_count != -1) return _count; - _count = 0; - foreach (var set in sets) - { - _count += set.Count; - } - return _count; - } - } - + public int Count => _count; + public HashSetCache(int bucketCapacity, int maxBucketCount = 10) { if (bucketCapacity <= 0) throw new ArgumentOutOfRangeException($"{nameof(bucketCapacity)} should be greater than 0"); @@ -53,7 +41,7 @@ public HashSetCache(int bucketCapacity, int maxBucketCount = 10) public bool Add(T item) { if (Contains(item)) return false; - _count = -1; + _count++; if (sets.First.Value.Count < bucketCapacity) return sets.First.Value.Add(item); var newSet = new HashSet { @@ -81,7 +69,7 @@ public void ExceptWith(IEnumerable items) { if (set.Remove(item)) { - _count = -1; + _count--; break; } }