Exemple #1
0
static void
downheap (Heap *h, unsigned n)
{
    heap_element v = h->data[n];

    while (n < h->sz / 2) {
	int cmp1, cmp2;
	unsigned new_n;

	assert (left_child(n) < h->sz);

	new_n = left_child(n);

	cmp1 = (*h->cmp)(v.data, h->data[new_n].data);

	if (right_child(n) < h->sz) {
	    cmp2 = (*h->cmp)(v.data, h->data[right_child(n)].data);
	    if (cmp2 > cmp1) {
		cmp1  = cmp2;
		new_n = right_child(n);
	    }
	}

	if (cmp1 > 0) {
	    assign (h, n, h->data[new_n]);
	    n = new_n;
	} else {
	    break;
	}
    }
    assign (h, n, v);
}
PB_DS_CLASS_T_DEC
void
PB_DS_CLASS_C_DEC::
assert_valid(const char* __file, int __line) const
{
#ifdef PB_DS_REGRESSION
  s_entry_allocator.check_allocated(m_a_entries, m_actual_size);
#endif

  resize_policy::assert_valid(__file, __line);
  PB_DS_DEBUG_VERIFY(m_size <= m_actual_size);
  for (size_type i = 0; i < m_size; ++i)
    {
#ifdef PB_DS_REGRESSION
      s_value_allocator.check_allocated(m_a_entries[i], 1);
#endif

      if (left_child(i) < m_size)
	PB_DS_DEBUG_VERIFY(!entry_cmp::operator()(m_a_entries[i], m_a_entries[left_child(i)]));

      PB_DS_DEBUG_VERIFY(parent(left_child(i)) == i);

      if (right_child(i) < m_size)
	PB_DS_DEBUG_VERIFY(!entry_cmp::operator()(m_a_entries[i], m_a_entries[right_child(i)]));

      PB_DS_DEBUG_VERIFY(parent(right_child(i)) == i);
    }
}
Exemple #3
0
int min_heap_remove(Min_heap *heap)
{
    size_t i;
    int res = heap->storage[0];
    int val = heap->storage[--heap->len];

    // Makes it safe to only check whether the left child is out of bounds. A
    // non-existent right child will never be selected as min_i.
    heap->storage[heap->len] = INT_MAX;
    i = 0;
    for (;;) {
        size_t min_i;

        if (left_child(i) >= heap->len)
            break;
        min_i =
          (heap->storage[left_child(i)] < heap->storage[right_child(i)]) ?
            left_child(i) : right_child(i);
        if (val <= heap->storage[min_i])
            break;
        heap->storage[i] = heap->storage[min_i];
        i = min_i;
    }
    heap->storage[i] = val;

    return res;
}
Exemple #4
0
static bool valid_min_heap_rec(Min_heap *heap, size_t i)
{
    bool left_valid;

    if (left_child(i) >= heap->len)
        return true;
    left_valid = valid_min_heap_rec(heap, left_child(i));
    if (right_child(i) >= heap->len)
        return left_valid;

    return left_valid && valid_min_heap_rec(heap, right_child(i));
}
Exemple #5
0
 void percolate_down(int i){
		int left,right,max=0,temp;
		if(i<0)
		 return -1;

	 left=left_child(i);
	 right=right_child(i);
	if(left!=-1 && h->a[left]>h->a[i])
			 max=left;
			 else
			 max=i;

		if(right!=-1 && h->a[right]>h->a[max])
			 max=right;

		if(max!=i){
		  //swap elements at  max & i
			 temp=h->a[max];
			 h->a[max]=h->a[i];
			 h->a[i]=temp;

			percolate_down(max);
		}

 }
