Ryujinx/Ryujinx.Graphics.Gpu/Memory/RangeList.cs

244 lines
5.8 KiB
C#
Raw Normal View History

2019-10-13 08:02:07 +02:00
using System.Collections.Generic;
namespace Ryujinx.Graphics.Gpu.Memory
{
class RangeList<T> where T : IRange<T>
{
private List<T> _items;
public int Count => _items.Count;
2019-10-13 08:02:07 +02:00
public RangeList()
{
_items = new List<T>();
}
public void Add(T item)
{
int index = BinarySearch(item.Address);
2019-10-13 08:02:07 +02:00
if (index < 0)
{
index = ~index;
2019-10-13 08:02:07 +02:00
}
_items.Insert(index, item);
2019-10-13 08:02:07 +02:00
}
public bool Remove(T item)
{
int index = BinarySearch(item.Address);
if (index >= 0)
2019-10-13 08:02:07 +02:00
{
while (index > 0 && _items[index - 1].Address == item.Address)
{
index--;
}
2019-10-13 08:02:07 +02:00
while (index < _items.Count)
2019-10-13 08:02:07 +02:00
{
if (_items[index].Equals(item))
2019-10-13 08:02:07 +02:00
{
_items.RemoveAt(index);
return true;
2019-10-13 08:02:07 +02:00
}
if (_items[index].Address > item.Address)
2019-10-13 08:02:07 +02:00
{
break;
}
2019-10-13 08:02:07 +02:00
index++;
}
}
2019-10-13 08:02:07 +02:00
return false;
}
2019-10-13 08:02:07 +02:00
public bool CanExitEarly(ulong address, ulong size)
{
int index = BinarySearch(address, size);
if (index >= 0)
{
T item = _items[index];
return address >= item.Address && address + size <= item.Address + item.Size;
2019-10-13 08:02:07 +02:00
}
return false;
}
public T FindFirstOverlap(T item)
{
return FindFirstOverlap(item.Address, item.Size);
}
public T FindFirstOverlap(ulong address, ulong size)
{
int index = BinarySearch(address, size);
2019-10-13 08:02:07 +02:00
if (index < 0)
{
return default(T);
2019-10-13 08:02:07 +02:00
}
return _items[index];
2019-10-13 08:02:07 +02:00
}
public T[] FindOverlaps(T item)
{
return FindOverlaps(item.Address, item.Size);
}
public T[] FindOverlaps(ulong address, ulong size)
{
List<T> overlapsList = new List<T>();
ulong endAddress = address + size;
lock (_items)
{
foreach (T item in _items)
{
if (item.Address >= endAddress)
{
break;
}
if (item.OverlapsWith(address, size))
{
overlapsList.Add(item);
}
}
}
return overlapsList.ToArray();
}
public T[] FindOverlapsNonOverlapping(T item)
{
return FindOverlapsNonOverlapping(item.Address, item.Size);
}
public T[] FindOverlapsNonOverlapping(ulong address, ulong size)
2019-10-13 08:02:07 +02:00
{
// This is a bit faster than FindOverlaps, but only works
// when none of the items on the list overlaps with each other.
2019-10-13 08:02:07 +02:00
List<T> overlapsList = new List<T>();
ulong endAddress = address + size;
int index = BinarySearch(address, size);
if (index >= 0)
2019-10-13 08:02:07 +02:00
{
while (index > 0 && _items[index - 1].OverlapsWith(address, size))
{
index--;
}
2019-10-13 08:02:07 +02:00
do
2019-10-13 08:02:07 +02:00
{
overlapsList.Add(_items[index++]);
}
while (index < _items.Count && _items[index].OverlapsWith(address, size));
}
2019-10-13 08:02:07 +02:00
return overlapsList.ToArray();
}
public T[] FindOverlaps(ulong address)
{
List<T> overlapsList = new List<T>();
2019-10-13 08:02:07 +02:00
int index = BinarySearch(address);
2019-10-13 08:02:07 +02:00
if (index >= 0)
{
while (index > 0 && _items[index - 1].Address == address)
{
index--;
}
while (index < _items.Count)
{
T overlap = _items[index++];
if (overlap.Address != address)
{
break;
2019-10-13 08:02:07 +02:00
}
overlapsList.Add(overlap);
2019-10-13 08:02:07 +02:00
}
}
return overlapsList.ToArray();
}
private int BinarySearch(ulong address)
{
int left = 0;
int right = _items.Count - 1;
while (left <= right)
{
int range = right - left;
int middle = left + (range >> 1);
T item = _items[middle];
if (item.Address == address)
{
return middle;
}
if (address < item.Address)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return ~left;
}
private int BinarySearch(ulong address, ulong size)
{
int left = 0;
int right = _items.Count - 1;
while (left <= right)
{
int range = right - left;
int middle = left + (range >> 1);
T item = _items[middle];
if (item.OverlapsWith(address, size))
{
return middle;
}
if (address < item.Address)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return ~left;
}
}
}