return heap->root->element;
}
+int
+next_power_of_2(int v)
+{
+ v |= v >> 1;
+ v |= v >> 2;
+ v |= v >> 4;
+ v |= v >> 8;
+ v |= v >> 16;
+ v++;
+ return v;
+}
void
internal_print (struct GNUNET_CONTAINER_heap_node *root)
struct GNUNET_CONTAINER_heap_node *ret;
struct GNUNET_CONTAINER_heap_node *parent;
int pos;
- int depth;
int i;
ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_heap_node));
pos = root->size + 1;
- depth = (int) log2 (pos);
ret->left_child = NULL;
ret->right_child = NULL;
- if (depth == 0)
+ if (0 == root->size)
{
ret->parent = NULL;
root->root = ret;
else
{
parent = root->root;
- for (i = depth; i > 1; i--)
+ for (i = next_power_of_2(pos) >> 2; i > 1; i >>= 1)
{
- if (((pos / (1 << (i - 1))) % 2) == 0)
+ if (((pos / i) % 2) == 0)
parent = parent->left_child;
else
parent = parent->right_child;
getPos (struct GNUNET_CONTAINER_Heap *root, unsigned int pos)
{
struct GNUNET_CONTAINER_heap_node *ret;
-
- int depth;
int i;
- depth = (int) log2 (pos);
ret = NULL;
if (pos > root->size)
{
else
{
ret = root->root;
- for (i = depth; i > 0; i--)
+ for (i = next_power_of_2(pos) >> 2; i > 0; i >>= 1)
{
- if (((pos / (1 << (i - 1))) % 2) == 0)
+ if (((pos / i) % 2) == 0)
ret = ret->left_child;
else
ret = ret->right_child;