Client Web Socket implementation (#46)

This commit is contained in:
2023-05-14 22:46:38 +02:00
committed by GitHub
parent a25f2e5acb
commit de08c0e3b0
13 changed files with 234 additions and 107 deletions
@@ -16,14 +16,14 @@ public sealed class AVLTree<T> : ICollection<T> where T : IComparable<T>
private class AVLNode<TKey>
{
public TKey Value;
public AVLNode<TKey> Left;
public AVLNode<TKey> Right;
public AVLNode<TKey> Left = default!;
public AVLNode<TKey> Right = default!;
public AVLNode(TKey value)
{
this.Value = value;
}
}
AVLNode<T> root;
AVLNode<T> root = default!;
private int count = 0;
private readonly bool isReadOnly = false;
#endregion
@@ -98,7 +98,7 @@ public sealed class AVLTree<T> : ICollection<T> where T : IComparable<T>
/// <param name="value">Value to be deleted.</param>
public bool Remove(T value)
{
this.root = this.Delete(this.root, value);
this.root = this.Delete(this.root, value)!;
return true;
}
/// <summary>
@@ -115,19 +115,19 @@ public sealed class AVLTree<T> : ICollection<T> where T : IComparable<T>
if (currentNode.Left != null)
{
queue.Enqueue(currentNode.Left);
currentNode.Left = null;
currentNode.Left = default!;
this.count--;
}
if (currentNode.Right != null)
{
queue.Enqueue(currentNode.Right);
currentNode.Right = null;
currentNode.Right = default!;
this.count--;
}
}
this.root = null;
this.root = default!;
this.count--;
}
/// <summary>
@@ -222,9 +222,8 @@ public sealed class AVLTree<T> : ICollection<T> where T : IComparable<T>
return current;
}
private AVLNode<T> Delete(AVLNode<T> current, T target)
private AVLNode<T>? Delete(AVLNode<T> current, T target)
{
AVLNode<T> parent;
if (current == null)
{ return null; }
else
@@ -232,7 +231,7 @@ public sealed class AVLTree<T> : ICollection<T> where T : IComparable<T>
//left subtree
if (target.CompareTo(current.Value) < 0)
{
current.Left = this.Delete(current.Left, target);
current.Left = this.Delete(current.Left, target)!;
if (this.BalanceFactor(current) == -2)//here
{
if (this.BalanceFactor(current.Right) <= 0)
@@ -248,7 +247,7 @@ public sealed class AVLTree<T> : ICollection<T> where T : IComparable<T>
//right subtree
else if (target.CompareTo(current.Value) > 0)
{
current.Right = this.Delete(current.Right, target);
current.Right = this.Delete(current.Right, target)!;
if (this.BalanceFactor(current) == 2)
{
if (this.BalanceFactor(current.Left) >= 0)
@@ -268,14 +267,14 @@ public sealed class AVLTree<T> : ICollection<T> where T : IComparable<T>
if (current.Right != null)
{
//delete its inorder successor
parent = current.Right;
var parent = current.Right;
while (parent.Left != null)
{
parent = parent.Left;
}
current.Value = parent.Value;
current.Right = this.Delete(current.Right, parent.Value);
current.Right = this.Delete(current.Right, parent.Value)!;
if (this.BalanceFactor(current) == 2)//rebalancing
{
if (this.BalanceFactor(current.Left) >= 0)
@@ -294,7 +293,7 @@ public sealed class AVLTree<T> : ICollection<T> where T : IComparable<T>
return current;
}
private AVLNode<T> Find(T target, AVLNode<T> current)
private AVLNode<T>? Find(T target, AVLNode<T> current)
{
if (current == null)
{
@@ -8,7 +8,7 @@
public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
{
#region Fields
private FibonacciNode<T> root;
private FibonacciNode<T> root = default!;
private int count;
#endregion
#region Properties
@@ -53,8 +53,8 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
{
Value = value,
Marked = false,
Child = null,
Parent = null,
Child = default!,
Parent = default!,
Degree = 0
};
node.Previous = node.Next = node;
@@ -68,7 +68,7 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
public void Merge(FibonacciHeap<T> otherHeap)
{
this.root = this.Merge(this.root, otherHeap.root);
otherHeap.root = null;
otherHeap.root = default!;
this.count += otherHeap.count;
}
/// <summary>
@@ -80,7 +80,7 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
var currentRoot = this.root;
if (currentRoot != null)
{
this.root = this.RemoveMinimum(this.root);
this.root = this.RemoveMinimum(this.root)!;
this.count--;
return currentRoot.Value;
}
@@ -115,8 +115,8 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
{
this.count = 0;
this.Remove(this.root);
this.root.Next = this.root.Previous = this.root.Parent = this.root.Child = null;
this.root = null;
this.root.Next = this.root.Previous = this.root.Parent = this.root.Child = default!;
this.root = default!;
}
/// <summary>
/// Return the heap structure as an array. Array is not sorted other than the
@@ -127,7 +127,7 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
{
if (this.count == 0)
{
return null;
return default!;
}
var array = new T[this.count];
@@ -212,12 +212,12 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
this.Remove(current.Child);
if (current.Parent != null)
{
current.Parent.Child = null;
current.Parent.Child = default!;
}
current = current.Next;
} while (current != node);
current.Next = current.Previous = current.Child = current.Parent = null;
current.Next = current.Previous = current.Child = current.Parent = default!;
}
}
/// <summary>
@@ -279,7 +279,7 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
do
{
current.Marked = false;
current.Parent = null;
current.Parent = default!;
current = current.Next;
} while (current != node);
}
@@ -288,7 +288,7 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
/// </summary>
/// <param name="node">Root of the provided tree.</param>
/// <returns></returns>
private FibonacciNode<T> RemoveMinimum(FibonacciNode<T> node)
private FibonacciNode<T>? RemoveMinimum(FibonacciNode<T> node)
{
this.RemoveParent(node.Child);
if (node.Next == node)
@@ -318,7 +318,7 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
break;
}
trees[node.Degree] = null;
trees[node.Degree] = default!;
if (node.Value.CompareTo(t.Value) < 0)
{
t.Previous.Next = t.Next;
@@ -379,7 +379,7 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
{
if (node.Next == node)
{
node.Parent.Child = null;
node.Parent.Child = default!;
}
else
{
@@ -413,13 +413,13 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
{
root = this.Cut(root, node);
var parent = node.Parent;
node.Parent = null;
node.Parent = default!;
while (parent != null && parent.Marked)
{
root = this.Cut(root, parent);
node = parent;
parent = node.Parent;
node.Parent = null;
node.Parent = default!;
}
if (parent != null && parent.Parent != null)
@@ -449,7 +449,7 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
var node = root;
if (node == null)
{
return null;
return default!;
}
do
@@ -467,7 +467,7 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
node = node.Next;
} while (node != root);
return null;
return default!;
}
IEnumerator IEnumerable.GetEnumerator()
{
@@ -479,11 +479,11 @@ public sealed class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
internal sealed class FibonacciNode<T>
{
#region Fields
private FibonacciNode<T> previous;
private FibonacciNode<T> next;
private FibonacciNode<T> child;
private FibonacciNode<T> parent;
private T value;
private FibonacciNode<T> previous = default!;
private FibonacciNode<T> next = default!;
private FibonacciNode<T> child = default!;
private FibonacciNode<T> parent = default!;
private T value = default!;
private int degree;
private bool marked;
#endregion
@@ -48,7 +48,7 @@ public sealed class SkipList<T> : ICollection<T> where T : IComparable<T>
{
this.maxLevel = maxLevel;
this.random = new Random();
this.head = new NodeSet<T>(default, maxLevel);
this.head = new NodeSet<T>(default!, maxLevel);
this.end = this.head;
for (var i = 0; i <= maxLevel; i++)
{
@@ -242,7 +242,7 @@ public sealed class SkipList<T> : ICollection<T> where T : IComparable<T>
return curNode;
}
return null;
return default!;
}
#endregion
}
@@ -13,7 +13,7 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
{
public TKey Key;
public int Priority;
public Node<TKey> Left, Right;
public Node<TKey>? Left, Right;
public Node(TKey key, int priority)
{
this.Key = key;
@@ -23,7 +23,7 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
}
}
private readonly Random randomGen;
private Node<T> root;
private Node<T> root = default!;
private int count;
#endregion
#region Properties
@@ -77,7 +77,7 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
public void Clear()
{
this.Clear(this.root);
this.root = null;
this.root = default!;
this.count = 0;
}
/// <summary>
@@ -93,7 +93,7 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
/// Returns the treap structure as an ordered array.
/// </summary>
/// <returns>Ordered array containing the values stored in the treap.</returns>
public T[] ToArray()
public T[]? ToArray()
{
if (this.root != null)
{
@@ -104,7 +104,7 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
}
else
{
return null;
return default!;
}
}
/// <summary>
@@ -135,16 +135,16 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
}
else if (key.CompareTo(node.Key) <= 0)
{
node.Left = this.InsertNode(node.Left, key);
if (node.Left.Priority > node.Priority)
node.Left = this.InsertNode(node.Left!, key);
if (node.Left!.Priority > node.Priority)
{
node = this.RotateRight(node);
}
}
else
{
node.Right = this.InsertNode(node.Right, key);
if (node.Right.Priority > node.Priority)
node.Right = this.InsertNode(node.Right!, key);
if (node.Right!.Priority > node.Priority)
{
node = this.RotateLeft(node);
}
@@ -156,20 +156,20 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
{
if (node == null)
{
return node;
return node!;
}
if (key.CompareTo(node.Key) < 0)
{
node.Left = this.RemoveNode(node.Left, key);
node.Left = this.RemoveNode(node.Left!, key);
}
else if (key.CompareTo(node.Key) > 0)
{
node.Right = this.RemoveNode(node.Right, key);
node.Right = this.RemoveNode(node.Right!, key);
}
else if (node.Left == null)
{
node = node.Right;
node = node.Right!;
}
else if (node.Right == null)
{
@@ -178,26 +178,26 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
else if (node.Left.Priority < node.Right.Priority)
{
node = this.RotateLeft(node);
node.Left = this.RemoveNode(node.Left, key);
node.Left = this.RemoveNode(node.Left!, key);
}
else
{
node = this.RotateRight(node);
node.Right = this.RemoveNode(node.Right, key);
node.Right = this.RemoveNode(node.Right!, key);
}
return node;
}
private Node<T> RotateRight(Node<T> node)
{
Node<T> temp = node.Left, temp2 = temp.Right;
Node<T> temp = node.Left!, temp2 = temp.Right!;
temp.Right = node;
node.Left = temp2;
return temp;
}
private Node<T> RotateLeft(Node<T> node)
{
Node<T> temp = node.Right, temp2 = temp.Left;
Node<T> temp = node.Right!, temp2 = temp.Left!;
temp.Left = node;
node.Right = temp2;
return temp;
@@ -220,26 +220,26 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
{
if (node == null)
{
return node;
return node!;
}
else
{
if (node.Key.CompareTo(key) < 0)
{
var found = this.Find(node.Left, key);
var found = this.Find(node.Left!, key);
if (found == null)
{
found = this.Find(node.Right, key);
found = this.Find(node.Right!, key);
}
return found;
}
else if (node.Key.CompareTo(key) > 0)
{
var found = this.Find(node.Right, key);
var found = this.Find(node.Right!, key);
if (found == null)
{
found = this.Find(node.Left, key);
found = this.Find(node.Left!, key);
}
return found;
@@ -254,10 +254,10 @@ public sealed class Treap<T> : ICollection<T> where T : IComparable<T>
{
if (node != null)
{
this.ToArray(node.Left, ref array, ref index);
this.ToArray(node.Left!, ref array, ref index);
array[index] = node.Key;
index++;
this.ToArray(node.Right, ref array, ref index);
this.ToArray(node.Right!, ref array, ref index);
}
}
private IEnumerator<T> GetEnumerator(Node<T> currentNode)