Beispiel #1
0
  // Remove a timer from the heap and list of timers.
  void remove_timer(per_timer_data& timer)
  {
    // Remove the timer from the heap.
    std::size_t index = timer.heap_index_;
    if (!heap_.empty() && index < heap_.size())
    {
      if (index == heap_.size() - 1)
      {
        heap_.pop_back();
      }
      else
      {
        swap_heap(index, heap_.size() - 1);
        heap_.pop_back();
        if (index > 0 && Time_Traits::less_than(
              heap_[index].time_, heap_[(index - 1) / 2].time_))
          up_heap(index);
        else
          down_heap(index);
      }
    }

    // Remove the timer from the linked list of active timers.
    if (timers_ == &timer)
      timers_ = timer.next_;
    if (timer.prev_)
      timer.prev_->next_ = timer.next_;
    if (timer.next_)
      timer.next_->prev_= timer.prev_;
    timer.next_ = 0;
    timer.prev_ = 0;
  }
Beispiel #2
0
void* heap_pop_min_data(heap_t* heap, long long* key) {
  node_t* top;
  node_t* last;
  void* data;
  long long keyval;
  
  if (is_empty(heap)) {return NULL;}

  top = (node_t*)heap->top;
  last = (node_t*)heap->last;
  
  data = top->data;
  keyval = top->key;
  
  swap(top, last);
  heap_remove_last_element(heap);

  if (heap->top != NULL) {
    down_heap(heap->top);
    heap->size--;
  }
  else {
    heap->size = 0;
  }

  if (key != NULL) {*key = keyval;}
  return data;
}
Beispiel #3
0
void down_heap(heap_t* heap, int findex) {
  int right_index =  ((findex + 1) * 2);
  int left_index = (findex * 2) + 1;

  if (right_index < heap->size) { // Two children
    long long int left_key = HEAP_NODE(heap, left_index).key;
    long long int right_key = HEAP_NODE(heap, right_index).key;
    int min_key_index = (left_key < right_key)? left_index : right_index;

    if (HEAP_NODE(heap, min_key_index).key < HEAP_NODE(heap, findex).key) {
      swap(&(HEAP_NODE(heap, findex)), &(HEAP_NODE(heap, min_key_index)));
      down_heap(heap, min_key_index);
    }
  }
  else if (left_index >= heap->size) { // No children
    return;
  }
  else { // Only left child
    long long int left_key = HEAP_NODE(heap, left_index).key;
    if (left_key < HEAP_NODE(heap, findex).key) {
      swap(&(HEAP_NODE(heap, findex)), &(HEAP_NODE(heap, left_index)));
      return;
    }
  }
}
oct_node pop_heap(node_heap *h)
{
	if (h->n <= 1) return 0;
 
	oct_node ret = h->buf[1];
	h->buf[1] = h->buf[--h->n];
 
	h->buf[h->n] = 0;
 
	h->buf[1]->heap_idx = 1;
	down_heap(h, h->buf[1]);
 
	return ret;
}
Beispiel #5
0
void* heap_pop_min_data(heap_t* heap, long long int* key) {
  int last_index = heap->size - 1;
  void* data = HEAP_NODE(heap, 0).data;
  if (key != NULL) {
    *key = HEAP_NODE(heap, 0).key;
  }
  HEAP_NODE(heap, 0).data = HEAP_NODE(heap, last_index).data;
  HEAP_NODE(heap, 0).key = HEAP_NODE(heap, last_index).key;

  heap->size--;

  down_heap(heap, 0);

  return data;
}
Beispiel #6
0
    void pop() {
      value_type tmp = c.back();
      c.back() = c.front();
      c.front() = tmp;

      size_type id_f = id[c.back()];
      size_type id_b = id[tmp];
      size_type i = index_array[ id_b ];
      index_array[ id_b ] = index_array[ id_f ];
      index_array[ id_f ] = i;

      c.pop_back();
      Node node(c.begin(), c.end(), c.begin(), id);
      down_heap(node, comp, index_array);       
    }
void heap_add(node_heap *h, oct_node p)
{
	if ((p->flags & ON_INHEAP)) {
		down_heap(h, p);
		up_heap(h, p);
		return;
	}
 
	p->flags |= ON_INHEAP;
	if (!h->n) h->n = 1;
	if (h->n >= h->alloc) {
		while (h->n >= h->alloc) h->alloc += 1024;
		h->buf = realloc(h->buf, sizeof(oct_node) * h->alloc);
	}
 
	p->heap_idx = h->n;
	h->buf[h->n++] = p;
	up_heap(h, p);
}
Beispiel #8
0
/* Inserts an element in to the toplist either if there is space left
   or the element is larger than the smallest element in the toplist.
   In the latter case, remove the smallest element from the toplist.
   Returns 1 if the element was actually inserted, 0 if not. */
int insert_into_toplist(toplist_t*list, void *element) {

  /* if there is room left, add it at the end (and update the heap) */
  if (list->elems < list->length) {
    list->heap[list->elems] = list->data + list->elems * list->size;
    memcpy(list->heap[list->elems], element, list->size);
    list->elems++;
    up_heap(list,list->elems-1);
    return(1);

  /* if it is smaller than the smallest element, simply drop it.
     if it is bigger, replace the smallest element (root of the heap)
     and update the heap */
  } else if ((list->smaller)(element, (list->heap)[0]) < 0) {
    memcpy(list->heap[0], element, list->size);
    down_heap(list);
    return(1);

  } else
    return(0);
}
Beispiel #9
0
void down_heap(node_t* node) {
  long long key = node->key;
  node_t* left = node->left;
  node_t* right = node->right;
  node_t* min = NULL;

  // No children. Stop downheaping.
  if (is_leaf(node)) {return;}
  // Only a left child. It must be the min
  else if (right == NULL) {min = left;}

  else {
    long long left_key = left->key;
    long long right_key = right->key;
    min = (left_key < right_key)? left : right;
  }

  if (key > min->key) {
    swap(node, min);
    down_heap(min);
  }
  
}
Beispiel #10
0
 inline void update_heap(TreeNode x, const Compare& comp, ExternalData& edata) {
   x = down_heap(x, comp, edata);
   (void)up_heap(x, comp, edata);
 }