From afdfeda8264c950b235a963e0cf9db745c1010e1 Mon Sep 17 00:00:00 2001 From: Alex Macocian Date: Thu, 18 Apr 2019 15:41:50 +0200 Subject: [PATCH] Implemented Fibonacci Heap + Tests --- SystemExtensions/Collections/BinaryHeap.cs | 8 +- SystemExtensions/Collections/FibonacciHeap.cs | 549 ++++++++++++++++++ SystemExtensions/Collections/PriorityQueue.cs | 2 +- SystemExtensions/Comparators.cs | 467 +++++++++++++++ SystemExtensions/SystemExtensions.csproj | 2 + .../Collections/BinaryHeapTests.cs | 3 +- .../Collections/FibonacciHeapTests.cs | 203 +++++++ .../Collections/PriorityQueueTests.cs | 1 + .../SystemExtensionsTests.csproj | 1 + 9 files changed, 1230 insertions(+), 6 deletions(-) create mode 100644 SystemExtensions/Collections/FibonacciHeap.cs create mode 100644 SystemExtensions/Comparators.cs create mode 100644 SystemExtensionsTests/Collections/FibonacciHeapTests.cs diff --git a/SystemExtensions/Collections/BinaryHeap.cs b/SystemExtensions/Collections/BinaryHeap.cs index bdc7544..562f94f 100644 --- a/SystemExtensions/Collections/BinaryHeap.cs +++ b/SystemExtensions/Collections/BinaryHeap.cs @@ -78,10 +78,10 @@ namespace SystemExtensions.Collections items[position] = value; } /// - /// Deletes the item at the root. Throws exception if there are no items in the heap. + /// Removes the item at the root. Throws exception if there are no items in the heap. /// - /// - public T DeleteMin() + /// Value removed. + public T RemoveMin() { if (count == 0) { @@ -93,7 +93,7 @@ namespace SystemExtensions.Collections return min; } /// - /// Function to return the heap structure as an array + /// Return the heap structure as an array /// /// Array with values sorted as in heap public T[] ToArray() diff --git a/SystemExtensions/Collections/FibonacciHeap.cs b/SystemExtensions/Collections/FibonacciHeap.cs new file mode 100644 index 0000000..d60633a --- /dev/null +++ b/SystemExtensions/Collections/FibonacciHeap.cs @@ -0,0 +1,549 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SystemExtensions.Collections +{ + public class FibonacciHeap : IDisposable + { + #region Fields + private FibonacciNode root; + private Comparison comparator; + private int count; + #endregion + #region Properties + /// + /// Count of values in the heap. + /// + public int Count + { + get + { + return count; + } + } + /// + /// Minimal value contained in the heap. + /// + public T Minimum + { + get + { + return root.Value; + } + } + #endregion + #region Constructors + public FibonacciHeap(Comparison comparator) + { + this.comparator = comparator; + } + #endregion + #region Public Methods + /// + /// Inserts value into the heap. + /// + /// Value to be inserted. + public void Insert(T value) + { + FibonacciNode node = new FibonacciNode(); + node.Value = value; + node.Previous = node.Next = node; + node.Degree = 0; + node.Marked = false; + node.Child = null; + node.Parent = null; + root = Merge(root, node); + count++; + } + /// + /// Merge current heap with another heap. The other heap will be disposed at the end of this method. + /// + /// The heap to be merged with the current heap. + public void Merge(FibonacciHeap otherHeap) + { + root = Merge(root, otherHeap.root); + otherHeap.root = null; + count += otherHeap.count; + otherHeap.Dispose(); + } + /// + /// Remove the minimum value from the heap. + /// + /// Minimum value. + public T RemoveMinimum() + { + FibonacciNode currentRoot = root; + if (currentRoot != null) + { + root = RemoveMinimum(root); + count--; + return currentRoot.Value; + } + else + { + throw new IndexOutOfRangeException("Heap is empty!"); + } + } + /// + /// Decrease the old value to a new provided value. + /// + /// Old value used to find the node to have its key decreased. + /// New value to be assigned to the node. + public void DecreaseKey(T oldValue, T value) + { + FibonacciNode node = Find(root, oldValue); + root = DecreaseKey(root, node, value); + } + /// + /// Determines whether the heap contains a specified value. + /// + /// Valye to locate in the heap. + /// + public bool Contains(T value) + { + return Find(root, value) != null; + } + /// + /// Clears the heap. + /// + public void Clear() + { + count = 0; + Remove(root); + root.Next = root.Previous = root.Parent = root.Child = null; + root = null; + } + /// + /// Return the heap structure as an array. Array is not sorted other than the + /// actual structure of the heap. + /// + /// Array with values from the heap. + public T[] ToArray() + { + if(count == 0) + { + return null; + } + T[] array = new T[count]; + if (count == 1) + { + array[0] = root.Value; + return array; + } + else + { + int index = 0; + RecursiveFillArray(root, ref array, ref index); + return array; + } + } + #endregion + #region Private Methods + /// + /// Recursively traverse the heap and copy its contents to an array. + /// + /// Current node. + /// Array to be filled with contents of heap. + /// Index of the next unintialized element in the array. + private void RecursiveFillArray(FibonacciNode currentNode, ref T[] array, ref int index) + { + FibonacciNode oldNode = currentNode; + do + { + array[index] = currentNode.Value; + index++; + if (currentNode.HasChildren()) + { + RecursiveFillArray(currentNode.Child, ref array, ref index); + } + currentNode = currentNode.Previous; + } while (currentNode != oldNode); + } + /// + /// Recursively remove the node and its children from the heap. + /// + /// Node to be removed. + private void Remove(FibonacciNode node) + { + if(node != null) + { + FibonacciNode current = node; + do + { + Remove(current.Child); + if (current.Parent != null) + { + current.Parent.Child = null; + } + current = current.Next; + } while (current != node); + current.Next = current.Previous = current.Child = current.Parent = null; + } + } + /// + /// Merge two heaps into a larger heap. + /// + /// Root of first heap. + /// Root of second heap. + private FibonacciNode Merge(FibonacciNode node1, FibonacciNode node2) + { + if(node1 == null) + { + return node2; + } + if(node2 == null) + { + return node1; + } + if(comparator(node1.Value, node2.Value) > 0) + { + FibonacciNode temp = node1; + node1 = node2; + node2 = temp; + } + FibonacciNode node1Next = node1.Next; + FibonacciNode node2Prev = node2.Previous; + node1.Next = node2; + node2.Previous = node1; + node1Next.Previous = node2Prev; + node2Prev.Next = node1Next; + return node1; + } + /// + /// Adds child to the parent. + /// + /// Parent node to accept child. + /// Child node to be added to the parent. + private void AddChild(FibonacciNode parent, FibonacciNode child) + { + child.Previous = child.Next = child; + child.Parent = parent; + parent.Degree++; + parent.Child = Merge(parent.Child, child); + } + /// + /// Removes the parent of the specified node. + /// + /// Node to be removed from its parent. + private void RemoveParent(FibonacciNode node) + { + if(node == null) + { + return; + } + FibonacciNode current = node; + do + { + current.Marked = false; + current.Parent = null; + current = current.Next; + } while (current != node); + } + /// + /// Removes the minimum node from the provided tree. + /// + /// Root of the provided tree. + /// + private FibonacciNode RemoveMinimum(FibonacciNode node) + { + RemoveParent(node.Child); + if(node.Next == node) + { + node = node.Child; + } + else + { + node.Next.Previous = node.Previous; + node.Previous.Next = node.Next; + node = Merge(node.Next, node.Child); + } + if(node == null) + { + return node; + } + + FibonacciNode[] trees = new FibonacciNode[64]; + while (true) + { + if(trees[node.Degree] != null) + { + FibonacciNode t = trees[node.Degree]; + if(t == node) + { + break; + } + trees[node.Degree] = null; + if(comparator(node.Value, t.Value) < 0) + { + t.Previous.Next = t.Next; + t.Next.Previous = t.Previous; + AddChild(node, t); + } + else + { + t.Previous.Next = t.Next; + t.Next.Previous = t.Previous; + if(node.Next == node) + { + t.Next = t.Previous = t; + AddChild(t, node); + node = t; + } + else + { + node.Previous.Next = t; + node.Next.Previous = t; + t.Next = node.Next; + t.Previous = node.Previous; + AddChild(t, node); + node = t; + } + } + continue; + } + else + { + trees[node.Degree] = node; + } + node = node.Next; + } + FibonacciNode min = node; + FibonacciNode start = node; + do + { + if(comparator(node.Value, min.Value) < 0) + { + min = node; + } + node = node.Next; + } while (node != start); + return min; + } + /// + /// Cut node from heap. + /// + /// Root of heap. + /// Node to be cut. + /// + private FibonacciNode Cut(FibonacciNode root, FibonacciNode node) + { + if(node.Next == node) + { + node.Parent.Child = null; + } + else + { + node.Next.Previous = node.Previous; + node.Previous.Next = node.Next; + node.Parent.Child = node.Next; + } + node.Next = node.Previous = node; + node.Marked = false; + return Merge(root, node); + } + /// + /// Decrease value of provided node substituting it with the provided value. + /// + /// Root of the heap. + /// Node to have value decreased. + /// New value of the node. It is only applied if the value is lower than the previous value. + /// + private FibonacciNode DecreaseKey(FibonacciNode root, FibonacciNode node, T value) + { + if(comparator(node.Value, value) < 0) + { + return root; + } + node.Value = value; + if(node.Parent != null) + { + if(comparator(node.Value, node.Parent.Value) < 0) + { + root = Cut(root, node); + FibonacciNode parent = node.Parent; + node.Parent = null; + while(parent != null && parent.Marked) + { + root = Cut(root, parent); + node = parent; + parent = node.Parent; + node.Parent = null; + } + if(parent != null && parent.Parent != null) + { + parent.Marked = true; + } + } + } + else + { + if(comparator(node.Value, root.Value) < 0) + { + root = node; + } + } + return root; + } + /// + /// Find the node that has the specified value in the heap. + /// + /// Root of the heap. + /// Value to be found. + /// + private FibonacciNode Find(FibonacciNode root, T value) + { + FibonacciNode node = root; + if(node == null) + { + return null; + } + do + { + if(comparator(node.Value, value) == 0) + { + return node; + } + FibonacciNode ret = Find(node.Child, value); + if(ret != null) + { + return ret; + } + node = node.Next; + } while (node != root); + return null; + } + #endregion + #region IDisposable Support + private bool disposedValue = false; + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (root != null) + { + if (disposing) + { + Remove(root); + } + root.Previous = root.Next = root.Parent = root.Child = null; + root = null; + } + + disposedValue = true; + } + } + public void Dispose() + { + // Do not change this code. Put cleanup code in Dispose(bool disposing) above. + Dispose(true); + // TODO: uncomment the following line if the finalizer is overridden above. + // GC.SuppressFinalize(this); + } + #endregion + } + + internal class FibonacciNode + { + #region Fields + private FibonacciNode previous; + private FibonacciNode next; + private FibonacciNode child; + private FibonacciNode parent; + private T value; + private int degree; + private bool marked; + #endregion + #region Properties + public FibonacciNode Previous + { + get + { + return previous; + } + set + { + previous = value; + } + } + public FibonacciNode Next + { + get + { + return next; + } + set + { + next = value; + } + } + public FibonacciNode Child + { + get + { + return child; + } + set + { + child = value; + } + } + public FibonacciNode Parent + { + get + { + return parent; + } + set + { + parent = value; + } + } + public bool Marked + { + get + { + return marked; + } + set + { + marked = value; + } + } + public T Value + { + get + { + return value; + } + set + { + this.value = value; + } + } + public int Degree + { + get + { + return degree; + } + set + { + degree = value; + } + } + #endregion + #region Public Methods + public bool HasChildren() + { + return child != null; + } + public bool HasParent() + { + return parent != null; + } + #endregion + } +} diff --git a/SystemExtensions/Collections/PriorityQueue.cs b/SystemExtensions/Collections/PriorityQueue.cs index 998f892..88c698b 100644 --- a/SystemExtensions/Collections/PriorityQueue.cs +++ b/SystemExtensions/Collections/PriorityQueue.cs @@ -39,7 +39,7 @@ namespace SystemExtensions.Collections { if (Count > 0) { - return binaryHeap.DeleteMin(); + return binaryHeap.RemoveMin(); } else { diff --git a/SystemExtensions/Comparators.cs b/SystemExtensions/Comparators.cs new file mode 100644 index 0000000..a309a5b --- /dev/null +++ b/SystemExtensions/Comparators.cs @@ -0,0 +1,467 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SystemExtensions +{ + public static class Comparators + { + #region Fields + private static Comparison intComparison = new Comparison(IntegerComparison); + private static Comparison floatComparison = new Comparison(FloatComparison); + private static Comparison doubleComparison = new Comparison(DoubleComparison); + private static Comparison boolComparison = new Comparison(BoolComparison); + private static Comparison decimalComparison = new Comparison(DecimalComparison); + private static Comparison byteComparison = new Comparison(ByteComparison); + private static Comparison longComparison = new Comparison(LongComparison); + private static Comparison uintComparison = new Comparison(UintComparison); + private static Comparison int16Comparison = new Comparison(Int16Comparison); + private static Comparison int32Comparison = new Comparison(Int32Comparison); + private static Comparison int64Comparison = new Comparison(int64Comparison); + private static Comparison uInt16Comparison = new Comparison(uInt16Comparison); + private static Comparison uInt32Comparison = new Comparison(uInt32Comparison); + private static Comparison uInt64Comparison = new Comparison(uInt64Comparison); + #endregion + #region Properties + /// + /// Comparison between two ints. + /// + public static Comparison Int + { + get + { + return intComparison; + } + } + /// + /// Comparison between two floats. + /// + public static Comparison Float + { + get + { + return floatComparison; + } + } + /// + /// Comparison between two doubles. + /// + public static Comparison Double + { + get + { + return doubleComparison; + } + } + /// + /// Comparison between two decimals. + /// + public static Comparison Decimal + { + get + { + return decimalComparison; + } + } + /// + /// Comparison between two bools. + /// + public static Comparison Bool + { + get + { + return boolComparison; + } + } + /// + /// Comparison between two bytes. + /// + public static Comparison Byte + { + get + { + return byteComparison; + } + } + /// + /// Comparison between two longs. + /// + public static Comparison Long + { + get + { + return longComparison; + } + } + /// + /// Comparison between two uints. + /// + public static Comparison Uint + { + get + { + return uintComparison; + } + } + /// + /// Comparison between two int16. + /// + public static Comparison Int16 + { + get + { + return int16Comparison; + } + } + /// + /// Comparison between two int32. + /// + public static Comparison Int32 + { + get + { + return int32Comparison; + } + } + /// + /// Comparison between two int64. + /// + public static Comparison Int64 + { + get + { + return int64Comparison; + } + } + /// + /// Comparison between two uint16. + /// + public static Comparison UInt16 + { + get + { + return uInt16Comparison; + } + } + /// + /// Comparison between two uint32. + /// + public static Comparison UInt32 + { + get + { + return uInt32Comparison; + } + } + /// + /// Comparison between two uint64. + /// + public static Comparison UInt64 + { + get + { + return uInt64Comparison; + } + } + #endregion + #region Private Methods + /// + /// Compares two UInt64. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int Uint64Comparison(UInt64 x, UInt64 y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two UInt32. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int Uint32Comparison(UInt32 x, UInt32 y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two UInt16. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int Uint16Comparison(UInt16 x, UInt16 y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two Int64. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int Int64Comparison(Int64 x, Int64 y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two UInt32. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int Int32Comparison(Int32 x, Int32 y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two UInt16. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int Int16Comparison(Int16 x, Int16 y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two uint. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int UintComparison(uint x, uint y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two Long. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int LongComparison(long x, long y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two Byte. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int ByteComparison(byte x, byte y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two Decimal. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int DecimalComparison(decimal x, decimal y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two Bool. + /// + /// First value. + /// Second value. + /// 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. + private static int BoolComparison(bool x, bool y) + { + if (x == y) + { + return 0; + } + else if (x == true && y == false) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two Double. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int DoubleComparison(double x, double y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two Float. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int FloatComparison(float x, float y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + /// + /// Compares two Integer. + /// + /// First value. + /// Second value. + /// Returns 1 if x > y. 0 if equal and -1 if y > x. + private static int IntegerComparison(int x, int y) + { + if (x == y) + { + return 0; + } + else if (x > y) + { + return 1; + } + else + { + return -1; + } + } + #endregion + } +} diff --git a/SystemExtensions/SystemExtensions.csproj b/SystemExtensions/SystemExtensions.csproj index 0166dee..79f3be0 100644 --- a/SystemExtensions/SystemExtensions.csproj +++ b/SystemExtensions/SystemExtensions.csproj @@ -42,7 +42,9 @@ + + diff --git a/SystemExtensionsTests/Collections/BinaryHeapTests.cs b/SystemExtensionsTests/Collections/BinaryHeapTests.cs index cf04ab9..b994387 100644 --- a/SystemExtensionsTests/Collections/BinaryHeapTests.cs +++ b/SystemExtensionsTests/Collections/BinaryHeapTests.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SystemExtensions; namespace SystemExtensions.Collections.Tests { @@ -77,7 +78,7 @@ namespace SystemExtensions.Collections.Tests { Assert.Fail(); } - System.Diagnostics.Debug.WriteLine(binaryHeap.DeleteMin()); + System.Diagnostics.Debug.WriteLine(binaryHeap.RemoveMin()); tries--; } } diff --git a/SystemExtensionsTests/Collections/FibonacciHeapTests.cs b/SystemExtensionsTests/Collections/FibonacciHeapTests.cs new file mode 100644 index 0000000..814579a --- /dev/null +++ b/SystemExtensionsTests/Collections/FibonacciHeapTests.cs @@ -0,0 +1,203 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SystemExtensions.Collections; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Threading; + +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 fibonacciHeap = new FibonacciHeap(new Comparison(IntegerComparison)); + [TestMethod()] + public void InsertTest() + { + for (int i = 0; i < 1000; i++) + { + fibonacciHeap.Insert(i); + } + if (fibonacciHeap.RemoveMinimum() != 0) + { + Assert.Fail(); + } + for (int i = 1; i < 1000; i++) + { + if (!fibonacciHeap.Contains(i)) + { + Assert.Fail(); + } + } + } + + [TestMethod()] + public void FibonacciHeapTest() + { + fibonacciHeap.Dispose(); + fibonacciHeap = new FibonacciHeap(new Comparison(IntegerComparison)); + if(fibonacciHeap.Count != 0) + { + Assert.Fail(); + } + } + + [TestMethod()] + public void MergeTest() + { + fibonacciHeap.Dispose(); + FibonacciHeap fibonacciHeap1 = new FibonacciHeap(new Comparison(IntegerComparison)); + FibonacciHeap fibonacciHeap2 = new FibonacciHeap(new Comparison(IntegerComparison)); + + for(int i = 0; i < 100; i++) + { + fibonacciHeap1.Insert(i); + } + + for(int i = 100; i < 300; i++) + { + fibonacciHeap2.Insert(i); + } + + fibonacciHeap1.Merge(fibonacciHeap2); + for(int i = 1; i < 300; i++) + { + if (!fibonacciHeap1.Contains(i)) + { + Assert.Fail(); + } + } + } + + [TestMethod()] + public void RemoveMinimumTest() + { + try + { + fibonacciHeap.RemoveMinimum(); + Assert.Fail(); + } + catch(IndexOutOfRangeException exception) + { + + } + catch(Exception e) + { + Assert.Fail(); + } + for (int i = 0; i < 1000; i++) + { + fibonacciHeap.Insert(i); + } + for (int i = 0; i < 1000; i++) + { + if (fibonacciHeap.RemoveMinimum() != i) + { + Assert.Fail(); + } + } + } + + [TestMethod()] + public void DecreaseKeyTest() + { + fibonacciHeap.Insert(5); + fibonacciHeap.Insert(10); + fibonacciHeap.Insert(7); + fibonacciHeap.DecreaseKey(5, 3); + if(fibonacciHeap.Contains(5) || !fibonacciHeap.Contains(3)) + { + Assert.Fail(); + } + fibonacciHeap.DecreaseKey(10, 1); + if(fibonacciHeap.RemoveMinimum() != 1) + { + Assert.Fail(); + } + } + + [TestMethod()] + public void ContainsTest() + { + for(int i = 0; i < 10000; i++) + { + fibonacciHeap.Insert(i); + } + if (!fibonacciHeap.Contains(5124)) + { + Assert.Fail(); + } + } + + [TestMethod()] + public void ClearTest() + { + for(int i = 0; i < 10000; i++) + { + fibonacciHeap.Insert(i); + } + fibonacciHeap.RemoveMinimum(); + fibonacciHeap.Clear(); + if(fibonacciHeap.Count > 0) + { + Assert.Fail(); + } + } + + [TestMethod()] + public void DisposeTest() + { + List> heaps = new List>(); + for(int i = 0; i < 10; i++) + { + heaps.Add(new Collections.FibonacciHeap(new Comparison(IntegerComparison))); + for(int j = 0; j < 10000; j++) + { + heaps[heaps.Count - 1].Insert(j); + } + } + for(int i = 0; i < 10; i++) + { + heaps[i].Dispose(); + } + GC.Collect(); + heaps.Clear(); + } + + [TestMethod()] + public void ToArrayTest() + { + for(int i = 999; i >= 0; i--) + { + fibonacciHeap.Insert(i); + } + fibonacciHeap.RemoveMinimum(); + int[] arr = fibonacciHeap.ToArray(); + + for(int i = 1; i < 512; i++) + { + if(arr[i - 1] != i) + { + Assert.Fail(); + } + } + } + } +} \ No newline at end of file diff --git a/SystemExtensionsTests/Collections/PriorityQueueTests.cs b/SystemExtensionsTests/Collections/PriorityQueueTests.cs index 68802ba..22af644 100644 --- a/SystemExtensionsTests/Collections/PriorityQueueTests.cs +++ b/SystemExtensionsTests/Collections/PriorityQueueTests.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SystemExtensions; namespace SystemExtensions.Collections.Tests { diff --git a/SystemExtensionsTests/SystemExtensionsTests.csproj b/SystemExtensionsTests/SystemExtensionsTests.csproj index f8ff112..8165f6e 100644 --- a/SystemExtensionsTests/SystemExtensionsTests.csproj +++ b/SystemExtensionsTests/SystemExtensionsTests.csproj @@ -57,6 +57,7 @@ +