Commit 7690a8a5 authored by Pekka Pessi's avatar Pekka Pessi

sofia-sip/heap.h: heap_remove() now set()s index to 0 on removed item

darcs-hash:20070713163844-65a35-509a6b4df8e13b039fbbc44bc368e026f3a4e8eb.gz
parent 5252dc0c
......@@ -240,20 +240,18 @@ scope type prefix##remove(heaptype h, size_t index) \
struct prefix##priv { size_t _size, _used; type _heap[1];}; \
struct prefix##priv *_priv = *(void **)&h; \
type *heap = _priv->_heap - 1; \
type retval; \
type retval[1]; \
type e; \
\
size_t top, left, right, move; \
\
move = _priv->_used; \
\
if (index - 1 >= _priv->_used) \
return (null); \
\
move = _priv->_used--; \
retval = heap[top = index]; \
set(retval, 0, heap[index]); \
\
for (;;) { \
for (top = index;;index = top) { \
left = 2 * top; \
right = 2 * top + 1; \
\
......@@ -264,11 +262,10 @@ scope type prefix##remove(heaptype h, size_t index) \
else \
top = left; \
set(heap, index, heap[top]); \
index = top; \
} \
\
if (index == move) \
return retval; \
return *retval; \
\
e = heap[move]; \
for (; index > 1; index = top) { \
......@@ -280,7 +277,7 @@ scope type prefix##remove(heaptype h, size_t index) \
\
set(heap, index, e); \
\
return retval; \
return *retval; \
} \
\
scope \
......
......@@ -58,7 +58,6 @@ int less1(type1 a, type1 b)
static inline
void set1(type1 *heap, size_t index, type1 e)
{
assert(index > 0);
e.index = index;
heap[index] = e;
}
......@@ -74,7 +73,6 @@ int less2(type2 a, type2 b)
static inline
void set2(type2 *heap, size_t index, type2 e)
{
assert(index > 0);
e->index = index;
heap[index] = e;
}
......@@ -145,7 +143,7 @@ int test_smooth_sort()
_set = 0;
write(1, ".", 1);
/* write(1, ".", 1); */
test_sort(array, 0, n);
......@@ -165,7 +163,7 @@ int test_smooth_sort()
array[i] = (int)(n - i - 1);
}
write(1, "/", 1);
/* write(1, "/", 1); */
test_sort(array, 0, n / 2);
test_sort(array, n / 2, n / 2);
......@@ -236,7 +234,8 @@ int test_value()
tests[e.value] |= 4;
previous = e.key;
TEST(heap1_remove(heap, 1).index, 1);
TEST(heap1_get(heap, 1).index, 1);
TEST(heap1_remove(heap, 1).index, 0);
}
TEST(n, N);
......@@ -266,7 +265,8 @@ int test_value()
e = heap1_get(heap, n);
TEST(e.index, n);
TEST(tests[e.value] & 8, 0); tests[e.value] |= 8;
TEST(heap1_remove(heap, n).index, n);
TEST(heap1_get(heap, n).index, n);
TEST(heap1_remove(heap, n).index, 0);
}
for (i = 1; i <= heap1_used(heap); i++) {
......@@ -285,7 +285,8 @@ int test_value()
tests[e.value] |= 8;
TEST_1(previous <= e.key);
previous = e.key;
TEST(heap1_remove(heap, 1).index, 1);
TEST(heap1_get(heap, 1).index, 1);
TEST(heap1_remove(heap, 1).index, 0);
}
for (i = 1; i <= N; i++) {
......@@ -445,7 +446,7 @@ int test_ref()
/* Remove rest */
for (n = 0, previous = 0; heap2_used(heap) > 0; n++) {
type2 e = heap2_remove(heap, 1);
TEST_1(e); TEST(e->index, 1);
TEST_1(e); TEST(e->index, 0);
TEST(tests[e->value] & 8, 0);
tests[e->value] |= 8;
TEST_1(previous <= e->key);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment