Files
Aaru.CommonTypes/Extents/ExtentsShort.cs

247 lines
9.1 KiB
C#
Raw Normal View History

// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : ExtentsShort.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Extent helpers.
//
// --[ Description ] ----------------------------------------------------------
//
// Provides extents for short types.
//
// --[ License ] --------------------------------------------------------------
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ----------------------------------------------------------------------------
2018-12-29 17:34:38 +00:00
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
namespace DiscImageChef.CommonTypes.Extents
{
2019-11-03 01:41:48 +00:00
/// <summary>Implements extents for <see cref="short" /></summary>
public class ExtentsShort
{
List<Tuple<short, short>> backend;
2019-11-03 01:41:48 +00:00
/// <summary>Initialize an empty list of extents</summary>
public ExtentsShort() => backend = new List<Tuple<short, short>>();
2019-11-03 01:41:48 +00:00
/// <summary>Initializes extents with an specific list</summary>
/// <param name="list">List of extents as tuples "start, end"</param>
2019-11-03 01:41:48 +00:00
public ExtentsShort(IEnumerable<Tuple<short, short>> list) => backend = list.OrderBy(t => t.Item1).ToList();
2019-11-03 01:41:48 +00:00
/// <summary>Gets a count of how many extents are stored</summary>
public int Count => backend.Count;
2019-11-03 01:41:48 +00:00
/// <summary>Adds the specified number to the corresponding extent, or creates a new one</summary>
/// <param name="item"></param>
public void Add(short item)
{
Tuple<short, short> removeOne = null;
Tuple<short, short> removeTwo = null;
Tuple<short, short> itemToAdd = null;
for(int i = 0; i < backend.Count; i++)
{
// Already contained in an extent
2019-11-03 01:41:48 +00:00
if(item >= backend[i].Item1 &&
item <= backend[i].Item2)
return;
// Expands existing extent start
if(item == backend[i].Item1 - 1)
{
removeOne = backend[i];
2019-11-03 01:41:48 +00:00
if(i > 0 &&
item == backend[i - 1].Item2 + 1)
{
removeTwo = backend[i - 1];
itemToAdd = new Tuple<short, short>(backend[i - 1].Item1, backend[i].Item2);
}
2019-11-03 01:41:48 +00:00
else
itemToAdd = new Tuple<short, short>(item, backend[i].Item2);
break;
}
// Expands existing extent end
2019-11-03 01:41:48 +00:00
if(item != backend[i].Item2 + 1)
continue;
removeOne = backend[i];
2019-11-03 01:41:48 +00:00
if(i < backend.Count - 1 &&
item == backend[i + 1].Item1 - 1)
{
removeTwo = backend[i + 1];
itemToAdd = new Tuple<short, short>(backend[i].Item1, backend[i + 1].Item2);
}
2019-11-03 01:41:48 +00:00
else
itemToAdd = new Tuple<short, short>(backend[i].Item1, item);
break;
}
if(itemToAdd != null)
{
backend.Remove(removeOne);
backend.Remove(removeTwo);
backend.Add(itemToAdd);
}
2019-11-03 01:41:48 +00:00
else
backend.Add(new Tuple<short, short>(item, item));
// Sort
backend = backend.OrderBy(t => t.Item1).ToList();
}
2019-11-03 01:41:48 +00:00
/// <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
/// for
/// </param>
/// <param name="run">If set to <c>true</c>, <see cref="end" /> indicates how many elements the extent runs for</param>
public void Add(short start, short end, bool run = false)
{
short realEnd;
2019-11-03 01:41:48 +00:00
if(run)
realEnd = (short)(start + end - 1);
else
realEnd = end;
// TODO: Optimize this
2019-11-03 01:41:48 +00:00
for(short t = start; t <= realEnd; t++)
Add(t);
}
2019-11-03 01:41:48 +00:00
/// <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>
2019-11-03 01:41:48 +00:00
public bool Contains(short item) => backend.Any(extent => item >= extent.Item1 && item <= extent.Item2);
2019-11-03 01:41:48 +00:00
/// <summary>Removes all extents from this instance</summary>
public void Clear() => backend.Clear();
2019-11-03 01:41:48 +00:00
/// <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(short item)
{
Tuple<short, short> toRemove = null;
Tuple<short, short> toAddOne = null;
Tuple<short, short> toAddTwo = null;
foreach(Tuple<short, short> extent in backend)
{
// Extent is contained and not a border
2019-11-03 01:41:48 +00:00
if(item > extent.Item1 &&
item < extent.Item2)
{
toRemove = extent;
toAddOne = new Tuple<short, short>(extent.Item1, (short)(item - 1));
toAddTwo = new Tuple<short, short>((short)(item + 1), extent.Item2);
2019-11-03 01:41:48 +00:00
break;
}
// Extent is left border, but not only element
2019-11-03 01:41:48 +00:00
if(item == extent.Item1 &&
item != extent.Item2)
{
toRemove = extent;
toAddOne = new Tuple<short, short>((short)(item + 1), extent.Item2);
2019-11-03 01:41:48 +00:00
break;
}
// Extent is right border, but not only element
2019-11-03 01:41:48 +00:00
if(item != extent.Item1 &&
item == extent.Item2)
{
toRemove = extent;
toAddOne = new Tuple<short, short>(extent.Item1, (short)(item - 1));
2019-11-03 01:41:48 +00:00
break;
}
// Extent is only element
2019-11-03 01:41:48 +00:00
if(item != extent.Item1 ||
item != extent.Item2)
continue;
toRemove = extent;
2019-11-03 01:41:48 +00:00
break;
}
// Item not found
2019-11-03 01:41:48 +00:00
if(toRemove == null)
return false;
backend.Remove(toRemove);
2019-11-03 01:41:48 +00:00
if(toAddOne != null)
backend.Add(toAddOne);
if(toAddTwo != null)
backend.Add(toAddTwo);
// Sort
backend = backend.OrderBy(t => t.Item1).ToList();
return true;
}
/// <summary>
2019-11-03 01:41:48 +00:00
/// 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>
2018-12-31 13:17:27 +00:00
public Tuple<short, short>[] ToArray() => backend.ToArray();
2019-11-03 01:41:48 +00:00
/// <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(short item, out short start)
{
start = 0;
2019-11-03 01:41:48 +00:00
foreach(Tuple<short, short> extent in backend.Where(extent => item >= extent.Item1 && item <= extent.Item2))
{
start = extent.Item1;
2019-11-03 01:41:48 +00:00
return true;
}
return false;
}
}
}