void minHeapify ( struct MinHeap * heap, int index ) {

        // get the left child index 
	int left_child_index = left_child ( index );
	// get the right child index 
	int right_child_index = right_child ( index );
	// get the current size 
	int current_size = heap -> current_size;
	int smallest = index;
  
	if ( left_child_index < current_size && heap -> array[left_child_index]->priority < heap -> array[smallest]->priority )
	           smallest =  left_child_index; 
	if ( right_child_index < current_size && heap -> array[right_child_index]->priority < heap -> array[smallest]->priority)
	  	   smallest = right_child_index;

        if ( smallest != index ) {

		MinHeapNode * smallestNode = heap -> array[smallest];
		MinHeapNode * currentNode = heap -> array[index];

		// set position of smallest node to 'index'
		// set position of index node to 'smallest' 
		heap -> current_position[smallestNode->element] = index;
		heap -> current_position[currentNode->element] = smallest;

		// swap nodes 
		swapMinHeapNodes( &heap->array[smallest], &heap->array[index] );

		minHeapify( heap, smallest);
	}

}
Exemple #7
0
static void sift_down(thread array[], unsigned long long size,
                      unsigned long long index) {
  unsigned long long max_index, left, right;
  thread *left_t, *right_t, *index_t;
  left = left_child(index);
  right = right_child(index);
  left_t = &array[left - 1];
  right_t = &array[right - 1];
  index_t = &array[index - 1];
  max_index = index;

  if (right <= size && t_less_than(right_t, index_t)) {
    max_index = right;
  }

  if (left <= size && t_less_than(left_t, index_t)) {
    max_index = left;
  }

  if (left <= size && right <= size) {
    if (t_less_than(left_t, index_t) && t_less_than(left_t, right_t)) {
      max_index = left;
    }
    if (t_less_than(right_t, index_t) && t_less_than(right_t, left_t)) {
      max_index = right;
    }
  }

  if (index != max_index) {
    swap(&array[index - 1], &array[max_index - 1]);
    sift_down(array, size, max_index);
  }
}
Exemple #8
0
void heapify(int arr[], int i, int heap_size)
{
    int left = left_child(i);
    int right = right_child(i);
    int largest = 0;
    int swap_var = 0;
    
    if(left < heap_size && arr[left] > arr[i])
    {
	largest = left;
    }
    else
    {
	largest = i;
    }
    if(right < heap_size && arr[right] > arr[largest])
    {
	largest = right;
    }
    if(largest != i)
    {
	swap_var = arr[i];
	arr[i] = arr[largest];
	arr[largest] = swap_var;
	heapify(arr, largest, heap_size);
    }
    
}
Exemple #9
0
static void percolate_down(int i, Item *arr, int n)
{
    int child;
    Item tmp;
    for(tmp = arr[i]; left_child(i) < n; i = child) {
        child = left_child(i);
        if(child != n-1 && arr[child+1] > arr[child]) {
            child++;
        }
        if(tmp < arr[child]) {
            arr[i] = arr[child];
        } else {
            break;
        }
    }
    arr[i] = tmp;
}
Exemple #10
0
void pre_order_traverse(int root)
{
    if(root > array_size || tree[root] == 0)
        return;
    printf("%3d", tree[root]);
    pre_order_traverse(left_child(root));
    pre_order_traverse(right_child(root));
}
void BST_pre_order_traverse(BST_t *bst, size_t current_node, void (*callback)(node_t value))
{
    if (current_node < bst->capacity && bst->tree[current_node]) {
        callback(bst->tree[current_node]);
        BST_pre_order_traverse(bst, left_child(current_node), callback);
        BST_pre_order_traverse(bst, right_child(current_node), callback);
    }
}
Exemple #12
0
static Bool
do_verify (Heap *h, unsigned n)
{
    if (left_child(n) < h->sz) {
	if((*h->cmp)(h->data[n].data, h->data[left_child(n)].data) > 0)
	    return FALSE;
	if (!do_verify (h, left_child(n)))
	    return FALSE;
    }
    if (right_child(n) < h->sz) {
	if((*h->cmp)(h->data[n].data, h->data[right_child(n)].data) > 0)
	    return FALSE;
	if (!do_verify (h, right_child(n)))
	    return FALSE;
    }
    return TRUE;
}
Exemple #13
0
static void cpudl_heapify_down(struct cpudl *cp, int idx)
{
	int l, r, largest;

	int orig_cpu = cp->elements[idx].cpu;
	u64 orig_dl = cp->elements[idx].dl;

	if (left_child(idx) >= cp->size)
		return;

	/* adapted from lib/prio_heap.c */
	while(1) {
		u64 largest_dl;
		l = left_child(idx);
		r = right_child(idx);
		largest = idx;
		largest_dl = orig_dl;

		if ((l < cp->size) && dl_time_before(orig_dl,
						cp->elements[l].dl)) {
			largest = l;
			largest_dl = cp->elements[l].dl;
		}
		if ((r < cp->size) && dl_time_before(largest_dl,
						cp->elements[r].dl))
			largest = r;

		if (largest == idx)
			break;

		/* pull largest child onto idx */
		cp->elements[idx].cpu = cp->elements[largest].cpu;
		cp->elements[idx].dl = cp->elements[largest].dl;
		cp->elements[cp->elements[idx].cpu].idx = idx;
		idx = largest;
	}
	/* actual push down of saved original values orig_* */
	cp->elements[idx].cpu = orig_cpu;
	cp->elements[idx].dl = orig_dl;
	cp->elements[cp->elements[idx].cpu].idx = idx;
}
Exemple #14
0
void
EdgeHeap::
downHeap(int pos)
{
  if(print) cerr << "downHeap " << pos << endl;
  if(pos >= unusedPos_-1) return;
  assert(pos < HeapSize);
  Edge* par = array[pos];
  assert(par->heapPos() == pos);
  double merit = par->merit();
  int lc = left_child(pos);
  int rc = right_child(pos);
  int largec;
  int lcthere = 0;
  Edge* lct=NULL;
  if(lc < unusedPos_)
    {
      assert(lc < HeapSize);
      lct = array[lc];
      if(lct)
	{ lcthere = 1;
	  assert(lct->heapPos() == lc);
	}
    }
  int rcthere = 0;
  Edge* rct=NULL;
  if(rc < unusedPos_)
    {
      rct = array[rc];
      if(rct)
	{
	  rcthere = 1;
	  assert(rct->heapPos() == rc);
	}
    }
  if(!lcthere && !rcthere) return;
  assert(lcthere);
  if(!rcthere || (lct->merit() > rct->merit()))
    largec = lc;
  else largec = rc;
  Edge* largeEdg = array[largec];
  if(merit >= largeEdg->merit())
    {
      if(print) cerr << "downheap of " << merit << " stopped by "
		     << *largeEdg << " " << largeEdg->merit() << endl;
      return;
    }
  array[pos] = largeEdg;
  largeEdg->heapPos() = pos;
  array[largec] = par;
  par->heapPos() = largec;
  downHeap(largec);
}
Exemple #15
0
void bubble_down(int *array, int *heap_size, int start){
	int temp = array[start];
	int i = start, compare;
	while(true){
		compare = left_child(i);
		if (compare >= *heap_size) break;
		if (compare + 1 < *heap_size && array[compare] > array[compare+1]){
			compare++;
		}
		if (temp < array[compare]) break;
		swap(array, i, compare);
		i = compare;
	}
}
node_t *BST_find(BST_t *bst, node_t value)
{
    size_t current_node = 1;
    
    while (current_node < bst->capacity && bst->tree[current_node])
        if (value < bst->tree[current_node])
            current_node = left_child(current_node);
        else if (value > bst->tree[current_node])
            current_node = right_child(current_node);
        else 
            return bst->tree + current_node;
        
    return NULL;
}
Priority_Queue *pqueue_shift_down(Priority_Queue *Q, int i) {
  int child;
  while((child = left_child(i)) < Q->size) {
    if((child+1 < Q->size) && (*(int *)((void **)Q->arr[child])[0] < *(int *)((void **)Q->arr[child+1])[0])) { ++child; }
    if(*(int *)((void **)Q->arr[i])[0] < *(int *)((void **)Q->arr[child])[0]) {
      void *tmp = Q->arr[child];
      Q->arr[child] = Q->arr[i];
      Q->arr[i] = tmp;
      i = child;
    }
    else { return Q; }
  }
  return Q;
}
Exemple #18
0
void heapify (priority_queue *pq, int n)
{
	int left, right, smallest;

	left = left_child(n);
	right = right_child(n);

	smallest = min(pq, n, left, right);

	if (smallest != n) {
		swap(pq, n, smallest);
		heapify(pq, smallest);
	}
}
Exemple #19
0
Heap *heap_shift_down(Heap *H, int i) {
  int child;
  while((child = left_child(i)) < H->size) {
    if((child+1 < H->size) && H->comparator(H->arr[child],H->arr[child+1]) == -1) { ++child; }
    if(H->comparator(H->arr[i], H->arr[child]) == -1) {
      void *tmp = H->arr[child];
      H->arr[child] = H->arr[i];
      H->arr[i] = tmp;
      i = child;
    }
    else { break; }
  }
  return H;
}
Exemple #20
0
static void heapify(size_t n, int *heap, size_t index) {
  int largest = index;
  size_t left = left_child(index);
  size_t right = right_child(index);

  if (left < n && heap[left] > heap[largest])
    largest = left;
  if (right < n && heap[right] > heap[largest])
    largest = right;

  if (largest != index) {
    swap(heap, index, largest);
    heapify(n, heap, largest);
  }
}
Exemple #21
0
void sift_down(int array[], int size, int index) {
  int min_index, left, right;
  min_index = index;
  left = left_child(array, index);
  right = right_child(array, index);
  if (left <= size && array[left - 1] < array[min_index - 1]) {
    min_index = left;
  }
  if (right <= size && array[right - 1] < array[min_index - 1]) {
    min_index = right;
  }
  if (index != min_index) {
    swap(&array[index - 1], &array[min_index - 1]);
    sift_down(array, size, min_index);
  }
}
Exemple #22
0
		boost::optional<BVHIntersection> intersect(Ray ray, int parent, int depth, double tmin_already) const {
			// 最大深度を超えたら終わりにする
			if (_depth_count <= depth) {
				return boost::none;
			}

			int node_index = parent - 1;
			auto intersection = lc::intersect(ray, _nodes[node_index].aabb);
			if (!intersection) {
				return boost::none;
			}

			// すでに判明しているtminが手前にあるなら、後半は判定する必要はない
			if (tmin_already < intersection->tmin) {
				return boost::none;
			}

			// 終端までやってきたので所属するポリゴンに総当たりして終了
			if (_nodes[node_index].isTerminal()) {
				boost::optional<BVHIntersection> r;
				for (auto index : _nodes[node_index].indices) {
					const Triangle &triangle = _triangles[index];
					if (auto intersection = lc::intersect(ray, triangle)) {
						double tmin = r ? r->tmin : tmin_already;
						if (intersection->tmin < tmin) {
							r = BVHIntersection(*intersection, index);
						}
					}
				}
				return r;
			}
			int child_L = left_child(parent);
			int child_R = right_child(parent);
			boost::optional<BVHIntersection> L = this->intersect(ray, child_L, depth + 1, tmin_already);
			if (L) {
				tmin_already = glm::min(tmin_already, L->tmin);
			}
			boost::optional<BVHIntersection> R = this->intersect(ray, child_R, depth + 1, tmin_already);

			if (!L) {
				return R;
			}
			if (!R) {
				return L;
			}
			return L->tmin < R->tmin ? L : R;
		}
