IdPool.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections.Generic;
  2. namespace Ryujinx.Core.OsHle.Utilities
  3. {
  4. class IdPool
  5. {
  6. private HashSet<int> Ids;
  7. private int CurrId;
  8. private int MinId;
  9. private int MaxId;
  10. public IdPool(int Min, int Max)
  11. {
  12. Ids = new HashSet<int>();
  13. CurrId = Min;
  14. MinId = Min;
  15. MaxId = Max;
  16. }
  17. public IdPool() : this(1, int.MaxValue) { }
  18. public int GenerateId()
  19. {
  20. lock (Ids)
  21. {
  22. for (int Cnt = MinId; Cnt < MaxId; Cnt++)
  23. {
  24. if (Ids.Add(CurrId))
  25. {
  26. return CurrId;
  27. }
  28. if (CurrId++ == MaxId)
  29. {
  30. CurrId = MinId;
  31. }
  32. }
  33. return -1;
  34. }
  35. }
  36. public bool DeleteId(int Id)
  37. {
  38. lock (Ids)
  39. {
  40. return Ids.Remove(Id);
  41. }
  42. }
  43. }
  44. }