Archived
1
0
Fork 0
forked from Mirror/Ryujinx
This repository has been archived on 2024-10-11. You can view files and clone it, but cannot push or open issues or pull requests.
jinx/Ryujinx.Core/OsHle/IdDictionary.cs

101 lines
2.1 KiB
C#
Raw Normal View History

2018-02-04 23:08:20 +00:00
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Ryujinx.Core.OsHle
2018-02-04 23:08:20 +00:00
{
class IdDictionary : IEnumerable<object>
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 bool ReplaceData(int Id, object Data)
{
if (Objs.ContainsKey(Id))
{
Objs[Id] = Data;
return true;
}
return false;
}
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 bool Delete(int Id)
2018-02-04 23:08:20 +00:00
{
if (Objs.TryRemove(Id, out object Obj))
{
if (Obj is IDisposable DisposableObj)
{
DisposableObj.Dispose();
}
FreeIdHint = Id;
return true;
2018-02-04 23:08:20 +00:00
}
return false;
2018-02-04 23:08:20 +00:00
}
IEnumerator<object> IEnumerable<object>.GetEnumerator()
2018-02-04 23:08:20 +00:00
{
return Objs.Values.GetEnumerator();
2018-02-04 23:08:20 +00:00
}
IEnumerator IEnumerable.GetEnumerator()
{
return Objs.Values.GetEnumerator();
2018-02-04 23:08:20 +00:00
}
}
}