mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-04-30 02:37:49 +02:00
Updated Utilities
This commit is contained in:
parent
0ee776be98
commit
c73761d36c
6 changed files with 289 additions and 21 deletions
21
Utilities/Comparers/ReverseComparer.cs
Normal file
21
Utilities/Comparers/ReverseComparer.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Utilities
|
||||
{
|
||||
public class ReverseComparer<T> : IComparer<T>
|
||||
{
|
||||
private IComparer<T> m_comparer;
|
||||
|
||||
public ReverseComparer(IComparer<T> comparer)
|
||||
{
|
||||
m_comparer = comparer;
|
||||
}
|
||||
|
||||
public int Compare(T x, T y)
|
||||
{
|
||||
return m_comparer.Compare(y, x);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,29 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
|
||||
namespace Utilities
|
||||
{
|
||||
public partial class Conversion
|
||||
{
|
||||
public static short ToInt16(object obj)
|
||||
{
|
||||
return ToInt16(obj, 0);
|
||||
}
|
||||
|
||||
public static short ToInt16(object obj, short defaultValue)
|
||||
{
|
||||
short result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToInt16(obj);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int ToInt32(object obj)
|
||||
{
|
||||
return ToInt32(obj, 0);
|
||||
|
@ -48,5 +63,200 @@ namespace Utilities
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ushort ToUInt16(object obj)
|
||||
{
|
||||
return ToUInt16(obj, 0);
|
||||
}
|
||||
|
||||
public static ushort ToUInt16(object obj, ushort defaultValue)
|
||||
{
|
||||
ushort result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToUInt16(obj);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static uint ToUInt32(object obj)
|
||||
{
|
||||
return ToUInt32(obj, 0);
|
||||
}
|
||||
|
||||
public static uint ToUInt32(object obj, uint defaultValue)
|
||||
{
|
||||
uint result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToUInt32(obj);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ulong ToUInt64(object obj)
|
||||
{
|
||||
return ToUInt64(obj, 0);
|
||||
}
|
||||
|
||||
public static ulong ToUInt64(object obj, ulong defaultValue)
|
||||
{
|
||||
ulong result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToUInt64(obj);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static float ToFloat(object obj)
|
||||
{
|
||||
return ToFloat(obj, 0);
|
||||
}
|
||||
|
||||
public static float ToFloat(object obj, float defaultValue)
|
||||
{
|
||||
float result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToSingle(obj);
|
||||
}
|
||||
catch
|
||||
{}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static double ToDouble(object obj)
|
||||
{
|
||||
return ToDouble(obj, 0);
|
||||
}
|
||||
|
||||
public static double ToDouble(object obj, double defaultValue)
|
||||
{
|
||||
double result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToDouble(obj);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static decimal ToDecimal(object obj)
|
||||
{
|
||||
return ToDecimal(obj, 0);
|
||||
}
|
||||
|
||||
public static decimal ToDecimal(object obj, decimal defaultValue)
|
||||
{
|
||||
decimal result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToDecimal(obj);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool ToBoolean(object obj)
|
||||
{
|
||||
return ToBoolean(obj, false);
|
||||
}
|
||||
|
||||
public static bool ToBoolean(object obj, bool defaultValue)
|
||||
{
|
||||
bool result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToBoolean(obj);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string ToString(object obj)
|
||||
{
|
||||
string result = String.Empty;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToString(obj);
|
||||
}
|
||||
catch
|
||||
{}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static char ToChar(object obj)
|
||||
{
|
||||
return ToChar(obj, new char());
|
||||
}
|
||||
|
||||
public static char ToChar(object obj, char defaultValue)
|
||||
{
|
||||
char result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToChar(obj);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static DateTime ToDateTime(object obj)
|
||||
{
|
||||
return ToDateTime(obj, DateTime.MinValue);
|
||||
}
|
||||
|
||||
public static DateTime ToDateTime(object obj, DateTime defaultValue)
|
||||
{
|
||||
DateTime result = defaultValue;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = Convert.ToDateTime(obj);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Utilities
|
||||
{
|
||||
|
@ -66,5 +67,35 @@ namespace Utilities
|
|||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
new public void Sort()
|
||||
{
|
||||
this.Sort(Comparer<TKey>.Default);
|
||||
}
|
||||
|
||||
public void Sort(ListSortDirection sortDirection)
|
||||
{
|
||||
Sort(Comparer<TKey>.Default, sortDirection);
|
||||
}
|
||||
|
||||
public void Sort(IComparer<TKey> comparer, ListSortDirection sortDirection)
|
||||
{
|
||||
if (sortDirection == ListSortDirection.Ascending)
|
||||
{
|
||||
Sort(comparer);
|
||||
}
|
||||
else
|
||||
{
|
||||
Sort(new ReverseComparer<TKey>(comparer));
|
||||
}
|
||||
}
|
||||
|
||||
public void Sort(IComparer<TKey> comparer)
|
||||
{
|
||||
this.Sort(delegate(KeyValuePair<TKey, TValue> a, KeyValuePair<TKey, TValue> b)
|
||||
{
|
||||
return comparer.Compare(a.Key, b.Key);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,21 +35,7 @@ namespace Utilities
|
|||
/// </summary>
|
||||
public int IndexOf(T item)
|
||||
{
|
||||
int insertIndex = FindIndexForSortedInsert(m_innerList, m_comparer, item);
|
||||
if (insertIndex == m_innerList.Count)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (m_comparer.Compare(item, m_innerList[insertIndex]) == 0)
|
||||
{
|
||||
int index = insertIndex;
|
||||
while (index > 0 && m_comparer.Compare(item, m_innerList[index - 1]) == 0)
|
||||
{
|
||||
index--;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
return -1;
|
||||
return FirstIndexOf(m_innerList, m_comparer, item);
|
||||
}
|
||||
|
||||
public bool Remove(T item)
|
||||
|
@ -117,6 +103,25 @@ namespace Utilities
|
|||
}
|
||||
}
|
||||
|
||||
public static int FirstIndexOf(List<T> list, Comparer<T> comparer, T item)
|
||||
{
|
||||
int insertIndex = FindIndexForSortedInsert(list, comparer, item);
|
||||
if (insertIndex == list.Count)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (comparer.Compare(item, list[insertIndex]) == 0)
|
||||
{
|
||||
int index = insertIndex;
|
||||
while (index > 0 && comparer.Compare(item, list[index - 1]) == 0)
|
||||
{
|
||||
index--;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int FindIndexForSortedInsert(List<T> list, Comparer<T> comparer, T item)
|
||||
{
|
||||
if (list.Count == 0)
|
||||
|
|
|
@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
|
|||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Tal Aloni")]
|
||||
[assembly: AssemblyProduct("Utilities")]
|
||||
[assembly: AssemblyCopyright("Copyright © Tal Aloni 2014")]
|
||||
[assembly: AssemblyCopyright("Copyright © Tal Aloni 2005-2016")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@
|
|||
<Compile Include="ByteUtils\ByteWriter.cs" />
|
||||
<Compile Include="ByteUtils\LittleEndianReader.cs" />
|
||||
<Compile Include="ByteUtils\LittleEndianWriter.cs" />
|
||||
<Compile Include="Comparers\ReverseComparer.cs" />
|
||||
<Compile Include="Conversion\BigEndianConverter.cs" />
|
||||
<Compile Include="Conversion\Conversion.SimpleTypes.cs" />
|
||||
<Compile Include="Conversion\LittleEndianConverter.cs" />
|
||||
|
|
Loading…
Add table
Reference in a new issue