Removed comparators in favor of IComparable implementation

This commit is contained in:
2019-06-20 02:19:05 +02:00
parent acb90e703f
commit e374d38d4e
13 changed files with 124 additions and 669 deletions
+11 -12
View File
@@ -13,7 +13,7 @@ namespace SystemExtensions.Collections
/// Read on https://simpledevcode.wordpress.com/2014/09/16/avl-tree-in-c/
/// </summary>
/// <typeparam name="T">Provided type.</typeparam>
public class AVLTree<T> : ICollection<T>
public class AVLTree<T> : ICollection<T> where T : IComparable<T>
{
#region Fields
private class AVLNode<T>
@@ -27,7 +27,6 @@ namespace SystemExtensions.Collections
}
}
AVLNode<T> root;
private Comparison<T> comparator;
private int count = 0;
private bool isReadOnly = false;
#endregion
@@ -51,9 +50,9 @@ namespace SystemExtensions.Collections
/// Initializes a new instance of an AVLTree collection.
/// </summary>
/// <param name="comparator"></param>
public AVLTree(Comparison<T> comparator)
public AVLTree()
{
this.comparator = comparator;
}
#endregion
#region Public Methods
@@ -86,7 +85,7 @@ namespace SystemExtensions.Collections
{
return false;
}
if (comparator(node.Value, value) == 0)
if (node.Value.CompareTo(value) == 0)
{
return true;
}
@@ -181,12 +180,12 @@ namespace SystemExtensions.Collections
current = n;
return current;
}
else if (comparator.Invoke(n.Value, current.Value) < 0)
else if (n.Value.CompareTo(current.Value) < 0)
{
current.Left = RecursiveInsertion(current.Left, n);
current = BalanceTree(current);
}
else if (comparator.Invoke(n.Value, current.Value) > 0)
else if (n.Value.CompareTo(current.Value) > 0)
{
current.Right = RecursiveInsertion(current.Right, n);
current = BalanceTree(current);
@@ -228,7 +227,7 @@ namespace SystemExtensions.Collections
else
{
//left subtree
if (comparator.Invoke(target, current.Value) < 0)
if (target.CompareTo(current.Value) < 0)
{
current.Left = Delete(current.Left, target);
if (BalanceFactor(current) == -2)//here
@@ -244,7 +243,7 @@ namespace SystemExtensions.Collections
}
}
//right subtree
else if (comparator.Invoke(target, current.Value) > 0)
else if (target.CompareTo(current.Value) > 0)
{
current.Right = Delete(current.Right, target);
if (BalanceFactor(current) == 2)
@@ -296,9 +295,9 @@ namespace SystemExtensions.Collections
{
return null;
}
if (comparator.Invoke(target, current.Value) < 0)
if (target.CompareTo(current.Value) < 0)
{
if (comparator.Invoke(target, current.Value) == 0)
if (target.CompareTo(current.Value) == 0)
{
return current;
}
@@ -307,7 +306,7 @@ namespace SystemExtensions.Collections
}
else
{
if (comparator.Invoke(target, current.Value) == 0)
if (target.CompareTo(current.Value) == 0)
{
return current;
}
+6 -9
View File
@@ -11,11 +11,10 @@ namespace SystemExtensions.Collections
/// Binary heap implementation.
/// </summary>
/// <typeparam name="T">Provided type.</typeparam>
public class BinaryHeap<T> : IEnumerable<T>
public class BinaryHeap<T> : IEnumerable<T> where T : IComparable<T>
{
#region Fields
T[] items;
Comparison<T> comparator;
int capacity, count, initialCapacity;
#endregion
#region Properties
@@ -62,24 +61,22 @@ namespace SystemExtensions.Collections
/// Constructor for a binary heap data structure.
/// </summary>
/// <param name="comparator">Function used to compare the elements.</param>
public BinaryHeap(Comparison<T> comparator)
public BinaryHeap()
{
capacity = 10;
initialCapacity = capacity;
items = new T[capacity];
this.comparator = comparator;
}
/// <summary>
/// Constructor for a binary heap data structure.
/// </summary>
/// <param name="comparator">Function used to compare the elements.</param>
/// <param name="capacity">Initial capacity of the heap. Used for initial alocation of the array.</param>
public BinaryHeap(Comparison<T> comparator, int capacity)
public BinaryHeap(int capacity)
{
this.capacity = capacity;
initialCapacity = capacity;
items = new T[capacity];
this.comparator = comparator;
}
#endregion
#region Public Methods
@@ -94,7 +91,7 @@ namespace SystemExtensions.Collections
Capacity = 2 * Capacity;
}
int position = ++count;
for (; position > 1 && comparator.Invoke(value, items[position / 2]) < 0; position = position / 2)
for (; position > 1 && value.CompareTo(items[position / 2]) < 0; position = position / 2)
{
items[position] = items[position / 2];
}
@@ -175,11 +172,11 @@ namespace SystemExtensions.Collections
for(; 2*index <= count; index = childIndex)
{
childIndex = 2 * index;
if(childIndex != Count && comparator.Invoke(items[childIndex], items[childIndex + 1]) > 0)
if(childIndex != Count && items[childIndex].CompareTo(items[childIndex + 1]) > 0)
{
childIndex++;
}
if(comparator.Invoke(temp, items[childIndex]) > 0)
if(temp.CompareTo(items[childIndex]) > 0)
{
items[index] = items[childIndex];
}
+10 -11
View File
@@ -11,11 +11,10 @@ namespace SystemExtensions.Collections
/// Fibonacci Heap implementation.
/// </summary>
/// <typeparam name="T">Provided type</typeparam>
public class FibonacciHeap<T> : IEnumerable<T>
public class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
{
#region Fields
private FibonacciNode<T> root;
private Comparison<T> comparator;
private int count;
#endregion
#region Properties
@@ -45,9 +44,9 @@ namespace SystemExtensions.Collections
/// Constructor for Fibonacci heap data structure.
/// </summary>
/// <param name="comparator">Function used to compare the elements.</param>
public FibonacciHeap(Comparison<T> comparator)
public FibonacciHeap()
{
this.comparator = comparator;
}
#endregion
#region Public Methods
@@ -238,7 +237,7 @@ namespace SystemExtensions.Collections
{
return node1;
}
if(comparator(node1.Value, node2.Value) > 0)
if(node1.Value.CompareTo(node2.Value) > 0)
{
FibonacciNode<T> temp = node1;
node1 = node2;
@@ -316,7 +315,7 @@ namespace SystemExtensions.Collections
break;
}
trees[node.Degree] = null;
if(comparator(node.Value, t.Value) < 0)
if(node.Value.CompareTo(t.Value) < 0)
{
t.Previous.Next = t.Next;
t.Next.Previous = t.Previous;
@@ -354,7 +353,7 @@ namespace SystemExtensions.Collections
FibonacciNode<T> start = node;
do
{
if(comparator(node.Value, min.Value) < 0)
if(node.Value.CompareTo(min.Value) < 0)
{
min = node;
}
@@ -393,14 +392,14 @@ namespace SystemExtensions.Collections
/// <returns></returns>
private FibonacciNode<T> DecreaseKey(FibonacciNode<T> root, FibonacciNode<T> node, T value)
{
if(comparator(node.Value, value) < 0)
if(node.Value.CompareTo(value) < 0)
{
return root;
}
node.Value = value;
if(node.Parent != null)
{
if(comparator(node.Value, node.Parent.Value) < 0)
if(node.Value.CompareTo(node.Parent.Value) < 0)
{
root = Cut(root, node);
FibonacciNode<T> parent = node.Parent;
@@ -420,7 +419,7 @@ namespace SystemExtensions.Collections
}
else
{
if(comparator(node.Value, root.Value) < 0)
if(node.Value.CompareTo(root.Value) < 0)
{
root = node;
}
@@ -442,7 +441,7 @@ namespace SystemExtensions.Collections
}
do
{
if(comparator(node.Value, value) == 0)
if(node.Value.CompareTo(value) == 0)
{
return node;
}
@@ -12,7 +12,7 @@ namespace SystemExtensions.Collections
/// Exposes some of the functionality of the Binary Heap as a queue.
/// </summary>
/// <typeparam name="T">Provided type.</typeparam>
public class PriorityQueue<T> : IQueue<T>
public class PriorityQueue<T> : IQueue<T> where T : IComparable<T>
{
#region Fields
private BinaryHeap<T> binaryHeap;
@@ -34,9 +34,9 @@ namespace SystemExtensions.Collections
/// Constructor for priority queue data structure.
/// </summary>
/// <param name="comparator">Function used to compare the elements.</param>
public PriorityQueue(Comparison<T> comparator)
public PriorityQueue()
{
binaryHeap = new BinaryHeap<T>(comparator);
binaryHeap = new BinaryHeap<T>();
}
#endregion
#region Public Methods
+7 -9
View File
@@ -11,7 +11,7 @@ namespace SystemExtensions.Collections
/// Treap implementation.
/// </summary>
/// <typeparam name="T">Provided type.</typeparam>
public class Treap<T> : ICollection<T>
public class Treap<T> : ICollection<T> where T : IComparable<T>
{
#region Fields
private class Item<T>
@@ -29,7 +29,6 @@ namespace SystemExtensions.Collections
}
private Random randomGen;
private Item<T> root;
private Comparison<T> comparator;
private int count;
#endregion
#region Properties
@@ -51,10 +50,9 @@ namespace SystemExtensions.Collections
/// Constructor for treap.
/// </summary>
/// <param name="comparator">Comparator method used to compare values.</param>
public Treap(Comparison<T> comparator)
public Treap()
{
randomGen = new Random();
this.comparator = comparator;
}
#endregion
#region Public Methods
@@ -138,7 +136,7 @@ namespace SystemExtensions.Collections
node = new Item<T>(key, randomGen.Next(0, 100));
return node;
}
else if (comparator(key, node.Key) <= 0)
else if (key.CompareTo(node.Key) <= 0)
{
node.Left = InsertNode(node.Left, key);
if(node.Left.Priority > node.Priority)
@@ -162,11 +160,11 @@ namespace SystemExtensions.Collections
{
return node;
}
if(comparator.Invoke(key, node.Key) < 0)
if(key.CompareTo(node.Key) < 0)
{
node.Left = RemoveNode(node.Left, key);
}
else if(comparator.Invoke(key, node.Key) > 0)
else if(key.CompareTo(node.Key) > 0)
{
node.Right = RemoveNode(node.Right, key);
}
@@ -224,7 +222,7 @@ namespace SystemExtensions.Collections
}
else
{
if(comparator.Invoke(node.Key, key) < 0)
if(node.Key.CompareTo(key) < 0)
{
Item<T> found = Find(node.Left, key);
if(found == null)
@@ -233,7 +231,7 @@ namespace SystemExtensions.Collections
}
return found;
}
else if(comparator.Invoke(node.Key, key) > 0)
else if(node.Key.CompareTo(key) > 0)
{
Item<T> found = Find(node.Right, key);
if (found == null)
-470
View File
@@ -1,470 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemExtensions
{
/// <summary>
/// Static class containing implementation of comparators to be used when creating and using collections.
/// </summary>
public static class Comparators
{
#region Fields
private static Comparison<int> intComparison = new Comparison<int>(IntegerComparison);
private static Comparison<float> floatComparison = new Comparison<float>(FloatComparison);
private static Comparison<double> doubleComparison = new Comparison<double>(DoubleComparison);
private static Comparison<bool> boolComparison = new Comparison<bool>(BoolComparison);
private static Comparison<decimal> decimalComparison = new Comparison<decimal>(DecimalComparison);
private static Comparison<byte> byteComparison = new Comparison<byte>(ByteComparison);
private static Comparison<long> longComparison = new Comparison<long>(LongComparison);
private static Comparison<uint> uintComparison = new Comparison<uint>(UintComparison);
private static Comparison<Int16> int16Comparison = new Comparison<Int16>(Int16Comparison);
private static Comparison<Int32> int32Comparison = new Comparison<Int32>(Int32Comparison);
private static Comparison<Int64> int64Comparison = new Comparison<Int64>(int64Comparison);
private static Comparison<UInt16> uInt16Comparison = new Comparison<UInt16>(uInt16Comparison);
private static Comparison<UInt32> uInt32Comparison = new Comparison<UInt32>(uInt32Comparison);
private static Comparison<UInt64> uInt64Comparison = new Comparison<UInt64>(uInt64Comparison);
#endregion
#region Properties
/// <summary>
/// Comparison between two ints.
/// </summary>
public static Comparison<int> Int
{
get
{
return intComparison;
}
}
/// <summary>
/// Comparison between two floats.
/// </summary>
public static Comparison<float> Float
{
get
{
return floatComparison;
}
}
/// <summary>
/// Comparison between two doubles.
/// </summary>
public static Comparison<double> Double
{
get
{
return doubleComparison;
}
}
/// <summary>
/// Comparison between two decimals.
/// </summary>
public static Comparison<decimal> Decimal
{
get
{
return decimalComparison;
}
}
/// <summary>
/// Comparison between two bools.
/// </summary>
public static Comparison<bool> Bool
{
get
{
return boolComparison;
}
}
/// <summary>
/// Comparison between two bytes.
/// </summary>
public static Comparison<byte> Byte
{
get
{
return byteComparison;
}
}
/// <summary>
/// Comparison between two longs.
/// </summary>
public static Comparison<long> Long
{
get
{
return longComparison;
}
}
/// <summary>
/// Comparison between two uints.
/// </summary>
public static Comparison<uint> Uint
{
get
{
return uintComparison;
}
}
/// <summary>
/// Comparison between two int16.
/// </summary>
public static Comparison<Int16> Int16
{
get
{
return int16Comparison;
}
}
/// <summary>
/// Comparison between two int32.
/// </summary>
public static Comparison<Int32> Int32
{
get
{
return int32Comparison;
}
}
/// <summary>
/// Comparison between two int64.
/// </summary>
public static Comparison<Int64> Int64
{
get
{
return int64Comparison;
}
}
/// <summary>
/// Comparison between two uint16.
/// </summary>
public static Comparison<UInt16> UInt16
{
get
{
return uInt16Comparison;
}
}
/// <summary>
/// Comparison between two uint32.
/// </summary>
public static Comparison<UInt32> UInt32
{
get
{
return uInt32Comparison;
}
}
/// <summary>
/// Comparison between two uint64.
/// </summary>
public static Comparison<UInt64> UInt64
{
get
{
return uInt64Comparison;
}
}
#endregion
#region Private Methods
/// <summary>
/// Compares two UInt64.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int Uint64Comparison(UInt64 x, UInt64 y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two UInt32.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int Uint32Comparison(UInt32 x, UInt32 y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two UInt16.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int Uint16Comparison(UInt16 x, UInt16 y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two Int64.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int Int64Comparison(Int64 x, Int64 y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two UInt32.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int Int32Comparison(Int32 x, Int32 y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two UInt16.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int Int16Comparison(Int16 x, Int16 y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two uint.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int UintComparison(uint x, uint y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two Long.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int LongComparison(long x, long y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two Byte.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int ByteComparison(byte x, byte y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two Decimal.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int DecimalComparison(decimal x, decimal y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two Bool.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x is true and y is false. 0 if they are the same and -1
/// if y is true and x is false.</returns>
private static int BoolComparison(bool x, bool y)
{
if (x == y)
{
return 0;
}
else if (x == true && y == false)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two Double.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int DoubleComparison(double x, double y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two Float.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int FloatComparison(float x, float y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
/// <summary>
/// Compares two Integer.
/// </summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>Returns 1 if x > y. 0 if equal and -1 if y > x.</returns>
private static int IntegerComparison(int x, int y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
#endregion
}
}
-1
View File
@@ -51,7 +51,6 @@
<Compile Include="Collections\IQueue.cs" />
<Compile Include="Collections\PriorityQueue.cs" />
<Compile Include="Collections\Treap.cs" />
<Compile Include="Comparators.cs" />
<Compile Include="ItemNotFoundException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Threading\PriorityThreadPool.cs" />
@@ -15,6 +15,72 @@ namespace SystemExtensions.Threading
/// </summary>
public class PriorityThreadPool : IDisposable
{
private class QueueEntry : IComparable<QueueEntry>
{
public TaskPriority TaskPriority;
public WaitCallback WaitCallback;
public Object Object;
public QueueEntry(TaskPriority priority, WaitCallback callback, object obj)
{
this.TaskPriority = priority;
this.WaitCallback = callback;
this.Object = obj;
}
public int CompareTo(QueueEntry other)
{
int priority1 = 0, priority2 = 0;
switch (this.TaskPriority)
{
case TaskPriority.Highest:
priority1 = 4;
break;
case TaskPriority.AboveNormal:
priority1 = 3;
break;
case TaskPriority.Normal:
priority1 = 2;
break;
case TaskPriority.BelowNormal:
priority1 = 1;
break;
case TaskPriority.Lowest:
priority1 = 0;
break;
}
switch (other.TaskPriority)
{
case TaskPriority.Highest:
priority2 = 4;
break;
case TaskPriority.AboveNormal:
priority2 = 3;
break;
case TaskPriority.Normal:
priority2 = 2;
break;
case TaskPriority.BelowNormal:
priority2 = 1;
break;
case TaskPriority.Lowest:
priority2 = 0;
break;
}
if (priority1 == priority2)
{
return 0;
}
else if (priority1 > priority2)
{
return -1;
}
else
{
return 1;
}
}
}
#region Enum
/// <summary>
/// Enum for priority of task scheduled
@@ -48,7 +114,7 @@ namespace SystemExtensions.Threading
/// List that contains current running threads and their status.
/// </summary>
private volatile List<WorkerThread> threadpool;
private volatile PriorityQueue<Tuple<TaskPriority, WaitCallback, object>> tasks;
private volatile PriorityQueue<QueueEntry> tasks;
private Thread observer;
private int maxThreads;
private object tasksLock = new object();
@@ -109,7 +175,7 @@ namespace SystemExtensions.Threading
public PriorityThreadPool()
{
threadpool = new List<WorkerThread>();
tasks = new PriorityQueue<Tuple<TaskPriority, WaitCallback, object>>(PriorityCompare);
tasks = new PriorityQueue<QueueEntry>();
maxThreads = System.Environment.ProcessorCount;
for (int i = 0; i < maxThreads; i++)
{
@@ -138,7 +204,7 @@ namespace SystemExtensions.Threading
public PriorityThreadPool(int maxThreads)
{
threadpool = new List<WorkerThread>();
tasks = new PriorityQueue<Tuple<TaskPriority, WaitCallback, object>>(PriorityCompare);
tasks = new PriorityQueue<QueueEntry>();
this.maxThreads = Math.Max(maxThreads, 1);
for (int i = 0; i < maxThreads; i++)
{
@@ -172,7 +238,7 @@ namespace SystemExtensions.Threading
public PriorityThreadPool(int lowest, int belowNormal, int normal, int aboveNormal, int highest)
{
threadpool = new List<WorkerThread>();
tasks = new PriorityQueue<Tuple<TaskPriority, WaitCallback, object>>(PriorityCompare);
tasks = new PriorityQueue<QueueEntry>();
for(int i = 0; i < lowest; i++)
{
WorkerThread worker = new WorkerThread();
@@ -250,7 +316,7 @@ namespace SystemExtensions.Threading
public void QueueUserWorkItem(WaitCallback waitCallback, object callbackState, TaskPriority taskPriority)
{
while (!Monitor.TryEnter(tasksLock));
tasks.Enqueue(new Tuple<TaskPriority, WaitCallback, object>(taskPriority, waitCallback, callbackState));
tasks.Enqueue(new QueueEntry(taskPriority, waitCallback, callbackState));
Monitor.Exit(tasksLock);
}
/// <summary>
@@ -265,64 +331,6 @@ namespace SystemExtensions.Threading
#endregion
#region Private Methods
/// <summary>
/// Function that compares two tasks based on their priorities
/// </summary>
/// <param name="tp1">First tuple to compare</param>
/// <param name="tp2">Second tuple to compare</param>
/// <returns>1 if tp1 is bigger. 0 if equal and -1 if tp1 is lower.</returns>
private static int PriorityCompare(Tuple<TaskPriority, WaitCallback, object> tp1, Tuple<TaskPriority, WaitCallback, object> tp2)
{
int priority1 = 0, priority2 = 0;
switch (tp1.Item1)
{
case TaskPriority.Highest:
priority1 = 4;
break;
case TaskPriority.AboveNormal:
priority1 = 3;
break;
case TaskPriority.Normal:
priority1 = 2;
break;
case TaskPriority.BelowNormal:
priority1 = 1;
break;
case TaskPriority.Lowest:
priority1 = 0;
break;
}
switch (tp2.Item1)
{
case TaskPriority.Highest:
priority2 = 4;
break;
case TaskPriority.AboveNormal:
priority2 = 3;
break;
case TaskPriority.Normal:
priority2 = 2;
break;
case TaskPriority.BelowNormal:
priority2 = 1;
break;
case TaskPriority.Lowest:
priority2 = 0;
break;
}
if (priority1 == priority2)
{
return 0;
}
else if(priority1 > priority2)
{
return -1;
}
else
{
return 1;
}
}
/// <summary>
/// Main loop that a thread from the pool is running.
/// </summary>
/// <threadId>Id of thread</threadId>
@@ -340,7 +348,7 @@ namespace SystemExtensions.Threading
//Finally, release the lock and invoke the task.
thisWorkerThread.working = true;
Tuple<TaskPriority, WaitCallback, object> task = null;
QueueEntry task = null;
while (!Monitor.TryEnter(tasksLock)) ;
if (tasks.Count > 0)
{
@@ -350,8 +358,8 @@ namespace SystemExtensions.Threading
if (task != null)
{
System.Diagnostics.Debug.WriteLine(Thread.CurrentThread.Name + " - Running task!");
WaitCallback waitCallback = task.Item2;
waitCallback.Invoke(task.Item3);
WaitCallback waitCallback = task.WaitCallback;
waitCallback.Invoke(task.Object);
}
thisWorkerThread.working = false;
}
@@ -11,26 +11,11 @@ namespace SystemExtensions.Collections.Tests
[TestClass()]
public class AVLTreeTests
{
private static int IntegerComparison(int x, int y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
private AVLTree<int> avlTree = new AVLTree<int>(new Comparison<int>(IntegerComparison));
private AVLTree<int> avlTree = new AVLTree<int>();
[TestMethod()]
public void AVLTreeTest()
{
avlTree = new AVLTree<int>(IntegerComparison);
avlTree = new AVLTree<int>();
}
[TestMethod()]
@@ -12,32 +12,17 @@ namespace SystemExtensions.Collections.Tests
[TestClass()]
public class BinaryHeapTests
{
private static int IntegerComparison(int x, int y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
private BinaryHeap<int> binaryHeap = new BinaryHeap<int>(new Comparison<int>(IntegerComparison));
private BinaryHeap<int> binaryHeap = new BinaryHeap<int>();
[TestMethod()]
public void BinaryHeapTest()
{
binaryHeap = new BinaryHeap<int>(new Comparison<int>(IntegerComparison));
binaryHeap = new BinaryHeap<int>();
}
[TestMethod()]
public void BinaryHeapTest1()
{
binaryHeap = new BinaryHeap<int>(new Comparison<int>(IntegerComparison), 100);
binaryHeap = new BinaryHeap<int>(100);
if (binaryHeap.Count != 0 && binaryHeap.Capacity != 100)
{
Assert.Fail();
@@ -12,22 +12,7 @@ namespace SystemExtensions.Collections.Tests
[TestClass()]
public class FibonacciHeapTests
{
private static int IntegerComparison(int x, int y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
FibonacciHeap<int> fibonacciHeap = new FibonacciHeap<int>(new Comparison<int>(IntegerComparison));
FibonacciHeap<int> fibonacciHeap = new FibonacciHeap<int>();
[TestMethod()]
public void InsertTest()
{
@@ -40,7 +25,7 @@ namespace SystemExtensions.Collections.Tests
[TestMethod()]
public void FibonacciHeapTest()
{
fibonacciHeap = new FibonacciHeap<int>(new Comparison<int>(IntegerComparison));
fibonacciHeap = new FibonacciHeap<int>();
if(fibonacciHeap.Count != 0)
{
Assert.Fail();
@@ -50,8 +35,8 @@ namespace SystemExtensions.Collections.Tests
[TestMethod()]
public void MergeTest()
{
FibonacciHeap<int> fibonacciHeap1 = new FibonacciHeap<int>(new Comparison<int>(IntegerComparison));
FibonacciHeap<int> fibonacciHeap2 = new FibonacciHeap<int>(new Comparison<int>(IntegerComparison));
FibonacciHeap<int> fibonacciHeap1 = new FibonacciHeap<int>();
FibonacciHeap<int> fibonacciHeap2 = new FibonacciHeap<int>();
for(int i = 0; i < 100; i++)
{
@@ -12,28 +12,13 @@ namespace SystemExtensions.Collections.Tests
[TestClass()]
public class PriorityQueueTests
{
private static int IntegerComparison(int x, int y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
private PriorityQueue<int> priorityQueue = new PriorityQueue<int>(new Comparison<int>(IntegerComparison));
private PriorityQueue<int> priorityQueue = new PriorityQueue<int>();
[TestMethod()]
public void PriorityQueueTest()
{
try
{
priorityQueue = new PriorityQueue<int>(new Comparison<int>(IntegerComparison));
priorityQueue = new PriorityQueue<int>();
if(priorityQueue.Count > 0)
{
Assert.Fail();
@@ -11,26 +11,11 @@ namespace SystemExtensions.Collections.Tests
[TestClass()]
public class TreapTests
{
private static int IntegerComparison(int x, int y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
Treap<int> treap = new Treap<int>(new Comparison<int>(IntegerComparison));
Treap<int> treap = new Treap<int>();
[TestMethod()]
public void TreapTest()
{
treap = new Treap<int>(new Comparison<int>(IntegerComparison));
treap = new Treap<int>();
}
[TestMethod()]