mirror of
https://github.com/AlexMacocian/SystemExtensions.git
synced 2026-07-15 14:19:29 +00:00
Implemented Serializable + Tests
This commit is contained in:
@@ -13,15 +13,17 @@ namespace SystemExtensions.Collections
|
||||
/// Read on https://simpledevcode.wordpress.com/2014/09/16/avl-tree-in-c/
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Provided type.</typeparam>
|
||||
[Serializable]
|
||||
public class AVLTree<T> : ICollection<T> where T : IComparable<T>
|
||||
{
|
||||
#region Fields
|
||||
private class AVLNode<T>
|
||||
[Serializable]
|
||||
private class AVLNode<TKey>
|
||||
{
|
||||
public T Value;
|
||||
public AVLNode<T> Left;
|
||||
public AVLNode<T> Right;
|
||||
public AVLNode(T value)
|
||||
public TKey Value;
|
||||
public AVLNode<TKey> Left;
|
||||
public AVLNode<TKey> Right;
|
||||
public AVLNode(TKey value)
|
||||
{
|
||||
this.Value = value;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace SystemExtensions.Collections
|
||||
/// Binary heap implementation.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Provided type.</typeparam>
|
||||
[Serializable]
|
||||
public class BinaryHeap<T> : IEnumerable<T> where T : IComparable<T>
|
||||
{
|
||||
#region Fields
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace SystemExtensions.Collections
|
||||
/// Fibonacci Heap implementation.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Provided type</typeparam>
|
||||
[Serializable]
|
||||
public class FibonacciHeap<T> : IEnumerable<T> where T : IComparable<T>
|
||||
{
|
||||
#region Fields
|
||||
@@ -460,7 +461,7 @@ namespace SystemExtensions.Collections
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
internal class FibonacciNode<T>
|
||||
{
|
||||
#region Fields
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace SystemExtensions.Collections
|
||||
/// Exposes some of the functionality of the Binary Heap as a queue.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Provided type.</typeparam>
|
||||
[Serializable]
|
||||
public class PriorityQueue<T> : IQueue<T> where T : IComparable<T>
|
||||
{
|
||||
#region Fields
|
||||
|
||||
@@ -11,9 +11,11 @@ namespace SystemExtensions.Collections
|
||||
/// Skip list implementation.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Provided type.</typeparam>
|
||||
[Serializable]
|
||||
public class SkipList<T> : ICollection<T> where T : IComparable<T>
|
||||
{
|
||||
#region Fields
|
||||
[Serializable]
|
||||
private class NodeSet<TKey>
|
||||
{
|
||||
public TKey Key;
|
||||
|
||||
@@ -11,15 +11,17 @@ namespace SystemExtensions.Collections
|
||||
/// Treap implementation.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Provided type.</typeparam>
|
||||
[Serializable]
|
||||
public class Treap<T> : ICollection<T> where T : IComparable<T>
|
||||
{
|
||||
#region Fields
|
||||
private class Item<T>
|
||||
[Serializable]
|
||||
private class Node<TKey>
|
||||
{
|
||||
public T Key;
|
||||
public TKey Key;
|
||||
public int Priority;
|
||||
public Item<T> Left, Right;
|
||||
public Item(T key, int priority)
|
||||
public Node<TKey> Left, Right;
|
||||
public Node(TKey key, int priority)
|
||||
{
|
||||
Key = key;
|
||||
Priority = priority;
|
||||
@@ -28,7 +30,7 @@ namespace SystemExtensions.Collections
|
||||
}
|
||||
}
|
||||
private Random randomGen;
|
||||
private Item<T> root;
|
||||
private Node<T> root;
|
||||
private int count;
|
||||
#endregion
|
||||
#region Properties
|
||||
@@ -130,10 +132,10 @@ namespace SystemExtensions.Collections
|
||||
}
|
||||
#endregion
|
||||
#region Private Methods
|
||||
private Item<T> InsertNode(Item<T> node, T key)
|
||||
private Node<T> InsertNode(Node<T> node, T key)
|
||||
{
|
||||
if(node == null) {
|
||||
node = new Item<T>(key, randomGen.Next(0, 100));
|
||||
node = new Node<T>(key, randomGen.Next(0, 100));
|
||||
return node;
|
||||
}
|
||||
else if (key.CompareTo(node.Key) <= 0)
|
||||
@@ -154,7 +156,7 @@ namespace SystemExtensions.Collections
|
||||
}
|
||||
return node;
|
||||
}
|
||||
private Item<T> RemoveNode(Item<T> node, T key)
|
||||
private Node<T> RemoveNode(Node<T> node, T key)
|
||||
{
|
||||
if(node == null)
|
||||
{
|
||||
@@ -188,21 +190,21 @@ namespace SystemExtensions.Collections
|
||||
}
|
||||
return node;
|
||||
}
|
||||
private Item<T> RotateRight(Item<T> node)
|
||||
private Node<T> RotateRight(Node<T> node)
|
||||
{
|
||||
Item<T> temp = node.Left, temp2 = temp.Right;
|
||||
Node<T> temp = node.Left, temp2 = temp.Right;
|
||||
temp.Right = node;
|
||||
node.Left = temp2;
|
||||
return temp;
|
||||
}
|
||||
private Item<T> RotateLeft(Item<T> node)
|
||||
private Node<T> RotateLeft(Node<T> node)
|
||||
{
|
||||
Item<T> temp = node.Right, temp2 = temp.Left;
|
||||
Node<T> temp = node.Right, temp2 = temp.Left;
|
||||
temp.Left = node;
|
||||
node.Right = temp2;
|
||||
return temp;
|
||||
}
|
||||
private void Clear(Item<T> node)
|
||||
private void Clear(Node<T> node)
|
||||
{
|
||||
if(node.Left != null)
|
||||
{
|
||||
@@ -214,7 +216,7 @@ namespace SystemExtensions.Collections
|
||||
}
|
||||
node.Left = node.Right = null;
|
||||
}
|
||||
private Item<T> Find(Item<T> node, T key)
|
||||
private Node<T> Find(Node<T> node, T key)
|
||||
{
|
||||
if(node == null)
|
||||
{
|
||||
@@ -224,7 +226,7 @@ namespace SystemExtensions.Collections
|
||||
{
|
||||
if(node.Key.CompareTo(key) < 0)
|
||||
{
|
||||
Item<T> found = Find(node.Left, key);
|
||||
Node<T> found = Find(node.Left, key);
|
||||
if(found == null)
|
||||
{
|
||||
found = Find(node.Right, key);
|
||||
@@ -233,7 +235,7 @@ namespace SystemExtensions.Collections
|
||||
}
|
||||
else if(node.Key.CompareTo(key) > 0)
|
||||
{
|
||||
Item<T> found = Find(node.Right, key);
|
||||
Node<T> found = Find(node.Right, key);
|
||||
if (found == null)
|
||||
{
|
||||
found = Find(node.Left, key);
|
||||
@@ -246,7 +248,7 @@ namespace SystemExtensions.Collections
|
||||
}
|
||||
}
|
||||
}
|
||||
private void ToArray(Item<T> node, ref T[] array, ref int index)
|
||||
private void ToArray(Node<T> node, ref T[] array, ref int index)
|
||||
{
|
||||
if(node != null)
|
||||
{
|
||||
@@ -256,9 +258,9 @@ namespace SystemExtensions.Collections
|
||||
ToArray(node.Right, ref array, ref index);
|
||||
}
|
||||
}
|
||||
private IEnumerator<T> GetEnumerator(Item<T> currentNode)
|
||||
private IEnumerator<T> GetEnumerator(Node<T> currentNode)
|
||||
{
|
||||
Queue<Item<T>> queue = new Queue<Item<T>>();
|
||||
Queue<Node<T>> queue = new Queue<Node<T>>();
|
||||
queue.Enqueue(currentNode);
|
||||
while(queue.Count > 0)
|
||||
{
|
||||
|
||||
@@ -5,6 +5,8 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.IO;
|
||||
|
||||
namespace SystemExtensions.Collections.Tests
|
||||
{
|
||||
@@ -124,5 +126,35 @@ namespace SystemExtensions.Collections.Tests
|
||||
|
||||
int[] array = avlTree.ToArray();
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void Serialize()
|
||||
{
|
||||
AVLTree<int> avlTree2 = new AVLTree<int>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
avlTree.Add(i);
|
||||
}
|
||||
BinaryFormatter serializer = new BinaryFormatter();
|
||||
string s = string.Empty;
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
serializer.Serialize(stream, avlTree);
|
||||
stream.Position = 0;
|
||||
avlTree2 = (AVLTree<int>)serializer.Deserialize(stream);
|
||||
}
|
||||
IEnumerator<int> avlTreeEnum = avlTree.GetEnumerator();
|
||||
IEnumerator<int> avlTree2Enum = avlTree2.GetEnumerator();
|
||||
|
||||
for(int i = 0; i < 100; i++)
|
||||
{
|
||||
if(avlTreeEnum.Current != avlTree2Enum.Current)
|
||||
{
|
||||
Assert.Fail();
|
||||
}
|
||||
avlTreeEnum.MoveNext();
|
||||
avlTree2Enum.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,9 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SystemExtensions;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Xml.Serialization;
|
||||
using System.IO;
|
||||
|
||||
namespace SystemExtensions.Collections.Tests
|
||||
{
|
||||
@@ -173,5 +176,34 @@ namespace SystemExtensions.Collections.Tests
|
||||
|
||||
Assert.Fail();
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void Serialize()
|
||||
{
|
||||
BinaryHeap<int> binaryHeap2 = new BinaryHeap<int>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
binaryHeap.Insert(i);
|
||||
}
|
||||
BinaryFormatter serializer = new BinaryFormatter();
|
||||
string s = string.Empty;
|
||||
using (var stream = new MemoryStream()) {
|
||||
serializer.Serialize(stream, binaryHeap);
|
||||
stream.Position = 0;
|
||||
binaryHeap2 = (BinaryHeap<int>)serializer.Deserialize(stream);
|
||||
}
|
||||
IEnumerator<int> binaryHeapEnum = binaryHeap.GetEnumerator();
|
||||
IEnumerator<int> binaryHeapEnum2 = binaryHeap2.GetEnumerator();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
if (binaryHeapEnum.Current != binaryHeapEnum2.Current)
|
||||
{
|
||||
Assert.Fail();
|
||||
}
|
||||
binaryHeapEnum.MoveNext();
|
||||
binaryHeapEnum2.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.IO;
|
||||
|
||||
namespace SystemExtensions.Collections.Tests
|
||||
{
|
||||
@@ -175,5 +177,35 @@ namespace SystemExtensions.Collections.Tests
|
||||
|
||||
Assert.Fail();
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void Serialize()
|
||||
{
|
||||
FibonacciHeap<int> fibonacciHeap2 = new FibonacciHeap<int>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
fibonacciHeap.Insert(i);
|
||||
}
|
||||
BinaryFormatter serializer = new BinaryFormatter();
|
||||
string s = string.Empty;
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
serializer.Serialize(stream, fibonacciHeap);
|
||||
stream.Position = 0;
|
||||
fibonacciHeap2 = (FibonacciHeap<int>)serializer.Deserialize(stream);
|
||||
}
|
||||
IEnumerator<int> enum1 = fibonacciHeap.GetEnumerator();
|
||||
IEnumerator<int> enum2 = fibonacciHeap2.GetEnumerator();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
if (enum1.Current != enum2.Current)
|
||||
{
|
||||
Assert.Fail();
|
||||
}
|
||||
enum1.MoveNext();
|
||||
enum2.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SystemExtensions;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.IO;
|
||||
|
||||
namespace SystemExtensions.Collections.Tests
|
||||
{
|
||||
@@ -81,5 +83,35 @@ namespace SystemExtensions.Collections.Tests
|
||||
Assert.Fail();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void Serialize()
|
||||
{
|
||||
PriorityQueue<int> priorityQueue2 = new PriorityQueue<int>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
priorityQueue.Enqueue(i);
|
||||
}
|
||||
BinaryFormatter serializer = new BinaryFormatter();
|
||||
string s = string.Empty;
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
serializer.Serialize(stream, priorityQueue);
|
||||
stream.Position = 0;
|
||||
priorityQueue2 = (PriorityQueue<int>)serializer.Deserialize(stream);
|
||||
}
|
||||
IEnumerator<int> enum1 = priorityQueue.GetEnumerator();
|
||||
IEnumerator<int> enum2 = priorityQueue2.GetEnumerator();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
if (enum1.Current != enum2.Current)
|
||||
{
|
||||
Assert.Fail();
|
||||
}
|
||||
enum1.MoveNext();
|
||||
enum2.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.IO;
|
||||
|
||||
namespace SystemExtensions.Collections.Tests
|
||||
{
|
||||
@@ -124,5 +126,35 @@ namespace SystemExtensions.Collections.Tests
|
||||
Assert.Fail();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void Serialize()
|
||||
{
|
||||
SkipList<int> skipList2 = new SkipList<int>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
skipList.Add(i);
|
||||
}
|
||||
BinaryFormatter serializer = new BinaryFormatter();
|
||||
string s = string.Empty;
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
serializer.Serialize(stream, skipList);
|
||||
stream.Position = 0;
|
||||
skipList2 = (SkipList<int>)serializer.Deserialize(stream);
|
||||
}
|
||||
IEnumerator<int> enum1 = skipList.GetEnumerator();
|
||||
IEnumerator<int> enum2 = skipList2.GetEnumerator();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
if (enum1.Current != enum2.Current)
|
||||
{
|
||||
Assert.Fail();
|
||||
}
|
||||
enum1.MoveNext();
|
||||
enum2.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.IO;
|
||||
|
||||
namespace SystemExtensions.Collections.Tests
|
||||
{
|
||||
@@ -110,5 +112,35 @@ namespace SystemExtensions.Collections.Tests
|
||||
|
||||
Assert.Fail();
|
||||
}
|
||||
|
||||
[TestMethod()]
|
||||
public void Serialize()
|
||||
{
|
||||
Treap<int> treap2 = new Treap<int>();
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
treap.Add(i);
|
||||
}
|
||||
BinaryFormatter serializer = new BinaryFormatter();
|
||||
string s = string.Empty;
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
serializer.Serialize(stream, treap);
|
||||
stream.Position = 0;
|
||||
treap2 = (Treap<int>)serializer.Deserialize(stream);
|
||||
}
|
||||
IEnumerator<int> enum1 = treap.GetEnumerator();
|
||||
IEnumerator<int> enum2 = treap2.GetEnumerator();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
if (enum1.Current != enum2.Current)
|
||||
{
|
||||
Assert.Fail();
|
||||
}
|
||||
enum1.MoveNext();
|
||||
enum2.MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@
|
||||
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<Choose>
|
||||
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
|
||||
|
||||
Reference in New Issue
Block a user