void TreeDB::balance(TreeNode*& root)
{
    fixHeight(root);
 
    //by convention, we are right heavy
    if(balanceFactor(root) == 2)
    {
        //if we have a zig-zag form, we must rotate right then left
        if(balanceFactor(root->right) < 0)
        {
            cout << "rebalancing..." << endl;
            rotateRight(root->right);
        }
        
        //if we have a linked list form, we only need to rotate left,
        //if we had a zig zag form, we already rotated right, now left
        cout << "rebalancing..." << endl;
        rotateLeft(root);
    }
    else if(balanceFactor(root) == -2)     //left heavy
    {
        //if we have a zig-zag form, we must rotate left then right
        if(balanceFactor(root->left) > 0)
        {
            cout << "rebalancing..." << endl;
            rotateLeft(root->left);
        }
        
        //if we have a linked list form, we only need to rotate right,
        //if we had a zig zag form, we already rotated left, now right
        cout << "rebalancing..." << endl;
        rotateRight(root);
    }
}
Example #2
0
void AvlTree::rebalance(int node) {
    for(int n = node; n != NIL; ) {
        const int p = parentNode(n);

        updateAggregates(n);

        switch(balanceFactor(n)) {
            case -2: {
                         const int right = rightNode(n);
                         if(balanceFactor(right) == 1) {
                             rotateRight(right);
                         }
                         rotateLeft(n);
                         break;
                     }
            case 2: {
                        const int left = leftNode(n);
                        if(balanceFactor(left) == -1) {
                            rotateLeft(left);
                        }
                        rotateRight(n);
                        break;
                    }
            case -1:
            case  1:
            case  0:
                    break;
            default:
                    // We should throw an error
                    assert(true == false);
        }

        n = p;
    }
}
Example #3
0
/**
 * Private method rebalance
 * Update heights and rebalances the tree
 * in order to mantain AVL property
 * @param n the node being rebalanced
 */
