Exemple #1
0
        void SplayTree::insert(node* n)
        {
            if (unlikely(header.parent == NULL))  // First element.
            {
                header.parent = header.child[LEFT] = header.child[RIGHT] = n;
                n->parent = n->child[LEFT] = n->child[RIGHT] = NULL;
            }
            else // Not first element.
            {
                // Find place to insert node and insert it.
                node* insert_location = header.parent;
                __find(insert_location, n);
                __insert(insert_location, n);

                // Fix up header nodes.
                header.child[LEFT] = leftmost(header.child[LEFT]);
                header.child[RIGHT] = rightmost(header.child[RIGHT]);

                // Splay new node.
                splay(n);
            }

            // Increment size count.
            (header_n()->data)++;
        }
/**
 * Debug function for Lowest common ancestor
 */
void SSTree::CheckLCA(ulong v)
{
    ulong len = br->rank(rightmost(v));
    v++;ulong w, temp, v1;
    for (w = 1; w < len - 1; w++)
    {
        temp = br->select(w);
        v1 = br->select(w+1);
        while(v1 < len)
        {
            if (lca(temp, v1) != lcaParen(temp, v1))
            {
                printf("conflict at (%lu, %lu)::lcaParen() = %lu and lca() = %lu\n", temp, v1, lcaParen(temp, v1), lca(temp, v1));
                exit(0);
            }
            v1 = br->select(br->rank(v1)+1);
        }

        // Check for the value v1 == len
        if (lca(temp, v1) != lcaParen(temp, v1))
        {
            printf("conflict at (%lu, %lu)::lcaParen() = %lu and lca() = %lu\n", temp, v1, lcaParen(temp, v1), lca(temp, v1));
            exit(0);
        }
    }
}
Exemple #3
0
int transpose_onerank(Agraph_t* g, int r, boolean reverse)
{
	int     i,c0,c1,rv;
	node_t  *v,*w;

	rv = 0;
	GD_rank(g)[r].candidate = FALSE;
	for (i = leftmost(g,r); i < rightmost(g,r); i++) {
		v = GD_rank(g)[r].v[i];
		w = GD_rank(g)[r].v[i+1];
		assert (ND_order(v) < ND_order(w));
		if (left2right(g,v,w)) continue;
		c0 = c1 = 0;
		if (r > GD_minrank(g)) {
			c0 += in_cross(v,w);
			c1 += in_cross(w,v);
		}
		if (r < GD_maxrank(g)) {
			c0 += out_cross(v,w);
			c1 += out_cross(w,v);
		}
		if ((c1 < c0) || ((c0 > 0) && reverse && (c1 == c0))) {
			exchange(g,v,w);
			rv += (c0 - c1);
		}
	}
	return rv;
}
Exemple #4
0
/*
 * defines ND_sortweight of each node in r0 w.r.t. r1
 * returns...
 */
