Blame src/heap-inl.h

Packit b5b901
/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
Packit b5b901
 *
Packit b5b901
 * Permission to use, copy, modify, and/or distribute this software for any
Packit b5b901
 * purpose with or without fee is hereby granted, provided that the above
Packit b5b901
 * copyright notice and this permission notice appear in all copies.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Packit b5b901
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Packit b5b901
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Packit b5b901
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Packit b5b901
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Packit b5b901
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Packit b5b901
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
#ifndef UV_SRC_HEAP_H_
Packit b5b901
#define UV_SRC_HEAP_H_
Packit b5b901
Packit b5b901
#include <stddef.h>  /* NULL */
Packit b5b901
Packit b5b901
#if defined(__GNUC__)
Packit b5b901
# define HEAP_EXPORT(declaration) __attribute__((unused)) static declaration
Packit b5b901
#else
Packit b5b901
# define HEAP_EXPORT(declaration) static declaration
Packit b5b901
#endif
Packit b5b901
Packit b5b901
struct heap_node {
Packit b5b901
  struct heap_node* left;
Packit b5b901
  struct heap_node* right;
Packit b5b901
  struct heap_node* parent;
Packit b5b901
};
Packit b5b901
Packit b5b901
/* A binary min heap.  The usual properties hold: the root is the lowest
Packit b5b901
 * element in the set, the height of the tree is at most log2(nodes) and
Packit b5b901
 * it's always a complete binary tree.
Packit b5b901
 *
Packit b5b901
 * The heap function try hard to detect corrupted tree nodes at the cost
Packit b5b901
 * of a minor reduction in performance.  Compile with -DNDEBUG to disable.
Packit b5b901
 */
Packit b5b901
struct heap {
Packit b5b901
  struct heap_node* min;
Packit b5b901
  unsigned int nelts;
Packit b5b901
};
Packit b5b901
Packit b5b901
/* Return non-zero if a < b. */
Packit b5b901
typedef int (*heap_compare_fn)(const struct heap_node* a,
Packit b5b901
                               const struct heap_node* b);
Packit b5b901
Packit b5b901
/* Public functions. */
Packit b5b901
HEAP_EXPORT(void heap_init(struct heap* heap));
Packit b5b901
HEAP_EXPORT(struct heap_node* heap_min(const struct heap* heap));
Packit b5b901
HEAP_EXPORT(void heap_insert(struct heap* heap,
Packit b5b901
                             struct heap_node* newnode,
Packit b5b901
                             heap_compare_fn less_than));
Packit b5b901
HEAP_EXPORT(void heap_remove(struct heap* heap,
Packit b5b901
                             struct heap_node* node,
Packit b5b901
                             heap_compare_fn less_than));
Packit b5b901
HEAP_EXPORT(void heap_dequeue(struct heap* heap, heap_compare_fn less_than));
Packit b5b901
Packit b5b901
/* Implementation follows. */
Packit b5b901
Packit b5b901
HEAP_EXPORT(void heap_init(struct heap* heap)) {
Packit b5b901
  heap->min = NULL;
Packit b5b901
  heap->nelts = 0;
Packit b5b901
}
Packit b5b901
Packit b5b901
HEAP_EXPORT(struct heap_node* heap_min(const struct heap* heap)) {
Packit b5b901
  return heap->min;
Packit b5b901
}
Packit b5b901
Packit b5b901
/* Swap parent with child. Child moves closer to the root, parent moves away. */
Packit b5b901
static void heap_node_swap(struct heap* heap,
Packit b5b901
                           struct heap_node* parent,
Packit b5b901
                           struct heap_node* child) {
Packit b5b901
  struct heap_node* sibling;
Packit b5b901
  struct heap_node t;
Packit b5b901
Packit b5b901
  t = *parent;
Packit b5b901
  *parent = *child;
Packit b5b901
  *child = t;
Packit b5b901
Packit b5b901
  parent->parent = child;
Packit b5b901
  if (child->left == child) {
Packit b5b901
    child->left = parent;
Packit b5b901
    sibling = child->right;
Packit b5b901
  } else {
Packit b5b901
    child->right = parent;
Packit b5b901
    sibling = child->left;
Packit b5b901
  }
Packit b5b901
  if (sibling != NULL)
Packit b5b901
    sibling->parent = child;
Packit b5b901
Packit b5b901
  if (parent->left != NULL)
Packit b5b901
    parent->left->parent = parent;
Packit b5b901
  if (parent->right != NULL)
Packit b5b901
    parent->right->parent = parent;
Packit b5b901
Packit b5b901
  if (child->parent == NULL)
Packit b5b901
    heap->min = child;
Packit b5b901
  else if (child->parent->left == parent)
Packit b5b901
    child->parent->left = child;
Packit b5b901
  else
Packit b5b901
    child->parent->right = child;
Packit b5b901
}
Packit b5b901
Packit b5b901
HEAP_EXPORT(void heap_insert(struct heap* heap,
Packit b5b901
                             struct heap_node* newnode,
Packit b5b901
                             heap_compare_fn less_than)) {
Packit b5b901
  struct heap_node** parent;
Packit b5b901
  struct heap_node** child;
Packit b5b901
  unsigned int path;
Packit b5b901
  unsigned int n;
Packit b5b901
  unsigned int k;
Packit b5b901
Packit b5b901
  newnode->left = NULL;
Packit b5b901
  newnode->right = NULL;
Packit b5b901
  newnode->parent = NULL;
Packit b5b901
Packit b5b901
  /* Calculate the path from the root to the insertion point.  This is a min
Packit b5b901
   * heap so we always insert at the left-most free node of the bottom row.
Packit b5b901
   */
Packit b5b901
  path = 0;
Packit b5b901
  for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2)