void AVLTree::rebalance(node *n) {
    if (!n) {
        return;
    }
    node *x = n;
    node *y = NULL;
    while (x->parent != NULL) {
        y = x->parent;
        y->height = max(height(y->leftChild), height(y->rightChild)) + 1;

        x = y;

        if (!balanced(x)) {
            if (height(x->rightChild) > height(x->leftChild)) {
                y = x->rightChild;
                if (height(y->rightChild) >= height(y->leftChild)) {
                    rotateLeft(x);
                } else {
                    rotateRight(y);
                    rotateLeft(x);
                }
            } else {
                y = x->leftChild;
                if (height(y->leftChild) >= height(y->rightChild)) {
                    rotateRight(x);
                } else {
                    rotateLeft(y);
                    rotateRight(x);
                }
            }
        }
    }
}
Example #4
0
void deleteFixup(RbtType *rbt, NodeType *x) {

    /* maintain red-black tree balance after deleting node x */

    while (x != rbt->root && x->color == BLACK) {
        if (x == x->parent->left) {
            NodeType *w = x->parent->right;
            if (w->color == RED) {
                w->color = BLACK;
                x->parent->color = RED;
                rotateLeft (rbt, x->parent);
                w = x->parent->right;
            }
            if (w->left->color == BLACK && w->right->color == BLACK) {
                w->color = RED;
                x = x->parent;
            } else {
                if (w->right->color == BLACK) {
                    w->left->color = BLACK;
                    w->color = RED;
                    rotateRight (rbt, w);
                    w = x->parent->right;
                }
                w->color = x->parent->color;
                x->parent->color = BLACK;
                w->right->color = BLACK;
                rotateLeft (rbt, x->parent);
                x = rbt->root;
            }
        } else {
            NodeType *w = x->parent->left;
            if (w->color == RED) {
                w->color = BLACK;
                x->parent->color = RED;
                rotateRight (rbt, x->parent);
                w = x->parent->left;
            }
            if (w->right->color == BLACK && w->left->color == BLACK) {
                w->color = RED;
                x = x->parent;
            } else {
                if (w->left->color == BLACK) {
                    w->right->color = BLACK;
                    w->color = RED;
                    rotateLeft (rbt, w);
                    w = x->parent->left;
                }
                w->color = x->parent->color;
                x->parent->color = BLACK;
                w->left->color = BLACK;
                rotateRight (rbt, x->parent);
                x = rbt->root;
            }
        }
    }
    x->color = BLACK;
}
Example #5
0
static void insertFixup(RbtType *rbt, NodeType *x) {

    /* maintain red-black tree balance after inserting node x */

    /* check red-black properties */
    while (x != rbt->root && x->parent->color == RED) {
        /* we have a violation */
        if (x->parent == x->parent->parent->left) {
            NodeType *y = x->parent->parent->right;
            if (y->color == RED) {

                /* uncle is RED */
                x->parent->color = BLACK;
                y->color = BLACK;
                x->parent->parent->color = RED;
                x = x->parent->parent;
            } else {

                /* uncle is BLACK */
                if (x == x->parent->right) {
                    /* make x a left child */
                    x = x->parent;
                    rotateLeft(rbt, x);
                }

                /* recolor and rotate */
                x->parent->color = BLACK;
                x->parent->parent->color = RED;
                rotateRight(rbt, x->parent->parent);
            }
        } else {

            /* mirror image of above code */
            NodeType *y = x->parent->parent->left;
            if (y->color == RED) {

                /* uncle is RED */
                x->parent->color = BLACK;
                y->color = BLACK;
                x->parent->parent->color = RED;
                x = x->parent->parent;
            } else {

                /* uncle is BLACK */
                if (x == x->parent->left) {
                    x = x->parent;
                    rotateRight(rbt, x);
                }
                x->parent->color = BLACK;
                x->parent->parent->color = RED;
                rotateLeft(rbt, x->parent->parent);
            }
        }
    }
    rbt->root->color = BLACK;
}
Example #6
0
static void deleteFixup(RBTree *tree, Node *x) {

  while (x != tree->root && x->color == BLACK) {
    if (x == x->parent->left) {
      Node *w = x->parent->right;
      if (w->color == RED) {
	w->color = BLACK;
	x->parent->color = RED;
	rotateLeft (tree, x->parent);
	w = x->parent->right;
      }
      if (w->left->color == BLACK && w->right->color == BLACK) {
	w->color = RED;
	x = x->parent;
      } else {
	if (w->right->color == BLACK) {
	  w->left->color = BLACK;
	  w->color = RED;
	  rotateRight (tree, w);
	  w = x->parent->right;
	}
	w->color = x->parent->color;
	x->parent->color = BLACK;
	w->right->color = BLACK;
	rotateLeft (tree, x->parent);
	x = tree->root;
      }
    } else {
      Node *w = x->parent->left;
      if (w->color == RED) {
	w->color = BLACK;
	x->parent->color = RED;
	rotateRight (tree, x->parent);
	w = x->parent->left;
      }
      if (w->right->color == BLACK && w->left->color == BLACK) {
	w->color = RED;
	x = x->parent;
      } else {
	if (w->left->color == BLACK) {
	  w->right->color = BLACK;
	  w->color = RED;
	  rotateLeft (tree, w);
	  w = x->parent->left;
	}
	w->color = x->parent->color;
	x->parent->color = BLACK;
	w->left->color = BLACK;
	rotateRight (tree, x->parent);
	x = tree->root;
      }
    }
  }
  x->color = BLACK;
}
Example #7
0
static void insertFixup(RBTree *tree, Node *x) {
  /* check Red-Black properties */
  while (x != tree->root && x->parent->color == RED) {
    /* we have a violation */
    if (x->parent == x->parent->parent->left) {
      Node *y = x->parent->parent->right;
      if (y->color == RED) {

	/* uncle is RED */
	x->parent->color = BLACK;
	y->color = BLACK;
	x->parent->parent->color = RED;
	x = x->parent->parent;
      } else {

	/* uncle is BLACK */
	if (x == x->parent->right) {
	  /* make x a left child */
	  x = x->parent;
	  rotateLeft(tree,x);
	}

	/* recolor and rotate */
	x->parent->color = BLACK;
	x->parent->parent->color = RED;
	rotateRight(tree, x->parent->parent);
      }
    } else {

      /* mirror image of above code */
      Node *y = x->parent->parent->left;
      if (y->color == RED) {

	/* uncle is RED */
	x->parent->color = BLACK;
	y->color = BLACK;
	x->parent->parent->color = RED;
	x = x->parent->parent;
      } else {

	/* uncle is BLACK */
	if (x == x->parent->left) {
	  x = x->parent;
	  rotateRight(tree, x);
	}
	x->parent->color = BLACK;
	x->parent->parent->color = RED;
	rotateLeft(tree,x->parent->parent);
      }
    }
  }
  tree->root->color = BLACK;
}
Example #8
0
static spltreeNode_t* insert_r(spltreeNode_t* current, spltreeNode_t* item,	uint8_t* context) {
	if (!current) {
		*context = 0;
		return item;
	}
	uint8_t direction = *context;
	if (current->key <= item->key) {
		*context = RIGHT;
		current->right = insert_r(current->right, item, context);

		if (*context == ZIGZIG_STEP) {
			*context = direction;
			return rotateLeft(rotateLeft(current));
		}
		if (*context == ZIGZAG_STEP) {
			*context = direction;
			current->right = rotateRight(current->right);
			return rotateLeft(current);
		}
		if (direction == ROOT)
			return rotateLeft(current);
		if (current->right == item) {
			*context = direction == RIGHT ? ZIGZIG_STEP : ZIGZAG_STEP;
			return current;
		}

		return current;
	} else {
		*context = LEFT;
		current->left = insert_r(current->left, item, context);

		if (*context == ZIGZIG_STEP) {
			*context = direction;
			return rotateRight(rotateRight(current));
		}
		if (*context == ZIGZAG_STEP) {
			*context = direction;
			current->left = rotateLeft(current->left);
			return rotateRight(current);
		}
		if (direction == ROOT)
			return rotateRight(current);
		if (current->left == item) {
			*context = direction == LEFT ? ZIGZIG_STEP : ZIGZAG_STEP;
			return current;
		}

		return current;
	}
}
Example #9
0
Node fix(Node root, Node n)
{
	Node rval = root;
	Node u, g;
	while ( n->parent != tnull )
	{
		u = getUncle(n);
		g = grandParent(n);

		if ( n->parent->color == BLACK ) {
			break;
		}
		// check what color the Uncle is

		if ( u != tnull && u->color == RED ) {
			n->parent->color = BLACK;
			u->color = BLACK;
			g->color = RED;
			n = g;
			// re-evaluate the current node
			continue;
		}

		if ( (n == n->parent->right) && (n->parent == g->left) )
		{
			rotateLeft(n);
			n = n->left;
		} else if ( (n == n->parent->left) && (n->parent == g->right)) {
			rotateRight(n);
			n = n->right;
		}

		n->parent->color = BLACK;
		g->color = RED;
		if ( n == n->parent->left ) {
			rotateRight(n->parent);
		} else {
			rotateLeft(n->parent);
		}
		n = n->parent;
		break;
	}
	if ( n->parent == tnull ) {
		rval = n;
	}
	rval->color = BLACK;
	return rval;
}
void RedBlackTree::insertFixup(TreeNode * pNode)
{
        if (pNode == m_pSentinel)
        {
            return; // impossible actually.   
        }

        TreeNode * pUncle = m_pSentinel;    
        TreeNode * pGrandParent = NULL;   
        while (pNode != m_pRoot && red == pNode->parent->color)    
        {   
            pUncle = getUncle(pNode);   
            pGrandParent = getGrandParent(pNode);   
            if (pUncle != NULL && pUncle != m_pSentinel && pUncle->color == red)   
            {   
                pNode->parent->color = black;   
                pUncle->color = black;   
                pGrandParent->color = red;   
                pNode = pGrandParent;   
            }   
            else  
            {   
                if (pNode->parent == pGrandParent->left)     
                {   
                    if (pNode == pNode->parent->right)   
                    {   
                        pNode = pNode->parent;   
                        rotateLeft(pNode);   
                    }   
                    pNode->parent->color = black;   
                    pGrandParent->color = red;   
                    rotateRight(pGrandParent);   
                }   
                else  
                {   
                    if (pNode == pNode->parent->left)   
                    {   
                        pNode = pNode->parent;   
                        rotateRight(pNode);   
                    }   
                    pNode->parent->color = black;   
                    pGrandParent->color = red;   
                    rotateLeft(pGrandParent);   
                }   
            }   
        }   
        m_pRoot->color = black;   
}
Example #11
0
FlightPanel::FlightPanel(QWidget *parent):
    QWidget(parent),
    wi(this),
    timer(this)
{
    up.reset(new QPushButton(QIcon(), QString("up"), this));
    down.reset(new QPushButton(QIcon(), QString("down"), this));
    left.reset(new QPushButton(QIcon(), QString("left"), this));
    right.reset(new QPushButton(QIcon(), QString("right"), this));

    up->resize(   controlHeight, controlHeight/2.5);
    down->resize( controlHeight, controlHeight/2.5);
    left->resize( controlHeight, controlHeight/2.5);
    right->resize(controlHeight, controlHeight/2.5);

    QRect rect = QRect(0, 0, this->width(), this->height() - 50);
    wi.setGeometry(rect);
    wi.setVisible(true);

    QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));

    QObject::connect(up.get(), SIGNAL(clicked()),    &wi, SLOT(rotateUp()));
    QObject::connect(down.get(), SIGNAL(clicked()),  &wi, SLOT(rotateDown()));
    QObject::connect(left.get(), SIGNAL(clicked()),  &wi, SLOT(rotateLeft()));
    QObject::connect(right.get(), SIGNAL(clicked()), &wi, SLOT(rotateRight()));
    timer.setInterval(200);
    timer.start();
}
/*
void MovieTree::fixUpAdd(Movie*)

Description:
Private method used to balance the tree after adding a movie.

Example:
MovieTree tree;
tree.addMoviebyTitle("Back to the Future", 1985, 96, 5);

Precondition:
The ends of the tree are nil. The tree is arranged correctly.

Postcondition:
The tree will be balanced.
*/
void MovieTree::fixUpAdd(Movie *x)
{
    x->isRed = true;

    while (x != root && x->parent->isRed) {
        if (x->parent == x->parent->parent->left) {
            Movie *y = x->parent->parent->right;

            if (y->isRed) {
                x->parent->isRed = false;
                y->isRed = false;
                x->parent->parent->isRed = true;
                x = x->parent->parent;
            } else {
                if (x == x->parent->right) {
                    x = x->parent;
                    rotateLeft(x);
                }

                x->parent->isRed = false;
                x->parent->parent->isRed = true;
                rotateRight(x->parent->parent);
            }
        } else {
            Movie *y = x->parent->parent->left;

            if (y->isRed) {
                x->parent->isRed = false;
                y->isRed = false;
                x->parent->parent->isRed = true;
                x = x->parent->parent;
            } else {
                if (x == x->parent->left) {
                    x = x->parent;
                    rotateRight(x);
                }

                x->parent->isRed = false;
                x->parent->parent->isRed = true;
                rotateLeft(x->parent->parent);
            }
        }
    }

    root->isRed = false;
}
Example #13
0
static spltreeNode_t* delete_max_rc(spltreeNode_t* splnode, spltreeNode_t** max) {
	if(!splnode->right){
		*max = splnode;
		return splnode->left;
	}
	splnode->right = delete_max_rc(splnode->right,max);
	if(!splnode->right) {
		return splnode;
	}
	return rotateLeft(splnode);
}
Example #14
0
void QMapDataBase::rebalance(QMapNodeBase *x)
{
    QMapNodeBase *&root = header.left;
    x->setColor(QMapNodeBase::Red);
    while (x != root && x->parent()->color() == QMapNodeBase::Red) {
        if (x->parent() == x->parent()->parent()->left) {
            QMapNodeBase *y = x->parent()->parent()->right;
            if (y && y->color() == QMapNodeBase::Red) {
                x->parent()->setColor(QMapNodeBase::Black);
                y->setColor(QMapNodeBase::Black);
                x->parent()->parent()->setColor(QMapNodeBase::Red);
                x = x->parent()->parent();
            } else {
                if (x == x->parent()->right) {
                    x = x->parent();
                    rotateLeft(x);
                }
                x->parent()->setColor(QMapNodeBase::Black);
                x->parent()->parent()->setColor(QMapNodeBase::Red);
                rotateRight (x->parent()->parent());
            }
        } else {
            QMapNodeBase *y = x->parent()->parent()->left;
            if (y && y->color() == QMapNodeBase::Red) {
                x->parent()->setColor(QMapNodeBase::Black);
                y->setColor(QMapNodeBase::Black);
                x->parent()->parent()->setColor(QMapNodeBase::Red);
                x = x->parent()->parent();
            } else {
                if (x == x->parent()->left) {
                    x = x->parent();
                    rotateRight(x);
                }
                x->parent()->setColor(QMapNodeBase::Black);
                x->parent()->parent()->setColor(QMapNodeBase::Red);
                rotateLeft(x->parent()->parent());
            }
        }
    }
    root->setColor(QMapNodeBase::Black);
}
Example #15
0
node* balancedTreeInsert(node* root , node* newnode)
{
	if(root == NULL)
	{
		return newnode ;
	}
	if(less(newnode->key,root->key))
	{
		root->lc = balancedTreeInsert(root->lc , newnode) ;
	}
	else
	{
		root->rc = balancedTreeInsert(root->rc , newnode) ;
	}
 
	root->ht = max(getHeight(root->lc) , getHeight(root->rc)) + 1 ;
 
	if(isBalanced(root))
		return root ;
 	z_cnt++ ;
	int balance = getBalance(root) ;
	node* ret = NULL ;
	if(balance > 1)
	{
		if(!less(newnode->key , root->lc->key))
		{
			root->lc = rotateLeft(root->lc) ;
		}
		ret = rotateRight(root) ;
	}
	if(balance < -1)
	{		
		if(less(newnode->key , root->rc->key))
		{
			root->rc = rotateRight(root->rc) ;
		}
		ret = rotateLeft(root) ;
	}
	return ret ;
}
Example #16
0
static spltreeNode_t* largest_r(spltreeNode_t* current, uint8_t* context, BOOL splay, BOOL del) {
	if (current->right == NULL) {
		*context = FOUND;
		return current;
	}
	if (splay) {
		current->right = largest_r(current->right, context, splay, del);
		if ((*context) == FOUND) {
			*context = ZIGZIG_STEP;
			return current;
		} else if ((*context) == ZIGZIG_STEP) {
			current = rotateLeft(rotateLeft(current));
			*context = FOUND;
			return current;
		}
	} else {
		spltreeNode_t* largest = largest_r(current->right, context, splay, del);
		if (current->right == largest) {
			current->right = largest->left;
		}
		return largest;
	}
	return NULL;
}
Example #17
0
void permute(int *v, const int start, const int n)
{
  // print(v, n);
	test(v, n);
  if (start < n) {
    int i, j;
    for (i = n-2; i >= start; i--) {
      for (j = i + 1; j < n; j++) {
    swap(v, i, j);
    permute(v, i+1, n);
      } // for j
      rotateLeft(v, i, n);
    } // for i
  }
} // permute
	// This function encodes a Region as a 64-bit integer so that it can be used as a key to access chunk data in the SQLite database.
	// A region actually contains more than 64-bits of data so some has to be lost here. Specifically we assume that we already know
	// the size of the region (so we only have to encode it's lower corner and not its upper corner or extents), and we also restrict
	// the range of valid coordinates. A Region's coordinates are represented by 3-bits of data, but we only support converting to a key 
	// if every coordinate can be represented by 21 bits of data. This way we can fit three coordinates only 63 bits of data. This limits
	// the range of values to +/- 2^20, which is enough for our purposes.
	uint64_t regionToKey(const PolyVox::Region& region)
	{
		// Cast to unsigned values so that bit shifting works predictably.
		uint32_t x = static_cast<uint32_t>(region.getLowerX());
		uint32_t y = static_cast<uint32_t>(region.getLowerY());
		uint32_t z = static_cast<uint32_t>(region.getLowerZ());

		// The magnitude of our input values is fairly restricted, but the values could stil be negative. This means the sign bit could
		// be set and this needs to be encoded as well. We therefore perform a left rotate on the bits to bring the sign bit into the LSB.
		x = rotateLeft(x);
		y = rotateLeft(y);
		z = rotateLeft(z);

		// Now convert to 64-bits
		uint64_t x64 = x;
		uint64_t y64 = y;
		uint64_t z64 = z;

		// Morten-encode the components to give our final key
		uint64_t result = EncodeMorton3(x64, y64, z64);

		// Return the combined value
		return result;
	}
 // Balances the subtree rooted at this node and returns the new root
 Node *balance() {
     int bal = getBalance();
     assert(std::abs(bal) <= 2);
     Node *result = this;
     if (bal == -2) {
         assert(std::abs(left->getBalance()) <= 1);
         if (left->getBalance() == +1)
             left = left->rotateLeft();
         result = rotateRight();
     } else if (bal == +2) {
         assert(std::abs(right->getBalance()) <= 1);
         if (right->getBalance() == -1)
             right = right->rotateRight();
         result = rotateLeft();
     }
     assert(std::abs(result->getBalance()) <= 1);
     return result;
 }
