Improve assertions in gh_heap

* Assert index matches any time a node is accessed by index, subsuming
  the assertion from #2139.
* Ensure all removed nodes are invalidated, so double-removes will
  trigger assertion failure. Added a test for this.
This commit is contained in:
dmiller 2022-07-29 15:21:13 +00:00
parent 10c4479b2d
commit 1c9e1ddbcb
2 changed files with 11 additions and 9 deletions

View file

@ -70,7 +70,9 @@
static gh_hnode_t **hnode_ptr(gh_heap_t *heap, unsigned int index) {
assert(index <= heap->count);
return &(heap->slots[index]);
gh_hnode_t **ptr = &(heap->slots[index]);
assert(index == heap->count || (*ptr)->index == index);
return ptr;
}
gh_hnode_t *gh_heap_find(gh_heap_t *heap, unsigned int index) {
@ -94,7 +96,6 @@ static int hnode_up(gh_heap_t *heap, gh_hnode_t *hnode)
parent_idx = (cur_idx - 1) >> 1;
parent_ptr = hnode_ptr(heap, parent_idx);
assert((*parent_ptr)->index == parent_idx);
if (heap->cmp_op(*parent_ptr, hnode))
break;
@ -235,13 +236,13 @@ int gh_heap_remove(gh_heap_t *heap, gh_hnode_t *hnode)
count--;
last = *hnode_ptr(heap, count);
heap->count = count;
if (last == hnode)
return 0;
last->index = cur_idx;
*cur_ptr = last;
if (!hnode_up(heap, *cur_ptr))
hnode_down(heap, *cur_ptr);
if (last != hnode)
{
last->index = cur_idx;
*cur_ptr = last;
if (!hnode_up(heap, *cur_ptr))
hnode_down(heap, *cur_ptr);
}
gh_hnode_invalidate(hnode);
return 0;

View file

@ -63,6 +63,7 @@ static int ghheap_ordering(void *tdata) {
gh_hnode_t *current;
current = gh_heap_pop(&heap);
assert(!gh_hnode_is_valid(current));
k = node2int(current);
if (k < n)