1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-11-10 17:36:39 +00:00
Ryujinx/Ryujinx.Core/OsHle/IdDictionary.cs

82 lines
1.6 KiB
C#
Raw Normal View History

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