Updates to Utilities

This commit is contained in:
Tal Aloni 2016-12-27 10:08:22 +02:00
parent 6cb61ca63e
commit 5508c749ce
13 changed files with 417 additions and 13 deletions

View file

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;
namespace Utilities
{
public class BlockingQueue<T>
{
private Queue<T> m_queue = new Queue<T>();
private int m_count = 0;
private bool m_stopping;
public void Enqueue(T item)
{
lock (m_queue)
{
m_queue.Enqueue(item);
m_count++;
if (m_queue.Count == 1)
{
Monitor.Pulse(m_queue);
}
}
}
public void Enqueue(List<T> items)
{
if (items.Count == 0)
{
return;
}
lock (m_queue)
{
foreach (T item in items)
{
m_queue.Enqueue(item);
m_count++;
}
if (m_queue.Count == items.Count)
{
Monitor.Pulse(m_queue);
}
}
}
/// <returns>Will return false if the BlockingQueue is stopped</returns>
public bool TryDequeue(out T item)
{
lock (m_queue)
{
while (m_queue.Count == 0)
{
Monitor.Wait(m_queue);
if (m_stopping)
{
item = default(T);
return false;
}
}
item = m_queue.Dequeue();
m_count--;
return true;
}
}
public void Stop()
{
lock (m_queue)
{
m_stopping = true;
Monitor.PulseAll(m_queue);
}
}
public int Count
{
get
{
return m_count;
}
}
}
}

View file

@ -7,10 +7,10 @@ namespace Utilities
{
public bool ContainsKey(TKey key)
{
return (this.IndexOf(key) != -1);
return (this.IndexOfKey(key) != -1);
}
public int IndexOf(TKey key)
public int IndexOfKey(TKey key)
{
for (int index = 0; index < this.Count; index++)
{

View file

@ -0,0 +1,163 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace Utilities
{
public class SortedList<T> : ICollection<T>
{
private List<T> m_innerList;
private Comparer<T> m_comparer;
public SortedList() : this(Comparer<T>.Default)
{
}
public SortedList(Comparer<T> comparer)
{
m_innerList = new List<T>();
m_comparer = comparer;
}
public void Add(T item)
{
int insertIndex = FindIndexForSortedInsert(m_innerList, m_comparer, item);
m_innerList.Insert(insertIndex, item);
}
public bool Contains(T item)
{
return IndexOf(item) != -1;
}
/// <summary>
/// Searches for the specified object and returns the zero-based index of the first occurrence within the entire SortedList<T>
/// </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;
}
public bool Remove(T item)
{
int index = IndexOf(item);
if (index >= 0)
{
m_innerList.RemoveAt(index);
return true;
}
return false;
}
public void RemoveAt(int index)
{
m_innerList.RemoveAt(index);
}
public void CopyTo(T[] array)
{
m_innerList.CopyTo(array);
}
public void CopyTo(T[] array, int arrayIndex)
{
m_innerList.CopyTo(array, arrayIndex);
}
public void Clear()
{
m_innerList.Clear();
}
public T this[int index]
{
get
{
return m_innerList[index];
}
}
public IEnumerator<T> GetEnumerator()
{
return m_innerList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return m_innerList.GetEnumerator();
}
public int Count
{
get
{
return m_innerList.Count;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public static int FindIndexForSortedInsert(List<T> list, Comparer<T> comparer, T item)
{
if (list.Count == 0)
{
return 0;
}
int lowerIndex = 0;
int upperIndex = list.Count - 1;
int comparisonResult;
while (lowerIndex < upperIndex)
{
int middleIndex = (lowerIndex + upperIndex) / 2;
T middle = list[middleIndex];
comparisonResult = comparer.Compare(middle, item);
if (comparisonResult == 0)
{
return middleIndex;
}
else if (comparisonResult > 0) // middle > item
{
upperIndex = middleIndex - 1;
}
else // middle < item
{
lowerIndex = middleIndex + 1;
}
}
// At this point any entry following 'middle' is greater than 'item',
// and any entry preceding 'middle' is lesser than 'item'.
// So we either put 'item' before or after 'middle'.
comparisonResult = comparer.Compare(list[lowerIndex], item);
if (comparisonResult < 0) // middle < item
{
return lowerIndex + 1;
}
else
{
return lowerIndex;
}
}
}
}