static boolean medians(Agraph_t *g, int r0, int r1)
{
	static int *list;
	static int list_extent;
	int     i,j,lm,rm,lspan,rspan;
	node_t  *n,**v;
	edge_t  *e;
	boolean hasfixed = FALSE;

	if (list_extent < GD_maxinoutdeg(g->root)) {
		list_extent = GD_maxinoutdeg(g->root);
		if (!list) list = realloc(list,sizeof(list[0])*list_extent);
		else list = realloc(list,sizeof(list[0])*list_extent);
	}
	v = GD_rank(g)[r0].v;
	for (i = leftmost(g,r0); i <= rightmost(g,r0); i++) {
		n = v[i]; j = 0;
		if (r1 > r0) for (e = agfstout(g,n); e; e = agnxtout(g,e))
			{if (ED_xpenalty(e) > 0) list[j++] = VAL(e->head,ED_headport(e));}
		else for (e = agfstin(g,n); e; e = agnxtin(g,e))
			{if (ED_xpenalty(e) > 0) list[j++] = VAL(e->tail,ED_tailport(e));}
		switch(j) {
			case 0:
				ND_sortweight(n) = -1;		/* no neighbor - median undefined */
				break;
			case 1:
				ND_sortweight(n) = list[0];
				break;
			case 2:
				ND_sortweight(n) = (list[0] + list[1])/2;
				break;
			default:
				qsort(list,j,sizeof(int),int_cmpf);
				if (j % 2) ND_sortweight(n) = list[j/2];
				else {
					/* weighted median */
					rm = j/2;
					lm = rm - 1;
					rspan = list[j-1] - list[rm];
					lspan = list[lm] - list[0];
					if (lspan == rspan)
						ND_sortweight(n) = (list[lm] + list[rm])/2;
					else {
						int w = list[lm]*rspan + list[rm]*lspan;
						ND_sortweight(n) = w / (lspan + rspan);
					}
				}
		}
	}
#ifdef NOTDEF
	/* this code was in the old mincross */
	for (i = 0; i < GD_rank(g)[r0].n; i++) {
		n = v[i];
		if ((ND_out(n).size == 0) && (ND_in(n).size == 0))
			hasfixed |= flat_sortweight(n);
	}
#endif
	return hasfixed;
}
Exemple #5
0
/* Returns the previous node of N.  NULL iff n is the minimum. */
static Node *prev_node(Node *n)
{
    if (n->left) return rightmost(n->left);
    while (n->parent) {
        if (RIGHTP(n)) return n->parent;
        n = n->parent;
    }
    return NULL;
}
Exemple #6
0
static void
screen_next_col(Screen *s)
{
	s->x_pos = 0;
	s->x_col++;

	if (s->x_col >= numfields(s)) {
		s->x_col = rightmost(s);
	}
}
Exemple #7
0
static void reorder(graph_t *g, int r, boolean reverse, boolean hasfixed)
{
	boolean changed, muststay;
	node_t  **vlist, **lp, **rp, **ep;
	int			i;

	changed = FALSE;
	vlist = GD_rank(g)[r].v;
	ep = &vlist[rightmost(g,r)];
	
	for (i = leftmost(g,r); i <= rightmost(g,r); i++) {
		lp = &vlist[leftmost(g,r)];
		/* find leftmost node that can be compared */
		while ((lp < ep) && (ND_sortweight(*lp) < 0)) lp++;
		if (lp >= ep) break;
		/* find the node that can be compared */
		muststay = FALSE;
		for (rp = lp + 1; rp < ep; rp++) {
			if (left2right(g,*lp,*rp)) { muststay = TRUE; break; }
			if (ND_sortweight(*rp) >= 0) break;	/* weight defined; it's comparable */
		}
		if (rp >= ep) break;
		if (muststay == FALSE) {
			register int    p1 = ND_sortweight(*lp);
			register int    p2 = ND_sortweight(*rp);
			if ((p1 > p2) || ((p1 == p2) && (reverse))) {
				exchange(g,*lp,*rp);
				changed = TRUE;
			}
		}
		lp = rp;
		if ((hasfixed == FALSE) && (reverse == FALSE)) ep--;
	}
                                                                                
	if (changed) {
		GD_rank(g)[r].changed = TRUE;
		GD_rank(g)[r].crossing_cache.valid = FALSE;
		if (r > 0) GD_rank(g)[r-1].crossing_cache.valid = FALSE;
		if (r + 1 > GD_rank(g)[r+1].n) GD_rank(g)[r-1].crossing_cache.valid = FALSE;
	}
}
Exemple #8
0
static void presort(Agraph_t *ug)
{
	int		r;
	int		i;
	Agraph_t	*mg;

	if (ug == ug->root) return;
	mg = GD_model(ug);
	for (r = GD_minrank(mg); r <= GD_maxrank(mg); r++) {
		qsort(GD_rank(mg)[r].v,GD_rank(mg)[r].n,sizeof(Agnode_t*),presort_cmpf);
		for (i = leftmost(mg,r); i < rightmost(mg,r); i++)
			ND_order(GD_rank(mg)[r].v[i]) = i;
	}
}
Exemple #9
0
static Node *core_bound(ScmTreeCore *tc, ScmTreeCoreBoundOp op, int pop)
{
    Node *root = ROOT(tc);
    if (root) {
        Node *n = (op == SCM_TREE_CORE_MIN)? leftmost(root) : rightmost(root);
        if (pop) {
            n = delete_node(tc, n);
            tc->num_entries--;
        }
        return n;
    } else {
        return NULL;
    }
}
Exemple #10
0
const RbTreeNode *RbTreeUtil::previous(const RbTreeNode *node)
{
    BSLS_ASSERT(node);

    if (node->leftChild()) {
        return rightmost(node->leftChild());                          // RETURN
    }
    const RbTreeNode *parent = node->parent();
    while (node == parent->leftChild()) {
        node   = parent;
        parent = node->parent();
    }

    return parent;
}
    void flatten(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(!root)
            return;
        flatten(root->left);
        flatten(root->right);
	if(!root->left)
		return;
        TreeNode* r = root->right;
        TreeNode* rm = rightmost(root->left);
        root->right = root->left;
        root->left = NULL;
        rm->right = r;
    }
