using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Ryujinx.Memory.Range
{
///
/// Sorted list of ranges that supports binary search.
///
/// Type of the range.
public class RangeList : IEnumerable where T : IRange
{
private readonly struct RangeItem where TValue : IRange
{
public readonly ulong Address;
public readonly ulong EndAddress;
public readonly TValue Value;
public RangeItem(TValue value)
{
Value = value;
Address = value.Address;
EndAddress = value.Address + value.Size;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool OverlapsWith(ulong address, ulong endAddress)
{
return Address < endAddress && address < EndAddress;
}
}
private const int BackingInitialSize = 1024;
private const int ArrayGrowthSize = 32;
private RangeItem[] _items;
private readonly int _backingGrowthSize;
public int Count { get; protected set; }
///
/// Creates a new range list.
///
/// The initial size of the backing array
public RangeList(int backingInitialSize = BackingInitialSize)
{
_backingGrowthSize = backingInitialSize;
_items = new RangeItem[backingInitialSize];
}
///
/// Adds a new item to the list.
///
/// The item to be added
public void Add(T item)
{
int index = BinarySearch(item.Address);
if (index < 0)
{
index = ~index;
}
Insert(index, new RangeItem(item));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Insert(int index, RangeItem item)
{
if (Count + 1 > _items.Length)
{
Array.Resize(ref _items, _items.Length + _backingGrowthSize);
}
if (index >= Count)
{
if (index == Count)
{
_items[Count++] = item;
}
}
else
{
Array.Copy(_items, index, _items, index + 1, Count - index);
_items[index] = item;
Count++;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void RemoveAt(int index)
{
if (index < --Count)
{
Array.Copy(_items, index + 1, _items, index, Count - index);
}
}
///
/// Removes an item from the list.
///
/// The item to be removed
/// True if the item was removed, or false if it was not found
public bool Remove(T item)
{
int index = BinarySearch(item.Address);
if (index >= 0)
{
while (index > 0 && _items[index - 1].Address == item.Address)
{
index--;
}
while (index < Count)
{
if (_items[index].Value.Equals(item))
{
RemoveAt(index);
return true;
}
if (_items[index].Address > item.Address)
{
break;
}
index++;
}
}
return false;
}
///
/// Updates an item's end address.
///
/// The item to be updated
public void UpdateEndAddress(T item)
{
int index = BinarySearch(item.Address);
if (index >= 0)
{
while (index > 0 && _items[index - 1].Address == item.Address)
{
index--;
}
while (index < Count)
{
if (_items[index].Value.Equals(item))
{
_items[index] = new RangeItem(item);
return;
}
if (_items[index].Address > item.Address)
{
break;
}
index++;
}
}
}
///
/// Gets the first item on the list overlapping in memory with the specified item.
///
///
/// Despite the name, this has no ordering guarantees of the returned item.
/// It only ensures that the item returned overlaps the specified item.
///
/// Item to check for overlaps
/// The overlapping item, or the default value for the type if none found
public T FindFirstOverlap(T item)
{
return FindFirstOverlap(item.Address, item.Size);
}
///
/// Gets the first item on the list overlapping the specified memory range.
///
///
/// Despite the name, this has no ordering guarantees of the returned item.
/// It only ensures that the item returned overlaps the specified memory range.
///
/// Start address of the range
/// Size in bytes of the range
/// The overlapping item, or the default value for the type if none found
public T FindFirstOverlap(ulong address, ulong size)
{
int index = BinarySearch(address, address + size);
if (index < 0)
{
return default(T);
}
return _items[index].Value;
}
///
/// Gets all items overlapping with the specified item in memory.
///
/// Item to check for overlaps
/// Output array where matches will be written. It is automatically resized to fit the results
/// The number of overlapping items found
public int FindOverlaps(T item, ref T[] output)
{
return FindOverlaps(item.Address, item.Size, ref output);
}
///
/// Gets all items on the list overlapping the specified memory range.
///
/// Start address of the range
/// Size in bytes of the range
/// Output array where matches will be written. It is automatically resized to fit the results
/// The number of overlapping items found
public int FindOverlaps(ulong address, ulong size, ref T[] output)
{
int outputIndex = 0;
ulong endAddress = address + size;
for (int i = 0; i < Count; i++)
{
ref RangeItem item = ref _items[i];
if (item.Address >= endAddress)
{
break;
}
if (item.OverlapsWith(address, endAddress))
{
if (outputIndex == output.Length)
{
Array.Resize(ref output, outputIndex + ArrayGrowthSize);
}
output[outputIndex++] = item.Value;
}
}
return outputIndex;
}
///
/// Gets all items overlapping with the specified item in memory.
///
///
/// This method only returns correct results if none of the items on the list overlaps with
/// each other. If that is not the case, this method should not be used.
/// This method is faster than the regular method to find all overlaps.
///
/// Item to check for overlaps
/// Output array where matches will be written. It is automatically resized to fit the results
/// The number of overlapping items found
public int FindOverlapsNonOverlapping(T item, ref T[] output)
{
return FindOverlapsNonOverlapping(item.Address, item.Size, ref output);
}
///
/// Gets all items on the list overlapping the specified memory range.
///
///
/// This method only returns correct results if none of the items on the list overlaps with
/// each other. If that is not the case, this method should not be used.
/// This method is faster than the regular method to find all overlaps.
///
/// Start address of the range
/// Size in bytes of the range
/// Output array where matches will be written. It is automatically resized to fit the results
/// The number of overlapping items found
public int FindOverlapsNonOverlapping(ulong address, ulong size, ref T[] output)
{
// This is a bit faster than FindOverlaps, but only works
// when none of the items on the list overlaps with each other.
int outputIndex = 0;
ulong endAddress = address + size;
int index = BinarySearch(address, endAddress);
if (index >= 0)
{
while (index > 0 && _items[index - 1].OverlapsWith(address, endAddress))
{
index--;
}
do
{
if (outputIndex == output.Length)
{
Array.Resize(ref output, outputIndex + ArrayGrowthSize);
}
output[outputIndex++] = _items[index++].Value;
}
while (index < Count && _items[index].OverlapsWith(address, endAddress));
}
return outputIndex;
}
///
/// Gets all items on the list with the specified memory address.
///
/// Address to find
/// Output array where matches will be written. It is automatically resized to fit the results
/// The number of matches found
public int FindOverlaps(ulong address, ref T[] output)
{
int index = BinarySearch(address);
int outputIndex = 0;
if (index >= 0)
{
while (index > 0 && _items[index - 1].Address == address)
{
index--;
}
while (index < Count)
{
ref RangeItem overlap = ref _items[index++];
if (overlap.Address != address)
{
break;
}
if (outputIndex == output.Length)
{
Array.Resize(ref output, outputIndex + ArrayGrowthSize);
}
output[outputIndex++] = overlap.Value;
}
}
return outputIndex;
}
///
/// Performs binary search on the internal list of items.
///
/// Address to find
/// List index of the item, or complement index of nearest item with lower value on the list
private int BinarySearch(ulong address)
{
int left = 0;
int right = Count - 1;
while (left <= right)
{
int range = right - left;
int middle = left + (range >> 1);
ref RangeItem item = ref _items[middle];
if (item.Address == address)
{
return middle;
}
if (address < item.Address)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return ~left;
}
///
/// Performs binary search for items overlapping a given memory range.
///
/// Start address of the range
/// End address of the range
/// List index of the item, or complement index of nearest item with lower value on the list
private int BinarySearch(ulong address, ulong endAddress)
{
int left = 0;
int right = Count - 1;
while (left <= right)
{
int range = right - left;
int middle = left + (range >> 1);
ref RangeItem item = ref _items[middle];
if (item.OverlapsWith(address, endAddress))
{
return middle;
}
if (address < item.Address)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return ~left;
}
public IEnumerator GetEnumerator()
{
for (int i = 0; i < Count; i++)
{
yield return _items[i].Value;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
for (int i = 0; i < Count; i++)
{
yield return _items[i].Value;
}
}
}
}