Esempio n. 1
0
  // Add a new timer to the queue. Returns true if this is the timer that is
  // earliest in the queue, in which case the reactor's event demultiplexing
  // function call may need to be interrupted and restarted.
  bool enqueue_timer(const time_type& time, per_timer_data& timer, wait_op* op)
  {
    // Enqueue the timer object.
    if (timer.prev_ == 0 && &timer != timers_)
    {
      if (this->is_positive_infinity(time))
      {
        // No heap entry is required for timers that never expire.
        timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
      }
      else
      {
        // Put the new timer at the correct position in the heap. This is done
        // first since push_back() can throw due to allocation failure.
        timer.heap_index_ = heap_.size();
        heap_entry entry = { time, &timer };
        heap_.push_back(entry);
        up_heap(heap_.size() - 1);
      }

      // Insert the new timer into the linked list of active timers.
      timer.next_ = timers_;
      timer.prev_ = 0;
      if (timers_)
        timers_->prev_ = &timer;
      timers_ = &timer;
    }

    // Enqueue the individual timer operation.
    timer.op_queue_.push(op);

    // Interrupt reactor only if newly added timer is first to expire.
    return timer.heap_index_ == 0 && timer.op_queue_.front() == op;
  }
Esempio n. 2
0
 void push(const IndexedType& x) {
   c.push_back(x);
   /*set index-array*/
   index_array[ id[x] ] = c.size()-1;
   Node node(c.begin(), c.end(), c.end() - 1, id);
   up_heap(node, comp, index_array);
 }
Esempio n. 3
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;
  }
Esempio n. 4
0
void up_heap(node_t* node) {
  node_t* parent = node->parent;

  if (is_root(node)) {return;}

  if (parent->key > node->key) {
    swap(node, parent);
    up_heap(parent);
  }
}
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);
}
Esempio n. 6
0
void up_heap(heap_t* heap, int findex) {
  int parent_index;
  if (findex == 0) {
    return;
  }

  parent_index = (findex - 1) / 2;

  if (HEAP_NODE(heap, parent_index).key > HEAP_NODE(heap, findex).key) {
    swap(&(HEAP_NODE(heap, findex)), &(HEAP_NODE(heap, parent_index)));
    up_heap(heap, parent_index);
  }
}
Esempio n. 7
0
void heap_insert(heap_t* heap, void* data, long long int key) {
  int findex = heap->size;
  if (findex == heap->private_size) {
    expand_heap(heap);
  }
  
  findex = heap->size;
  HEAP_NODE(heap, findex).key = key;
  HEAP_NODE(heap, findex).data = data;
  up_heap(heap, findex);

  heap->size++;
}
Esempio n. 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);
}
Esempio n. 9
0
void heap_insert(heap_t* heap, void* data, long long key) {
  node_t* next_node;
  node_t* new_node;
  
  if (heap->free == NULL) {
    heap->free = (void*) malloc(sizeof(node_t));
    init_node((node_t*)heap->free);
  }

  new_node = (node_t*)heap->free;
  heap->free = new_node->parent;

  init_node(new_node);
  new_node->key = key;
  new_node->data = data;
  
  if (is_empty(heap)) {
    heap->top = new_node;
    heap->last = new_node;
    new_node->parent = NULL;
    heap->size++;
    return;
  }
  
  next_node = next(heap->last);
  if (next_node->left == NULL) {
    next_node->left = new_node;
    new_node->parent = next_node;
  }
  else {
    next_node->right = new_node;
    new_node->parent = next_node;
  }

  up_heap(new_node);

  heap->last = new_node;
  
  heap->size++;
}
Esempio n. 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);
 }