Skip to content

Commit

Permalink
Cache count
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Jan 7, 2020
1 parent 15fd05b commit 625f7a8
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/neo/IO/Caching/HashSetCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace Neo.IO.Caching
{
public class HashSetCache<T> : IReadOnlyCollection<T> where T : IEquatable<T>
{
/// <summary>
/// Cached count
/// </summary>
private int _count = -1;

/// <summary>
/// Sets where the Hashes are stored
/// </summary>
Expand All @@ -25,12 +30,13 @@ public int Count
{
get
{
int count = 0;
if (_count != -1) return _count;
_count = 0;
foreach (var set in sets)
{
count += set.Count;
_count += set.Count;
}
return count;
return _count;
}
}

Expand All @@ -47,6 +53,7 @@ public HashSetCache(int bucketCapacity, int maxBucketCount = 10)
public bool Add(T item)
{
if (Contains(item)) return false;
_count = -1;
if (sets.First.Value.Count < bucketCapacity) return sets.First.Value.Add(item);
var newSet = new HashSet<T>
{
Expand All @@ -72,7 +79,11 @@ public void ExceptWith(IEnumerable<T> items)
{
foreach (var set in sets)
{
if (set.Remove(item)) break;
if (set.Remove(item))
{
_count = -1;
break;
}
}
}
}
Expand Down

0 comments on commit 625f7a8

Please sign in to comment.