using System.Collections;
using System.Collections.Concurrent;

namespace ThingsGateway.NewLife.Collections;

/// <summary>并行哈希集合</summary>
/// <remarks>
/// 主要用于频繁添加删除而又要遍历的场合
/// </remarks>
public class ConcurrentHashSet<T> : IEnumerable<T> where T : notnull
{
    private readonly ConcurrentDictionary<T, Byte> _dic = new();

    /// <summary>是否空集合</summary>
    public Boolean IsEmpty => _dic.IsEmpty;

    /// <summary>元素个数</summary>
    public Int32 Count => _dic.Count;

    /// <summary>是否包含元素</summary>
    /// <param name="item"></param>
    /// <returns></returns>
    public Boolean Contain(T item) => _dic.ContainsKey(item);

    /// <summary>尝试添加</summary>
    /// <param name="item"></param>
    /// <returns></returns>
    public Boolean TryAdd(T item) => _dic.TryAdd(item, 0);

    /// <summary>尝试删除</summary>
    /// <param name="item"></param>
    /// <returns></returns>
    public Boolean TryRemove(T item) => _dic.TryRemove(item, out _);

    #region IEnumerable<T> 成员
    IEnumerator<T> IEnumerable<T>.GetEnumerator() => _dic.Keys.GetEnumerator();
    #endregion

    #region IEnumerable 成员
    IEnumerator IEnumerable.GetEnumerator() => _dic.Keys.GetEnumerator();
    #endregion
}