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);
    }
}
Example #2
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);
}
Example #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;
}
Example #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));
}
Example #5
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);
    }
    
}
Example #6
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);
  }
}
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);
	}

}
Example #8
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);
		}

 }
Example #9
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));
}
Example #10
0
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);
    }
}
Example #11
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;
}
Example #12
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);
}
Example #13
0
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;
}
Example #14
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);
	}
}
Example #15
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);
  }
}
Example #16
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);
  }
}
Example #17
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;
		}
Example #18
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;
}
Example #19
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);
    }
}
Example #20
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);
  }
}
Example #21
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);
	}

}
Example #22
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);
}
Example #23
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;
}
Example #24
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;
		}
Example #25
0
/*
 * 向树添加一个新值,参数是需要被添加的值,它必须原先并不在树中
 */
void insert( TREE_TYPE value)
{
    assert( value != 0);
    int current = 1;
    while( tree[current] != 0 )
    {
       if( value < tree[current] )
       {
           current = left_child(current);
       }else
       {
           current = right_child(current);
       }
       if( current > array_size )
       {
           reallocTree(current);
       }
    }
    assert( current < array_size );
    tree[current] = value;
}
Example #26
0
int BST_insert(BST_t *bst, node_t value)
{
    size_t current_node = 1;
    
    if (value == 0)
        fprintf(stderr, "error: value can not be zero!\n");
    
    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 0;
    }
    
    if (current_node < bst->capacity) {
        bst->tree[current_node] = value;
        return 1;
    } else
        return 0;
}
Example #27
0
TREE_TYPE *find( TREE_TYPE value)
{
    int current = 1;
    while( tree[current] != value )
    {
        if( value < tree[current] )
            current = left_child(current);
        else
            current = right_child(current);
        //assert( current < array_size );
        if( current > array_size )
            break;
    }
    if( current < array_size)
    {
        //printf("current is %d", current);
        //printf("find is %d", *(tree+current));
        return tree+current;
    }
    else
        return tree;
}
const cv::Mat& RegressionTree::operator()(
	const std::vector<double>& feature_pixel_values
) const
/*!
	requires
		- All the index values in splits are less than feature_pixel_values.size()
		- leaf_values.size() is a power of 2.
		  (i.e. we require a tree with all the levels fully filled out.
		- leaf_values.size() == splits.size()+1
		  (i.e. there needs to be the right number of leaves given the number of splits in the tree)
	ensures
		- runs through the tree and returns the vector at the leaf we end up in.
!*/
{
	unsigned long i = 0;
	while (i < splits.size())
	{
		if (feature_pixel_values[splits[i].idx1] - feature_pixel_values[splits[i].idx2] > splits[i].thresh)
			i = left_child(i);
		else
			i = right_child(i);
	}
	return leaf_values[i - splits.size()];
}
static void cpudl_heapify(struct cpudl *cp, int idx)
{
	int l, r, largest;

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

		if ((l < cp->size) && dl_time_before(cp->elements[idx].dl,
							cp->elements[l].dl))
			largest = l;
		if ((r < cp->size) && dl_time_before(cp->elements[largest].dl,
							cp->elements[r].dl))
			largest = r;
		if (largest == idx)
			break;

		/* Push idx down the heap one level and bump one up */
		cpudl_exchange(cp, largest, idx);
		idx = largest;
	}
}
Example #30
0
static char *test_right_child() {
  mu_assert("right child of 0 is 2", right_child(0) == 2);
  mu_assert("right child of 1 is 4", right_child(1) == 4);
  return 0;
}