IdDictionary.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.HLE.HOS
  5. {
  6. class IdDictionary
  7. {
  8. private ConcurrentDictionary<int, object> Objs;
  9. public IdDictionary()
  10. {
  11. Objs = new ConcurrentDictionary<int, object>();
  12. }
  13. public bool Add(int Id, object Data)
  14. {
  15. return Objs.TryAdd(Id, Data);
  16. }
  17. public int Add(object Data)
  18. {
  19. for (int Id = 1; Id < int.MaxValue; Id++)
  20. {
  21. if (Objs.TryAdd(Id, Data))
  22. {
  23. return Id;
  24. }
  25. }
  26. throw new InvalidOperationException();
  27. }
  28. public object GetData(int Id)
  29. {
  30. if (Objs.TryGetValue(Id, out object Data))
  31. {
  32. return Data;
  33. }
  34. return null;
  35. }
  36. public T GetData<T>(int Id)
  37. {
  38. if (Objs.TryGetValue(Id, out object Data) && Data is T)
  39. {
  40. return (T)Data;
  41. }
  42. return default(T);
  43. }
  44. public object Delete(int Id)
  45. {
  46. if (Objs.TryRemove(Id, out object Obj))
  47. {
  48. return Obj;
  49. }
  50. return null;
  51. }
  52. public ICollection<object> Clear()
  53. {
  54. ICollection<object> Values = Objs.Values;
  55. Objs.Clear();
  56. return Values;
  57. }
  58. }
  59. }