Exemple #12
0
static void
screen_scroll_right(Screen *s)
{
	s->x_pos++;

	if (s->x_pos > maxlen(s,s->x_col)) {

		s->x_pos = 0;
		s->x_col++;

		if (s->x_col >= numfields(s)) {

			s->x_col = rightmost(s);
			s->x_pos = maxlen(s,s->x_col);
		}
	}
}
Exemple #13
0
        const SplayTree::node* SplayTree::predecessor(const node* n) const
        {
            // If left child, predecessor is just the largest of the left
            // subtree.
            if (likely(NULL != n->child[LEFT]))
            {
                return rightmost(n->child[LEFT]);
            }

            // Else, need to work up the tree to find predecessor.
            const node* y = n->parent;
            while ((NULL != y) && (n == y->child[LEFT]))
            {
                n = y;
                y = y->parent;
            }

            return y;
        }
Exemple #14
0
MapvNodeBase * MapvBase::unbalancing_removal( MapvNodeBase ** n )
{
  MapvNodeBase * t = *n ;

  while ( t != header() && t->parent ) {
    if      ( t->left  ) { t = t->left ; }
    else if ( t->right ) { t = t->right ; }
    else { // Move to parent and remove this leaf
      *n = t->parent ; t->parent = 0 ;
      if ( (*n)->left == t ) (*n)->left  = 0 ;
      else                   (*n)->right = 0 ;
    }
  }

  if ( t == header() ) {

    header()->parent = 0 ;
    header()->left   = 0 ;
    header()->right  = 0 ;
    header()->color  = red ;  /* Color the header node red */

    Count = 0 ;

    left_end.parent = 0 ;
    left_end.left   = 0 ;
    left_end.right  = 0 ;
    left_end.color  = black ;

    right_end.parent = 0 ;
    right_end.left   = 0 ;
    right_end.right  = 0 ;
    right_end.color  = black ;

    leftmost(  header()->left  = nREnd() ); // left end of the tree
    rightmost( header()->right = nEnd() );  // right end of the tree

    t = 0 ;
  }

  return t ;
}
void preorder(struct node *x){
	struct node *temp = NULL;
	while(x != NULL){
		if(x->left == NULL){
			printf("%c",x->data);
			x = x->right;
		}
		else{
			temp = rightmost(x->left, x);
			if(temp->right ==  x){
				temp->right = NULL;
				x = x->right;
			}
			else{
				printf("%c",x->data);
				temp->right = x;
				x = x->left;
			}
		}
	}
}
Exemple #16
0
static void
screen_end(Screen *s)
{
	s->x_col = rightmost(s);
	s->x_pos = 0;
}
Exemple #17
0
void MapvBase::insert( MapvNodeBase * y , MapvNodeBase * z , bool z_lt_y )
{
  z->remove_from_container();

  if ( y == nEnd() ) { // First node inserted
    root(z);
    leftmost(z);
    rightmost(z);
    z->parent = header() ; // header is 'super-root'
  }
  else {
    if ( z_lt_y ) {
      y->left = z ;
      // maintain leftmost() pointing to minimum node
      if ( y == leftmost() ) leftmost(z);
    }
    else {
      y->right = z;
      // maintain rightmost() pointing to maximum node
      if ( y == rightmost() ) rightmost(z);
    }
    z->parent = y ;
  }
  z->left  = 0 ;
  z->right = 0 ;
  z->color = red ;
  ++Count ;

  // -------------------------------------------------------------------
  // Rebalance, 'y' and 'z' are reused as a local variable

  while ( z != nRoot() && z->parent->color == red ) {
    if ( z->parent == z->parent->parent->left ) {
      y = z->parent->parent->right ;
      if ( y && y->color == red ) {
	z->parent->color         = black;
	y->color                 = black;
	z->parent->parent->color = red;
	z = z->parent->parent ;
      }
      else {
	if ( z == z->parent->right ) {
	    z = z->parent ;
	    rotate_left(z);
	}
	z->parent->color         = black;
	z->parent->parent->color = red;
	rotate_right( z->parent->parent );
      }
    }
    else {
      y = z->parent->parent->left ;
      if ( y && y->color == red ) {
	z->parent->color         = black;
	y->color                 = black;
	z->parent->parent->color = red;
	z = z->parent->parent ;
      }
      else {
	if ( z == z->parent->left ) {
	    z = z->parent ;
	    rotate_right(z);
	}
	z->parent->color         = black;
	z->parent->parent->color = red;
	rotate_left(z->parent->parent);
      }
    }
  }
  nRoot()->color = black;
}
Exemple #18
0
void VideoCorrect::correctImage(Mat& inputFrame, Mat& outputFrame, bool developerMode){
	
	resize(inputFrame, inputFrame, CAMERA_RESOLUTION);
	inputFrame.copyTo(img);

	//Convert to YCbCr color space
	cvtColor(img, ycbcr, CV_BGR2YCrCb);

	//Skin color thresholding
	inRange(ycbcr, Scalar(0, 150 - Cr, 100 - Cb), Scalar(255, 150 + Cr, 100 + Cb), bw);

	if(IS_INITIAL_FRAME){
		face = detectFaces(img);
		if(face.x != 0){
			lastFace = face;
		}
		else{
			outputFrame = img;
			return;
		}
		prevSize = Size(face.width/2, face.height/2);
		head = Mat::zeros(bw.rows, bw.cols, bw.type());
		ellipse(head, Point(face.x + face.width/2, face.y + face.height/2), prevSize, 0, 0, 360, Scalar(255,255,255,0), -1, 8, 0);
		if(face.x > 0 && face.y > 0 && face.width > 0 && face.height > 0 
			&& (face.x + face.width) < img.cols && (face.y + face.height) < img.rows){
			img(face).copyTo(bestImg);
		}
		putText(img, "Give your best pose!", Point(face.x, face.y), CV_FONT_HERSHEY_SIMPLEX, 0.4, Scalar(255,255,255,0), 1, CV_AA);
	}

	firstFrameCounter--;

	if(face.x == 0) //missing face prevention
		face = lastFace;

	//Mask the background out
	bw &= head;

	//Compute more accurate image moments after background removal
	m = moments(bw, true);
	angle = (atan((2*m.nu11)/(m.nu20-m.nu02))/2)*180/PI;
	center = Point(m.m10/m.m00,m.m01/m.m00);

	//Smooth rotation (running average)
	bufferCounter++;
	rotationBuffer[ bufferCounter % SMOOTHER_SIZE ] = angle;
	smoothAngle += (angle - rotationBuffer[(bufferCounter + 1) % SMOOTHER_SIZE]) / SMOOTHER_SIZE;

	//Expand borders
	copyMakeBorder( img, img, BORDER_EXPAND, BORDER_EXPAND, BORDER_EXPAND, BORDER_EXPAND, 
					BORDER_REPLICATE, Scalar(255,255,255,0));

	if(!IS_INITIAL_FRAME){
		//Rotate the image to correct the leaning angle
		rotateImage(img, smoothAngle);
	
		//After rotation detect faces
		face = detectFaces(img);
		if(face.x != 0)
			lastFace = face;

		//Create background mask around the face
		head = Mat::zeros(bw.rows, bw.cols, bw.type());
		ellipse(head, Point(face.x - BORDER_EXPAND + face.width/2, face.y -BORDER_EXPAND + face.height/2),
					  prevSize, 0, 0, 360, Scalar(255,255,255,0), -1, 8, 0);

		//Draw a rectangle around the face
		//rectangle(img, face, Scalar(255,255,255,0), 1, 8, 0);

		//Overlay the ideal pose
		if(replaceFace && center.x > 0 && center.y > 0){
			center = Point(face.x + face.width/2, face.y + face.width/2);
			overlayImage(img, bestImg, center, smoothSize);
		}

	} else{
		face.x += BORDER_EXPAND; //position alignment after border expansion (not necessary if we detect the face after expansion)
		face.y += BORDER_EXPAND;
	}
	
	//Smooth ideal image size (running average)
	sizeBuffer[ bufferCounter % SMOOTHER_SIZE ] = face.width;
	smoothSize += (face.width - sizeBuffer[(bufferCounter + 1) % SMOOTHER_SIZE]) / SMOOTHER_SIZE;

	//Get ROI
	center = Point(face.x + face.width/2, face.y + face.width/2);
	roi = getROI(img, center);
	if(roi.x > 0 && roi.y > 0 && roi.width > 0 && roi.height > 0 
		&& (roi.x + roi.width) < img.cols && (roi.y + roi.height) < img.rows){
		img = img(roi);
	}

	//Resize the final image
	resize(img, img, CAMERA_RESOLUTION);

	if(developerMode){

		Mat developerScreen(img.rows, 
							img.cols + 
							inputFrame.cols +
							bw.cols, CV_8UC3);

		Mat left(developerScreen, Rect(0, 0, img.size().width, img.size().height));
		img.copyTo(left);

		Mat center(developerScreen, Rect(img.cols, 0, inputFrame.cols, inputFrame.rows));
		inputFrame.copyTo(center);

		cvtColor(bw, bw, CV_GRAY2BGR);
		Mat right(developerScreen, Rect(img.size().width + inputFrame.size().width, 0, bw.size().width, bw.size().height));
		bw.copyTo(right);

		Mat rightmost(developerScreen, Rect(img.size().width + inputFrame.size().width + bw.size().width - bestImg.size().width, 0,
											bestImg.size().width, bestImg.size().height));
		bestImg.copyTo(rightmost);

		outputFrame = developerScreen;
	}
	else{
		outputFrame = img;
	}
}
Exemple #19
0
void MapvBase::remove( MapvNodeBase * node )
{
  static const char method_name[] = "MapvBase::remove" ;

  if ( container(node) != this ) {
    std::string msg(method_name);
    msg.append(" given object not in this container");
    throw std::invalid_argument( msg );
  }

  if ( 1 == Count ) { // The last node ?

    if ( node != leftmost() || node != rightmost() || node != nRoot() ) {
      std::string msg(method_name);
      msg.append(" internal data structure corrupted" );
      throw std::runtime_error( msg );
    }

    leftmost( nREnd() );
    rightmost( nEnd() );
    root(0);
    Count = 0 ;
    header()->color = red ;
    node->left = node->right = node->parent = 0 ; node->color = 0 ;
    return ;
  }

  MapvNodeBase * z = node ;
  MapvNodeBase * y = node ;
  MapvNodeBase * x = 0 ;
  MapvNodeBase * x_parent = 0 ;

  // Ready to remove

  if ( y->left == 0 ) {       // z has at most one non-null child. y == z
    x = y->right ;            // x might be null
  }
  else if ( y->right == 0 ) { // z has exactly one non-null child. y == z
    x = y->left ;             // z is not null
  }
  else {                      // z has two non-null children.
     y = y->right ;           // Set y to z's successor.
     while ( y->left ) y = y->left ;
     x = y->right ;           // x might be null
  }

  if ( y != z ) { // relink y in place of z. y is z's successor
    z->left->parent = y ;
    y->left = z->left ;
    if ( y != z->right ) {
      x_parent = y->parent ;
      if ( x ) x->parent = x_parent ;
      y->parent->left = x;   // y must be a left child
      y->right = z->right;
      z->right->parent = y;
    } else {
      x_parent = y;  // needed in case x == 0
    }
    if ( nRoot() == z) {
      root(y);
    }
    else if ( z->parent->left == z) {
      z->parent->left = y;
    }
    else {
      z->parent->right = y;
    }
    y->parent = z->parent;
    { int c = y->color; y->color = z->color; z->color = c ; }
    y = z;
    // y points to node to be actually deleted
  }
  else {  // y == z
    x_parent = y->parent ;
    if ( x ) x->parent = x_parent ; // possibly x == 0
    if ( nRoot() == z) {
      root(x);
    }
    else if ( z->parent->left == z ) {
      z->parent->left = x;
    }
    else {
      z->parent->right = x;
    }
    if ( leftmost() == z )  {
      if ( z->right == 0 ) { // z->left must be null also
	// makes leftmost() == nEnd() if z == nRoot()
	leftmost( z->parent );
      }
      else {
	leftmost( minimum(x) );
      }
    }
    if ( rightmost() == z )  {
      if ( z->left == 0 ) { // z->right must be null also
	// makes rightmost() == nEnd() if z == nRoot()
	rightmost( z->parent );
      }
      else { // x == z->left
	rightmost( maximum(x) );
      }
    }
  }
  if ( y->color != red ) {
    while ( x != nRoot() && ( x == 0 || x->color == black ) ) {
      if ( x == x_parent->left ) {
	MapvNodeBase * w = x_parent->right ;
	if ( w->color == red ) {
	  w->color        = black;
	  x_parent->color = red;
	  rotate_left(x_parent);
	  w = x_parent->right ;
	}
	if ((w->left  == 0 || w->left->color  == black) &&
	    (w->right == 0 || w->right->color == black)) {
	  w->color = red ;
	  x = x_parent ;
	  x_parent = x_parent->parent ;
	}
	else {
	  if (w->right == 0 || w->right->color == black) {
	      if ( w->left ) w->left->color = black;
	      w->color = red;
	      rotate_right(w);
	      w = x_parent->right ;
	  }
	  w->color = x_parent->color ;
	  x_parent->color = black;
	  if ( w->right ) w->right->color = black;
	  rotate_left(x_parent);
	  break;
	}
      }
      else {  // same as then clause with "right" and "left" exchanged
	MapvNodeBase * w = x_parent->left ;
	if ( w->color == red ) {
	  w->color = black;
	  x_parent->color = red;
	  rotate_right(x_parent);
	  w = x_parent->left ;
	}
	if ((w->right == 0 || w->right->color == black) &&
	    (w->left  == 0 || w->left->color  == black)) {
	  w->color = red;
	  x = x_parent ;
	  x_parent = x_parent->parent ;
	}
	else {
	  if ( w->left == 0 || w->left->color == black ) {
	    if ( w->right ) w->right->color = black;
	    w->color = red;
	    rotate_left(w);
	    w = x_parent->left ;
	  }
	  w->color = x_parent->color ;
	  x_parent->color = black;
	  if ( w->left ) w->left->color = black;
	  rotate_right(x_parent);
	  break;
	}
      }
    }
    if ( x ) x->color = black;
  }

  y->left = y->right = y->parent = 0 ; y->color = 0 ;

  --Count ; // Decrement the tree's count
}