Exemple #1
0
        private void Up(int index)
        {
            if (index == 0)
            {
                return;
            }
            int parentIndex = (index - 1) / 2;

            if (m_comparer.Compare(m_array[parentIndex].Key, m_array[index].Key) <= 0)
            {
                return;
            }

            HeapItem swap = m_array[index];

            while (true)
            {
                MoveItem(parentIndex, index);
                index = parentIndex;

                if (index == 0)
                {
                    break;
                }
                parentIndex = (index - 1) / 2;
                if (m_comparer.Compare(m_array[parentIndex].Key, swap.Key) <= 0)
                {
                    break;
                }
            }

            MoveItem(ref swap, index);
            return;
        }
Exemple #2
0
        private void Reallocate()
        {
            HeapItem[] newArray = new HeapItem[m_capacity * 2];
            Array.Copy(m_array, newArray, m_capacity);

            m_array     = newArray;
            m_capacity *= 2;
        }
Exemple #3
0
        public void Insert(V value, K key)
        {
            if (m_count == m_capacity)
            {
                Reallocate();
            }

            HeapItem item = new HeapItem()
            {
                Key   = key,
                Value = value
            };

            m_array[m_count] = item;

            Up(m_count);
            m_count++;
        }
Exemple #4
0
        private void Down(int index)
        {
            if (m_count == index + 1)
            {
                return;
            }

            int left  = index * 2 + 1;
            int right = left + 1;

            HeapItem swap = m_array[index];

            while (right <= m_count)                                                                   // While the current node has children
            {
                if (right == m_count || m_comparer.Compare(m_array[left].Key, m_array[right].Key) < 0) // Only the left child exists or the left child is smaller
                {
                    if (m_comparer.Compare(swap.Key, m_array[left].Key) <= 0)
                    {
                        break;
                    }

                    MoveItem(left, index);

                    index = left;
                    left  = index * 2 + 1;
                    right = left + 1;
                }
                else // Right child exists and is smaller
                {
                    if (m_comparer.Compare(swap.Key, m_array[right].Key) <= 0)
                    {
                        break;
                    }

                    MoveItem(right, index);

                    index = right;
                    left  = index * 2 + 1;
                    right = left + 1;
                }
            }

            MoveItem(ref swap, index);
        }
Exemple #5
0
 private void MoveItem(ref HeapItem fromItem, int toIndex)
 {
     m_array[toIndex] = fromItem;
 }
Exemple #6
0
 private void MoveItem(HeapItem <K> fromItem, int toIndex)
 {
     m_array[toIndex]           = fromItem;
     m_array[toIndex].HeapIndex = toIndex;
 }