pike.git / lib / modules / ADT.pmod / Heap.pike

version» Context lines:

pike.git/lib/modules/ADT.pmod/Heap.pike:1:   #pike __REAL_VERSION__    + //! This class implements a (min-)heap. The value of a child node will + //! always be greater than or equal to the value of its parent node. + //! Thus, the top node of the heap will always hold the smallest value. +    #define SWAP(X,Y) do{ mixed tmp=values[X]; values[X]=values[Y]; values[Y]=tmp; }while(0)      static private array values=allocate(10);   static private int num_values;      #ifdef DEBUG   void verify_heap()   {    for(int e=1; e<num_values; e++)    if(values[(e-1)/2] > values[e])
pike.git/lib/modules/ADT.pmod/Heap.pike:59:    {    SWAP(elem, parent);    elem=parent;    }    adjust_down(elem);    return 1;    }    return 0;   }    - //! @fixme - //! Document this function + //! Push an element onto the heap. The heap will automatically sort itself + //! so that the smallest value will be at the top.   void push(mixed value)   {    if(num_values >= sizeof(values))    values+=allocate(10+sizeof(values)/4);       values[num_values++]=value;    adjust_up(num_values-1);    verify_heap();   }    - //! @fixme - //! Document this function + //! Takes a value in the heap and sorts it through the heap to maintain + //! its sort criteria (increasing order).   void adjust(mixed value)   {    int pos=search(values, value);    if(pos>=0)    if(!adjust_up(pos))    adjust_down(pos);    verify_heap();   }      //! Removes and returns the item on top of the heap,
pike.git/lib/modules/ADT.pmod/Heap.pike:109:    values=values[..num_values+10];    }    verify_heap();    return ret;   }      //! Returns the number of elements in the heap.   int _sizeof() { return num_values; }      // compat + //! Removes and returns the item on top of the heap, + //! which also is the smallest value in the heap.   mixed top() { return pop(); } -  +  + // compat + //! Returns the number of elements in the heap.   int size() { return _sizeof(); } -  +  + //! Returns the item on top of the heap (which is also the smallest value + //! in the heap) without removing it. + mixed peek() + { +  if (!num_values) +  return UNDEFINED; +  +  return values[0]; + }