Remove some unused extension methods and move some others to CollectionExtensions
This commit is contained in:
parent
a726fde352
commit
6269e957e6
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
|
||||
using BizHawk.Common;
|
||||
using BizHawk.Common.CollectionExtensions;
|
||||
using BizHawk.Emulation.Common;
|
||||
|
||||
namespace BizHawk.Client.Common
|
||||
|
|
|
@ -52,6 +52,7 @@
|
|||
<Compile Include="Buffer.cs" />
|
||||
<Compile Include="Colors.cs" />
|
||||
<Compile Include="CustomCollections.cs" />
|
||||
<Compile Include="Extensions\CollectionExtensions.cs" />
|
||||
<Compile Include="Extensions\Extensions.cs" />
|
||||
<Compile Include="Extensions\IOExtensions.cs" />
|
||||
<Compile Include="Extensions\NumberExtensions.cs" />
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace BizHawk.Common.CollectionExtensions
|
||||
{
|
||||
public static class CollectionExtensions
|
||||
{
|
||||
public static int LowerBoundBinarySearch<T, TKey>(this IList<T> list, Func<T, TKey> keySelector, TKey key) where TKey : IComparable<TKey>
|
||||
{
|
||||
int min = 0;
|
||||
int max = list.Count;
|
||||
int mid;
|
||||
TKey midKey;
|
||||
while (min < max)
|
||||
{
|
||||
mid = (max + min) / 2;
|
||||
T midItem = list[mid];
|
||||
midKey = keySelector(midItem);
|
||||
int comp = midKey.CompareTo(key);
|
||||
if (comp < 0)
|
||||
{
|
||||
min = mid + 1;
|
||||
}
|
||||
else if (comp > 0)
|
||||
{
|
||||
max = mid - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
|
||||
//did we find it exactly?
|
||||
if (min == max && keySelector(list[min]).CompareTo(key) == 0)
|
||||
{
|
||||
return min;
|
||||
}
|
||||
|
||||
mid = min;
|
||||
|
||||
//we didnt find it. return something corresponding to lower_bound semantics
|
||||
|
||||
if (mid == list.Count)
|
||||
{
|
||||
return max; // had to go all the way to max before giving up; lower bound is max
|
||||
}
|
||||
|
||||
if (mid == 0)
|
||||
{
|
||||
return -1; // had to go all the way to min before giving up; lower bound is min
|
||||
}
|
||||
|
||||
midKey = keySelector(list[mid]);
|
||||
if (midKey.CompareTo(key) >= 0)
|
||||
{
|
||||
return mid - 1;
|
||||
}
|
||||
|
||||
return mid;
|
||||
}
|
||||
|
||||
// http://stackoverflow.com/questions/1766328/can-linq-use-binary-search-when-the-collection-is-ordered
|
||||
public static T BinarySearch<T, TKey>(this IList<T> list, Func<T, TKey> keySelector, TKey key)
|
||||
where TKey : IComparable<TKey>
|
||||
{
|
||||
int min = 0;
|
||||
int max = list.Count;
|
||||
while (min < max)
|
||||
{
|
||||
int mid = (max + min) / 2;
|
||||
T midItem = list[mid];
|
||||
TKey midKey = keySelector(midItem);
|
||||
int comp = midKey.CompareTo(key);
|
||||
if (comp < 0)
|
||||
{
|
||||
min = mid + 1;
|
||||
}
|
||||
else if (comp > 0)
|
||||
{
|
||||
max = mid - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return midItem;
|
||||
}
|
||||
}
|
||||
if (min == max &&
|
||||
keySelector(list[min]).CompareTo(key) == 0)
|
||||
{
|
||||
return list[min];
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Item not found");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,24 +9,6 @@ namespace BizHawk.Common
|
|||
{
|
||||
public static class Extensions
|
||||
{
|
||||
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)
|
||||
{
|
||||
TValue ret;
|
||||
dict.TryGetValue(key, out ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue defaultvalue)
|
||||
{
|
||||
TValue ret;
|
||||
if (!dict.TryGetValue(key, out ret))
|
||||
{
|
||||
return defaultvalue;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static string GetDirectory(this Assembly asm)
|
||||
{
|
||||
var codeBase = asm.CodeBase;
|
||||
|
@ -36,126 +18,6 @@ namespace BizHawk.Common
|
|||
return Path.GetDirectoryName(path);
|
||||
}
|
||||
|
||||
public static IEnumerable<LinkedListNode<T>> EnumerateNodes<T>(this LinkedList<T> list)
|
||||
{
|
||||
var node = list.First;
|
||||
while (node != null)
|
||||
{
|
||||
yield return node;
|
||||
node = node.Next;
|
||||
}
|
||||
}
|
||||
|
||||
public static int LowerBoundBinarySearch<T, TKey>(this IList<T> list, Func<T, TKey> keySelector, TKey key) where TKey : IComparable<TKey>
|
||||
{
|
||||
int min = 0;
|
||||
int max = list.Count;
|
||||
int mid;
|
||||
TKey midKey;
|
||||
while (min < max)
|
||||
{
|
||||
mid = (max + min) / 2;
|
||||
T midItem = list[mid];
|
||||
midKey = keySelector(midItem);
|
||||
int comp = midKey.CompareTo(key);
|
||||
if (comp < 0)
|
||||
{
|
||||
min = mid + 1;
|
||||
}
|
||||
else if (comp > 0)
|
||||
{
|
||||
max = mid - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
|
||||
//did we find it exactly?
|
||||
if (min == max && keySelector(list[min]).CompareTo(key) == 0)
|
||||
{
|
||||
return min;
|
||||
}
|
||||
|
||||
mid = min;
|
||||
|
||||
//we didnt find it. return something corresponding to lower_bound semantics
|
||||
|
||||
if (mid == list.Count)
|
||||
{
|
||||
return max; // had to go all the way to max before giving up; lower bound is max
|
||||
}
|
||||
|
||||
if (mid == 0)
|
||||
{
|
||||
return -1; // had to go all the way to min before giving up; lower bound is min
|
||||
}
|
||||
|
||||
midKey = keySelector(list[mid]);
|
||||
if (midKey.CompareTo(key) >= 0)
|
||||
{
|
||||
return mid - 1;
|
||||
}
|
||||
|
||||
return mid;
|
||||
}
|
||||
|
||||
// http://stackoverflow.com/questions/1766328/can-linq-use-binary-search-when-the-collection-is-ordered
|
||||
public static T BinarySearch<T, TKey>(this IList<T> list, Func<T, TKey> keySelector, TKey key)
|
||||
where TKey : IComparable<TKey>
|
||||
{
|
||||
int min = 0;
|
||||
int max = list.Count;
|
||||
while (min < max)
|
||||
{
|
||||
int mid = (max + min) / 2;
|
||||
T midItem = list[mid];
|
||||
TKey midKey = keySelector(midItem);
|
||||
int comp = midKey.CompareTo(key);
|
||||
if (comp < 0)
|
||||
{
|
||||
min = mid + 1;
|
||||
}
|
||||
else if (comp > 0)
|
||||
{
|
||||
max = mid - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return midItem;
|
||||
}
|
||||
}
|
||||
if (min == max &&
|
||||
keySelector(list[min]).CompareTo(key) == 0)
|
||||
{
|
||||
return list[min];
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Item not found");
|
||||
}
|
||||
|
||||
public static bool ContainsStartsWith(this IEnumerable<string> options, string str)
|
||||
{
|
||||
return options.Any(opt => opt.StartsWith(str));
|
||||
}
|
||||
|
||||
public static string GetOptionValue(this IEnumerable<string> options, string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var opt in options)
|
||||
{
|
||||
if (opt.StartsWith(str))
|
||||
{
|
||||
return opt.Split('=')[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void SaveAsHex(this byte[] buffer, TextWriter writer)
|
||||
{
|
||||
foreach (var b in buffer)
|
||||
|
|
|
@ -27,7 +27,7 @@ using System;
|
|||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using BizHawk.Common;
|
||||
using BizHawk.Common.CollectionExtensions;
|
||||
using BizHawk.Common.NumberExtensions;
|
||||
|
||||
namespace BizHawk.Emulation.DiscSystem
|
||||
|
|
Loading…
Reference in New Issue