| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- namespace Ryujinx.Core.OsHle
- {
- class IdDictionary : IEnumerable<object>
- {
- private ConcurrentDictionary<int, object> Objs;
- private int FreeIdHint = 1;
- public IdDictionary()
- {
- Objs = new ConcurrentDictionary<int, object>();
- }
- public int Add(object Data)
- {
- if (Objs.TryAdd(FreeIdHint, Data))
- {
- return FreeIdHint++;
- }
- return AddSlow(Data);
- }
- private int AddSlow(object Data)
- {
- for (int Id = 1; Id < int.MaxValue; Id++)
- {
- if (Objs.TryAdd(Id, Data))
- {
- return Id;
- }
- }
- throw new InvalidOperationException();
- }
- public bool ReplaceData(int Id, object Data)
- {
- if (Objs.ContainsKey(Id))
- {
- Objs[Id] = Data;
- return true;
- }
- return false;
- }
- public object GetData(int Id)
- {
- if (Objs.TryGetValue(Id, out object Data))
- {
- return Data;
- }
- return null;
- }
- public T GetData<T>(int Id)
- {
- if (Objs.TryGetValue(Id, out object Data) && Data is T)
- {
- return (T)Data;
- }
- return default(T);
- }
- public bool Delete(int Id)
- {
- if (Objs.TryRemove(Id, out object Obj))
- {
- if (Obj is IDisposable DisposableObj)
- {
- DisposableObj.Dispose();
- }
- FreeIdHint = Id;
- return true;
- }
- return false;
- }
- IEnumerator<object> IEnumerable<object>.GetEnumerator()
- {
- return Objs.Values.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return Objs.Values.GetEnumerator();
- }
- }
- }
|