Packit b5b901
    path = (path << 1) | (n & 1);
Packit b5b901
Packit b5b901
  /* Now traverse the heap using the path we calculated in the previous step. */
Packit b5b901
  parent = child = &heap->min;
Packit b5b901
  while (k > 0) {
Packit b5b901
    parent = child;
Packit b5b901
    if (path & 1)
Packit b5b901
      child = &(*child)->right;
Packit b5b901
    else
Packit b5b901
      child = &(*child)->left;
Packit b5b901
    path >>= 1;
Packit b5b901
    k -= 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  /* Insert the new node. */
Packit b5b901
  newnode->parent = *parent;
Packit b5b901
  *child = newnode;
Packit b5b901
  heap->nelts += 1;
Packit b5b901
Packit b5b901
  /* Walk up the tree and check at each node if the heap property holds.
Packit b5b901
   * It's a min heap so parent < child must be true.
Packit b5b901
   */
Packit b5b901
  while (newnode->parent != NULL && less_than(newnode, newnode->parent))
Packit b5b901
    heap_node_swap(heap, newnode->parent, newnode);
Packit b5b901
}
Packit b5b901
Packit b5b901
HEAP_EXPORT(void heap_remove(struct heap* heap,
Packit b5b901
                             struct heap_node* node,
Packit b5b901
                             heap_compare_fn less_than)) {
Packit b5b901
  struct heap_node* smallest;
Packit b5b901
  struct heap_node** max;
Packit b5b901
  struct heap_node* child;
Packit b5b901
  unsigned int path;
Packit b5b901
  unsigned int k;
Packit b5b901
  unsigned int n;
Packit b5b901
Packit b5b901
  if (heap->nelts == 0)
Packit b5b901
    return;
Packit b5b901
Packit b5b901
  /* Calculate the path from the min (the root) to the max, the left-most node
Packit b5b901
   * of the bottom row.
Packit b5b901
   */
Packit b5b901
  path = 0;
Packit b5b901
  for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2)
Packit b5b901
    path = (path << 1) | (n & 1);
Packit b5b901
Packit b5b901
  /* Now traverse the heap using the path we calculated in the previous step. */
Packit b5b901
  max = &heap->min;
Packit b5b901
  while (k > 0) {
Packit b5b901
    if (path & 1)
Packit b5b901
      max = &(*max)->right;
Packit b5b901
    else
Packit b5b901
      max = &(*max)->left;
Packit b5b901
    path >>= 1;
Packit b5b901
    k -= 1;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  heap->nelts -= 1;
Packit b5b901
Packit b5b901
  /* Unlink the max node. */
Packit b5b901
  child = *max;
Packit b5b901
  *max = NULL;
Packit b5b901
Packit b5b901
  if (child == node) {
Packit b5b901
    /* We're removing either the max or the last node in the tree. */
Packit b5b901
    if (child == heap->min) {
Packit b5b901
      heap->min = NULL;
Packit b5b901
    }
Packit b5b901
    return;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  /* Replace the to be deleted node with the max node. */
Packit b5b901
  child->left = node->left;
Packit b5b901
  child->right = node->right;
Packit b5b901
  child->parent = node->parent;
Packit b5b901
Packit b5b901
  if (child->left != NULL) {
Packit b5b901
    child->left->parent = child;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (child->right != NULL) {
Packit b5b901
    child->right->parent = child;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  if (node->parent == NULL) {
Packit b5b901
    heap->min = child;
Packit b5b901
  } else if (node->parent->left == node) {
Packit b5b901
    node->parent->left = child;
Packit b5b901
  } else {
Packit b5b901
    node->parent->right = child;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  /* Walk down the subtree and check at each node if the heap property holds.
Packit b5b901
   * It's a min heap so parent < child must be true.  If the parent is bigger,
Packit b5b901
   * swap it with the smallest child.
Packit b5b901
   */
Packit b5b901
  for (;;) {
Packit b5b901
    smallest = child;
Packit b5b901
    if (child->left != NULL && less_than(child->left, smallest))
Packit b5b901
      smallest = child->left;
Packit b5b901
    if (child->right != NULL && less_than(child->right, smallest))
Packit b5b901
      smallest = child->right;
Packit b5b901
    if (smallest == child)
Packit b5b901
      break;
Packit b5b901
    heap_node_swap(heap, child, smallest);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  /* Walk up the subtree and check that each parent is less than the node
Packit b5b901
   * this is required, because `max` node is not guaranteed to be the
Packit b5b901
   * actual maximum in tree
Packit b5b901
   */
Packit b5b901
  while (child->parent != NULL && less_than(child, child->parent))
Packit b5b901
    heap_node_swap(heap, child->parent, child);
Packit b5b901
}
Packit b5b901
Packit b5b901
HEAP_EXPORT(void heap_dequeue(struct heap* heap, heap_compare_fn less_than)) {
Packit b5b901
  heap_remove(heap, heap->min, less_than);
Packit b5b901
}
Packit b5b901
Packit b5b901
#undef HEAP_EXPORT
Packit b5b901
Packit b5b901
#endif  /* UV_SRC_HEAP_H_ */