2022-02-18 01:53:18 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace ARMeilleure.Translation
|
|
|
|
{
|
|
|
|
internal class TranslatorCache<T>
|
|
|
|
{
|
|
|
|
private readonly IntervalTree<ulong, T> _tree;
|
2023-10-12 16:11:15 +00:00
|
|
|
private readonly ReaderWriterLockSlim _treeLock;
|
2022-02-18 01:53:18 +00:00
|
|
|
|
|
|
|
public int Count => _tree.Count;
|
|
|
|
|
|
|
|
public TranslatorCache()
|
|
|
|
{
|
|
|
|
_tree = new IntervalTree<ulong, T>();
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock = new ReaderWriterLockSlim();
|
2022-02-18 01:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryAdd(ulong address, ulong size, T value)
|
|
|
|
{
|
|
|
|
return AddOrUpdate(address, size, value, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool AddOrUpdate(ulong address, ulong size, T value, Func<ulong, T, T> updateFactoryCallback)
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.EnterWriteLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
bool result = _tree.AddOrUpdate(address, address + size, value, updateFactoryCallback);
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.ExitWriteLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T GetOrAdd(ulong address, ulong size, T value)
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.EnterWriteLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
value = _tree.GetOrAdd(address, address + size, value);
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.ExitWriteLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Remove(ulong address)
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.EnterWriteLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
bool removed = _tree.Remove(address) != 0;
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.ExitWriteLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.EnterWriteLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
_tree.Clear();
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.ExitWriteLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool ContainsKey(ulong address)
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.EnterReadLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
bool result = _tree.ContainsKey(address);
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.ExitReadLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryGetValue(ulong address, out T value)
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.EnterReadLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
bool result = _tree.TryGet(address, out value);
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.ExitReadLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int GetOverlaps(ulong address, ulong size, ref ulong[] overlaps)
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.EnterReadLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
int count = _tree.Get(address, address + size, ref overlaps);
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.ExitReadLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<T> AsList()
|
|
|
|
{
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.EnterReadLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
List<T> list = _tree.AsList();
|
2023-10-12 16:11:15 +00:00
|
|
|
_treeLock.ExitReadLock();
|
2022-02-18 01:53:18 +00:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|