IdDictionary.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 ICollection<object> Values => _objs.Values;
  10. public IdDictionary()
  11. {
  12. _objs = new ConcurrentDictionary<int, object>();
  13. }
  14. public bool Add(int id, object data)
  15. {
  16. return _objs.TryAdd(id, data);
  17. }
  18. public int Add(object data)
  19. {
  20. for (int id = 1; id < int.MaxValue; id++)
  21. {
  22. if (_objs.TryAdd(id, data))
  23. {
  24. return id;
  25. }
  26. }
  27. throw new InvalidOperationException();
  28. }
  29. public object GetData(int id)
  30. {
  31. if (_objs.TryGetValue(id, out object data))
  32. {
  33. return data;
  34. }
  35. return null;
  36. }
  37. public T GetData<T>(int id)
  38. {
  39. if (_objs.TryGetValue(id, out object data) && data is T)
  40. {
  41. return (T)data;
  42. }
  43. return default(T);
  44. }
  45. public object Delete(int id)
  46. {
  47. if (_objs.TryRemove(id, out object obj))
  48. {
  49. return obj;
  50. }
  51. return null;
  52. }
  53. public ICollection<object> Clear()
  54. {
  55. ICollection<object> values = _objs.Values;
  56. _objs.Clear();
  57. return values;
  58. }
  59. }
  60. }