Ryujinx/Ryujinx.HLE/HOS/IdDictionary.cs

73 lines
1.5 KiB
C#
Raw Normal View History

2018-02-05 00:08:20 +01:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Ryujinx.HLE.HOS
2018-02-05 00:08:20 +01:00
{
class IdDictionary
2018-02-05 00:08:20 +01:00
{
private ConcurrentDictionary<int, object> Objs;
2018-02-05 00:08:20 +01:00
public IdDictionary()
{
Objs = new ConcurrentDictionary<int, object>();
2018-02-05 00:08:20 +01:00
}
public bool Add(int Id, object Data)
{
return Objs.TryAdd(Id, Data);
}
public int Add(object Data)
{
for (int Id = 1; Id < int.MaxValue; Id++)
2018-02-05 00:08:20 +01:00
{
if (Objs.TryAdd(Id, Data))
{
return Id;
}
2018-02-05 00:08:20 +01:00
}
throw new InvalidOperationException();
2018-02-05 00:08:20 +01:00
}
public object GetData(int Id)
{
if (Objs.TryGetValue(Id, out object Data))
{
return Data;
}
return null;
}
public T GetData<T>(int Id)
2018-02-05 00:08:20 +01:00
{
if (Objs.TryGetValue(Id, out object Data) && Data is T)
2018-02-05 00:08:20 +01:00
{
return (T)Data;
2018-02-05 00:08:20 +01:00
}
return default(T);
}
public object Delete(int Id)
2018-02-05 00:08:20 +01:00
{
if (Objs.TryRemove(Id, out object Obj))
2018-02-05 00:08:20 +01:00
{
return Obj;
2018-02-05 00:08:20 +01:00
}
return null;
2018-02-05 00:08:20 +01:00
}
public ICollection<object> Clear()
2018-02-05 00:08:20 +01:00
{
ICollection<object> Values = Objs.Values;
2018-02-05 00:08:20 +01:00
Objs.Clear();
return Values;
2018-02-05 00:08:20 +01:00
}
}
}