OrderedSet.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.Collections.Generic;
  2. namespace Jint.Runtime
  3. {
  4. internal sealed class OrderedSet<T>
  5. {
  6. internal readonly List<T> _list;
  7. private readonly HashSet<T> _set;
  8. public OrderedSet()
  9. {
  10. _list = new List<T>();
  11. _set = new HashSet<T>();
  12. }
  13. public T this[int index]
  14. {
  15. get => _list[index];
  16. set
  17. {
  18. if (_set.Add(value))
  19. {
  20. _list[index] = value;
  21. }
  22. }
  23. }
  24. public void Add(T item)
  25. {
  26. if (_set.Add(item))
  27. {
  28. _list.Add(item);
  29. }
  30. }
  31. public void Clear()
  32. {
  33. _list.Clear();
  34. _set.Clear();
  35. }
  36. public bool Contains(T item) => _set.Contains(item);
  37. public int Count => _list.Count;
  38. public bool Remove(T item)
  39. {
  40. _set.Remove(item);
  41. return _list.Remove(item);
  42. }
  43. }
  44. }