Branch: Tag:

2015-11-17

2015-11-17 14:07:12 by Henrik Grubbström (Grubba) <grubba@grubba.org>

ADT.Heap: Improved robustness.

Make sure to invalidate Element()->pos for elements not on the Heap.

Potential fix for [bug 7555 (#7555):5].

7:   //! Heap element.   class Element (mixed value)   { -  int pos; +  int pos = -1;       constant is_adt_heap_element = 1;   
150:   //! @[peek()], @[push()], @[remove()]   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)    {
166:    }    values[num_values]=0;    verify_heap(); -  return ret; +  return value->value;   }      //! Returns the number of elements in the heap.
215:    }    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);