Cleaned code and upgraded framework to 4.7.2.

This commit is contained in:
2019-07-19 11:10:34 +03:00
parent f0a5d509ea
commit 2ebd821bbd
13 changed files with 101 additions and 135 deletions
+7 -10
View File
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemExtensions.Collections
{
@@ -36,7 +33,8 @@ namespace SystemExtensions.Collections
/// <summary>
/// Count of items currently stored in the tree.
/// </summary>
public int Count {
public int Count
{
get
{
return count;
@@ -51,7 +49,6 @@ namespace SystemExtensions.Collections
/// <summary>
/// Initializes a new instance of an AVLTree collection.
/// </summary>
/// <param name="comparator"></param>
public AVLTree()
{
@@ -83,7 +80,7 @@ namespace SystemExtensions.Collections
public bool Contains(T value)
{
AVLNode<T> node = Find(value, root);
if(node == null)
if (node == null)
{
return false;
}
@@ -113,16 +110,16 @@ namespace SystemExtensions.Collections
Queue<AVLNode<T>> queue = new Queue<AVLNode<T>>();
queue.Enqueue(root);
while(queue.Count > 0)
while (queue.Count > 0)
{
AVLNode<T> currentNode = queue.Dequeue();
if(currentNode.Left != null)
if (currentNode.Left != null)
{
queue.Enqueue(currentNode.Left);
currentNode.Left = null;
count--;
}
if(currentNode.Right != null)
if (currentNode.Right != null)
{
queue.Enqueue(currentNode.Right);
currentNode.Right = null;
@@ -293,7 +290,7 @@ namespace SystemExtensions.Collections
}
private AVLNode<T> Find(T target, AVLNode<T> current)
{
if(current == null)
if (current == null)
{
return null;
}
+10 -12
View File
@@ -2,8 +2,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemExtensions.Collections
{
@@ -42,10 +40,12 @@ namespace SystemExtensions.Collections
/// <summary>
/// Capacity of the heap.
/// </summary>
public int Capacity { get => capacity;
public int Capacity
{
get => capacity;
set
{
if(value > capacity)
if (value > capacity)
{
Array.Resize(ref items, value);
capacity = value;
@@ -61,7 +61,6 @@ namespace SystemExtensions.Collections
/// <summary>
/// Constructor for a binary heap data structure.
/// </summary>
/// <param name="comparator">Function used to compare the elements.</param>
public BinaryHeap()
{
capacity = 10;
@@ -71,7 +70,6 @@ namespace SystemExtensions.Collections
/// <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(int capacity)
{
@@ -87,7 +85,7 @@ namespace SystemExtensions.Collections
/// <param name="value">Value to be added.</param>
public void Add(T value)
{
if(count == Capacity - 1)
if (count == Capacity - 1)
{
Capacity = 2 * Capacity;
}
@@ -168,7 +166,7 @@ namespace SystemExtensions.Collections
/// <returns>Enumerator that iterates over the heap.</returns>
public IEnumerator<T> GetEnumerator()
{
for(int i = 0; i < count; i++)
for (int i = 0; i < count; i++)
{
yield return items[i + 1];
}
@@ -183,14 +181,14 @@ namespace SystemExtensions.Collections
{
T temp = items[index];
int childIndex = 0;
for(; 2*index <= count; index = childIndex)
for (; 2 * index <= count; index = childIndex)
{
childIndex = 2 * index;
if(childIndex != Count && items[childIndex].CompareTo(items[childIndex + 1]) > 0)
if (childIndex != Count && items[childIndex].CompareTo(items[childIndex + 1]) > 0)
{
childIndex++;
}
if(temp.CompareTo(items[childIndex]) > 0)
if (temp.CompareTo(items[childIndex]) > 0)
{
items[index] = items[childIndex];
}
@@ -206,7 +204,7 @@ namespace SystemExtensions.Collections
/// </summary>
private void BuildHeap()
{
for(int i = count / 2; i > 0; i--)
for (int i = count / 2; i > 0; i--)
{
BubbleDown(i);
}
+27 -31
View File
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemExtensions.Collections
{
@@ -44,7 +41,6 @@ namespace SystemExtensions.Collections
/// <summary>
/// Constructor for Fibonacci heap data structure.
/// </summary>
/// <param name="comparator">Function used to compare the elements.</param>
public FibonacciHeap()
{
@@ -131,7 +127,7 @@ namespace SystemExtensions.Collections
/// <returns>Array with values from the heap.</returns>
public T[] ToArray()
{
if(count == 0)
if (count == 0)
{
return null;
}
@@ -186,7 +182,7 @@ namespace SystemExtensions.Collections
{
Queue<FibonacciNode<T>> queue = new Queue<FibonacciNode<T>>();
queue.Enqueue(currentNode);
while(queue.Count > 0)
while (queue.Count > 0)
{
currentNode = queue.Dequeue();
FibonacciNode<T> oldNode = currentNode;
@@ -199,8 +195,8 @@ namespace SystemExtensions.Collections
}
currentNode = currentNode.Previous;
} while (currentNode != oldNode);
}
}
}
/// <summary>
/// Recursively remove the node and its children from the heap.
@@ -208,7 +204,7 @@ namespace SystemExtensions.Collections
/// <param name="node">Node to be removed.</param>
private void Remove(FibonacciNode<T> node)
{
if(node != null)
if (node != null)
{
FibonacciNode<T> current = node;
do
@@ -230,15 +226,15 @@ namespace SystemExtensions.Collections
/// <param name="node2">Root of second heap.</param>
private FibonacciNode<T> Merge(FibonacciNode<T> node1, FibonacciNode<T> node2)
{
if(node1 == null)
if (node1 == null)
{
return node2;
}
if(node2 == null)
if (node2 == null)
{
return node1;
}
if(node1.Value.CompareTo(node2.Value) > 0)
if (node1.Value.CompareTo(node2.Value) > 0)
{
FibonacciNode<T> temp = node1;
node1 = node2;
@@ -270,7 +266,7 @@ namespace SystemExtensions.Collections
/// <param name="node">Node to be removed from its parent.</param>
private void RemoveParent(FibonacciNode<T> node)
{
if(node == null)
if (node == null)
{
return;
}
@@ -290,7 +286,7 @@ namespace SystemExtensions.Collections
private FibonacciNode<T> RemoveMinimum(FibonacciNode<T> node)
{
RemoveParent(node.Child);
if(node.Next == node)
if (node.Next == node)
{
node = node.Child;
}
@@ -300,7 +296,7 @@ namespace SystemExtensions.Collections
node.Previous.Next = node.Next;
node = Merge(node.Next, node.Child);
}
if(node == null)
if (node == null)
{
return node;
}
@@ -308,15 +304,15 @@ namespace SystemExtensions.Collections
FibonacciNode<T>[] trees = new FibonacciNode<T>[64];
while (true)
{
if(trees[node.Degree] != null)
if (trees[node.Degree] != null)
{
FibonacciNode<T> t = trees[node.Degree];
if(t == node)
if (t == node)
{
break;
}
trees[node.Degree] = null;
if(node.Value.CompareTo(t.Value) < 0)
if (node.Value.CompareTo(t.Value) < 0)
{
t.Previous.Next = t.Next;
t.Next.Previous = t.Previous;
@@ -326,7 +322,7 @@ namespace SystemExtensions.Collections
{
t.Previous.Next = t.Next;
t.Next.Previous = t.Previous;
if(node.Next == node)
if (node.Next == node)
{
t.Next = t.Previous = t;
AddChild(t, node);
@@ -354,7 +350,7 @@ namespace SystemExtensions.Collections
FibonacciNode<T> start = node;
do
{
if(node.Value.CompareTo(min.Value) < 0)
if (node.Value.CompareTo(min.Value) < 0)
{
min = node;
}
@@ -370,7 +366,7 @@ namespace SystemExtensions.Collections
/// <returns></returns>
private FibonacciNode<T> Cut(FibonacciNode<T> root, FibonacciNode<T> node)
{
if(node.Next == node)
if (node.Next == node)
{
node.Parent.Child = null;
}
@@ -393,26 +389,26 @@ namespace SystemExtensions.Collections
/// <returns></returns>
private FibonacciNode<T> DecreaseKey(FibonacciNode<T> root, FibonacciNode<T> node, T value)
{
if(node.Value.CompareTo(value) < 0)
if (node.Value.CompareTo(value) < 0)
{
return root;
}
node.Value = value;
if(node.Parent != null)
if (node.Parent != null)
{
if(node.Value.CompareTo(node.Parent.Value) < 0)
if (node.Value.CompareTo(node.Parent.Value) < 0)
{
root = Cut(root, node);
FibonacciNode<T> parent = node.Parent;
node.Parent = null;
while(parent != null && parent.Marked)
while (parent != null && parent.Marked)
{
root = Cut(root, parent);
node = parent;
parent = node.Parent;
node.Parent = null;
}
if(parent != null && parent.Parent != null)
if (parent != null && parent.Parent != null)
{
parent.Marked = true;
}
@@ -420,7 +416,7 @@ namespace SystemExtensions.Collections
}
else
{
if(node.Value.CompareTo(root.Value) < 0)
if (node.Value.CompareTo(root.Value) < 0)
{
root = node;
}
@@ -436,18 +432,18 @@ namespace SystemExtensions.Collections
private FibonacciNode<T> Find(FibonacciNode<T> root, T value)
{
FibonacciNode<T> node = root;
if(node == null)
if (node == null)
{
return null;
}
do
{
if(node.Value.CompareTo(value) == 0)
if (node.Value.CompareTo(value) == 0)
{
return node;
}
FibonacciNode<T> ret = Find(node.Child, value);
if(ret != null)
if (ret != null)
{
return ret;
}
@@ -537,7 +533,7 @@ namespace SystemExtensions.Collections
}
set
{
this.value = value;
this.value = value;
}
}
public int Degree
+1 -5
View File
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace SystemExtensions.Collections
{
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemExtensions.Collections
{
@@ -34,7 +31,6 @@ namespace SystemExtensions.Collections
/// <summary>
/// Constructor for priority queue data structure.
/// </summary>
/// <param name="comparator">Function used to compare the elements.</param>
public PriorityQueue()
{
binaryHeap = new BinaryHeap<T>();
+14 -17
View File
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemExtensions.Collections
{
@@ -53,11 +50,11 @@ namespace SystemExtensions.Collections
/// <param name="maxLevel">Maximum level of the skip list.</param>
public SkipList(int maxLevel = 10)
{
this.maxLevel = maxLevel;
this.maxLevel = maxLevel;
random = new Random();
head = new NodeSet<T>(default(T), maxLevel);
end = head;
for(int i = 0; i <= maxLevel; i++)
for (int i = 0; i <= maxLevel; i++)
{
head.Next[i] = end;
}
@@ -84,7 +81,7 @@ namespace SystemExtensions.Collections
NodeSet<T> newNode = new NodeSet<T>(item, newLevel);
for (var i = 0; i <= newLevel; i++)
{
if(i > curNode.Level)
if (i > curNode.Level)
{
curNode = head;
}
@@ -108,7 +105,7 @@ namespace SystemExtensions.Collections
NodeSet<T> curNode = head;
for (var i = 0; i <= maxLevel; i++)
{
if(i > curNode.Level)
if (i > curNode.Level)
{
curNode = head;
}
@@ -141,7 +138,7 @@ namespace SystemExtensions.Collections
/// </summary>
public void Clear()
{
for(int i = 0; i < maxLevel; i++)
for (int i = 0; i < maxLevel; i++)
{
head.Next[i] = end;
}
@@ -154,7 +151,7 @@ namespace SystemExtensions.Collections
/// <returns>True if item is present in the collection.</returns>
public bool Contains(T item)
{
if(Find(item) != null)
if (Find(item) != null)
{
return true;
}
@@ -168,7 +165,7 @@ namespace SystemExtensions.Collections
public void CopyTo(T[] array, int arrayIndex)
{
NodeSet<T> node = head.Next[0];
while(node != end)
while (node != end)
{
array[arrayIndex] = node.Key;
arrayIndex++;
@@ -184,7 +181,7 @@ namespace SystemExtensions.Collections
T[] array = new T[count];
int index = 0;
NodeSet<T> curNode = head.Next[0];
while(curNode != end)
while (curNode != end)
{
array[index] = curNode.Key;
index++;
@@ -199,7 +196,7 @@ namespace SystemExtensions.Collections
public IEnumerator<T> GetEnumerator()
{
NodeSet<T> curNode = head.Next[0];
while(curNode != end)
while (curNode != end)
{
yield return curNode.Key;
curNode = curNode.Next[0];
@@ -215,15 +212,15 @@ namespace SystemExtensions.Collections
{
NodeSet<T> curNode = head;
for(int i = level; i >= 0; i--)
for (int i = level; i >= 0; i--)
{
while(curNode.Next[i] != end)
while (curNode.Next[i] != end)
{
if(curNode.Next[i].Key.CompareTo(key) > 0)
if (curNode.Next[i].Key.CompareTo(key) > 0)
{
break;
}
else if(curNode.Next[i].Key.CompareTo(key) == 0)
else if (curNode.Next[i].Key.CompareTo(key) == 0)
{
return curNode.Next[i];
}
@@ -232,7 +229,7 @@ namespace SystemExtensions.Collections
}
curNode = curNode.Next[0];
if(curNode != end && curNode.Key.CompareTo(key) == 0)
if (curNode != end && curNode.Key.CompareTo(key) == 0)
{
return curNode;
}
+25 -26
View File
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemExtensions.Collections
{
@@ -44,14 +41,15 @@ namespace SystemExtensions.Collections
return count;
}
}
public bool IsReadOnly => throw new NotImplementedException();
/// <summary>
/// Not implemented.
/// </summary>
public bool IsReadOnly => false;
#endregion
#region Constructors
/// <summary>
/// Constructor for treap.
/// </summary>
/// <param name="comparator">Comparator method used to compare values.</param>
public Treap()
{
randomGen = new Random();
@@ -134,14 +132,15 @@ namespace SystemExtensions.Collections
#region Private Methods
private Node<T> InsertNode(Node<T> node, T key)
{
if(node == null) {
if (node == null)
{
node = new Node<T>(key, randomGen.Next(0, 100));
return node;
}
else if (key.CompareTo(node.Key) <= 0)
{
node.Left = InsertNode(node.Left, key);
if(node.Left.Priority > node.Priority)
if (node.Left.Priority > node.Priority)
{
node = RotateRight(node);
}
@@ -149,7 +148,7 @@ namespace SystemExtensions.Collections
else
{
node.Right = InsertNode(node.Right, key);
if(node.Right.Priority > node.Priority)
if (node.Right.Priority > node.Priority)
{
node = RotateLeft(node);
}
@@ -158,27 +157,27 @@ namespace SystemExtensions.Collections
}
private Node<T> RemoveNode(Node<T> node, T key)
{
if(node == null)
if (node == null)
{
return node;
return node;
}
if(key.CompareTo(node.Key) < 0)
if (key.CompareTo(node.Key) < 0)
{
node.Left = RemoveNode(node.Left, key);
}
else if(key.CompareTo(node.Key) > 0)
else if (key.CompareTo(node.Key) > 0)
{
node.Right = RemoveNode(node.Right, key);
}
}
else if (node.Left == null)
{
node = node.Right;
}
else if(node.Right == null)
else if (node.Right == null)
{
node = node.Left;
}
else if(node.Left.Priority < node.Right.Priority)
else if (node.Left.Priority < node.Right.Priority)
{
node = RotateLeft(node);
node.Left = RemoveNode(node.Left, key);
@@ -206,11 +205,11 @@ namespace SystemExtensions.Collections
}
private void Clear(Node<T> node)
{
if(node.Left != null)
if (node.Left != null)
{
Clear(node.Left);
}
if(node.Right != null)
if (node.Right != null)
{
Clear(node.Right);
}
@@ -218,22 +217,22 @@ namespace SystemExtensions.Collections
}
private Node<T> Find(Node<T> node, T key)
{
if(node == null)
if (node == null)
{
return node;
}
else
{
if(node.Key.CompareTo(key) < 0)
if (node.Key.CompareTo(key) < 0)
{
Node<T> found = Find(node.Left, key);
if(found == null)
if (found == null)
{
found = Find(node.Right, key);
}
return found;
}
else if(node.Key.CompareTo(key) > 0)
else if (node.Key.CompareTo(key) > 0)
{
Node<T> found = Find(node.Right, key);
if (found == null)
@@ -250,7 +249,7 @@ namespace SystemExtensions.Collections
}
private void ToArray(Node<T> node, ref T[] array, ref int index)
{
if(node != null)
if (node != null)
{
ToArray(node.Left, ref array, ref index);
array[index] = node.Key;
@@ -262,15 +261,15 @@ namespace SystemExtensions.Collections
{
Queue<Node<T>> queue = new Queue<Node<T>>();
queue.Enqueue(currentNode);
while(queue.Count > 0)
while (queue.Count > 0)
{
currentNode = queue.Dequeue();
yield return currentNode.Key;
if(currentNode.Left != null)
if (currentNode.Left != null)
{
queue.Enqueue(currentNode.Left);
}
if(currentNode.Right != null)
if (currentNode.Right != null)
{
queue.Enqueue(currentNode.Right);
}
+1 -4
View File
@@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemExtensions
{
/// <summary>
/// Custom implementation of an exception.
/// </summary>
[Serializable]
public class ItemNotFoundException : Exception
{
/// <summary>
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
@@ -1,17 +1,11 @@
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace SystemExtensions.Security
{
@@ -406,7 +400,7 @@ namespace SystemExtensions.Security
// The user refused the elevation.
// Do nothing and return directly ...
return false;
}
}
}
else
{
+1 -1
View File
@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SystemExtensions</RootNamespace>
<AssemblyName>SystemExtensions</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SystemExtensions.Collections;
namespace SystemExtensions.Threading
@@ -239,7 +236,7 @@ namespace SystemExtensions.Threading
{
threadpool = new List<WorkerThread>();
tasks = new PriorityQueue<QueueEntry>();
for(int i = 0; i < lowest; i++)
for (int i = 0; i < lowest; i++)
{
WorkerThread worker = new WorkerThread();
worker.thread = new Thread(() =>
@@ -304,7 +301,7 @@ namespace SystemExtensions.Threading
worker.thread.Start();
threadpool.Add(worker);
}
}
}
#endregion
#region Public Methods
/// <summary>
@@ -315,7 +312,7 @@ namespace SystemExtensions.Threading
/// <param name="taskPriority">Priority of task. Affects its position into the queue.</param>
public void QueueUserWorkItem(WaitCallback waitCallback, object callbackState, TaskPriority taskPriority)
{
while (!Monitor.TryEnter(tasksLock));
while (!Monitor.TryEnter(tasksLock)) ;
tasks.Enqueue(new QueueEntry(taskPriority, waitCallback, callbackState));
Monitor.Exit(tasksLock);
}
@@ -384,7 +381,7 @@ namespace SystemExtensions.Threading
if (statistics.Initialized)
{
double loopDuration = (DateTime.Now - statistics.LastUpdate).TotalMilliseconds;
if(statistics.LoopFrequency == 0)
if (statistics.LoopFrequency == 0)
{
statistics.LoopFrequency = loopDuration;
}
@@ -403,10 +400,10 @@ namespace SystemExtensions.Threading
//This part of code adjusts the performance counter. Ideally, the value should be 0.
if(tasks.Count > 0)
if (tasks.Count > 0)
{
statistics.PerformanceCounter++;
if(statistics.PerformanceCounter > 5)
if (statistics.PerformanceCounter > 5)
{
statistics.PerformanceCounter = 5;
}
@@ -414,18 +411,18 @@ namespace SystemExtensions.Threading
else
{
statistics.PerformanceCounter--;
if(statistics.PerformanceCounter < -10)
if (statistics.PerformanceCounter < -10)
{
statistics.PerformanceCounter = -10;
}
}
//This part of code adjusts thread priorities based on the current performance of the threadpool
if(tasks.Count > 0)
if (tasks.Count > 0)
{
//If there are tasks pending, find a thread with priority under Normal and upgrade its priority.
Thread t = FindThreadWithLowPriority();
if(t != null)
if (t != null)
{
UpgradeThreadPriority(t);
}
@@ -434,7 +431,7 @@ namespace SystemExtensions.Threading
{
//If there are no tasks pending, find a thread with priority above Lowest and downgrade its priority.
Thread t = FindThreadWithAcceptablePriority();
if(t != null)
if (t != null)
{
DowngradeThreadPriority(t);
}
@@ -460,7 +457,7 @@ namespace SystemExtensions.Threading
threadpool.Add(worker);
}
}
else if(statistics.PerformanceCounter <= -10)
else if (statistics.PerformanceCounter <= -10)
{
if (threadpool.Count > maxThreads / 4)
{
@@ -489,9 +486,9 @@ namespace SystemExtensions.Threading
/// <returns>Thread with low priority.</returns>
private Thread FindThreadWithLowPriority()
{
foreach(WorkerThread t in threadpool)
foreach (WorkerThread t in threadpool)
{
if(t.thread.Priority == ThreadPriority.Lowest || t.thread.Priority == ThreadPriority.BelowNormal)
if (t.thread.Priority == ThreadPriority.Lowest || t.thread.Priority == ThreadPriority.BelowNormal)
{
return t.thread;
}
@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SystemExtensionsTests</RootNamespace>
<AssemblyName>SystemExtensionsTests</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>