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