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
{
2018-12-01 21:01:59 +01:00
private ConcurrentDictionary<int, object> _objs;
2018-02-05 00:08:20 +01:00
public IdDictionary()
{
2018-12-01 21:01:59 +01:00
_objs = new ConcurrentDictionary<int, object>();
2018-02-05 00:08:20 +01:00
}
2018-12-01 21:01:59 +01:00
public bool Add(int id, object data)
{
2018-12-01 21:01:59 +01:00
return _objs.TryAdd(id, data);
}
2018-12-01 21:01:59 +01:00
public int Add(object data)
{
2018-12-01 21:01:59 +01:00
for (int id = 1; id < int.MaxValue; id++)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
if (_objs.TryAdd(id, data))
{
2018-12-01 21:01:59 +01:00
return id;
}
2018-02-05 00:08:20 +01:00
}
throw new InvalidOperationException();
2018-02-05 00:08:20 +01:00
}
2018-12-01 21:01:59 +01:00
public object GetData(int id)
{
2018-12-01 21:01:59 +01:00
if (_objs.TryGetValue(id, out object data))
{
2018-12-01 21:01:59 +01:00
return data;
}
return null;
}
2018-12-01 21:01:59 +01:00
public T GetData<T>(int id)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
if (_objs.TryGetValue(id, out object data) && data is T)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
return (T)data;
2018-02-05 00:08:20 +01:00
}
return default(T);
}
2018-12-01 21:01:59 +01:00
public object Delete(int id)
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +01:00
if (_objs.TryRemove(id, out object obj))
2018-02-05 00:08:20 +01:00
{
2018-12-01 21:01:59 +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
{
2018-12-01 21:01:59 +01:00
ICollection<object> values = _objs.Values;
2018-02-05 00:08:20 +01:00
2018-12-01 21:01:59 +01:00
_objs.Clear();
2018-12-01 21:01:59 +01:00
return values;
2018-02-05 00:08:20 +01:00
}
}
}