|
using System.Collections;
using System.Collections.Generic;
using Pfz.Caching;
using Pfz.Threading;
namespace Pfz.Collections
{
/// <summary>
/// A very simple hashset collection that's thread-safe and also calls TrimExcess automatically.
/// </summary>
public class AutoTrimHashSet<T>:
ThreadSafeDisposable,
ICollection<T>
{
private HashSet<T> _hashSet;
/// <summary>
/// Creates a new hashset.
/// </summary>
public AutoTrimHashSet()
{
_hashSet = new HashSet<T>();
GCUtils.Collected += _Collected;
}
/// <summary>
/// Creates a new hashset, using the given comparer.
/// </summary>
public AutoTrimHashSet(IEqualityComparer<T> comparer)
{
_hashSet = new HashSet<T>(comparer);
GCUtils.Collected += _Collected;
}
/// <summary>
/// Returns the comparer used by this hashset.
/// </summary>
public IEqualityComparer<T> Comparer
{
get
{
var hashSet = _hashSet;
CheckUndisposed();
return hashSet.Comparer;
}
}
/// <summary>
/// Unregisters this from GCUtils.Collected.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
GCUtils.Collected -= _Collected;
_hashSet = null;
}
base.Dispose(disposing);
}
private void _Collected()
{
try
{
lock(DisposeLock)
{
if (WasDisposed)
{
GCUtils.Collected -= _Collected;
return;
}
_hashSet.TrimExcess();
}
}
catch
{
}
}
/// <summary>
/// Clears this hashset.
/// </summary>
public void Clear()
{
lock(DisposeLock)
{
CheckUndisposed();
_hashSet.Clear();
}
}
/// <summary>
/// Tries to add an item to the hashset. Returns true if the item was added.
/// </summary>
public bool Add(T item)
{
lock(DisposeLock)
{
CheckUndisposed();
return _hashSet.Add(item);
}
}
/// <summary>
/// Removes an item. Returns true if the item was found and removed.
/// </summary>
public bool Remove(T item)
{
lock(DisposeLock)
{
CheckUndisposed();
return _hashSet.Remove(item);
}
}
/// <summary>
/// Verifies if an item exists.
/// </summary>
public bool Contains(T item)
{
lock(DisposeLock)
{
CheckUndisposed();
return _hashSet.Contains(item);
}
}
/// <summary>
/// Copies the values from this hashset to an array.
/// </summary>
public void CopyTo(T[] array, int arrayIndex)
{
lock(DisposeLock)
{
CheckUndisposed();
_hashSet.CopyTo(array, arrayIndex);
}
}
/// <summary>
/// Gets the number of items in this hashset.
/// </summary>
public int Count
{
get
{
lock(DisposeLock)
{
CheckUndisposed();
return _hashSet.Count;
}
}
}
/// <summary>
/// Gets a list that is a copy of this hashset.
/// </summary>
public List<T> ToList()
{
lock(DisposeLock)
{
CheckUndisposed();
int count = _hashSet.Count;
var result = new List<T>(count);
foreach(T value in _hashSet)
result.Add(value);
return result;
}
}
/// <summary>
/// Gets an enumerator over a copy of this hashset.
/// </summary>
public IEnumerator<T> GetEnumerator()
{
return ToList().GetEnumerator();
}
/// <summary>
/// Returns an array that's a copy of this hashset.
/// </summary>
public T[] ToArray()
{
lock(DisposeLock)
{
CheckUndisposed();
int count = _hashSet.Count;
var result = new T[count];
int index = -1;
foreach(T value in _hashSet)
{
index++;
result[index] = value;
}
return result;
}
}
#region ICollection<T> Members
void ICollection<T>.Add(T item)
{
Add(item);
}
bool ICollection<T>.IsReadOnly
{
get
{
return false;
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.
At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.
Want more info or simply want to contact me?
Take a look at:
http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com
Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).