2017-06-08 18:41:41 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : ExtentsSByte.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Extent helpers.
|
2017-06-08 18:41:41 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Provides extents for sbyte types.
|
2017-06-08 18:41:41 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2017-06-08 18:41:41 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 19:33:46 +00:00
|
|
|
|
|
2017-06-08 18:41:41 +01:00
|
|
|
|
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>>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-22 16:35:31 +00:00
|
|
|
|
public ExtentsSByte(IEnumerable<Tuple<sbyte, sbyte>> list)
|
2017-06-08 19:05:35 +01:00
|
|
|
|
{
|
|
|
|
|
|
backend = list.OrderBy(t => t.Item1).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-22 16:35:31 +00:00
|
|
|
|
public int Count => backend.Count;
|
2017-06-08 18:41:41 +01:00
|
|
|
|
|
|
|
|
|
|
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
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(item >= backend[i].Item1 && item <= backend[i].Item2) return;
|
2017-06-08 18:41:41 +01:00
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
else itemToAdd = new Tuple<sbyte, sbyte>(item, backend[i].Item2);
|
2017-06-08 18:41:41 +01:00
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Expands existing extent end
|
2017-12-21 06:06:19 +00:00
|
|
|
|
if(item != backend[i].Item2 + 1) continue;
|
2017-06-08 18:41:41 +01:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
removeOne = backend[i];
|
2017-06-08 18:41:41 +01:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
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);
|
2017-06-08 18:41:41 +01:00
|
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
|
else itemToAdd = new Tuple<sbyte, sbyte>(backend[i].Item1, item);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
2017-06-08 18:41:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(itemToAdd != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
backend.Remove(removeOne);
|
|
|
|
|
|
backend.Remove(removeTwo);
|
|
|
|
|
|
backend.Add(itemToAdd);
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
else backend.Add(new Tuple<sbyte, sbyte>(item, item));
|
2017-06-08 18:41:41 +01:00
|
|
|
|
|
|
|
|
|
|
// Sort
|
|
|
|
|
|
backend = backend.OrderBy(t => t.Item1).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-22 16:35:31 +00:00
|
|
|
|
public void Add(sbyte start, sbyte end, bool run = false)
|
2017-06-08 18:41:41 +01:00
|
|
|
|
{
|
|
|
|
|
|
sbyte realEnd;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(run) realEnd = (sbyte)(start + end - 1);
|
|
|
|
|
|
else realEnd = end;
|
2017-06-08 18:41:41 +01:00
|
|
|
|
|
|
|
|
|
|
// TODO: Optimize this
|
2017-12-19 20:33:03 +00:00
|
|
|
|
for(sbyte t = start; t <= realEnd; t++) Add(t);
|
2017-06-08 18:41:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool Contains(sbyte item)
|
|
|
|
|
|
{
|
2017-12-21 07:08:26 +00:00
|
|
|
|
return backend.Any(extent => item >= extent.Item1 && item <= extent.Item2);
|
2017-06-08 18:41:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2017-12-21 06:06:19 +00:00
|
|
|
|
if(item != extent.Item1 || item != extent.Item2) continue;
|
|
|
|
|
|
|
|
|
|
|
|
toRemove = extent;
|
|
|
|
|
|
break;
|
2017-06-08 18:41:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Item not found
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(toRemove == null) return false;
|
2017-06-08 18:41:41 +01:00
|
|
|
|
|
|
|
|
|
|
backend.Remove(toRemove);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(toAddOne != null) backend.Add(toAddOne);
|
|
|
|
|
|
if(toAddTwo != null) backend.Add(toAddTwo);
|
2017-06-08 18:41:41 +01:00
|
|
|
|
|
|
|
|
|
|
// Sort
|
|
|
|
|
|
backend = backend.OrderBy(t => t.Item1).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Tuple<sbyte, sbyte>[] ToArray()
|
|
|
|
|
|
{
|
|
|
|
|
|
return backend.ToArray();
|
|
|
|
|
|
}
|
2017-09-21 18:31:17 +01:00
|
|
|
|
|
|
|
|
|
|
public bool GetStart(sbyte item, out sbyte start)
|
|
|
|
|
|
{
|
|
|
|
|
|
start = 0;
|
2017-12-21 07:08:26 +00:00
|
|
|
|
foreach(Tuple<sbyte, sbyte> extent in backend.Where(extent => item >= extent.Item1 && item <= extent.Item2)) {
|
|
|
|
|
|
start = extent.Item1;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-09-21 18:31:17 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-06-08 18:41:41 +01:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|