Last active
November 25, 2018 08:20
-
-
Save mjs3339/eb016b584df1040a4c279bd95b838f78 to your computer and use it in GitHub Desktop.
C# BigHashSet Class Greater than 2GB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DebuggerTypeProxy(typeof(HashSetDebugViewInt<>))] | |
[DebuggerDisplay("Count = {Count}")] | |
[Serializable] | |
public class BigHashSet<T> : IEnumerable<T> | |
{ | |
private readonly BigSet<T> _set; | |
public BigHashSet() : this(new BigComparer<T>()) | |
{ | |
} | |
public BigHashSet(IBigEqualityComparer<T> comparer) | |
{ | |
if(comparer == null) comparer = new BigComparer<T>(); | |
_set = new BigSet<T>(); | |
_set._comparer = comparer; | |
} | |
public BigHashSet(ulong size) : this(new BigComparer<T>(), size) | |
{ | |
} | |
public BigHashSet(IBigEqualityComparer<T> comparer, ulong size) | |
{ | |
if(comparer == null) | |
comparer = new BigComparer<T>(); | |
_set = new BigSet<T>(size); | |
_set._comparer = comparer; | |
} | |
public BigHashSet(IEnumerable<T> collection) | |
{ | |
_set = new BigSet<T>(); | |
_set._comparer = new BigComparer<T>(); | |
foreach(var item in collection) | |
_set.Add(item); | |
} | |
public ulong Count => _set.Count; | |
IEnumerator<T> IEnumerable<T>.GetEnumerator() | |
{ | |
return new Enumerator(this); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return new Enumerator(this); | |
} | |
public bool Add(T item) | |
{ | |
return _set.Insert(item, true); | |
} | |
public void AddRange(IEnumerable<T> collection) | |
{ | |
foreach(var item in collection) | |
_set.Insert(item, true); | |
} | |
public bool Contains(T item) | |
{ | |
return _set.Insert(item, false); | |
} | |
public Enumerator GetEnumerator() | |
{ | |
return new Enumerator(this); | |
} | |
[Serializable] | |
public struct Enumerator : IEnumerator<T> | |
{ | |
private readonly BigHashSet<T> _ths; | |
private ulong _index; | |
public T Current{get; private set;} | |
object IEnumerator.Current | |
{ | |
get | |
{ | |
if(_index == 0 || _index == _ths._set.Count + 1) | |
throw new InvalidOperationException($"Enumerator out of range: {_index}"); | |
return Current; | |
} | |
} | |
internal Enumerator(BigHashSet<T> set) | |
{ | |
_ths = set; | |
_index = 0; | |
Current = default; | |
} | |
public void Dispose() | |
{ | |
} | |
public bool MoveNext() | |
{ | |
if(_index < _ths._set.Count) | |
{ | |
Current = _ths._set._slots[_index].value; | |
_index++; | |
return true; | |
} | |
_index = _ths._set.Count + 1; | |
Current = default; | |
return false; | |
} | |
void IEnumerator.Reset() | |
{ | |
_index = 0; | |
Current = default; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment