void SwapWithParent(int i)
        {
            HeapElem parent = A[i >> 1];

            putAtI(i >> 1, A[i]);
            putAtI(i, parent);
        }
        internal void insert(int o, int priority)
        {
            //System.Diagnostics.Debug.WriteLine("insert "+ o.ToString() + " with pr "+ priority.ToString());

            heapSize++;
            int      i = heapSize;
            HeapElem h;

            if (cache[o] != null)
            {
                throw new InvalidOperationException("the element already in the queue");
            }
            A[i] = cache[o] = h = new HeapElem(i, priority, o);
            while (i > 1 && A[i >> 1].priority > priority)
            {
                SwapWithParent(i);
                i >>= 1;
            }
            A[i] = h;
        }
        /// <summary>
        /// sets the object priority to c
        /// </summary>
        internal void decrease_priority(int o, int newPriority)
        {
            //System.Diagnostics.Debug.WriteLine("delcrease "+ o.ToString()+" to "+ newPriority.ToString());

            HeapElem h = cache[o];

            h.priority = newPriority;
            int i = h.indexToA;

            while (i > 1)
            {
                if (A[i].priority < A[i >> 1].priority)
                {
                    SwapWithParent(i);
                }
                else
                {
                    break;
                }
                i >>= 1;
            }
        }
 void putAtI(int i,HeapElem h)
 {
     A[i]=h;
     h.indexToA=i;
 }
        internal void insert(int o, int priority)
        {
            //System.Diagnostics.Debug.WriteLine("insert "+ o.ToString() + " with pr "+ priority.ToString());

            heapSize++;
            int i=heapSize;
            HeapElem h;
            if(cache[o]!=null)
            {
                throw new InvalidOperationException("the element already in the queue");
            }
            A[i]=cache[o]=h=new HeapElem(i,priority,o);
            while(i>1 && A[i>>1].priority>priority)
            {
                SwapWithParent(i);
                i>>=1;
            }
            A[i]=h;
        }
 void putAtI(int i, HeapElem h)
 {
     A[i]       = h;
     h.indexToA = i;
 }