Updated Utilities

This commit is contained in:
Tal Aloni 2018-10-31 22:51:41 +02:00
parent 4e850aa38a
commit ff368056c5
6 changed files with 85 additions and 6 deletions

View file

@ -134,10 +134,22 @@ namespace Utilities
} }
} }
public static void WriteUTF8String(Stream stream, string value)
{
byte[] bytes = UnicodeEncoding.UTF8.GetBytes(value);
stream.Write(bytes, 0, bytes.Length);
}
public static void WriteUTF16String(Stream stream, string value) public static void WriteUTF16String(Stream stream, string value)
{ {
byte[] bytes = UnicodeEncoding.Unicode.GetBytes(value); byte[] bytes = UnicodeEncoding.Unicode.GetBytes(value);
stream.Write(bytes, 0, bytes.Length); stream.Write(bytes, 0, bytes.Length);
} }
public static void WriteUTF16BEString(Stream stream, string value)
{
byte[] bytes = UnicodeEncoding.BigEndianUnicode.GetBytes(value);
stream.Write(bytes, 0, bytes.Length);
}
} }
} }

View file

@ -6,6 +6,14 @@ namespace Utilities
{ {
public partial class KeyValuePairList<TKey, TValue> : List<KeyValuePair<TKey, TValue>> public partial class KeyValuePairList<TKey, TValue> : List<KeyValuePair<TKey, TValue>>
{ {
public KeyValuePairList()
{
}
public KeyValuePairList(List<KeyValuePair<TKey, TValue>> collection) : base(collection)
{
}
public bool ContainsKey(TKey key) public bool ContainsKey(TKey key)
{ {
return (this.IndexOfKey(key) != -1); return (this.IndexOfKey(key) != -1);
@ -97,5 +105,10 @@ namespace Utilities
return comparer.Compare(a.Key, b.Key); return comparer.Compare(a.Key, b.Key);
}); });
} }
public new KeyValuePairList<TKey, TValue> GetRange(int index, int count)
{
return new KeyValuePairList<TKey, TValue>(base.GetRange(index, count));
}
} }
} }

View file

@ -35,6 +35,36 @@ namespace Utilities
return m_reverse.ContainsKey(value); return m_reverse.ContainsKey(value);
} }
public bool TryGetKey(T2 value, out T1 key)
{
return m_reverse.TryGetValue(value, out key);
}
public bool TryGetValue(T1 key, out T2 value)
{
return m_forward.TryGetValue(key, out value);
}
public void RemoveKey(T1 key)
{
T2 value;
if (m_forward.TryGetValue(key, out value))
{
m_forward.Remove(key);
m_reverse.Remove(value);
}
}
public void RemoveValue(T2 value)
{
T1 key;
if (m_reverse.TryGetValue(value, out key))
{
m_forward.Remove(key);
m_reverse.Remove(value);
}
}
public T2 this[T1 key] public T2 this[T1 key]
{ {
get get

View file

@ -105,15 +105,25 @@ namespace Utilities
public static int FirstIndexOf(List<T> list, Comparer<T> comparer, T item) public static int FirstIndexOf(List<T> list, Comparer<T> comparer, T item)
{ {
int insertIndex = FindIndexForSortedInsert(list, comparer, item); return FirstIndexOf(list, comparer.Compare, item);
}
public static int FindIndexForSortedInsert(List<T> list, Comparer<T> comparer, T item)
{
return FindIndexForSortedInsert(list, comparer.Compare, item);
}
public static int FirstIndexOf(List<T> list, Comparison<T> compare, T item)
{
int insertIndex = FindIndexForSortedInsert(list, compare, item);
if (insertIndex == list.Count) if (insertIndex == list.Count)
{ {
return -1; return -1;
} }
if (comparer.Compare(item, list[insertIndex]) == 0) if (compare(item, list[insertIndex]) == 0)
{ {
int index = insertIndex; int index = insertIndex;
while (index > 0 && comparer.Compare(item, list[index - 1]) == 0) while (index > 0 && compare(item, list[index - 1]) == 0)
{ {
index--; index--;
} }
@ -122,7 +132,7 @@ namespace Utilities
return -1; return -1;
} }
public static int FindIndexForSortedInsert(List<T> list, Comparer<T> comparer, T item) public static int FindIndexForSortedInsert(List<T> list, Comparison<T> compare, T item)
{ {
if (list.Count == 0) if (list.Count == 0)
{ {
@ -136,7 +146,7 @@ namespace Utilities
{ {
int middleIndex = (lowerIndex + upperIndex) / 2; int middleIndex = (lowerIndex + upperIndex) / 2;
T middle = list[middleIndex]; T middle = list[middleIndex];
comparisonResult = comparer.Compare(middle, item); comparisonResult = compare(middle, item);
if (comparisonResult == 0) if (comparisonResult == 0)
{ {
return middleIndex; return middleIndex;
@ -154,7 +164,7 @@ namespace Utilities
// At this point any entry following 'middle' is greater than 'item', // At this point any entry following 'middle' is greater than 'item',
// and any entry preceding 'middle' is lesser than 'item'. // and any entry preceding 'middle' is lesser than 'item'.
// So we either put 'item' before or after 'middle'. // So we either put 'item' before or after 'middle'.
comparisonResult = comparer.Compare(list[lowerIndex], item); comparisonResult = compare(list[lowerIndex], item);
if (comparisonResult < 0) // middle < item if (comparisonResult < 0) // middle < item
{ {
return lowerIndex + 1; return lowerIndex + 1;

View file

@ -74,6 +74,11 @@ namespace Utilities
get; get;
} }
public abstract bool SupportsNamedStreams
{
get;
}
public static string GetParentDirectory(string path) public static string GetParentDirectory(string path)
{ {
if (path == String.Empty) if (path == String.Empty)

View file

@ -78,5 +78,14 @@ namespace Utilities
{ {
get; get;
} }
/// <summary>
/// Indicates support for opening named streams (alternate data streams).
/// Named streams are opened using the filename:stream syntax.
/// </summary>
bool SupportsNamedStreams
{
get;
}
} }
} }