mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
Added helpers for extents.
This commit is contained in:
@@ -45,6 +45,14 @@
|
||||
<Compile Include="ArrayIsEmpty.cs" />
|
||||
<Compile Include="BigEndianMarshal.cs" />
|
||||
<Compile Include="CompareBytes.cs" />
|
||||
<Compile Include="Extents\ExtentsLong.cs" />
|
||||
<Compile Include="Extents\ExtentsULong.cs" />
|
||||
<Compile Include="Extents\ExtentsInt.cs" />
|
||||
<Compile Include="Extents\ExtentsUInt.cs" />
|
||||
<Compile Include="Extents\ExtentsShort.cs" />
|
||||
<Compile Include="Extents\ExtentsUShort.cs" />
|
||||
<Compile Include="Extents\ExtentsByte.cs" />
|
||||
<Compile Include="Extents\ExtentsSByte.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
@@ -58,6 +66,9 @@
|
||||
<Link>LICENSE.LGPL</Link>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Extents\" />
|
||||
</ItemGroup>
|
||||
<ProjectExtensions>
|
||||
<MonoDevelop>
|
||||
<Properties>
|
||||
@@ -70,7 +81,7 @@
|
||||
<inheritsSet />
|
||||
<inheritsScope />
|
||||
</TextStylePolicy>
|
||||
<CSharpFormattingPolicy IndentBlock="True" IndentBraces="False" IndentSwitchSection="True" IndentSwitchCaseSection="True" LabelPositioning="OneLess" NewLinesForBracesInTypes="True" NewLinesForBracesInMethods="True" NewLinesForBracesInProperties="True" NewLinesForBracesInAccessors="True" NewLinesForBracesInAnonymousMethods="True" NewLinesForBracesInControlBlocks="True" NewLinesForBracesInAnonymousTypes="True" NewLinesForBracesInObjectCollectionArrayInitializers="True" NewLinesForBracesInLambdaExpressionBody="True" NewLineForElse="True" NewLineForCatch="True" NewLineForFinally="True" SpacingAfterMethodDeclarationName="False" SpaceWithinMethodDeclarationParenthesis="False" SpaceBetweenEmptyMethodDeclarationParentheses="False" SpaceAfterMethodCallName="False" SpaceWithinMethodCallParentheses="False" SpaceBetweenEmptyMethodCallParentheses="False" SpaceAfterControlFlowStatementKeyword="False" SpaceWithinExpressionParentheses="False" SpaceWithinCastParentheses="False" SpaceWithinOtherParentheses="False" SpaceAfterCast="False" SpacesIgnoreAroundVariableDeclaration="False" SpaceBeforeOpenSquareBracket="False" SpaceBetweenEmptySquareBrackets="False" SpaceWithinSquareBrackets="False" SpaceAfterColonInBaseTypeDeclaration="True" SpaceAfterComma="True" SpaceAfterDot="False" SpaceAfterSemicolonsInForStatement="True" SpaceBeforeColonInBaseTypeDeclaration="True" SpaceBeforeComma="False" SpaceBeforeDot="False" SpaceBeforeSemicolonsInForStatement="False" SpacingAroundBinaryOperator="Single" WrappingPreserveSingleLine="True" WrappingKeepStatementsOnSingleLine="True" PlaceSystemDirectiveFirst="True" NewLineForMembersInObjectInit="True" NewLineForMembersInAnonymousTypes="True" NewLineForClausesInQuery="True" scope="text/x-csharp">
|
||||
<CSharpFormattingPolicy IndentBlock="True" IndentBraces="False" IndentSwitchSection="True" IndentSwitchCaseSection="True" LabelPositioning="OneLess" NewLinesForBracesInTypes="True" NewLinesForBracesInMethods="True" NewLinesForBracesInProperties="True" NewLinesForBracesInAccessors="True" NewLinesForBracesInAnonymousMethods="True" NewLinesForBracesInControlBlocks="True" NewLinesForBracesInAnonymousTypes="True" NewLinesForBracesInObjectCollectionArrayInitializers="True" NewLinesForBracesInLambdaExpressionBody="True" NewLineForElse="True" NewLineForCatch="True" NewLineForFinally="True" NewLineForMembersInObjectInit="True" NewLineForMembersInAnonymousTypes="True" NewLineForClausesInQuery="True" SpacingAfterMethodDeclarationName="False" SpaceWithinMethodDeclarationParenthesis="False" SpaceBetweenEmptyMethodDeclarationParentheses="False" SpaceAfterMethodCallName="False" SpaceWithinMethodCallParentheses="False" SpaceBetweenEmptyMethodCallParentheses="False" SpaceAfterControlFlowStatementKeyword="False" SpaceWithinExpressionParentheses="False" SpaceWithinCastParentheses="False" SpaceWithinOtherParentheses="False" SpaceAfterCast="False" SpacesIgnoreAroundVariableDeclaration="False" SpaceBeforeOpenSquareBracket="False" SpaceBetweenEmptySquareBrackets="False" SpaceWithinSquareBrackets="False" SpaceAfterColonInBaseTypeDeclaration="True" SpaceAfterComma="True" SpaceAfterDot="False" SpaceAfterSemicolonsInForStatement="True" SpaceBeforeColonInBaseTypeDeclaration="True" SpaceBeforeComma="False" SpaceBeforeDot="False" SpaceBeforeSemicolonsInForStatement="False" SpacingAroundBinaryOperator="Single" WrappingPreserveSingleLine="True" WrappingKeepStatementsOnSingleLine="True" PlaceSystemDirectiveFirst="True" scope="text/x-csharp">
|
||||
<inheritsSet />
|
||||
<inheritsScope />
|
||||
</CSharpFormattingPolicy>
|
||||
|
||||
200
DiscImageChef.Helpers/Extents/ExtentsByte.cs
Normal file
200
DiscImageChef.Helpers/Extents/ExtentsByte.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
// /***************************************************************************
|
||||
// The Disc Image Chef
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : ExtentsByte.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Component : Component
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Description
|
||||
//
|
||||
// --[ License ] --------------------------------------------------------------
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as
|
||||
// published by the Free Software Foundation; either version 2.1 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2017 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Extents
|
||||
{
|
||||
public class ExtentsByte
|
||||
{
|
||||
List<Tuple<byte, byte>> backend;
|
||||
|
||||
public ExtentsByte()
|
||||
{
|
||||
backend = new List<Tuple<byte, byte>>();
|
||||
}
|
||||
|
||||
public int Count { get { return backend.Count; } }
|
||||
|
||||
public void Add(byte item)
|
||||
{
|
||||
Tuple<byte, byte> removeOne = null;
|
||||
Tuple<byte, byte> removeTwo = null;
|
||||
Tuple<byte, byte> itemToAdd = null;
|
||||
|
||||
for(int i = 0; i < backend.Count; i++)
|
||||
{
|
||||
// Already contained in an extent
|
||||
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)
|
||||
{
|
||||
removeTwo = backend[i - 1];
|
||||
itemToAdd = new Tuple<byte, byte>(backend[i - 1].Item1, backend[i].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<byte, byte>(item, backend[i].Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Expands existing extent end
|
||||
if(item == backend[i].Item2 + 1)
|
||||
{
|
||||
removeOne = backend[i];
|
||||
|
||||
if(i < backend.Count - 1 && item == backend[i + 1].Item1 - 1)
|
||||
{
|
||||
removeTwo = backend[i + 1];
|
||||
itemToAdd = new Tuple<byte, byte>(backend[i].Item1, backend[i + 1].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<byte, byte>(backend[i].Item1, item);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(itemToAdd != null)
|
||||
{
|
||||
backend.Remove(removeOne);
|
||||
backend.Remove(removeTwo);
|
||||
backend.Add(itemToAdd);
|
||||
}
|
||||
else
|
||||
backend.Add(new Tuple<byte, byte>(item, item));
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
|
||||
public void Add(byte start, byte end)
|
||||
{
|
||||
Add(start, end, false);
|
||||
}
|
||||
|
||||
public void Add(byte start, byte end, bool run)
|
||||
{
|
||||
byte realEnd;
|
||||
if(run)
|
||||
realEnd = (byte)(start + end - 1);
|
||||
else
|
||||
realEnd = end;
|
||||
|
||||
// TODO: Optimize this
|
||||
for(byte t = start; t <= realEnd; t++)
|
||||
Add(t);
|
||||
}
|
||||
|
||||
public bool Contains(byte item)
|
||||
{
|
||||
foreach(Tuple<byte, byte> extent in backend)
|
||||
if(item >= extent.Item1 && item <= extent.Item2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
backend.Clear();
|
||||
}
|
||||
|
||||
public bool Remove(byte item)
|
||||
{
|
||||
Tuple<byte, byte> toRemove = null;
|
||||
Tuple<byte, byte> toAddOne = null;
|
||||
Tuple<byte, byte> toAddTwo = null;
|
||||
|
||||
foreach(Tuple<byte, byte> extent in backend)
|
||||
{
|
||||
// Extent is contained and not a border
|
||||
if(item > extent.Item1 && item < extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<byte, byte>(extent.Item1, (byte)(item - 1));
|
||||
toAddTwo = new Tuple<byte, byte>((byte)(item + 1), extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is left border, but not only element
|
||||
if(item == extent.Item1 && item != extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<byte, byte>((byte)(item + 1), extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is right border, but not only element
|
||||
if(item != extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<byte, byte>(extent.Item1, (byte)(item - 1));
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is only element
|
||||
if(item == extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Item not found
|
||||
if(toRemove == null)
|
||||
return false;
|
||||
|
||||
backend.Remove(toRemove);
|
||||
if(toAddOne != null)
|
||||
backend.Add(toAddOne);
|
||||
if(toAddTwo != null)
|
||||
backend.Add(toAddTwo);
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Tuple<byte, byte>[] ToArray()
|
||||
{
|
||||
return backend.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
200
DiscImageChef.Helpers/Extents/ExtentsInt.cs
Normal file
200
DiscImageChef.Helpers/Extents/ExtentsInt.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
// /***************************************************************************
|
||||
// The Disc Image Chef
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : ExtentsInt.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Component : Component
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Description
|
||||
//
|
||||
// --[ License ] --------------------------------------------------------------
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as
|
||||
// published by the Free Software Foundation; either version 2.1 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License aint with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2017 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Extents
|
||||
{
|
||||
public class ExtentsInt
|
||||
{
|
||||
List<Tuple<int, int>> backend;
|
||||
|
||||
public ExtentsInt()
|
||||
{
|
||||
backend = new List<Tuple<int, int>>();
|
||||
}
|
||||
|
||||
public int Count { get { return backend.Count; } }
|
||||
|
||||
public void Add(int item)
|
||||
{
|
||||
Tuple<int, int> removeOne = null;
|
||||
Tuple<int, int> removeTwo = null;
|
||||
Tuple<int, int> itemToAdd = null;
|
||||
|
||||
for(int i = 0; i < backend.Count; i++)
|
||||
{
|
||||
// Already contained in an extent
|
||||
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)
|
||||
{
|
||||
removeTwo = backend[i - 1];
|
||||
itemToAdd = new Tuple<int, int>(backend[i - 1].Item1, backend[i].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<int, int>(item, backend[i].Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Expands existing extent end
|
||||
if(item == backend[i].Item2 + 1)
|
||||
{
|
||||
removeOne = backend[i];
|
||||
|
||||
if(i < backend.Count - 1 && item == backend[i + 1].Item1 - 1)
|
||||
{
|
||||
removeTwo = backend[i + 1];
|
||||
itemToAdd = new Tuple<int, int>(backend[i].Item1, backend[i + 1].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<int, int>(backend[i].Item1, item);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(itemToAdd != null)
|
||||
{
|
||||
backend.Remove(removeOne);
|
||||
backend.Remove(removeTwo);
|
||||
backend.Add(itemToAdd);
|
||||
}
|
||||
else
|
||||
backend.Add(new Tuple<int, int>(item, item));
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
|
||||
public void Add(int start, int end)
|
||||
{
|
||||
Add(start, end, false);
|
||||
}
|
||||
|
||||
public void Add(int start, int end, bool run)
|
||||
{
|
||||
int realEnd;
|
||||
if(run)
|
||||
realEnd = start + end - 1;
|
||||
else
|
||||
realEnd = end;
|
||||
|
||||
// TODO: Optimize this
|
||||
for(int t = start; t <= realEnd; t++)
|
||||
Add(t);
|
||||
}
|
||||
|
||||
public bool Contains(int item)
|
||||
{
|
||||
foreach(Tuple<int, int> extent in backend)
|
||||
if(item >= extent.Item1 && item <= extent.Item2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
backend.Clear();
|
||||
}
|
||||
|
||||
public bool Remove(int item)
|
||||
{
|
||||
Tuple<int, int> toRemove = null;
|
||||
Tuple<int, int> toAddOne = null;
|
||||
Tuple<int, int> toAddTwo = null;
|
||||
|
||||
foreach(Tuple<int, int> extent in backend)
|
||||
{
|
||||
// Extent is contained and not a border
|
||||
if(item > extent.Item1 && item < extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<int, int>(extent.Item1, item - 1);
|
||||
toAddTwo = new Tuple<int, int>(item + 1, extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is left border, but not only element
|
||||
if(item == extent.Item1 && item != extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<int, int>(item + 1, extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is right border, but not only element
|
||||
if(item != extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<int, int>(extent.Item1, item - 1);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is only element
|
||||
if(item == extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Item not found
|
||||
if(toRemove == null)
|
||||
return false;
|
||||
|
||||
backend.Remove(toRemove);
|
||||
if(toAddOne != null)
|
||||
backend.Add(toAddOne);
|
||||
if(toAddTwo != null)
|
||||
backend.Add(toAddTwo);
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Tuple<int, int>[] ToArray()
|
||||
{
|
||||
return backend.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
200
DiscImageChef.Helpers/Extents/ExtentsLong.cs
Normal file
200
DiscImageChef.Helpers/Extents/ExtentsLong.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
// /***************************************************************************
|
||||
// The Disc Image Chef
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : ExtentsLong.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Component : Component
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Description
|
||||
//
|
||||
// --[ License ] --------------------------------------------------------------
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as
|
||||
// published by the Free Software Foundation; either version 2.1 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2017 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Extents
|
||||
{
|
||||
public class ExtentsLong
|
||||
{
|
||||
List<Tuple<long, long>> backend;
|
||||
|
||||
public ExtentsLong()
|
||||
{
|
||||
backend = new List<Tuple<long, long>>();
|
||||
}
|
||||
|
||||
public int Count { get { return backend.Count; } }
|
||||
|
||||
public void Add(long item)
|
||||
{
|
||||
Tuple<long, long> removeOne = null;
|
||||
Tuple<long, long> removeTwo = null;
|
||||
Tuple<long, long> itemToAdd = null;
|
||||
|
||||
for(int i = 0; i < backend.Count; i++)
|
||||
{
|
||||
// Already contained in an extent
|
||||
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)
|
||||
{
|
||||
removeTwo = backend[i - 1];
|
||||
itemToAdd = new Tuple<long, long>(backend[i - 1].Item1, backend[i].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<long, long>(item, backend[i].Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Expands existing extent end
|
||||
if(item == backend[i].Item2 + 1)
|
||||
{
|
||||
removeOne = backend[i];
|
||||
|
||||
if(i < backend.Count - 1 && item == backend[i + 1].Item1 - 1)
|
||||
{
|
||||
removeTwo = backend[i + 1];
|
||||
itemToAdd = new Tuple<long, long>(backend[i].Item1, backend[i + 1].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<long, long>(backend[i].Item1, item);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(itemToAdd != null)
|
||||
{
|
||||
backend.Remove(removeOne);
|
||||
backend.Remove(removeTwo);
|
||||
backend.Add(itemToAdd);
|
||||
}
|
||||
else
|
||||
backend.Add(new Tuple<long, long>(item, item));
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
|
||||
public void Add(long start, long end)
|
||||
{
|
||||
Add(start, end, false);
|
||||
}
|
||||
|
||||
public void Add(long start, long end, bool run)
|
||||
{
|
||||
long realEnd;
|
||||
if(run)
|
||||
realEnd = start + end - 1;
|
||||
else
|
||||
realEnd = end;
|
||||
|
||||
// TODO: Optimize this
|
||||
for(long t = start; t <= realEnd; t++)
|
||||
Add(t);
|
||||
}
|
||||
|
||||
public bool Contains(long item)
|
||||
{
|
||||
foreach(Tuple<long, long> extent in backend)
|
||||
if(item >= extent.Item1 && item <= extent.Item2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
backend.Clear();
|
||||
}
|
||||
|
||||
public bool Remove(long item)
|
||||
{
|
||||
Tuple<long, long> toRemove = null;
|
||||
Tuple<long, long> toAddOne = null;
|
||||
Tuple<long, long> toAddTwo = null;
|
||||
|
||||
foreach(Tuple<long, long> extent in backend)
|
||||
{
|
||||
// Extent is contained and not a border
|
||||
if(item > extent.Item1 && item < extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<long, long>(extent.Item1, item - 1);
|
||||
toAddTwo = new Tuple<long, long>(item + 1, extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is left border, but not only element
|
||||
if(item == extent.Item1 && item != extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<long, long>(item + 1, extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is right border, but not only element
|
||||
if(item != extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<long, long>(extent.Item1, item - 1);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is only element
|
||||
if(item == extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Item not found
|
||||
if(toRemove == null)
|
||||
return false;
|
||||
|
||||
backend.Remove(toRemove);
|
||||
if(toAddOne != null)
|
||||
backend.Add(toAddOne);
|
||||
if(toAddTwo != null)
|
||||
backend.Add(toAddTwo);
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Tuple<long, long>[] ToArray()
|
||||
{
|
||||
return backend.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
200
DiscImageChef.Helpers/Extents/ExtentsSByte.cs
Normal file
200
DiscImageChef.Helpers/Extents/ExtentsSByte.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
// /***************************************************************************
|
||||
// The Disc Image Chef
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : ExtentsSByte.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Component : Component
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Description
|
||||
//
|
||||
// --[ License ] --------------------------------------------------------------
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as
|
||||
// published by the Free Software Foundation; either version 2.1 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2017 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Extents
|
||||
{
|
||||
public class ExtentsSByte
|
||||
{
|
||||
List<Tuple<sbyte, sbyte>> backend;
|
||||
|
||||
public ExtentsSByte()
|
||||
{
|
||||
backend = new List<Tuple<sbyte, sbyte>>();
|
||||
}
|
||||
|
||||
public int Count { get { return backend.Count; } }
|
||||
|
||||
public void Add(sbyte item)
|
||||
{
|
||||
Tuple<sbyte, sbyte> removeOne = null;
|
||||
Tuple<sbyte, sbyte> removeTwo = null;
|
||||
Tuple<sbyte, sbyte> itemToAdd = null;
|
||||
|
||||
for(int i = 0; i < backend.Count; i++)
|
||||
{
|
||||
// Already contained in an extent
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Expands existing extent end
|
||||
if(item == backend[i].Item2 + 1)
|
||||
{
|
||||
removeOne = backend[i];
|
||||
|
||||
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);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(itemToAdd != null)
|
||||
{
|
||||
backend.Remove(removeOne);
|
||||
backend.Remove(removeTwo);
|
||||
backend.Add(itemToAdd);
|
||||
}
|
||||
else
|
||||
backend.Add(new Tuple<sbyte, sbyte>(item, item));
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
|
||||
public void Add(sbyte start, sbyte end)
|
||||
{
|
||||
Add(start, end, false);
|
||||
}
|
||||
|
||||
public void Add(sbyte start, sbyte end, bool run)
|
||||
{
|
||||
sbyte realEnd;
|
||||
if(run)
|
||||
realEnd = (sbyte)(start + end - 1);
|
||||
else
|
||||
realEnd = end;
|
||||
|
||||
// TODO: Optimize this
|
||||
for(sbyte t = start; t <= realEnd; t++)
|
||||
Add(t);
|
||||
}
|
||||
|
||||
public bool Contains(sbyte item)
|
||||
{
|
||||
foreach(Tuple<sbyte, sbyte> extent in backend)
|
||||
if(item >= extent.Item1 && item <= extent.Item2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
backend.Clear();
|
||||
}
|
||||
|
||||
public bool Remove(sbyte item)
|
||||
{
|
||||
Tuple<sbyte, sbyte> toRemove = null;
|
||||
Tuple<sbyte, sbyte> toAddOne = null;
|
||||
Tuple<sbyte, sbyte> toAddTwo = null;
|
||||
|
||||
foreach(Tuple<sbyte, sbyte> extent in backend)
|
||||
{
|
||||
// Extent is contained and not a border
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<sbyte, sbyte>(extent.Item1, (sbyte)(item - 1));
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is only element
|
||||
if(item == extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Item not found
|
||||
if(toRemove == null)
|
||||
return false;
|
||||
|
||||
backend.Remove(toRemove);
|
||||
if(toAddOne != null)
|
||||
backend.Add(toAddOne);
|
||||
if(toAddTwo != null)
|
||||
backend.Add(toAddTwo);
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Tuple<sbyte, sbyte>[] ToArray()
|
||||
{
|
||||
return backend.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
200
DiscImageChef.Helpers/Extents/ExtentsShort.cs
Normal file
200
DiscImageChef.Helpers/Extents/ExtentsShort.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
// /***************************************************************************
|
||||
// The Disc Image Chef
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : ExtentsShort.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Component : Component
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Description
|
||||
//
|
||||
// --[ License ] --------------------------------------------------------------
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as
|
||||
// published by the Free Software Foundation; either version 2.1 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License ashort with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2017 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Extents
|
||||
{
|
||||
public class ExtentsShort
|
||||
{
|
||||
List<Tuple<short, short>> backend;
|
||||
|
||||
public ExtentsShort()
|
||||
{
|
||||
backend = new List<Tuple<short, short>>();
|
||||
}
|
||||
|
||||
public int Count { get { return backend.Count; } }
|
||||
|
||||
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
|
||||
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)
|
||||
{
|
||||
removeTwo = backend[i - 1];
|
||||
itemToAdd = new Tuple<short, short>(backend[i - 1].Item1, backend[i].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<short, short>(item, backend[i].Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Expands existing extent end
|
||||
if(item == backend[i].Item2 + 1)
|
||||
{
|
||||
removeOne = backend[i];
|
||||
|
||||
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);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<short, short>(backend[i].Item1, item);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(itemToAdd != null)
|
||||
{
|
||||
backend.Remove(removeOne);
|
||||
backend.Remove(removeTwo);
|
||||
backend.Add(itemToAdd);
|
||||
}
|
||||
else
|
||||
backend.Add(new Tuple<short, short>(item, item));
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
|
||||
public void Add(short start, short end)
|
||||
{
|
||||
Add(start, end, false);
|
||||
}
|
||||
|
||||
public void Add(short start, short end, bool run)
|
||||
{
|
||||
short realEnd;
|
||||
if(run)
|
||||
realEnd = (short)(start + end - 1);
|
||||
else
|
||||
realEnd = end;
|
||||
|
||||
// TODO: Optimize this
|
||||
for(short t = start; t <= realEnd; t++)
|
||||
Add(t);
|
||||
}
|
||||
|
||||
public bool Contains(short item)
|
||||
{
|
||||
foreach(Tuple<short, short> extent in backend)
|
||||
if(item >= extent.Item1 && item <= extent.Item2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
backend.Clear();
|
||||
}
|
||||
|
||||
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
|
||||
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);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is left border, but not only element
|
||||
if(item == extent.Item1 && item != extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<short, short>((short)(item + 1), extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is right border, but not only element
|
||||
if(item != extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<short, short>(extent.Item1, (short)(item - 1));
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is only element
|
||||
if(item == extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Item not found
|
||||
if(toRemove == null)
|
||||
return false;
|
||||
|
||||
backend.Remove(toRemove);
|
||||
if(toAddOne != null)
|
||||
backend.Add(toAddOne);
|
||||
if(toAddTwo != null)
|
||||
backend.Add(toAddTwo);
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Tuple<short, short>[] ToArray()
|
||||
{
|
||||
return backend.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
200
DiscImageChef.Helpers/Extents/ExtentsUInt.cs
Normal file
200
DiscImageChef.Helpers/Extents/ExtentsUInt.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
// /***************************************************************************
|
||||
// The Disc Image Chef
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : ExtentsUInt.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Component : Component
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Description
|
||||
//
|
||||
// --[ License ] --------------------------------------------------------------
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as
|
||||
// published by the Free Software Foundation; either version 2.1 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License auint with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2017 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Extents
|
||||
{
|
||||
public class ExtentsUInt
|
||||
{
|
||||
List<Tuple<uint, uint>> backend;
|
||||
|
||||
public ExtentsUInt()
|
||||
{
|
||||
backend = new List<Tuple<uint, uint>>();
|
||||
}
|
||||
|
||||
public int Count { get { return backend.Count; } }
|
||||
|
||||
public void Add(uint item)
|
||||
{
|
||||
Tuple<uint, uint> removeOne = null;
|
||||
Tuple<uint, uint> removeTwo = null;
|
||||
Tuple<uint, uint> itemToAdd = null;
|
||||
|
||||
for(int i = 0; i < backend.Count; i++)
|
||||
{
|
||||
// Already contained in an extent
|
||||
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)
|
||||
{
|
||||
removeTwo = backend[i - 1];
|
||||
itemToAdd = new Tuple<uint, uint>(backend[i - 1].Item1, backend[i].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<uint, uint>(item, backend[i].Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Expands existing extent end
|
||||
if(item == backend[i].Item2 + 1)
|
||||
{
|
||||
removeOne = backend[i];
|
||||
|
||||
if(i < backend.Count - 1 && item == backend[i + 1].Item1 - 1)
|
||||
{
|
||||
removeTwo = backend[i + 1];
|
||||
itemToAdd = new Tuple<uint, uint>(backend[i].Item1, backend[i + 1].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<uint, uint>(backend[i].Item1, item);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(itemToAdd != null)
|
||||
{
|
||||
backend.Remove(removeOne);
|
||||
backend.Remove(removeTwo);
|
||||
backend.Add(itemToAdd);
|
||||
}
|
||||
else
|
||||
backend.Add(new Tuple<uint, uint>(item, item));
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
|
||||
public void Add(uint start, uint end)
|
||||
{
|
||||
Add(start, end, false);
|
||||
}
|
||||
|
||||
public void Add(uint start, uint end, bool run)
|
||||
{
|
||||
uint realEnd;
|
||||
if(run)
|
||||
realEnd = start + end - 1;
|
||||
else
|
||||
realEnd = end;
|
||||
|
||||
// TODO: Optimize this
|
||||
for(uint t = start; t <= realEnd; t++)
|
||||
Add(t);
|
||||
}
|
||||
|
||||
public bool Contains(uint item)
|
||||
{
|
||||
foreach(Tuple<uint, uint> extent in backend)
|
||||
if(item >= extent.Item1 && item <= extent.Item2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
backend.Clear();
|
||||
}
|
||||
|
||||
public bool Remove(uint item)
|
||||
{
|
||||
Tuple<uint, uint> toRemove = null;
|
||||
Tuple<uint, uint> toAddOne = null;
|
||||
Tuple<uint, uint> toAddTwo = null;
|
||||
|
||||
foreach(Tuple<uint, uint> extent in backend)
|
||||
{
|
||||
// Extent is contained and not a border
|
||||
if(item > extent.Item1 && item < extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<uint, uint>(extent.Item1, item - 1);
|
||||
toAddTwo = new Tuple<uint, uint>(item + 1, extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is left border, but not only element
|
||||
if(item == extent.Item1 && item != extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<uint, uint>(item + 1, extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is right border, but not only element
|
||||
if(item != extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<uint, uint>(extent.Item1, item - 1);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is only element
|
||||
if(item == extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Item not found
|
||||
if(toRemove == null)
|
||||
return false;
|
||||
|
||||
backend.Remove(toRemove);
|
||||
if(toAddOne != null)
|
||||
backend.Add(toAddOne);
|
||||
if(toAddTwo != null)
|
||||
backend.Add(toAddTwo);
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Tuple<uint, uint>[] ToArray()
|
||||
{
|
||||
return backend.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
200
DiscImageChef.Helpers/Extents/ExtentsULong.cs
Normal file
200
DiscImageChef.Helpers/Extents/ExtentsULong.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
// /***************************************************************************
|
||||
// The Disc Image Chef
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : ExtentsULong.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Component : Component
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Description
|
||||
//
|
||||
// --[ License ] --------------------------------------------------------------
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as
|
||||
// published by the Free Software Foundation; either version 2.1 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License aulong with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2017 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Extents
|
||||
{
|
||||
public class ExtentsULong
|
||||
{
|
||||
List<Tuple<ulong, ulong>> backend;
|
||||
|
||||
public ExtentsULong()
|
||||
{
|
||||
backend = new List<Tuple<ulong, ulong>>();
|
||||
}
|
||||
|
||||
public int Count { get { return backend.Count; } }
|
||||
|
||||
public void Add(ulong item)
|
||||
{
|
||||
Tuple<ulong, ulong> removeOne = null;
|
||||
Tuple<ulong, ulong> removeTwo = null;
|
||||
Tuple<ulong, ulong> itemToAdd = null;
|
||||
|
||||
for(int i = 0; i < backend.Count; i++)
|
||||
{
|
||||
// Already contained in an extent
|
||||
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)
|
||||
{
|
||||
removeTwo = backend[i - 1];
|
||||
itemToAdd = new Tuple<ulong, ulong>(backend[i - 1].Item1, backend[i].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<ulong, ulong>(item, backend[i].Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Expands existing extent end
|
||||
if(item == backend[i].Item2 + 1)
|
||||
{
|
||||
removeOne = backend[i];
|
||||
|
||||
if(i < backend.Count - 1 && item == backend[i + 1].Item1 - 1)
|
||||
{
|
||||
removeTwo = backend[i + 1];
|
||||
itemToAdd = new Tuple<ulong, ulong>(backend[i].Item1, backend[i + 1].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<ulong, ulong>(backend[i].Item1, item);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(itemToAdd != null)
|
||||
{
|
||||
backend.Remove(removeOne);
|
||||
backend.Remove(removeTwo);
|
||||
backend.Add(itemToAdd);
|
||||
}
|
||||
else
|
||||
backend.Add(new Tuple<ulong, ulong>(item, item));
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
|
||||
public void Add(ulong start, ulong end)
|
||||
{
|
||||
Add(start, end, false);
|
||||
}
|
||||
|
||||
public void Add(ulong start, ulong end, bool run)
|
||||
{
|
||||
ulong realEnd;
|
||||
if(run)
|
||||
realEnd = start + end - 1;
|
||||
else
|
||||
realEnd = end;
|
||||
|
||||
// TODO: Optimize this
|
||||
for(ulong t = start; t <= realEnd; t++)
|
||||
Add(t);
|
||||
}
|
||||
|
||||
public bool Contains(ulong item)
|
||||
{
|
||||
foreach(Tuple<ulong, ulong> extent in backend)
|
||||
if(item >= extent.Item1 && item <= extent.Item2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
backend.Clear();
|
||||
}
|
||||
|
||||
public bool Remove(ulong item)
|
||||
{
|
||||
Tuple<ulong, ulong> toRemove = null;
|
||||
Tuple<ulong, ulong> toAddOne = null;
|
||||
Tuple<ulong, ulong> toAddTwo = null;
|
||||
|
||||
foreach(Tuple<ulong, ulong> extent in backend)
|
||||
{
|
||||
// Extent is contained and not a border
|
||||
if(item > extent.Item1 && item < extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<ulong, ulong>(extent.Item1, item - 1);
|
||||
toAddTwo = new Tuple<ulong, ulong>(item + 1, extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is left border, but not only element
|
||||
if(item == extent.Item1 && item != extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<ulong, ulong>(item + 1, extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is right border, but not only element
|
||||
if(item != extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<ulong, ulong>(extent.Item1, item - 1);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is only element
|
||||
if(item == extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Item not found
|
||||
if(toRemove == null)
|
||||
return false;
|
||||
|
||||
backend.Remove(toRemove);
|
||||
if(toAddOne != null)
|
||||
backend.Add(toAddOne);
|
||||
if(toAddTwo != null)
|
||||
backend.Add(toAddTwo);
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Tuple<ulong, ulong>[] ToArray()
|
||||
{
|
||||
return backend.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
200
DiscImageChef.Helpers/Extents/ExtentsUShort.cs
Normal file
200
DiscImageChef.Helpers/Extents/ExtentsUShort.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
// /***************************************************************************
|
||||
// The Disc Image Chef
|
||||
// ----------------------------------------------------------------------------
|
||||
//
|
||||
// Filename : ExtentsUShort.cs
|
||||
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Component : Component
|
||||
//
|
||||
// --[ Description ] ----------------------------------------------------------
|
||||
//
|
||||
// Description
|
||||
//
|
||||
// --[ License ] --------------------------------------------------------------
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as
|
||||
// published by the Free Software Foundation; either version 2.1 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but
|
||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License aushort with this library; if not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
// ----------------------------------------------------------------------------
|
||||
// Copyright © 2011-2017 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Extents
|
||||
{
|
||||
public class ExtentsUShort
|
||||
{
|
||||
List<Tuple<ushort, ushort>> backend;
|
||||
|
||||
public ExtentsUShort()
|
||||
{
|
||||
backend = new List<Tuple<ushort, ushort>>();
|
||||
}
|
||||
|
||||
public int Count { get { return backend.Count; } }
|
||||
|
||||
public void Add(ushort item)
|
||||
{
|
||||
Tuple<ushort, ushort> removeOne = null;
|
||||
Tuple<ushort, ushort> removeTwo = null;
|
||||
Tuple<ushort, ushort> itemToAdd = null;
|
||||
|
||||
for(int i = 0; i < backend.Count; i++)
|
||||
{
|
||||
// Already contained in an extent
|
||||
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)
|
||||
{
|
||||
removeTwo = backend[i - 1];
|
||||
itemToAdd = new Tuple<ushort, ushort>(backend[i - 1].Item1, backend[i].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<ushort, ushort>(item, backend[i].Item2);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Expands existing extent end
|
||||
if(item == backend[i].Item2 + 1)
|
||||
{
|
||||
removeOne = backend[i];
|
||||
|
||||
if(i < backend.Count - 1 && item == backend[i + 1].Item1 - 1)
|
||||
{
|
||||
removeTwo = backend[i + 1];
|
||||
itemToAdd = new Tuple<ushort, ushort>(backend[i].Item1, backend[i + 1].Item2);
|
||||
}
|
||||
else
|
||||
itemToAdd = new Tuple<ushort, ushort>(backend[i].Item1, item);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(itemToAdd != null)
|
||||
{
|
||||
backend.Remove(removeOne);
|
||||
backend.Remove(removeTwo);
|
||||
backend.Add(itemToAdd);
|
||||
}
|
||||
else
|
||||
backend.Add(new Tuple<ushort, ushort>(item, item));
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
}
|
||||
|
||||
public void Add(ushort start, ushort end)
|
||||
{
|
||||
Add(start, end, false);
|
||||
}
|
||||
|
||||
public void Add(ushort start, ushort end, bool run)
|
||||
{
|
||||
ushort realEnd;
|
||||
if(run)
|
||||
realEnd = (ushort)(start + end - 1);
|
||||
else
|
||||
realEnd = end;
|
||||
|
||||
// TODO: Optimize this
|
||||
for(ushort t = start; t <= realEnd; t++)
|
||||
Add(t);
|
||||
}
|
||||
|
||||
public bool Contains(ushort item)
|
||||
{
|
||||
foreach(Tuple<ushort, ushort> extent in backend)
|
||||
if(item >= extent.Item1 && item <= extent.Item2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
backend.Clear();
|
||||
}
|
||||
|
||||
public bool Remove(ushort item)
|
||||
{
|
||||
Tuple<ushort, ushort> toRemove = null;
|
||||
Tuple<ushort, ushort> toAddOne = null;
|
||||
Tuple<ushort, ushort> toAddTwo = null;
|
||||
|
||||
foreach(Tuple<ushort, ushort> extent in backend)
|
||||
{
|
||||
// Extent is contained and not a border
|
||||
if(item > extent.Item1 && item < extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<ushort, ushort>(extent.Item1, (ushort)(item - 1));
|
||||
toAddTwo = new Tuple<ushort, ushort>((ushort)(item + 1), extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is left border, but not only element
|
||||
if(item == extent.Item1 && item != extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<ushort, ushort>((ushort)(item + 1), extent.Item2);
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is right border, but not only element
|
||||
if(item != extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
toAddOne = new Tuple<ushort, ushort>(extent.Item1, (ushort)(item - 1));
|
||||
break;
|
||||
}
|
||||
|
||||
// Extent is only element
|
||||
if(item == extent.Item1 && item == extent.Item2)
|
||||
{
|
||||
toRemove = extent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Item not found
|
||||
if(toRemove == null)
|
||||
return false;
|
||||
|
||||
backend.Remove(toRemove);
|
||||
if(toAddOne != null)
|
||||
backend.Add(toAddOne);
|
||||
if(toAddTwo != null)
|
||||
backend.Add(toAddTwo);
|
||||
|
||||
// Sort
|
||||
backend = backend.OrderBy(t => t.Item1).ToList();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Tuple<ushort, ushort>[] ToArray()
|
||||
{
|
||||
return backend.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user