| | 1 | | using System; |
| | 2 | | using System.Collections.Concurrent; |
| | 3 | | using System.Collections.Generic; |
| | 4 | |
|
| | 5 | | namespace SharpHoundCommonLib; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// A concurrent implementation of a hashset using a ConcurrentDictionary as the backing structure. |
| | 9 | | /// </summary> |
| | 10 | | public class ConcurrentHashSet : IDisposable{ |
| | 11 | | private ConcurrentDictionary<string, byte> _backingDictionary; |
| | 12 | |
|
| 0 | 13 | | public ConcurrentHashSet() { |
| 0 | 14 | | _backingDictionary = new ConcurrentDictionary<string, byte>(); |
| 0 | 15 | | } |
| | 16 | |
|
| 162 | 17 | | public ConcurrentHashSet(StringComparer comparison) { |
| 81 | 18 | | _backingDictionary = new ConcurrentDictionary<string, byte>(comparison); |
| 81 | 19 | | } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Attempts to add an item to the set. Returns true if adding was successful, false otherwise |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="item"></param> |
| | 25 | | /// <returns></returns> |
| 27 | 26 | | public bool Add(string item) { |
| 27 | 27 | | return _backingDictionary.TryAdd(item, byte.MinValue); |
| 27 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Attempts to remove an item from the set. Returns true of removing was successful, false otherwise |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="item"></param> |
| | 34 | | /// <returns></returns> |
| 0 | 35 | | public bool Remove(string item) { |
| 0 | 36 | | return _backingDictionary.TryRemove(item, out _); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Checks if the given item is in the set |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="item"></param> |
| | 43 | | /// <returns></returns> |
| 27 | 44 | | public bool Contains(string item) { |
| 27 | 45 | | return _backingDictionary.ContainsKey(item); |
| 27 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Returns all values in the set |
| | 50 | | /// </summary> |
| | 51 | | /// <returns></returns> |
| 0 | 52 | | public IEnumerable<string> Values() { |
| 0 | 53 | | return _backingDictionary.Keys; |
| 0 | 54 | | } |
| | 55 | |
|
| 0 | 56 | | public void Dispose() { |
| 0 | 57 | | _backingDictionary = null; |
| 0 | 58 | | GC.SuppressFinalize(this); |
| 0 | 59 | | } |
| | 60 | | } |