Exemple #23
0
void p_queue::max_heapify(int array[], int length, int i) {
    
    int left = left_child(i);
    int right = right_child(i);
    int largest = i;

    if (left < length && array[i] < array[left])
        largest = left;
    if (right < length && array[largest] < array[right])
        largest = right;

    if (largest != i) {
        
        std::swap(array[i], array[largest]);
        max_heapify(array, length, largest);
    }
}
Exemple #24
0
void siftdown(int pos, pqueue* pq)
{
  int j;
  
 while (!is_leaf(pos, pq)) 
    {
      j = left_child(pos, pq);
      if ( (j < (pq->n-1)) && (greater(pq->elems[j+1], pq->elems[j])) ) 
	j++; /* j is now index of child with greater value */
     
      if ( !greater(pq->elems[j], pq->elems[pos]) )
	return;

      swap(pos, j, pq);
      pos = j;  /* Move down */
    }
}
Exemple #25
0
int get_max(int vector[], int dad){
  int left = left_child(dad);
  int right = right_child(dad);
  int larger = 0;
  
  if(left <= sizeof(vector) && vector[left] > vector[dad]){
    larger = left;
  }else{
    larger = dad;
  }

  if(right <= sizeof(vector) && vector[right] > vector[dad]){
    larger = right;
  }

  return larger;
}
Exemple #26
0
static void sift_down_log(int array[], int size, int index, log *l) {
  int min_index, left, right;
  min_index = index;
  left = left_child(array, index);
  right = right_child(array, index);
  if (left <= size && array[left - 1] < array[min_index - 1]) {
    min_index = left;
  }
  if (right <= size && array[right - 1] < array[min_index - 1]) {
    min_index = right;
  }
  if (index != min_index) {
    l->logs[l->size].i = index - 1;
    l->logs[l->size].j = min_index - 1;
    l->size++;
    swap(&array[index - 1], &array[min_index - 1]);
    sift_down_log(array, size, min_index, l);
  }
}
Exemple #27
0
void heapify(int varArr[],int current,int end)
{
	int l=left_child(current),r=right_child(current);
	int largest;

	if (l<end&&varArr[current]<varArr[l])
		largest=l;
	else
		largest=current;

	if (r<end&&varArr[largest]<varArr[r])
		largest=r;

	if(largest!=current)
	{
		swap(&varArr[current],&varArr[largest]);
		heapify(varArr,largest,end);
	}

}
Exemple #28
0
void
AnsTreeHeap::
downHeap(int pos)
{
  if(print) cerr << "downHeap " << pos << endl;
  if(pos >= unusedPos_-1) return;
  AnsTreePair* par = array[pos];
  double merit = par->first;
  int lc = left_child(pos);
  int rc = right_child(pos);
  int largec;
  int lcthere = 0;
  AnsTreePair* lct;
  if(lc < unusedPos_)
    {
      lct = array[lc];
      if(lct) lcthere = 1;
    }
  int rcthere = 0;
  AnsTreePair* rct;
  if(rc < unusedPos_)
    {
      rct = array[rc];
      if(rct) rcthere = 1;
    }
  if(!lcthere && !rcthere) return;
  assert(lcthere);
  if(!rcthere || (lct->first > rct->first))
    largec = lc;
  else largec = rc;
  AnsTreePair* largeatp = array[largec];
  if(merit <= largeatp->first) 
    {
      if(print) cerr << "downheap of " << merit << " stopped by "
		     << " " << largeatp->first << endl;
      return;
    }
  array[pos] = largeatp;
  array[largec] = par;
  downHeap(largec);
}
Exemple #29
0
		bool is_visible(const Ray &ray, int parent, int depth, double tmin_target) const {
			// 最大深度を超えたら終わりにする
			if (_depth_count <= depth) {
				return true;
			}

			int node_index = parent - 1;
			auto intersection = lc::intersect(ray, _nodes[node_index].aabb);
			if (!intersection) {
				return true;
			}

			// すでに判明しているtminが手前にあるなら、後半は判定する必要はない
			if (tmin_target < intersection->tmin) {
				return true;
			}

			// 終端までやってきたので所属するポリゴンに総当たりして終了
			if (_nodes[node_index].isTerminal()) {
				boost::optional<BVHIntersection> r;
				for (auto index : _nodes[node_index].indices) {
					const Triangle &triangle = _triangles[index];
					if (auto intersection = lc::intersect(ray, triangle, tmin_target)) {
						if (intersection->tmin < tmin_target) {
							return false;
						}
					}
				}
				return true;
			}
			int child_L = left_child(parent);
			int child_R = right_child(parent);
			if (this->is_visible(ray, child_L, depth + 1, tmin_target) == false) {
				return false;
			}
			if (this->is_visible(ray, child_R, depth + 1, tmin_target) == false) {
				return false;
			}

			return true;
		}
 size_t bubble_down(size_t n)
 {
     while (true)
     {
         size_t left = left_child(n);
         size_t right = 1+left;
         size_t smallest = small_elem(n, left, right);
         if (smallest != n)
         {
             std::swap(m_data[smallest], m_data[n]);
             // update 'smallest' which is now at 'n'
             set_heap_index(m_data[n], n);
             n = smallest;
         }
         else
         {
             set_heap_index(m_data[n], n);
             return n;
         }
     }
 }