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