mirror of
https://github.com/AlexMacocian/SystemExtensions.git
synced 2026-07-24 12:06:27 +00:00
@@ -60,7 +60,7 @@ namespace System.Collections.Generic
|
||||
public void Add(T value)
|
||||
{
|
||||
count++;
|
||||
AVLNode<T> newItem = new AVLNode<T>(value);
|
||||
var newItem = new AVLNode<T>(value);
|
||||
if (root == null)
|
||||
{
|
||||
root = newItem;
|
||||
@@ -77,7 +77,7 @@ namespace System.Collections.Generic
|
||||
/// <returns>True if the value is in the tree.</returns>
|
||||
public bool Contains(T value)
|
||||
{
|
||||
AVLNode<T> node = Find(value, root);
|
||||
var node = Find(value, root);
|
||||
if (node == null)
|
||||
{
|
||||
return false;
|
||||
@@ -105,12 +105,12 @@ namespace System.Collections.Generic
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
Queue<AVLNode<T>> queue = new Queue<AVLNode<T>>();
|
||||
var queue = new Queue<AVLNode<T>>();
|
||||
queue.Enqueue(root);
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
AVLNode<T> currentNode = queue.Dequeue();
|
||||
var currentNode = queue.Dequeue();
|
||||
if (currentNode.Left != null)
|
||||
{
|
||||
queue.Enqueue(currentNode.Left);
|
||||
@@ -134,11 +134,11 @@ namespace System.Collections.Generic
|
||||
/// <param name="arrayIndex">Starting index of the provided array.</param>
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
Queue<AVLNode<T>> queue = new Queue<AVLNode<T>>();
|
||||
var queue = new Queue<AVLNode<T>>();
|
||||
queue.Enqueue(root);
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
AVLNode<T> currentNode = queue.Dequeue();
|
||||
var currentNode = queue.Dequeue();
|
||||
array[arrayIndex++] = currentNode.Value;
|
||||
if (currentNode.Left != null)
|
||||
{
|
||||
@@ -164,7 +164,7 @@ namespace System.Collections.Generic
|
||||
/// <returns>Array containing the values contained in the tree.</returns>
|
||||
public T[] ToArray()
|
||||
{
|
||||
T[] array = new T[count];
|
||||
var array = new T[count];
|
||||
CopyTo(array, 0);
|
||||
return array;
|
||||
}
|
||||
@@ -191,7 +191,7 @@ namespace System.Collections.Generic
|
||||
}
|
||||
private AVLNode<T> BalanceTree(AVLNode<T> current)
|
||||
{
|
||||
int b_factor = BalanceFactor(current);
|
||||
var b_factor = BalanceFactor(current);
|
||||
if (b_factor > 1)
|
||||
{
|
||||
if (BalanceFactor(current.Left) > 0)
|
||||
@@ -299,7 +299,9 @@ namespace System.Collections.Generic
|
||||
return current;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Find(target, current.Left);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -308,7 +310,9 @@ namespace System.Collections.Generic
|
||||
return current;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Find(target, current.Right);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -318,46 +322,46 @@ namespace System.Collections.Generic
|
||||
}
|
||||
private int GetHeight(AVLNode<T> current)
|
||||
{
|
||||
int height = 0;
|
||||
var height = 0;
|
||||
if (current != null)
|
||||
{
|
||||
int l = GetHeight(current.Left);
|
||||
int r = GetHeight(current.Right);
|
||||
int m = Max(l, r);
|
||||
var l = GetHeight(current.Left);
|
||||
var r = GetHeight(current.Right);
|
||||
var m = Max(l, r);
|
||||
height = m + 1;
|
||||
}
|
||||
return height;
|
||||
}
|
||||
private int BalanceFactor(AVLNode<T> current)
|
||||
{
|
||||
int l = GetHeight(current.Left);
|
||||
int r = GetHeight(current.Right);
|
||||
int b_factor = l - r;
|
||||
var l = GetHeight(current.Left);
|
||||
var r = GetHeight(current.Right);
|
||||
var b_factor = l - r;
|
||||
return b_factor;
|
||||
}
|
||||
private AVLNode<T> RotateRR(AVLNode<T> parent)
|
||||
{
|
||||
AVLNode<T> pivot = parent.Right;
|
||||
var pivot = parent.Right;
|
||||
parent.Right = pivot.Left;
|
||||
pivot.Left = parent;
|
||||
return pivot;
|
||||
}
|
||||
private AVLNode<T> RotateLL(AVLNode<T> parent)
|
||||
{
|
||||
AVLNode<T> pivot = parent.Left;
|
||||
var pivot = parent.Left;
|
||||
parent.Left = pivot.Right;
|
||||
pivot.Right = parent;
|
||||
return pivot;
|
||||
}
|
||||
private AVLNode<T> RotateLR(AVLNode<T> parent)
|
||||
{
|
||||
AVLNode<T> pivot = parent.Left;
|
||||
var pivot = parent.Left;
|
||||
parent.Left = RotateRR(pivot);
|
||||
return RotateLL(parent);
|
||||
}
|
||||
private AVLNode<T> RotateRL(AVLNode<T> parent)
|
||||
{
|
||||
AVLNode<T> pivot = parent.Right;
|
||||
var pivot = parent.Right;
|
||||
parent.Right = RotateLL(pivot);
|
||||
return RotateRR(parent);
|
||||
}
|
||||
@@ -367,12 +371,12 @@ namespace System.Collections.Generic
|
||||
}
|
||||
private IEnumerator<T> GetEnumerator(AVLNode<T> rootNode)
|
||||
{
|
||||
Queue<AVLNode<T>> queue = new Queue<AVLNode<T>>();
|
||||
var queue = new Queue<AVLNode<T>>();
|
||||
queue.Enqueue(rootNode);
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
AVLNode<T> currentNode = queue.Dequeue();
|
||||
var currentNode = queue.Dequeue();
|
||||
yield return currentNode.Value;
|
||||
if (currentNode.Left != null)
|
||||
{
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace System.Collections.Generic
|
||||
{
|
||||
Capacity = 2 * Capacity;
|
||||
}
|
||||
int position = ++count;
|
||||
var position = ++count;
|
||||
for (; position > 1 && value.CompareTo(items[position / 2]) < 0; position /= 2)
|
||||
{
|
||||
items[position] = items[position / 2];
|
||||
@@ -105,7 +105,7 @@ namespace System.Collections.Generic
|
||||
{
|
||||
throw new IndexOutOfRangeException("Heap is empty!");
|
||||
}
|
||||
T min = items[1];
|
||||
var min = items[1];
|
||||
items[1] = items[count--];
|
||||
BubbleDown(1);
|
||||
return min;
|
||||
@@ -120,7 +120,7 @@ namespace System.Collections.Generic
|
||||
{
|
||||
throw new IndexOutOfRangeException("Heap is empty!");
|
||||
}
|
||||
T min = items[1];
|
||||
var min = items[1];
|
||||
return min;
|
||||
}
|
||||
/// <summary>
|
||||
@@ -129,7 +129,7 @@ namespace System.Collections.Generic
|
||||
/// <returns>Array with values sorted as in heap</returns>
|
||||
public T[] ToArray()
|
||||
{
|
||||
T[] newArray = new T[count];
|
||||
var newArray = new T[count];
|
||||
Array.Copy(items, 1, newArray, 0, count);
|
||||
return newArray;
|
||||
}
|
||||
@@ -168,7 +168,7 @@ namespace System.Collections.Generic
|
||||
/// <returns>Enumerator that iterates over the heap.</returns>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
yield return items[i + 1];
|
||||
}
|
||||
@@ -181,7 +181,7 @@ namespace System.Collections.Generic
|
||||
/// <param name="index">Index of element to bubble</param>
|
||||
private void BubbleDown(int index)
|
||||
{
|
||||
T temp = items[index];
|
||||
var temp = items[index];
|
||||
int childIndex;
|
||||
for (; 2 * index <= count; index = childIndex)
|
||||
{
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
/// <param name="value">Value to be added.</param>
|
||||
public void Add(T value)
|
||||
{
|
||||
FibonacciNode<T> node = new FibonacciNode<T>
|
||||
var node = new FibonacciNode<T>
|
||||
{
|
||||
Value = value,
|
||||
Marked = false,
|
||||
@@ -77,7 +77,7 @@
|
||||
/// <returns>Minimum value.</returns>
|
||||
public T Remove()
|
||||
{
|
||||
FibonacciNode<T> currentRoot = root;
|
||||
var currentRoot = root;
|
||||
if (currentRoot != null)
|
||||
{
|
||||
root = RemoveMinimum(root);
|
||||
@@ -96,7 +96,7 @@
|
||||
/// <param name="value">New value to be assigned to the node.</param>
|
||||
public void DecreaseKey(T oldValue, T value)
|
||||
{
|
||||
FibonacciNode<T> node = Find(root, oldValue);
|
||||
var node = Find(root, oldValue);
|
||||
root = DecreaseKey(root, node, value);
|
||||
}
|
||||
/// <summary>
|
||||
@@ -129,7 +129,7 @@
|
||||
{
|
||||
return null;
|
||||
}
|
||||
T[] array = new T[count];
|
||||
var array = new T[count];
|
||||
if (count == 1)
|
||||
{
|
||||
array[0] = root.Value;
|
||||
@@ -137,7 +137,7 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
int index = 0;
|
||||
var index = 0;
|
||||
RecursiveFillArray(root, ref array, ref index);
|
||||
return array;
|
||||
}
|
||||
@@ -160,7 +160,7 @@
|
||||
/// <param name="index">Index of the next unintialized element in the array.</param>
|
||||
private void RecursiveFillArray(FibonacciNode<T> currentNode, ref T[] array, ref int index)
|
||||
{
|
||||
FibonacciNode<T> oldNode = currentNode;
|
||||
var oldNode = currentNode;
|
||||
do
|
||||
{
|
||||
array[index] = currentNode.Value;
|
||||
@@ -178,12 +178,12 @@
|
||||
/// <param name="currentNode">Current node in the iteration.</param>
|
||||
private IEnumerator<T> GetEnumerator(FibonacciNode<T> currentNode)
|
||||
{
|
||||
Queue<FibonacciNode<T>> queue = new Queue<FibonacciNode<T>>();
|
||||
var queue = new Queue<FibonacciNode<T>>();
|
||||
queue.Enqueue(currentNode);
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
currentNode = queue.Dequeue();
|
||||
FibonacciNode<T> oldNode = currentNode;
|
||||
var oldNode = currentNode;
|
||||
do
|
||||
{
|
||||
yield return currentNode.Value;
|
||||
@@ -204,7 +204,7 @@
|
||||
{
|
||||
if (node != null)
|
||||
{
|
||||
FibonacciNode<T> current = node;
|
||||
var current = node;
|
||||
do
|
||||
{
|
||||
Remove(current.Child);
|
||||
@@ -234,12 +234,12 @@
|
||||
}
|
||||
if (node1.Value.CompareTo(node2.Value) > 0)
|
||||
{
|
||||
FibonacciNode<T> temp = node1;
|
||||
var temp = node1;
|
||||
node1 = node2;
|
||||
node2 = temp;
|
||||
}
|
||||
FibonacciNode<T> node1Next = node1.Next;
|
||||
FibonacciNode<T> node2Prev = node2.Previous;
|
||||
var node1Next = node1.Next;
|
||||
var node2Prev = node2.Previous;
|
||||
node1.Next = node2;
|
||||
node2.Previous = node1;
|
||||
node1Next.Previous = node2Prev;
|
||||
@@ -268,7 +268,7 @@
|
||||
{
|
||||
return;
|
||||
}
|
||||
FibonacciNode<T> current = node;
|
||||
var current = node;
|
||||
do
|
||||
{
|
||||
current.Marked = false;
|
||||
@@ -299,12 +299,12 @@
|
||||
return node;
|
||||
}
|
||||
|
||||
FibonacciNode<T>[] trees = new FibonacciNode<T>[64];
|
||||
var trees = new FibonacciNode<T>[64];
|
||||
while (true)
|
||||
{
|
||||
if (trees[node.Degree] != null)
|
||||
{
|
||||
FibonacciNode<T> t = trees[node.Degree];
|
||||
var t = trees[node.Degree];
|
||||
if (t == node)
|
||||
{
|
||||
break;
|
||||
@@ -344,8 +344,8 @@
|
||||
}
|
||||
node = node.Next;
|
||||
}
|
||||
FibonacciNode<T> min = node;
|
||||
FibonacciNode<T> start = node;
|
||||
var min = node;
|
||||
var start = node;
|
||||
do
|
||||
{
|
||||
if (node.Value.CompareTo(min.Value) < 0)
|
||||
@@ -397,7 +397,7 @@
|
||||
if (node.Value.CompareTo(node.Parent.Value) < 0)
|
||||
{
|
||||
root = Cut(root, node);
|
||||
FibonacciNode<T> parent = node.Parent;
|
||||
var parent = node.Parent;
|
||||
node.Parent = null;
|
||||
while (parent != null && parent.Marked)
|
||||
{
|
||||
@@ -429,7 +429,7 @@
|
||||
/// <returns></returns>
|
||||
private FibonacciNode<T> Find(FibonacciNode<T> root, T value)
|
||||
{
|
||||
FibonacciNode<T> node = root;
|
||||
var node = root;
|
||||
if (node == null)
|
||||
{
|
||||
return null;
|
||||
@@ -440,7 +440,7 @@
|
||||
{
|
||||
return node;
|
||||
}
|
||||
FibonacciNode<T> ret = Find(node.Child, value);
|
||||
var ret = Find(node.Child, value);
|
||||
if (ret != null)
|
||||
{
|
||||
return ret;
|
||||
|
||||
@@ -50,7 +50,7 @@
|
||||
random = new Random();
|
||||
head = new NodeSet<T>(default, maxLevel);
|
||||
end = head;
|
||||
for (int i = 0; i <= maxLevel; i++)
|
||||
for (var i = 0; i <= maxLevel; i++)
|
||||
{
|
||||
head.Next[i] = end;
|
||||
}
|
||||
@@ -64,8 +64,8 @@
|
||||
/// <param name="item">Item to be added.</param>
|
||||
public void Add(T item)
|
||||
{
|
||||
NodeSet<T> curNode = head;
|
||||
int newLevel = 0;
|
||||
var curNode = head;
|
||||
var newLevel = 0;
|
||||
while (random.Next(0, 2) > 0 && newLevel < maxLevel)
|
||||
{
|
||||
newLevel++;
|
||||
@@ -74,7 +74,7 @@
|
||||
{
|
||||
level = newLevel;
|
||||
}
|
||||
NodeSet<T> newNode = new NodeSet<T>(item, newLevel);
|
||||
var newNode = new NodeSet<T>(item, newLevel);
|
||||
for (var i = 0; i <= newLevel; i++)
|
||||
{
|
||||
if (i > curNode.Level)
|
||||
@@ -97,8 +97,8 @@
|
||||
/// <returns>True if removal was successful.</returns>
|
||||
public bool Remove(T item)
|
||||
{
|
||||
bool removed = false;
|
||||
NodeSet<T> curNode = head;
|
||||
var removed = false;
|
||||
var curNode = head;
|
||||
for (var i = 0; i <= maxLevel; i++)
|
||||
{
|
||||
if (i > curNode.Level)
|
||||
@@ -134,7 +134,7 @@
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
for (int i = 0; i < maxLevel; i++)
|
||||
for (var i = 0; i < maxLevel; i++)
|
||||
{
|
||||
head.Next[i] = end;
|
||||
}
|
||||
@@ -160,7 +160,7 @@
|
||||
/// <param name="arrayIndex">Index to start insertion in the array.</param>
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
NodeSet<T> node = head.Next[0];
|
||||
var node = head.Next[0];
|
||||
while (node != end)
|
||||
{
|
||||
array[arrayIndex] = node.Key;
|
||||
@@ -174,9 +174,9 @@
|
||||
/// <returns>Array filled with elements from the collection.</returns>
|
||||
public T[] ToArray()
|
||||
{
|
||||
T[] array = new T[count];
|
||||
int index = 0;
|
||||
NodeSet<T> curNode = head.Next[0];
|
||||
var array = new T[count];
|
||||
var index = 0;
|
||||
var curNode = head.Next[0];
|
||||
while (curNode != end)
|
||||
{
|
||||
array[index] = curNode.Key;
|
||||
@@ -191,7 +191,7 @@
|
||||
/// <returns></returns>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
NodeSet<T> curNode = head.Next[0];
|
||||
var curNode = head.Next[0];
|
||||
while (curNode != end)
|
||||
{
|
||||
yield return curNode.Key;
|
||||
@@ -206,9 +206,9 @@
|
||||
}
|
||||
private NodeSet<T> Find(T key)
|
||||
{
|
||||
NodeSet<T> curNode = head;
|
||||
var curNode = head;
|
||||
|
||||
for (int i = level; i >= 0; i--)
|
||||
for (var i = level; i >= 0; i--)
|
||||
{
|
||||
while (curNode.Next[i] != end)
|
||||
{
|
||||
|
||||
@@ -97,8 +97,8 @@
|
||||
{
|
||||
if (root != null)
|
||||
{
|
||||
T[] array = new T[count];
|
||||
int index = 0;
|
||||
var array = new T[count];
|
||||
var index = 0;
|
||||
ToArray(root, ref array, ref index);
|
||||
return array;
|
||||
}
|
||||
@@ -221,7 +221,7 @@
|
||||
{
|
||||
if (node.Key.CompareTo(key) < 0)
|
||||
{
|
||||
Node<T> found = Find(node.Left, key);
|
||||
var found = Find(node.Left, key);
|
||||
if (found == null)
|
||||
{
|
||||
found = Find(node.Right, key);
|
||||
@@ -230,7 +230,7 @@
|
||||
}
|
||||
else if (node.Key.CompareTo(key) > 0)
|
||||
{
|
||||
Node<T> found = Find(node.Right, key);
|
||||
var found = Find(node.Right, key);
|
||||
if (found == null)
|
||||
{
|
||||
found = Find(node.Left, key);
|
||||
@@ -255,7 +255,7 @@
|
||||
}
|
||||
private IEnumerator<T> GetEnumerator(Node<T> currentNode)
|
||||
{
|
||||
Queue<Node<T>> queue = new Queue<Node<T>>();
|
||||
var queue = new Queue<Node<T>>();
|
||||
queue.Enqueue(currentNode);
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user