Example #20
0
void MAX::scrllD(int num, int time)
{
	int dill[8] = {1, 2, 3, 4, 5, 6, 7, 8};
	for ( int n = 1; n <= (num*8)+1; n++) 
	{
		//Displaying character based on values in dill array
		maxSingle(dill[0],0);
		maxSingle(dill[1],127);
		maxSingle(dill[2],127);
		maxSingle(dill[3],65); 
		maxSingle(dill[4],99);
		maxSingle(dill[5],62);
		maxSingle(dill[6],28);
		maxSingle(dill[7],0);
        //Shifting vales in dill array one to the left
		rotateLeft(dill,8);
        
		delay(time);
	}
}
Example #21
0
View::View(const QString &name, QWidget *parent)
    : QFrame(parent)
{
    setFrameStyle(Sunken | StyledPanel);
    graphicsView = new GraphicsView(this);
    graphicsView->setRenderHint(QPainter::Antialiasing, true);
    graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
    graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
    graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);

    int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
    QSize iconSize(size, size);

    QToolButton *zoomInIcon = new QToolButton;
    zoomInIcon->setAutoRepeat(true);
    zoomInIcon->setAutoRepeatInterval(33);
    zoomInIcon->setAutoRepeatDelay(0);
    zoomInIcon->setIcon(QPixmap(":/img/zoomin.png"));
    zoomInIcon->setIconSize(iconSize);
    QToolButton *zoomOutIcon = new QToolButton;
    zoomOutIcon->setAutoRepeat(true);
    zoomOutIcon->setAutoRepeatInterval(33);
    zoomOutIcon->setAutoRepeatDelay(0);
    zoomOutIcon->setIcon(QPixmap(":/img/zoomout.png"));
    zoomOutIcon->setIconSize(iconSize);
    zoomSlider = new QSlider;
    zoomSlider->setMinimum(0);
    zoomSlider->setMaximum(500);
    zoomSlider->setValue(250);
    zoomSlider->setTickPosition(QSlider::TicksRight);

    // Zoom slider layout
    QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
    zoomSliderLayout->addWidget(zoomInIcon);
    zoomSliderLayout->addWidget(zoomSlider);
    zoomSliderLayout->addWidget(zoomOutIcon);

    QToolButton *rotateLeftIcon = new QToolButton;
    rotateLeftIcon->setIcon(QPixmap(":/img/rotateleft.png"));
    rotateLeftIcon->setIconSize(iconSize);
    QToolButton *rotateRightIcon = new QToolButton;
    rotateRightIcon->setIcon(QPixmap(":/img/rotateright.png"));
    rotateRightIcon->setIconSize(iconSize);
    rotateSlider = new QSlider;
    rotateSlider->setOrientation(Qt::Horizontal);
    rotateSlider->setMinimum(-360);
    rotateSlider->setMaximum(360);
    rotateSlider->setValue(0);
    rotateSlider->setTickPosition(QSlider::TicksBelow);

    // Rotate slider layout
    QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
    rotateSliderLayout->addWidget(rotateLeftIcon);
    rotateSliderLayout->addWidget(rotateSlider);
    rotateSliderLayout->addWidget(rotateRightIcon);

    resetButton = new QToolButton;
    resetButton->setText(tr("reset"));
    resetButton->setEnabled(false);

    // Label layout
    QHBoxLayout *labelLayout = new QHBoxLayout;
    label = new QLabel(name);

    printButton = new QToolButton;
    printButton->setIcon(QIcon(QPixmap(":/img/fileprint.png")));

    labelLayout->addWidget(label);
    labelLayout->addStretch();
    labelLayout->addWidget(printButton);

    QGridLayout *topLayout = new QGridLayout;
    topLayout->addLayout(labelLayout, 0, 0);
    topLayout->addWidget(graphicsView, 1, 0);
    topLayout->addLayout(zoomSliderLayout, 1, 1);
    topLayout->addLayout(rotateSliderLayout, 2, 0);
    topLayout->addWidget(resetButton, 2, 1);
    setLayout(topLayout);

    connect(resetButton, SIGNAL(clicked()), this, SLOT(resetView()));
    connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
    connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
    connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)),
            this, SLOT(setResetButtonEnabled()));
    connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)),
            this, SLOT(setResetButtonEnabled()));
    connect(rotateLeftIcon, SIGNAL(clicked()), this, SLOT(rotateLeft()));
    connect(rotateRightIcon, SIGNAL(clicked()), this, SLOT(rotateRight()));
    connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn()));
    connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut()));
    connect(printButton, SIGNAL(clicked()), this, SLOT(print()));

    setupMatrix();
}
Example #22
0
void Lander::update(float dt)
{
	if (_bLandOver || _bLandOK)
	{
		return;
	}

    if (!_bCtrlByHuman)
    {
        if (_tickCount == _vecActions.size())
        {
            _actionFlag = NONE;
        }
        else
        {
            _actionFlag = _vecActions[_tickCount++];
        }
        
        switch (_actionFlag)
        {
            case 0:
            
                break;
            case 1:
                rotateLeft(dt);
                break;
            case 2:
                rotateRight(dt);
                break;
            case 3:
                thrust(dt);
                break;
            default:
                break;
        }
        if (_actionFlag == 3)
        {
            _fire->setVisible(true);
        }
        else
        {
            _fire->setVisible(false);
        }
    }
    else
    {
        if (_actionFlag & ROT_LEFT)
        {
            rotateLeft(dt);
        }
        if (_actionFlag & ROT_RIGHT)
        {
            rotateRight(dt);
        }
        if (_actionFlag & THRUST)
        {
            thrust(dt);
            _fire->setVisible(true);
        }
        else
        {
            _fire->setVisible(false);
        }
    }
    


//	_velocity.y += GRAVITY * dt;
    _velocity.y += GRAVITY_PER_TICK;
    
    auto pos = getPosition() + _velocity;
    if (pos.x > CLIENT_WIDTH)
    {
        pos.x = 0;
    }
    if (pos.x < 0)
    {
        pos.x = CLIENT_WIDTH;
    }
    
	setPosition(pos);
	
	if (getPositionY() <= _targetPos.y)
	{
		_bLandOver = true;
        calcFitnessScore();
	}
}
Example #23
0
void Medicine::handleObjectMenuSelect(uint8 messageType,Object* srcObject)
{
    if(PlayerObject* player = dynamic_cast<PlayerObject*>(srcObject))
    {
        switch(messageType)
        {
        case radId_itemRotateRight:
        {
            // Rotate the item 90 degrees to the right
            rotateRight(90.0f);
            gMessageLib->sendDataTransform053(this);
        }
        break;

        case radId_itemRotateLeft:
        {
            // Rotate the item 90 degrees to the left
            rotateLeft(90.0f);
            gMessageLib->sendDataTransform053(this);
        }
        break;

        case radId_itemUse:
        {
            switch(mItemType)
            {
            case ItemType_Stimpack_A:
            case ItemType_Stimpack_B:
            case ItemType_Stimpack_C:
            case ItemType_Stimpack_D:
            case ItemType_Stimpack_E:
                handleStimpackMenuSelect(messageType, player, stim);
                break;
            case ItemType_Ranged_Stimpack_A:
            case ItemType_Ranged_Stimpack_B:
            case ItemType_Ranged_Stimpack_C:
            case ItemType_Ranged_Stimpack_D:
            case ItemType_Ranged_Stimpack_E:
                handleStimpackMenuSelect(messageType, player, rangedstim);
                break;
            case ItemType_Wound_Action_A:
            case ItemType_Wound_Action_B:
            case ItemType_Wound_Action_C:
            case ItemType_Wound_Action_D:
            case ItemType_Wound_Action_E:
                handleWoundPackMenuSelect(messageType, player, action);
                break;
            case ItemType_Wound_Constitution_A:
            case ItemType_Wound_Constitution_B:
            case ItemType_Wound_Constitution_C:
            case ItemType_Wound_Constitution_D:
            case ItemType_Wound_Constitution_E:
                handleWoundPackMenuSelect(messageType, player, constitution);
                break;
            case ItemType_Wound_Health_A:
            case ItemType_Wound_Health_B:
            case ItemType_Wound_Health_C:
            case ItemType_Wound_Health_D:
            case ItemType_Wound_Health_E:
                handleWoundPackMenuSelect(messageType, player, health);
                break;
            case ItemType_Wound_Quickness_A:
            case ItemType_Wound_Quickness_B:
            case ItemType_Wound_Quickness_C:
            case ItemType_Wound_Quickness_D:
            case ItemType_Wound_Quickness_E:
                handleWoundPackMenuSelect(messageType, player, quickness);
                break;
            case ItemType_Wound_Stamina_A:
            case ItemType_Wound_Stamina_B:
            case ItemType_Wound_Stamina_C:
            case ItemType_Wound_Stamina_D:
            case ItemType_Wound_Stamina_E:
                handleWoundPackMenuSelect(messageType, player, stamina);
                break;
            case ItemType_Wound_Strength_A:
            case ItemType_Wound_Strength_B:
            case ItemType_Wound_Strength_C:
            case ItemType_Wound_Strength_D:
            case ItemType_Wound_Strength_E:
                handleWoundPackMenuSelect(messageType, player, strength);
                break;
            }

        }
        break;

        default:
            break;
        }

    }
}
Example #24
0
bool CardDisplay::CheckUserInput(JButton key)
{
    if (JGE_BTN_SEC == key || JGE_BTN_PRI == key || JGE_BTN_UP == key || JGE_BTN_DOWN == key)
    {
        if (listener)
        {
            listener->ButtonPressed(mId, 0);
            return true;
        }
    }
    if (!mObjects.size()) return false;

    if (mActionButton == key)
    {
        if (mObjects[mCurr] && mObjects[mCurr]->ButtonPressed())
        {
            CardGui * cardg = (CardGui *) mObjects[mCurr];
            if (tc)
            {
                tc->toggleTarget(cardg->card);
                return true;
            }
            else
            {
                if (observer) observer->ButtonPressed(cardg);
                return true;
            }
        }
        return true;
    }

    switch (key)
    {
    case JGE_BTN_LEFT:
    {
        int n = mCurr;
        n--;
        if (n < start_item)
        {
            if (n < 0)
            {
                n = 0;
            }
            else
            {
                rotateLeft();
            }
        }
        if (n != mCurr && mObjects[mCurr] != NULL && mObjects[mCurr]->Leaving(JGE_BTN_LEFT))
        {
            mCurr = n;
            mObjects[mCurr]->Entering();
        }
        return true;
    }
    case JGE_BTN_RIGHT:
    {
        int n = mCurr;
        n++;
        if (n >= (int) (mObjects.size()))
        {
            n = mObjects.size() - 1;
        }
        if (n >= start_item + nb_displayed_items)
        {
            rotateRight();
        }
        if (n != mCurr && mObjects[mCurr] != NULL && mObjects[mCurr]->Leaving(JGE_BTN_RIGHT))
        {
            mCurr = n;
            mObjects[mCurr]->Entering();
        }
        return true;
    }
    default:
    {
      bool result = false;
      unsigned int distance2;
      unsigned int minDistance2 = -1;
      int n = mCurr;
      int x1,y1;
      JButton key;
      JGE* jge = observer?observer->getInput():JGE::GetInstance();
      if(jge)
      {
          if (jge->GetLeftClickCoordinates(x1, y1))
          {
              for (size_t i = 0; i < mObjects.size(); i++)
              {
                  float top, left;
                  if (mObjects[i]->getTopLeft(top, left))
                  {
                      distance2 = static_cast<unsigned int>((top - y1) * (top - y1) + (left - x1) * (left - x1));
                      if (distance2 < minDistance2)
                      {
                          minDistance2 = distance2;
                          n = i;
                      }
                  }
              }

              if (n < mCurr)
                  key = JGE_BTN_LEFT;
              else
                  key = JGE_BTN_RIGHT;

              if (n < start_item)
              {
                  rotateLeft();
              }
              else if (n >= (int)(mObjects.size()) && mObjects.size())
              {
                  n = mObjects.size() - 1;
              }
              if (n >= start_item + nb_displayed_items)
              {
                  rotateRight();
              }

              if (n != mCurr && mObjects[mCurr] != NULL && mObjects[mCurr]->Leaving(key))
              {
                  mCurr = n;
                  mObjects[mCurr]->Entering();
                  result = true;
              }
              jge->LeftClickedProcessed();
          }
      }
      return result;
    }
    }

    return false;
}
Example #25
0
void Enemy::think()
{
    // TODO: Move the following stuff into something like ramEnemyShip(Vector3 enemyLoc, Vector3 enemyVel);
    // Placeholder: attempt to ram player. Can be modified for "travel to coords (x,y)"
    boost::shared_ptr<Ship> playerShip = manager.getRoomMgr()->giveCurrentRoom()->getPlayerShip();
    Vector3 playerCoords = playerShip->getLocation();
    Vector3 playerVelocity = playerShip->getVelocity();
    float playerDeltaX = playerCoords.x - location.x;
    float playerDeltaY = playerCoords.y - location.y;
    Vector3 distanceToPlayer;
    float distance_to_player = abs(sqrt( (playerDeltaX*playerDeltaX) + (playerDeltaY*playerDeltaY) ));
    distanceToPlayer.x = playerCoords.x - location.x;
    distanceToPlayer.y = playerCoords.y - location.y;
    
    // What is the angle between this ship and the player ship?
    float angle = 0.0f;
    angle = atan(distanceToPlayer.x / distanceToPlayer.y);
    if (distanceToPlayer.x <= 0 && distanceToPlayer.y >= 0)
    {
        angle = 6.28318f - (-1*angle);
    } else if (distanceToPlayer.x >= 0 && distanceToPlayer.y >= 0)
    {
        angle = angle;
    } else if (distanceToPlayer.x <= 0 && distanceToPlayer.y <= 0)
    {
        angle = 3.14159 + angle;
    } else if (distanceToPlayer.x >= 0 && distanceToPlayer.y <= 0)
    {
        angle = 3.14159 - (-1*angle);
    } else
    {
        fprintf(stderr, "Odd location at enemy think()\n");
    }

    // Figure out which is the fastest way to face the enemy

    // How much the ship has to turn to face zero? These are both positive as they are used
    // For figuring out what is the shortest path. Of course, turning left causes the rotation to be negative
    float distance_to_zero_via_right = 6.28318f - shipRotation;
    float distance_to_zero_via_left = shipRotation;

    float enemy_distance_to_zero_via_right = 6.28318f - angle;
    float enemy_distance_to_zero_via_left = angle;
    float rotational_distance = 0.0f;

    if (shipRotation+0.1 > angle)
    {
        float distance_by_turning_left = shipRotation - angle;
        float distance_by_turning_right_via_zero = distance_to_zero_via_right + enemy_distance_to_zero_via_left;

        if (distance_by_turning_left < distance_by_turning_right_via_zero)
        {
            rotational_distance = distance_by_turning_left;
            rotateLeft();
        }
        else
        {
            rotational_distance = distance_by_turning_right_via_zero;
            rotateRight();
        }
    } else if (shipRotation-0.1 < angle)
    {
        float distance_by_turning_right = angle - shipRotation;
        float distance_by_turning_left_via_zero = distance_to_zero_via_left + enemy_distance_to_zero_via_right;
        
        if (distance_by_turning_right < distance_by_turning_left_via_zero)
        {
            rotational_distance = distance_by_turning_right;
            rotateRight();
        }
        else
        {
            rotational_distance = distance_by_turning_left_via_zero;
            rotateLeft();
        }
    }

    if (distance_to_player > 100.0)
        accelerate();
    else
        come_to_halt();
}
/*
void MovieTree::fixUpDelete(Movie*)

Description:
Private method used to balance the tree after deleting a movie.

Example:
MovieTree tree;
tree.deleteMovie("Back to the Future");

Precondition:
The ends of the tree are nil. The tree is arranged correctly.

Postcondition:
The tree will be balanced.
*/
void MovieTree::fixUpDelete(Movie *x)
{
    while (x != root && !(x->isRed)) {
        if (x == x->parent->left) {
            Movie *w = x->parent->right;

            if (w->isRed) {
                w->isRed = false;
                x->parent->isRed = true;
                rotateLeft(x->parent);
                w = x->parent->right;
            }

            if (!(w->left->isRed) && !(w->right->isRed)) {
                w->isRed = true;
                x = x->parent;
            } else {
                if (!(w->right->isRed)) {
                    w->left->isRed = false;
                    w->isRed = true;
                    rotateRight(w);
                    w = x->parent->right;
                }

                w->isRed = x->parent->isRed;
                w->parent->isRed = false;
                w->right->isRed = false;
                rotateLeft(x->parent);
                x = root;
            }
        } else {
            Movie *w = x->parent->left;

            if (w->isRed) {
                w->isRed = false;
                x->parent->isRed = true;
                rotateRight(x->parent);
                w = x->parent->left;
            }

            if (!(w->right->isRed) && !(w->left->isRed)) {
                w->isRed = true;
                x = x->parent;
            } else {
                if (!(w->left->isRed)) {
                    w->right->isRed = false;
                    w->isRed = true;
                    rotateLeft(w);
                    w = x->parent->left;
                }

                w->isRed = x->parent->isRed;
                x->parent->isRed = false;
                w->left->isRed = false;
                rotateRight(x->parent);
                x = root;
            }
        }
    }

    x->isRed = false;
}
Example #27
0
void QMapDataBase::freeNodeAndRebalance(QMapNodeBase *z)
{
    QMapNodeBase *&root = header.left;
    QMapNodeBase *y = z;
    QMapNodeBase *x;
    QMapNodeBase *x_parent;
    if (y->left == 0) {
        x = y->right;
    } else {
        if (y->right == 0) {
            x = y->left;
        } else {
            y = y->right;
            while (y->left != 0)
                y = y->left;
            x = y->right;
        }
    }
    if (y != z) {
        z->left->setParent(y);
        y->left = z->left;
        if (y != z->right) {
            x_parent = y->parent();
            if (x)
                x->setParent(y->parent());
            y->parent()->left = x;
            y->right = z->right;
            z->right->setParent(y);
        } else {
            x_parent = y;
        }
        if (root == z)
            root = y;
        else if (z->parent()->left == z)
            z->parent()->left = y;
        else
            z->parent()->right = y;
        y->setParent(z->parent());
        // Swap the colors
        QMapNodeBase::Color c = y->color();
        y->setColor(z->color());
        z->setColor(c);
        y = z;
    } else {
        x_parent = y->parent();
        if (x)
            x->setParent(y->parent());
        if (root == z)
            root = x;
        else if (z->parent()->left == z)
            z->parent()->left = x;
        else
            z->parent()->right = x;
    }
    if (y->color() != QMapNodeBase::Red) {
        while (x != root && (x == 0 || x->color() == QMapNodeBase::Black)) {
            if (x == x_parent->left) {
                QMapNodeBase *w = x_parent->right;
                if (w->color() == QMapNodeBase::Red) {
                    w->setColor(QMapNodeBase::Black);
                    x_parent->setColor(QMapNodeBase::Red);
                    rotateLeft(x_parent);
                    w = x_parent->right;
                }
                if ((w->left == 0 || w->left->color() == QMapNodeBase::Black) &&
                    (w->right == 0 || w->right->color() == QMapNodeBase::Black)) {
                    w->setColor(QMapNodeBase::Red);
                    x = x_parent;
                    x_parent = x_parent->parent();
                } else {
                    if (w->right == 0 || w->right->color() == QMapNodeBase::Black) {
                        if (w->left)
                            w->left->setColor(QMapNodeBase::Black);
                        w->setColor(QMapNodeBase::Red);
                        rotateRight(w);
                        w = x_parent->right;
                    }
                    w->setColor(x_parent->color());
                    x_parent->setColor(QMapNodeBase::Black);
                    if (w->right)
                        w->right->setColor(QMapNodeBase::Black);
                    rotateLeft(x_parent);
                    break;
                }
            } else {
            QMapNodeBase *w = x_parent->left;
            if (w->color() == QMapNodeBase::Red) {
                w->setColor(QMapNodeBase::Black);
                x_parent->setColor(QMapNodeBase::Red);
                rotateRight(x_parent);
                w = x_parent->left;
            }
            if ((w->right == 0 || w->right->color() == QMapNodeBase::Black) &&
                (w->left == 0 || w->left->color() == QMapNodeBase::Black)) {
                w->setColor(QMapNodeBase::Red);
                x = x_parent;
                x_parent = x_parent->parent();
            } else {
                if (w->left == 0 || w->left->color() == QMapNodeBase::Black) {
                    if (w->right)
                        w->right->setColor(QMapNodeBase::Black);
                    w->setColor(QMapNodeBase::Red);
                    rotateLeft(w);
                    w = x_parent->left;
                }
                w->setColor(x_parent->color());
                x_parent->setColor(QMapNodeBase::Black);
                if (w->left)
                    w->left->setColor(QMapNodeBase::Black);
                rotateRight(x_parent);
                break;
            }
        }
    }
    if (x)
        x->setColor(QMapNodeBase::Black);
    }
    free(y);
    --size;
}
Example #28
0
View::View(const QString &name, QWidget *parent)
    : QFrame(parent)
{
    setFrameStyle(Sunken | StyledPanel);
    graphicsView = new GraphicsView(this);
    graphicsView->setRenderHint(QPainter::Antialiasing, false);
    graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
    graphicsView->setOptimizationFlags(QGraphicsView::DontSavePainterState);
    graphicsView->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
    graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);

    int size = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
    QSize iconSize(size, size);

    QToolButton *zoomInIcon = new QToolButton;
    zoomInIcon->setAutoRepeat(true);
    zoomInIcon->setAutoRepeatInterval(33);
    zoomInIcon->setAutoRepeatDelay(0);
    zoomInIcon->setIcon(QPixmap(":/zoomin.png"));
    zoomInIcon->setIconSize(iconSize);
    QToolButton *zoomOutIcon = new QToolButton;
    zoomOutIcon->setAutoRepeat(true);
    zoomOutIcon->setAutoRepeatInterval(33);
    zoomOutIcon->setAutoRepeatDelay(0);
    zoomOutIcon->setIcon(QPixmap(":/zoomout.png"));
    zoomOutIcon->setIconSize(iconSize);
    zoomSlider = new QSlider;
    zoomSlider->setMinimum(0);
    zoomSlider->setMaximum(500);
    zoomSlider->setValue(250);
    zoomSlider->setTickPosition(QSlider::TicksRight);

    // Zoom slider layout
    QVBoxLayout *zoomSliderLayout = new QVBoxLayout;
    zoomSliderLayout->addWidget(zoomInIcon);
    zoomSliderLayout->addWidget(zoomSlider);
    zoomSliderLayout->addWidget(zoomOutIcon);

    QToolButton *rotateLeftIcon = new QToolButton;
    rotateLeftIcon->setIcon(QPixmap(":/rotateleft.png"));
    rotateLeftIcon->setIconSize(iconSize);
    QToolButton *rotateRightIcon = new QToolButton;
    rotateRightIcon->setIcon(QPixmap(":/rotateright.png"));
    rotateRightIcon->setIconSize(iconSize);
    rotateSlider = new QSlider;
    rotateSlider->setOrientation(Qt::Horizontal);
    rotateSlider->setMinimum(-360);
    rotateSlider->setMaximum(360);
    rotateSlider->setValue(0);
    rotateSlider->setTickPosition(QSlider::TicksBelow);

    // Rotate slider layout
    QHBoxLayout *rotateSliderLayout = new QHBoxLayout;
    rotateSliderLayout->addWidget(rotateLeftIcon);
    rotateSliderLayout->addWidget(rotateSlider);
    rotateSliderLayout->addWidget(rotateRightIcon);

    resetButton = new QToolButton;
    resetButton->setText(tr("0"));
    resetButton->setEnabled(false);


    // Label layout
    QHBoxLayout *labelLayout = new QHBoxLayout;
    label = new QLabel(name);
    label2 = new QLabel(tr("Pointer Mode"));
    selectModeButton = new QToolButton;
    //selectModeButton->setText(tr("Select"));
    selectModeButton->setIcon(QPixmap(":/choose.png"));
    selectModeButton->setIconSize(iconSize);
    selectModeButton->setCheckable(true);
    selectModeButton->setChecked(true);
    dragModeButton = new QToolButton;
    //dragModeButton->setText(tr("Drag"));
    dragModeButton->setIcon(QPixmap(":/Press-and-Drag.png"));
    dragModeButton ->setIconSize(iconSize);
    dragModeButton->setCheckable(true);
    dragModeButton->setChecked(false);
    Btn_Pathfinding = new QToolButton;
    //Btn_Pathfinding ->setText(tr("PathFinding"));
    Btn_Pathfinding ->setIcon(QPixmap(":/pathfinding.png"));
    Btn_Pathfinding ->setIconSize(iconSize);
    Btn_Pathfinding ->setEnabled(true);
    InputDataBtn = new QToolButton;
    //InputDataBtn ->setText(tr("InputData"));
    InputDataBtn->setIcon(QPixmap(":/inputdata.png"));
    InputDataBtn->setIconSize(iconSize);
    InputDataBtn ->setEnabled(true);

    //Scene choose
    label3 = new QLabel(tr("HelpPoint Number"));
    combobox = new QComboBox;
    combobox-> addItem("1");
    combobox-> addItem("2");
    combobox-> addItem("3");

    //scene_check = new QToolButton;
    //scene_check -> setText(tr("Scene_test"));
    //scene_check -> setEnabled(true);

    //EditWindow
    EditWindow = new QTextEdit;
    EditWindow ->setStyleSheet("QTextEdit{background:darkGray}");
    EditWindow->setReadOnly(true);



