private static IEnumerable <TSource> ConsumeHeap <TSource>(this IHeap <TSource> heap)
 {
     while (heap.Count > 0)
     {
         yield return(heap.Remove());
     }
 }
Beispiel #2
0
        public void SizeIsZeroAfterHeapBecomesEmpty()
        {
            IHeap <int> heap = NewHeapInstance <int>();

            heap.Insert(5);
            heap.Remove();
            Assert.AreEqual(0, heap.Size);
        }
Beispiel #3
0
        public void EmptyFlagTrueAfterHeapBecomesEmpty()
        {
            IHeap <int> heap = NewHeapInstance <int>();

            heap.Insert(5);
            heap.Remove();
            Assert.IsTrue(heap.IsEmpty);
        }
        private static IEnumerable <TSource> OrderByThenTakeImpl <TSource, TKey>(IEnumerable <TSource> source, int count, IHeap <TSource> heap)
        {
            foreach (var item in source)
            {
                heap.Add(item);
                if (heap.Count > count)
                {
                    // Remove minimum/maximum value
                    heap.Remove();
                }
            }

            return(heap.ConsumeHeap().Reverse());
        }
Beispiel #5
0
        public void TestInsertionAndRemoval()
        {
            var test_vectors = new[]
            {
                new
                {
                    m_initial_capacity       = 10,
                    m_objects_to_insert      = new int[] { },
                    m_remove_count           = 7,
                    m_more_objects_to_insert = new int[] { }
                },

                new
                {
                    m_initial_capacity       = 10,
                    m_objects_to_insert      = new int[] { 10 },
                    m_remove_count           = 6,
                    m_more_objects_to_insert = new int[] { }
                },

                new
                {
                    m_initial_capacity       = 5,
                    m_objects_to_insert      = new int[] { 0, -10, 100, 4, 5, 3, -5, 0 },
                    m_remove_count           = 5,
                    m_more_objects_to_insert = new int[] { -90, 100, 34, 3, -10, 0 }
                },

                new
                {
                    m_initial_capacity       = 3,
                    m_objects_to_insert      = new int[] { -10, 30, 50, 74, 120, 80, 90, 80, 85, -50, 35, 55, 130 },
                    m_remove_count           = 7,
                    m_more_objects_to_insert = new int[] { 25, -25, 74, 90, 55, -10 }
                },

                new
                {
                    m_initial_capacity       = 4,
                    m_objects_to_insert      = new int[] { -19, 20, 34, 3, -2, 21, 100, -150, 49, 27, -5 },
                    m_remove_count           = 20,
                    m_more_objects_to_insert = new int[] { -19, 33, -9, 18, 100, 17, -2, 27 }
                }
            };

            // The list of comparators
            Comparison <int>[] comparators =
            {
                null,

                // Orders integers in ascending order
                (x, y) =>
                {
                    if (x == y)
                    {
                        return(0);
                    }
                    else if (x < y)
                    {
                        return(-1);
                    }
                    else
                    {
                        return(1);
                    }
                },

                // Orders integers in descending order
                (x, y) =>
                {
                    if (x == y)
                    {
                        return(0);
                    }
                    else if (x < y)
                    {
                        return(1);
                    }
                    else
                    {
                        return(-1);
                    }
                }
            };

            foreach (var comparator in comparators)
            {
                foreach (var test_vector in test_vectors)
                {
                    IHeap <int> heap = NewHeapInstance(test_vector.m_initial_capacity, comparator);

                    // Populate the heap
                    List <int> reference_data = new List <int>(test_vector.m_objects_to_insert.Length);
                    foreach (var value in test_vector.m_objects_to_insert)
                    {
                        heap.Insert(value);
                        reference_data.Add(value);
                    }

                    Assert.AreEqual(reference_data.Count, heap.Size);

                    // Sort the reference data array in the specified order
                    if (comparator != null)
                    {
                        reference_data.Sort(comparator);
                    }
                    else
                    {
                        reference_data.Sort();
                    }

                    // Try removing the specified number of elements from the heap
                    for (int i = 0; i < test_vector.m_remove_count; ++i)
                    {
                        if (heap.IsEmpty)
                        {
                            Assert.Throws <IndexOutOfRangeException>(() => heap.Remove());
                            continue;
                        }

                        Assert.AreEqual(reference_data[0], heap.Remove());
                        reference_data.RemoveAt(0);
                        Assert.AreEqual(reference_data.Count, heap.Size);
                    }

                    Assert.AreEqual(reference_data.Count, heap.Size);
                    Assert.IsTrue(heap.Size == 0 && heap.IsEmpty || heap.Size > 0 && !heap.IsEmpty);

                    // Insert more objects
                    foreach (var value in test_vector.m_more_objects_to_insert)
                    {
                        heap.Insert(value);
                        reference_data.Add(value);
                    }

                    // Sort the reference data array in the specified order
                    if (comparator != null)
                    {
                        reference_data.Sort(comparator);
                    }
                    else
                    {
                        reference_data.Sort();
                    }

                    while (!heap.IsEmpty)
                    {
                        Assert.AreEqual(reference_data[0], heap.Remove());
                        reference_data.RemoveAt(0);
                    }

                    Assert.AreEqual(reference_data.Count, heap.Size);
                    Assert.IsTrue(heap.Size == 0 && heap.IsEmpty || heap.Size > 0 && !heap.IsEmpty);
                }
            }
        }
Beispiel #6
0
        public void ExceptionThrownIfRemovingFromEmptyHeap()
        {
            IHeap <int> heap = NewHeapInstance <int>();

            Assert.Throws <IndexOutOfRangeException>(() => heap.Remove());
        }