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.      //! Heap element.   class Element (mixed value)   { -  int pos; +  int pos = -1;       constant is_adt_heap_element = 1;       protected int `<(mixed other) { return value < other; }    protected int `>(mixed other) { return value > other; }   }      #define SWAP(X,Y) do{ mixed tmp=values[X]; (values[X]=values[Y])->pos = X; (values[Y]=tmp)->pos = Y; }while(0)      protected array(Element) values=allocate(10);
pike.git/lib/modules/ADT.pmod/Heap.pike:138:    adjust_down(pos);    }    verify_heap();    return ret;   }      //! Removes and returns the item on top of the heap,   //! which also is the smallest value in the heap.   mixed pop()   { -  mixed ret; +     if(!num_values)    error("Heap underflow!\n");    -  ret = values[0]->value; +  Element value = values[0]; +  value->pos = -1;    num_values--;    if(num_values)    {    (values[0] = values[num_values])->pos = 0;    adjust_down(0);       if(num_values * 3 + 10 < sizeof(values))    values=values[..num_values+10];    }    values[num_values]=0;    verify_heap(); -  return ret; +  return value->value;   }      //! Returns the number of elements in the heap.   int _sizeof() { return num_values; }      //! Removes and returns the item on top of the heap,   //! which also is the smallest value in the heap.   //! @deprecated pop   __deprecated__ mixed top() { return pop(); }   
pike.git/lib/modules/ADT.pmod/Heap.pike:197:   {    int pos;    if (objectp(value) && value->is_adt_heap_element) {    pos = value->pos;    } else {    pos = search(map(values, lambda(Element x) { return x && x->value; }),    value);    }    if ((pos < 0) || (pos >= num_values)) return;    +  value = values[pos];    values[pos] = values[--num_values];    values[num_values] = 0; -  +  value->pos = -1;    if (pos < num_values) {    if (!adjust_up(pos))    adjust_down(pos);    }       if(num_values * 3 + 10 < sizeof(values))    values=values[..num_values+10];       verify_heap();   }