#ifndef QT_NO_OPENGL
    //openGlButton->setEnabled(QGLFormat::hasOpenGL());
#else
    openGlButton->setEnabled(false);
#endif
    //printButton = new QToolButton;
    //printButton->setIcon(QIcon(QPixmap(":/fileprint.png")));

    QButtonGroup *pointerModeGroup = new QButtonGroup;
    pointerModeGroup->setExclusive(true);
    pointerModeGroup->addButton(selectModeButton);
    pointerModeGroup->addButton(dragModeButton);

    labelLayout->addWidget(label);
   //labelLayout->addStretch();
    labelLayout->addWidget(InputDataBtn);
    //labelLayout->addStretch();
    labelLayout->addWidget(Btn_Pathfinding);
    labelLayout->addStretch();
    labelLayout->addWidget(label2);
    labelLayout->addWidget(selectModeButton);
    labelLayout->addWidget(dragModeButton);
    labelLayout->addStretch();
    labelLayout->addWidget(label3);
    labelLayout->addWidget(combobox);

    QGridLayout *topLayout = new QGridLayout;
    topLayout->addLayout(labelLayout, 0, 0);
    topLayout->addWidget(graphicsView, 1, 0);
    topLayout->addLayout(zoomSliderLayout, 1, 1);
    topLayout->addWidget(EditWindow,1,2);
    topLayout->addLayout(rotateSliderLayout, 2, 0);
    topLayout->addWidget(resetButton, 2, 1);
    setLayout(topLayout);

    connect(resetButton, SIGNAL(clicked()), this, SLOT(resetView()));
    connect(zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
    connect(rotateSlider, SIGNAL(valueChanged(int)), this, SLOT(setupMatrix()));
    connect(graphicsView->verticalScrollBar(), SIGNAL(valueChanged(int)),
            this, SLOT(setResetButtonEnabled()));
    connect(graphicsView->horizontalScrollBar(), SIGNAL(valueChanged(int)),
            this, SLOT(setResetButtonEnabled()));
    connect(selectModeButton, SIGNAL(toggled(bool)), this, SLOT(togglePointerMode()));
    connect(dragModeButton, SIGNAL(toggled(bool)), this, SLOT(togglePointerMode()));
    //connect(antialiasButton, SIGNAL(toggled(bool)), this, SLOT(toggleAntialiasing()));
    //connect(openGlButton, SIGNAL(toggled(bool)), this, SLOT(toggleOpenGL()));
    connect(rotateLeftIcon, SIGNAL(clicked()), this, SLOT(rotateLeft()));
    connect(rotateRightIcon, SIGNAL(clicked()), this, SLOT(rotateRight()));
    connect(zoomInIcon, SIGNAL(clicked()), this, SLOT(zoomIn()));
    connect(zoomOutIcon, SIGNAL(clicked()), this, SLOT(zoomOut()));
    //connect(scene_check, SIGNAL(clicked()), this, SLOT(BtnsetScene()));


    setupMatrix();

    my_serialport = new QSerialPort;
    my_serialport->setPortName("COM3");
    my_serialport->open(QIODevice::ReadWrite);
    my_serialport->setBaudRate(38400);
    my_serialport->setDataBits(QSerialPort::Data8);
    my_serialport->setParity(QSerialPort::NoParity);
    my_serialport->setFlowControl(QSerialPort::NoFlowControl);
    connect(my_serialport,SIGNAL(readyRead()),this,SLOT(my_readuart()));

}
void RedBlackTree::removeFixup(TreeNode * pNode)
{
    TreeNode * pSibling = NULL;   
    while ((pNode != m_pRoot) && (pNode->color == black))   
    {   
        pSibling = getSibling(pNode);   
        if (pNode == pNode->parent->left) // left child node   
        {   
            if (pSibling->color == red)   
            {   
                // case 1, can change to case 2, 3, 4   
                pNode->parent->color = red;   
                pSibling->color = black;   
                rotateLeft(pNode->parent);   
                // change to new sibling,    
                pSibling = pNode->parent->right;   
            }   
            // case 2;   
            if ((black == pSibling->left->color) && (black == pSibling->right->color))    
            {    
                pSibling->color = red;    
                pNode = pNode->parent;    
            }   
            else  
            {   
                if (black == pSibling->right->color)    
                {    
                    pSibling->color = red;    
                    pSibling->left->color = black;    
                    rotateRight(pSibling);    
                    pSibling = pNode->parent->right;    
                }   
                pSibling->color = pNode->parent->color;   
                pNode->parent->color = black;   
                pSibling->right->color = black;   
                rotateLeft(pNode->parent);   
                break;    
            }   
        }   
        else  
        {   
            if (pSibling->color == red)   
            {   
                // case 1, can change to case 2, 3, 4   
                pNode->parent->color = red;   
                pSibling->color = black;   
                rotateRight(pNode->parent);   
                // change to new sibling,    
                pSibling = pNode->parent->left;   
            }   
            // case 2;   
            if ((black == pSibling->left->color) && (black == pSibling->right->color))    
            {    
                pSibling->color = red;    
                pNode = pNode->parent;    
            }   
            else  
            {   
                if (black == pSibling->left->color)    
                {    
                    pSibling->color = red;    
                    pSibling->right->color = black;    
                    rotateLeft(pSibling);    
                    pSibling = pNode->parent->left;    
                }   
                pSibling->color = pNode->parent->color;   
                pNode->parent->color = black;   
                pSibling->left->color = black;   
                rotateRight(pNode->parent);   
                break;    
            }   
        }   
    }   
    pNode->color = black;   
}
Example #30
0
/// process a full block
void Keccak::processBlock(const void* data)
{
#if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
#define LITTLEENDIAN(x) swap(x)
#else
#define LITTLEENDIAN(x) (x)
#endif

  const uint64_t* data64 = (const uint64_t*) data;
  // mix data into state
  for (unsigned int i = 0; i < m_blockSize / 8; i++)
    m_hash[i] ^= LITTLEENDIAN(data64[i]);

  // re-compute state
  for (unsigned int round = 0; round < KeccakRounds; round++)
  {
    // Theta
    uint64_t coefficients[5];
    for (unsigned int i = 0; i < 5; i++)
      coefficients[i] = m_hash[i] ^ m_hash[i + 5] ^ m_hash[i + 10] ^ m_hash[i + 15] ^ m_hash[i + 20];

    for (unsigned int i = 0; i < 5; i++)
    {
      uint64_t one = coefficients[mod5(i + 4)] ^ rotateLeft(coefficients[mod5(i + 1)], 1);
      m_hash[i     ] ^= one;
      m_hash[i +  5] ^= one;
      m_hash[i + 10] ^= one;
      m_hash[i + 15] ^= one;
      m_hash[i + 20] ^= one;
    }

    // temporary
    uint64_t one;

    // Rho Pi
    uint64_t last = m_hash[1];
    one = m_hash[10]; m_hash[10] = rotateLeft(last,  1); last = one;
    one = m_hash[ 7]; m_hash[ 7] = rotateLeft(last,  3); last = one;
    one = m_hash[11]; m_hash[11] = rotateLeft(last,  6); last = one;
    one = m_hash[17]; m_hash[17] = rotateLeft(last, 10); last = one;
    one = m_hash[18]; m_hash[18] = rotateLeft(last, 15); last = one;
    one = m_hash[ 3]; m_hash[ 3] = rotateLeft(last, 21); last = one;
    one = m_hash[ 5]; m_hash[ 5] = rotateLeft(last, 28); last = one;
    one = m_hash[16]; m_hash[16] = rotateLeft(last, 36); last = one;
    one = m_hash[ 8]; m_hash[ 8] = rotateLeft(last, 45); last = one;
    one = m_hash[21]; m_hash[21] = rotateLeft(last, 55); last = one;
    one = m_hash[24]; m_hash[24] = rotateLeft(last,  2); last = one;
    one = m_hash[ 4]; m_hash[ 4] = rotateLeft(last, 14); last = one;
    one = m_hash[15]; m_hash[15] = rotateLeft(last, 27); last = one;
    one = m_hash[23]; m_hash[23] = rotateLeft(last, 41); last = one;
    one = m_hash[19]; m_hash[19] = rotateLeft(last, 56); last = one;
    one = m_hash[13]; m_hash[13] = rotateLeft(last,  8); last = one;
    one = m_hash[12]; m_hash[12] = rotateLeft(last, 25); last = one;
    one = m_hash[ 2]; m_hash[ 2] = rotateLeft(last, 43); last = one;
    one = m_hash[20]; m_hash[20] = rotateLeft(last, 62); last = one;
    one = m_hash[14]; m_hash[14] = rotateLeft(last, 18); last = one;
    one = m_hash[22]; m_hash[22] = rotateLeft(last, 39); last = one;
    one = m_hash[ 9]; m_hash[ 9] = rotateLeft(last, 61); last = one;
    one = m_hash[ 6]; m_hash[ 6] = rotateLeft(last, 20); last = one;
                      m_hash[ 1] = rotateLeft(last, 44);

    // Chi
    for (unsigned int j = 0; j < 25; j += 5)
    {
      // temporaries
      uint64_t one = m_hash[j];
      uint64_t two = m_hash[j + 1];

      m_hash[j]     ^= m_hash[j + 2] & ~two;
      m_hash[j + 1] ^= m_hash[j + 3] & ~m_hash[j + 2];
      m_hash[j + 2] ^= m_hash[j + 4] & ~m_hash[j + 3];
      m_hash[j + 3] ^=      one      & ~m_hash[j + 4];
      m_hash[j + 4] ^=      two      & ~one;
    }

    // Iota
    m_hash[0] ^= XorMasks[round];
  }
}