2018-02-04 16:08:20 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
2018-08-16 17:47:36 -06:00
|
|
|
namespace Ryujinx.HLE.HOS
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-03-19 12:58:46 -06:00
|
|
|
class IdDictionary
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
private ConcurrentDictionary<int, object> _objs;
|
2018-02-04 16:08:20 -07:00
|
|
|
|
2018-03-11 22:04:52 -06:00
|
|
|
public IdDictionary()
|
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
_objs = new ConcurrentDictionary<int, object>();
|
2018-02-04 16:08:20 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
public bool Add(int id, object data)
|
2018-03-20 09:18:25 -06:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
return _objs.TryAdd(id, data);
|
2018-03-20 09:18:25 -06:00
|
|
|
}
|
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
public int Add(object data)
|
2018-03-11 22:04:52 -06:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
for (int id = 1; id < int.MaxValue; id++)
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
if (_objs.TryAdd(id, data))
|
2018-03-11 22:04:52 -06:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
return id;
|
2018-03-11 22:04:52 -06:00
|
|
|
}
|
2018-02-04 16:08:20 -07:00
|
|
|
}
|
|
|
|
|
2018-03-11 22:04:52 -06:00
|
|
|
throw new InvalidOperationException();
|
2018-02-04 16:08:20 -07:00
|
|
|
}
|
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
public object GetData(int id)
|
2018-03-11 22:04:52 -06:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
if (_objs.TryGetValue(id, out object data))
|
2018-03-11 22:04:52 -06:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
return data;
|
2018-03-11 22:04:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
public T GetData<T>(int id)
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
if (_objs.TryGetValue(id, out object data) && data is T)
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
return (T)data;
|
2018-02-04 16:08:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return default(T);
|
|
|
|
}
|
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
public object Delete(int id)
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
if (_objs.TryRemove(id, out object obj))
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
return obj;
|
2018-02-04 16:08:20 -07:00
|
|
|
}
|
2018-03-11 22:04:52 -06:00
|
|
|
|
2018-03-19 12:58:46 -06:00
|
|
|
return null;
|
2018-02-04 16:08:20 -07:00
|
|
|
}
|
|
|
|
|
2018-03-19 12:58:46 -06:00
|
|
|
public ICollection<object> Clear()
|
2018-02-04 16:08:20 -07:00
|
|
|
{
|
2018-12-06 04:16:24 -07:00
|
|
|
ICollection<object> values = _objs.Values;
|
2018-02-04 16:08:20 -07:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
_objs.Clear();
|
2018-03-19 12:58:46 -06:00
|
|
|
|
2018-12-06 04:16:24 -07:00
|
|
|
return values;
|
2018-02-04 16:08:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|