mirror of
https://github.com/aaru-dps/Aaru.CommonTypes.git
synced 2025-12-16 19:24:30 +00:00
Reformat.
This commit is contained in:
@@ -42,38 +42,22 @@ using System.Linq;
|
||||
|
||||
namespace DiscImageChef.CommonTypes.Extents
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements extents for <see cref="sbyte" />
|
||||
/// </summary>
|
||||
/// <summary>Implements extents for <see cref="sbyte" /></summary>
|
||||
public class ExtentsSByte
|
||||
{
|
||||
List<Tuple<sbyte, sbyte>> backend;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize an empty list of extents
|
||||
/// </summary>
|
||||
public ExtentsSByte()
|
||||
{
|
||||
backend = new List<Tuple<sbyte, sbyte>>();
|
||||
}
|
||||
/// <summary>Initialize an empty list of extents</summary>
|
||||
public ExtentsSByte() => backend = new List<Tuple<sbyte, sbyte>>();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes extents with an specific list
|
||||
/// </summary>
|
||||
/// <summary>Initializes extents with an specific list</summary>
|
||||
/// <param name="list">List of extents as tuples "start, end"</param>
|
||||
public ExtentsSByte(IEnumerable<Tuple<sbyte, sbyte>> list)
|
||||
{
|
||||
backend = list.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
public ExtentsSByte(IEnumerable<Tuple<sbyte, sbyte>> list) => backend = list.OrderBy(t => t.Item1).ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a count of how many extents are stored
|
||||
/// </summary>
|
||||
/// <summary>Gets a count of how many extents are stored</summary>
|
||||
public int Count => backend.Count;
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified number to the corresponding extent, or creates a new one
|
||||
/// </summary>
|
||||
/// <summary>Adds the specified number to the corresponding extent, or creates a new one</summary>
|
||||
/// <param name="item"></param>
|
||||
public void Add(sbyte item)
|
||||
{
|
||||
@@ -84,34 +68,41 @@ namespace DiscImageChef.CommonTypes.Extents
|
||||
for(int i = 0; i < backend.Count; i++)
|
||||
{
|
||||
// Already contained in an extent
|
||||
if(item >= backend[i].Item1 && item <= backend[i].Item2) return;
|
||||
if(item >= backend[i].Item1 &&
|
||||
item <= backend[i].Item2)
|
||||
return;
|
||||
|
||||
// Expands existing extent start
|
||||
if(item == backend[i].Item1 - 1)
|
||||
{
|
||||
removeOne = backend[i];
|
||||
|
||||
if(i > 0 && item == backend[i - 1].Item2 + 1)
|
||||
if(i > 0 &&
|
||||
item == backend[i - 1].Item2 + 1)
|
||||
{
|
||||
removeTwo = backend[i - 1];
|
||||
itemToAdd = new Tuple<sbyte, sbyte>(backend[i - 1].Item1, backend[i].Item2);
|
||||
}
|
||||
else itemToAdd = new Tuple<sbyte, sbyte>(item, backend[i].Item2);
|
||||
else
|
||||
itemToAdd = new Tuple<sbyte, sbyte>(item, backend[i].Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Expands existing extent end
|
||||
if(item != backend[i].Item2 + 1) continue;
|
||||
if(item != backend[i].Item2 + 1)
|
||||
continue;
|
||||
|
||||
removeOne = backend[i];
|
||||
|
||||
if(i < backend.Count - 1 && item == backend[i + 1].Item1 - 1)
|
||||
if(i < backend.Count - 1 &&
|
||||
item == backend[i + 1].Item1 - 1)
|
||||
{
|
||||
removeTwo = backend[i + 1];
|
||||
itemToAdd = new Tuple<sbyte, sbyte>(backend[i].Item1, backend[i + 1].Item2);
|
||||
}
|
||||
else itemToAdd = new Tuple<sbyte, sbyte>(backend[i].Item1, item);
|
||||
else
|
||||
itemToAdd = new Tuple<sbyte, sbyte>(backend[i].Item1, item);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -122,15 +113,14 @@ namespace DiscImageChef.CommonTypes.Extents
|
||||
backend.Remove(removeTwo);
|
||||
backend.Add(itemToAdd);
|
||||
}
|
||||
else backend.Add(new Tuple<sbyte, sbyte>(item, item));
|
||||
else
|
||||
backend.Add(new Tuple<sbyte, sbyte>(item, item));
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new extent
|
||||
/// </summary>
|
||||
/// <summary>Adds a new extent</summary>
|
||||
/// <param name="start">First element of the extent</param>
|
||||
/// <param name="end">
|
||||
/// Last element of the extent or if <see cref="run" /> is <c>true</c> how many elements the extent runs
|
||||
@@ -140,34 +130,26 @@ namespace DiscImageChef.CommonTypes.Extents
|
||||
public void Add(sbyte start, sbyte end, bool run = false)
|
||||
{
|
||||
sbyte realEnd;
|
||||
if(run) realEnd = (sbyte)(start + end - 1);
|
||||
else realEnd = end;
|
||||
|
||||
if(run)
|
||||
realEnd = (sbyte)(start + end - 1);
|
||||
else
|
||||
realEnd = end;
|
||||
|
||||
// TODO: Optimize this
|
||||
for(sbyte t = start; t <= realEnd; t++) Add(t);
|
||||
for(sbyte t = start; t <= realEnd; t++)
|
||||
Add(t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the specified item is contained by an extent on this instance
|
||||
/// </summary>
|
||||
/// <summary>Checks if the specified item is contained by an extent on this instance</summary>
|
||||
/// <param name="item">Item to seach for</param>
|
||||
/// <returns><c>true</c> if any of the extents on this instance contains the item</returns>
|
||||
public bool Contains(sbyte item)
|
||||
{
|
||||
return backend.Any(extent => item >= extent.Item1 && item <= extent.Item2);
|
||||
}
|
||||
public bool Contains(sbyte item) => backend.Any(extent => item >= extent.Item1 && item <= extent.Item2);
|
||||
|
||||
/// <summary>
|
||||
/// Removes all extents from this instance
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
backend.Clear();
|
||||
}
|
||||
/// <summary>Removes all extents from this instance</summary>
|
||||
public void Clear() => backend.Clear();
|
||||
|
||||
/// <summary>
|
||||
/// Removes an item from the extents in this instance
|
||||
/// </summary>
|
||||
/// <summary>Removes an item from the extents in this instance</summary>
|
||||
/// <param name="item">Item to remove</param>
|
||||
/// <returns><c>true</c> if the item was contained in a known extent and removed, false otherwise</returns>
|
||||
public bool Remove(sbyte item)
|
||||
@@ -179,43 +161,57 @@ namespace DiscImageChef.CommonTypes.Extents
|
||||
foreach(Tuple<sbyte, sbyte> extent in backend)
|
||||
{
|
||||
// Extent is contained and not a border
|
||||
if(item > extent.Item1 && item < extent.Item2)
|
||||
if(item > extent.Item1 &&
|
||||
item < extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<sbyte, sbyte>(extent.Item1, (sbyte)(item - 1));
|
||||
toAddTwo = new Tuple<sbyte, sbyte>((sbyte)(item + 1), extent.Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is left border, but not only element
|
||||
if(item == extent.Item1 && item != extent.Item2)
|
||||
if(item == extent.Item1 &&
|
||||
item != extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<sbyte, sbyte>((sbyte)(item + 1), extent.Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is right border, but not only element
|
||||
if(item != extent.Item1 && item == extent.Item2)
|
||||
if(item != extent.Item1 &&
|
||||
item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<sbyte, sbyte>(extent.Item1, (sbyte)(item - 1));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is only element
|
||||
if(item != extent.Item1 || item != extent.Item2) continue;
|
||||
if(item != extent.Item1 ||
|
||||
item != extent.Item2)
|
||||
continue;
|
||||
|
||||
toRemove = extent;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Item not found
|
||||
if(toRemove == null) return false;
|
||||
if(toRemove == null)
|
||||
return false;
|
||||
|
||||
backend.Remove(toRemove);
|
||||
if(toAddOne != null) backend.Add(toAddOne);
|
||||
if(toAddTwo != null) backend.Add(toAddTwo);
|
||||
|
||||
if(toAddOne != null)
|
||||
backend.Add(toAddOne);
|
||||
|
||||
if(toAddTwo != null)
|
||||
backend.Add(toAddTwo);
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
@@ -224,24 +220,24 @@ namespace DiscImageChef.CommonTypes.Extents
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the list of extents to an array of <see cref="Tuple" /> where T1 is first element of the extent and T2 is
|
||||
/// last element
|
||||
/// Converts the list of extents to an array of <see cref="Tuple" /> where T1 is first element of the extent and
|
||||
/// T2 is last element
|
||||
/// </summary>
|
||||
/// <returns>Array of <see cref="Tuple" /></returns>
|
||||
public Tuple<sbyte, sbyte>[] ToArray() => backend.ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first element of the extent that contains the specified item
|
||||
/// </summary>
|
||||
/// <summary>Gets the first element of the extent that contains the specified item</summary>
|
||||
/// <param name="item">Item</param>
|
||||
/// <param name="start">First element of extent</param>
|
||||
/// <returns><c>true</c> if item was found in an extent, false otherwise</returns>
|
||||
public bool GetStart(sbyte item, out sbyte start)
|
||||
{
|
||||
start = 0;
|
||||
|
||||
foreach(Tuple<sbyte, sbyte> extent in backend.Where(extent => item >= extent.Item1 && item <= extent.Item2))
|
||||
{
|
||||
start = extent.Item1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user