void adjustAfterInsertion(rbtree *root, rbtree *n) { // New node is RED n->color=RED; // Correcting double red problems, if they exist if (n != NULL && n != root && isRed(n->parent)) { // Case A. Recolor, and move up to see if more work // needed if (isRed(sibling(n->parent))) { setcolor(n->parent,BLACK); setcolor(sibling(n->parent),BLACK); setcolor(grandparent(n),RED); adjustAfterInsertion(root, grandparent(n)); } // Case B: Restructure for a parent who is the left child of the // grandparent. This will require a single right rotation if n is // also // a left child, or a left-right rotation otherwise. else if (n->parent == (grandparent(n))->left) { if (n == n->parent->right) { n=n->parent; left_rotate(root, n); } setcolor(n->parent,BLACK); setcolor(grandparent(n),RED); right_rotate(root, grandparent(n)); } // Case C: Restructure for a parent who is the right child of the // grandparent. This will require a single left rotation if n is // also // a right child, or a right-left rotation otherwise. else if (n->parent == (grandparent(n))->right) { if (n == n->parent->left) { n=n->parent; right_rotate(root, n); } setcolor(n->parent,BLACK); setcolor(grandparent(n),RED); left_rotate(root, grandparent(n)); } } // At last: Color the root black root->color=BLACK; }
//移动一个棋子严格到(fx,fy)-(jx,jy)线段上,以达到将军或者躲将的目的 void MoveToFuckOrAvoid::Segment:: F**k(int id, bool red, int fx, int fy, int jx, int jy){ //red表示当前是走红还是走黑,(fx, fy)表示车,(jx, jy)表示将 string str = red? "JCMPXSB" : "jcmpxsb"; for(int i = 0; i < nrow; i++){ for(int j = 0; j < ncol; j++){ if(!red && !isBlack(id, i, j)) continue; else if(red && !isRed(id, i, j)) continue; char c = d[id].At(i, j); int dx[5], dy[5], n = 0; if(c == str[1]) Che_segment(id, i, j, fx, fy, jx, jy, dx, dy, n); else if(c == str[2]) Ma_segment(id, i, j, fx, fy, jx, jy, dx, dy, n); else if(c == str[3]) Che_segment(id, i, j, fx, fy, jx, jy, dx, dy, n);//没错 else if(c == str[4]) Xiang_segment(id, i, j, fx, fy, jx, jy, dx, dy, n); else if(c == str[5]) Shi_segment(id, i, j, fx, fy, jx, jy, dx, dy, n); else if(c == str[6]) Bin_segment(id, red, i, j, fx, fy, jx, jy, dx, dy, n); for(int k = 0; k < n; k++){ if(d[id].At(dx[k], dy[k]) != '_') continue; if(red) Newd(id, fx, fy, i, j, dx[k], dy[k]); else Newd(id, -1, -1, i, j, dx[k], dy[k]); Filter(red); } } } }
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::deletemin(RBTNode<T, KEY> *h) { if (h->pLeft == NULL) { // be careful here hasn't check if the h is empty. //std::cout << "Delete min " << h->key << std::endl; delete h; return NULL; } if (!isRed(h->pLeft) && !isRed(h->pLeft->pLeft)) h = MoveRedLeft(h); h->pLeft = deletemin(h->pLeft); if (isRed(h->pRight)) h = RotateLeft(h); return h; }
Node* TreeBARSub(Tree* aTree, Node* curnode, int which, int index) { Node* sibling = curnode->parent->child[which]; if (isRed(sibling)) { sibling->red = 0; curnode->parent->red = 1; TreeRotate(aTree, curnode->parent, !which, index); sibling = curnode->parent->child[which]; } if (!sibling) curnode = curnode->parent; else if (isBlack(sibling->child[!which]) && isBlack(sibling->child[which])) { sibling->red = 1; curnode = curnode->parent; } else { if (isBlack(sibling->child[which])) { sibling->child[!which]->red = 0; sibling->red = 1; TreeRotate(aTree, sibling, which, index); sibling = curnode->parent->child[which]; } sibling->red = curnode->parent->red; curnode->parent->red = 0; sibling->child[which]->red = 0; TreeRotate(aTree, curnode->parent, !which, index); curnode = aTree->index[index].root; } return curnode; }
void test_isRed_given_a_node_with_black_color_should_return_0(void) { int result; setNode(&node1, NULL, NULL, 'b'); Node *root = &node1; result = isRed(&root); TEST_ASSERT_EQUAL(0, result); }
static RBTreeNodeHandle rbtree_delete_priv(RBTreeNodeHandle root, i64 key, RBTreeNodeHandle *deleteNode){ if (NULL == root) return NULL; if (key < root->key){ if (NULL == root->left) return NULL; /*delete minimal node*/ if (!isRed(root->left) && !isRed(root->left->left)) root = moveRedLeft(root); root->left = rbtree_delete_priv(root->left, key, deleteNode); }else{ if (isRed(root->left)) root = rotateRight(root); /*delete leaf node*/ if ((root->right == NULL) && (root->key == key)){ /*AGILE_LOGD("find the key: %lld", key);*/ *deleteNode = root; return NULL; } if (NULL != root->right){ if (!isRed(root->right) && !isRed(root->right->left)) root = moveRedRight(root); if (root->key == key){ RBTreeNodeHandle min_node = NULL; min_node = min(root->right); AGILE_LOGD("the min key: %lld", min_node->key); root->key = min_node->key; root->value = min_node->value; root->right = deleteMin(root->right, deleteNode); /**deleteNode = min_node;*/ }else{ root->right = rbtree_delete_priv(root->right, key, deleteNode); } } } return fixup(root); }
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::MoveRedRight(RBTNode<T, KEY> *h) { regaincolors(h); if (isRed(h->pLeft->pLeft)) { h = RotateRight(h); flipcolors(h); } return h; }
void removeElement(const Key& k) // remove using key throw(NonexistentElementException) { BTPosition u = finder(k, BST::T.root()); // find the node if (u.isNull()) // not found? throw NonexistentElementException("Remove nonexistent element"); BTPosition r = remover(u); // remove u if (BST::T.isRoot(r) || isRed(r) || wasParentRed(r)) setBlack(r); // fix by color change else // r, parent both black remedyDoubleBlack(r); // fix double-black r }
template<typename T, typename KEY> void RBTree<T, KEY>::deletemin() { if (pRoot) // check if the tree is empty. { if (!isRed(pRoot->pLeft)) // Root contains only one node. Both its left and right child are black. pRoot->color = RED; pRoot = deletemin(pRoot); if (pRoot) pRoot->color = BLACK; } }
template<typename T, typename KEY> void RBTree<T, KEY>::deletemax() { if (pRoot) // check if the tree is empty. { if (!isRed(pRoot->pLeft)) pRoot->color = RED; pRoot = deletemax(pRoot); if (pRoot) pRoot->color = BLACK; } }
void TreeBalanceAfterAdd(Tree* aTree, Node* curnode, int index) { while (curnode && isRed(curnode->parent) && curnode->parent->parent) { if (curnode->parent == curnode->parent->parent->child[LEFT]) curnode = TreeBAASub(aTree, curnode, RIGHT, index); else curnode = TreeBAASub(aTree, curnode, LEFT, index); } aTree->index[index].root->red = 0; }
/* LOGIC LOOP */ void worldLoop(int noOfGenerations){ int x, y, i; cell_t* cell; for(i = 0 ; i < 4 * noOfGenerations ; i++){ // if(i % 4 == 1) // fprintf(stdout, "Iteration %d Red\n", (i/4) + 1); // // if(i % 4 == 3) // fprintf(stdout, "Iteration %d Black\n", (i/4) + 1); for(y = 0 ; y < worldSideLen ; y++){ for(x = 0 ; x < worldSideLen ; x++){ cell = getCell(x, y); if(i % 4 == 0){ cell->starvation--; cell->breeding++; } if (((i % 4 == 0) && isRed(x, y)) || ((i % 4 == 2) && !isRed(x, y))) { switch(cell->type){ case EMPTY: break; case ICE: break; case TREE: break; case SQUIRREL: doSquirrelStuff(x, y, cell); break; case TREE_WITH_SQUIRREL: doSquirrelStuff(x, y, cell); break; case WOLF: doWolfStuff(x, y, cell); break; } } else if(i%4==3 || i%4==1){ update(cell); } } } //if (i%4==1 || i%4==3) //printWorld2d(stdout); /* pressEntertoContinue(); */ } }
template<typename T, typename KEY> void RBTree<T, KEY>::deleteone(KEY Key) { if (pRoot) { //std::cout << "Deleting: " << Key << std::endl; if (!isRed(pRoot->pLeft)) pRoot->color = RED; pRoot = deleteone(pRoot, Key); if (pRoot) pRoot->color = BLACK; } }
/*pre-order traverse the binary tree*/ static void rbtree_dump_priv(RBTreeNodeHandle root, i32 print_flag, i32 *num){ if (NULL == root) return; rbtree_dump_priv(root->left, print_flag, num); *num = *num + 1; if (print_flag){ AGILE_LOGI("node %d: [parent(key: %lld, color: %s) key: %lld - color: %s]", *num, root->parent ? root->parent->key : -1, root->parent ? (isRed(root->parent) ? "RED" : "BLACK") : "NULL", root->key, (isRed(root) ? "RED" : "BLACK")); } rbtree_dump_priv(root->right, print_flag, num); }
void findReds(unsigned char *src, unsigned char *mask, int iw, int ih, short *rect) { int recX = rect[0], recY = rect[1], recW = rect[2], recH = rect[3]; int y, x; for (y = 0; y < recH; y++) { int sy = (recY + y) * iw; for (x = 0; x < recW; x++) { int p = (recX + x + sy) * 4; mask[x + y * recW] = ((isRed(src, p)) ? 1 : 0); } } }
QString CardOCR::suit(const QImage * img_wb, const QImage * img) { if ((img_wb->width() != img->width()) || (img_wb->height() != img->height())) return QString(); const int w = img->width(); const int h = img->height(); QRgb clMinValue; qreal minVal = 1.0; for (int x = 0; x < w; ++x) { for (int y = 0; y < h; ++y) { QRgb rgb_wb = img_wb->pixel(x, y); //рассматриваем только черные точки if (rgb_wb == qRgb(0, 0, 0)) { QRgb rgb = img->pixel(x, y); QColor cl(rgb); if (cl.valueF() < minVal) { minVal = cl.valueF(); clMinValue = rgb; //qDebug() << x << y; } } } } if (isRed(clMinValue)) return "h"; else if (isBlue(clMinValue)) return "d"; else if (isGreen(clMinValue)) return "c"; else return "s"; return QString(); }
void dialateMaskIfRed(unsigned char *src, int iw, int ih, unsigned char *mask, unsigned char *out, short *rect) { int recX = rect[0], recY = rect[1], recW = rect[2], recH = rect[3]; int y, x; for (y = 1; y < recH - 1; y++) { int row = recW * y; int sy = (recY + y) * iw; for (x = 1; x < recW - 1; x++) { int p = (recX + x + sy) * 4; char b = (mask[row + x] | mask[row + x + 1] | mask[row + x - 1] | mask[row + x - recW] | mask[row + x + recW]); if (b != 0 && isRed(src, p)) out[row + x] = 1; else out[row + x] = mask[row + x]; } } }
void CamCapture::showSegmentation() { CvScalar pixel; for (int x = 0; x < width_var; ++x) { for (int y = 0; y < height_var; ++y) { if(isRed(x,y)) { setPixel3C(pixel,0,0,255); } else if(isBlue(x, y)) { setPixel3C(pixel,255,0,0); } else if(isYellow(x, y)) { setPixel3C(pixel,0,255,255); } else if(isGreen(x, y)) { setPixel3C(pixel,0,255,0); } else if(isWhite(x, y)) { setPixel3C(pixel, 255, 255, 255); } else if(isBlack(x, y)) { setPixel3C(pixel, 127, 127, 127); } else { setPixel3C(pixel,0,0,0); } cvSet2D(showSeg, y, x, pixel); } } }
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::put(RBTNode<T, KEY> *h, T Value, KEY Key) { if (h == NULL) { h = new RBTNode<T, KEY>(Value, Key, RED); return h; } if (h->key > Key) h->pLeft = put(h->pLeft, Value, Key); else if (h->key < Key) h->pRight = put(h->pRight, Value, Key); else h->value = Value; if (!isRed(h->pLeft) && isRed(h->pRight)) { h = RotateLeft(h); } if (isRed(h->pLeft) && isRed(h->pLeft->pLeft)) { h=RotateRight(h); } if (isRed(h->pLeft) && isRed(h->pRight)) { flipcolors(h); } return h; }
Node* TreeBAASub(Tree* aTree, Node* curnode, int which, int index) { Node* uncle = curnode->parent->parent->child[which]; if (isRed(uncle)) { curnode->parent->red = uncle->red = 0; curnode = curnode->parent->parent; curnode->red = 1; } else { if (curnode == curnode->parent->child[which]) { curnode = curnode->parent; TreeRotate(aTree, curnode, !which, index); } curnode->parent->red = 0; curnode->parent->parent->red = 1; TreeRotate(aTree, curnode->parent->parent, which, index); } return curnode; }
static RBTreeNodeHandle rbtree_insert_priv(RBTreeNodeHandle root, i64 key, void *value){ /*insert the node at the bottom*/ if (NULL == root){ RBTreeNodeHandle node = (RBTreeNodeHandle)mag_malloc(sizeof(RBTREE_NODE_t)); if (NULL != node){ node->color = RBTREE_TRUE; node->key = key; node->value = value; node->left = NULL; node->right = NULL; node->parent = NULL; } return node; } /*split 4-node on the way down*/ if (isRed(root->left) && isRed(root->right)) colorFlip(root); /*standard binary tree insert*/ if (root->key == key){ root->value = value; ++gRepeatKeyNum; }else if (root->key < key){ root->right = rbtree_insert_priv(root->right, key, value); root->right->parent = root; }else{ root->left = rbtree_insert_priv(root->left, key, value); root->left->parent = root; } /*fix right-leaning red node: this will assure that a 3-node is the left child*/ if (isRed(root->right) && !isRed(root->left)) root = rotateLeft(root); /*fix two reds in a row: this will rebalance a 4-node.*/ if (isRed(root->left) && isRed(root->left->left)) root = rotateRight(root); return root; }
template<typename T, typename KEY> RBTNode<T, KEY> *RBTree<T, KEY>::deleteone(RBTNode<T, KEY> *h, KEY Key) { // Delete a node. // However will be not sure Key exists in the tree. Check if the Key exists before deleting. if (Key < h->key) { if (!isRed(h->pLeft) && !isRed(h->pLeft->pLeft)) h = MoveRedLeft(h); h->pLeft = deleteone(h->pLeft, Key); } else{ if (isRed(h->pLeft)) h = RotateRight(h); if ((Key == h->key) && !(h->pRight) ) { //std::cout << "Delete one specified node: " << h->key << ", value: " << h->value << std::endl; delete h; return NULL; } if (!isRed(h->pRight) && !isRed(h->pRight->pLeft)) h = MoveRedRight(h); if (Key == h->key) { //std::cout << "To delete " << h->key << ", delete subtree's min node" << std::endl; RBTNode<T, KEY> *temp = getmin(h->pRight); h->value = temp->value; h->key = temp->key; h->pRight = deletemin(h->pRight); } else h->pRight = deleteone(h->pRight, Key); } if (isRed(h->pRight)) h = RotateLeft(h); return h; }
typename RedBlackBST<T>::pointer_type RedBlackBST<T>::insert(pointer_type node, const value_type& val){ if (node == nullptr){ return new Node<T>(val, true); } if (val < node->data){ node->left = insert(node->left, val); } else{ node->right = insert(node->right, val); } if (isRed(node->right) && !isRed(node->left)){ // std::cout << "Rotate Left" << std::endl; node = rotateLeft(node); } if (isRed(node->left) && isRed(node->left->left)){ // std::cout << "Rotate Right" << std::endl; node = rotateRight(node); } if (isRed(node->left) && isRed(node->right)){ // std::cout << "Color Flip" << std::endl; flipColor(node); } return node; }
void ColorSeqDetector::input(IplImage *inputImg) { if(!img) { img = cvCreateImage(cvGetSize(inputImg), IPL_DEPTH_32F, 1); cvZero(img); } if(!stateImg) { stateImg = cvCreateImage(cvGetSize(inputImg), IPL_DEPTH_8U, 3); cvZero(stateImg); } else if(img->width != inputImg->width || img->height != inputImg->height) { cvReleaseImage(&img); img = cvCreateImage(cvGetSize(inputImg), IPL_DEPTH_32F, 1); cvReleaseImage(&stateImg); stateImg = cvCreateImage(cvGetSize(inputImg), IPL_DEPTH_8U, 3); cvZero(stateImg); cvZero(img); } uchar *data; int step; CvSize(size); uchar *state_data; int state_step; float *float_data; int float_step; cvGetRawData(inputImg, (uchar**)&data, &step, &size); step /= sizeof(data[0]); cvGetRawData(img, (uchar**)&float_data, &float_step, &size); float_step /= sizeof(float_data[0]); cvGetRawData(stateImg, (uchar**)&state_data, &state_step, &size); state_step /= sizeof(state_data[0]); for(int y = 0; y < size.height; y++, data += step, state_data+=state_step, float_data+=float_step ) for(int x = 0; x < size.width; x++ ) { uchar h = data[3*x]; uchar s = data[3*x+1]; uchar v = data[3*x+2]; uchar state = state_data[3*x]; uchar sstate = state_data[3*x+1]; uchar bstate = state_data[3*x+1]; float score = float_data[x]; switch(state){ case 0: // invalid state if(isRed(h,s,v)) { state = 1; sstate = 0; bstate = 0; } break; case 1: // red state if(isRed(h,s,v)) { sstate++; if(sstate > period+2) { sstate = 0; state = 0; } }else if(sstate >= period-2 && isGreen(h,s,v)) { state = 2; sstate = 0; bstate = 0; }else{ bstate ++; if(bstate > 1) { state = 0; sstate = 0; } } break; case 2: // green state if(isGreen(h,s,v)) { sstate++; if(sstate > period+2) { sstate = 0; state = 0; } }else if(sstate >= period - 2 && isBlue(h,s,v)) { state = 3; sstate = 0; bstate = 0; }else{ bstate ++; if(bstate > 1) { state = 0; sstate = 0; } } break; case 3: // blue state if(isBlue(h,s,v)) { sstate++; if(sstate > period+2) { sstate = 0; state = 0; } }else if(sstate >= period-2 && isYellow(h,s,v)) { state = 4; sstate = 0; bstate = 0; }else{ bstate ++; if(bstate > 1) { state = 0; sstate = 0; } } break; case 4: // yellow state if(isYellow(h,s,v)) { sstate++; if(sstate > period+2) { sstate = 0; state = 0; } }else if(sstate >= period-2 && isRed(h,s,v)) { state = 1; sstate = 0; bstate = 0; score = 1.; //float_data[x]=1.; }else{ bstate ++; if(bstate > 1) { state = 0; sstate = 0; } } break; default: state = 0; sstate = 0; break; } state_data[3*x] = state; state_data[3*x+1] = sstate; //float_data[x] = (float) state / 4.f; score -= 0.005; if(score < 0. ) { score = 0.; } float_data[x] = score; // if(state == 0){ // float_data[x] = 0.; // } } emitNamedEvent("output", img); }
void adjustAfterRemoval(rbtree *root, rbtree *n) { while (n != root && isBlack(n)) { if (n == n->parent->left) { // Pulled up node is a left child rbtree *sib; sib = sibling(n); if(sib==NULL) return; if (isRed(sib)) { setcolor(sib, BLACK); setcolor(n->parent, RED); left_rotate(root, n->parent); sib = n->parent->right; } if (isBlack(sib->left) && isBlack(sib->right)) { setcolor(sib, RED); n = n->parent; } else { if (isBlack(sib->right)) { setcolor(sib->left, BLACK); setcolor(sib, RED); right_rotate(root, sib); sib = n->parent->right; } setcolor(sib, n->parent->color); setcolor(n->parent, BLACK); setcolor(sib->right, BLACK); left_rotate(root,n->parent); n = root; } } else { // pulled up node is a right child rbtree *sib = sibling(n); if(sib==NULL) return; if (isRed(sib)) { setcolor(sib, BLACK); setcolor(n->parent, RED); right_rotate(root, n->parent); sib = n->parent->left; } if (isBlack(sib->left) && isBlack(sib->right)) { setcolor(sib, RED); n = n->parent; } else { if (isBlack(sib->left)) { setcolor(sib->right, BLACK); setcolor(sib, RED); left_rotate(root,sib); sib = n->parent->left; } setcolor(sib, n->parent->color); setcolor(n->parent, BLACK); setcolor(sib->left, BLACK); right_rotate(root, n->parent); n = root; } } } setcolor(n, BLACK); }
bool hasRedChild(const BTPosition& p) const // does p have red child? { return (isRed(BST::T.leftChild(p)) || isRed(BST::T.rightChild(p))); }
int suitsDiffer(card_t *c1, card_t *c2) { return (isRed(c1) && isBlack(c2)) || (isRed(c2) && isBlack(c1)); }
bool PathFindingApp::update(yam2d::ESContext* ctx, float deltaTime) { if (!m_appRunning) return false; if (deltaTime > 0.1f) deltaTime = 0.1f; #if defined(_WIN32) if( isKeyReleased(yam2d::KEY_SPACE) ) { quit(); } // Restart search if r pressed if (isKeyReleased(yam2d::KEY_R)) { m_textureStartCase = 0; m_texturePathFound = 0; } #endif if (m_textureStartCase == 0) { // Delete old and load new m_texturePathFound = 0; const char* const inFileName = "input.png"; char buf[100]; sprintf_s(buf, "Start finding path from input image: \"%s\"", inFileName); yam2d::esLogMessage(buf); m_text->setText(buf); m_textureStartCase = new yam2d::Texture(inFileName, true); // Copy input data to map. m_searchTimer = 0.0f; int width = m_textureStartCase->getWidth(); int height = m_textureStartCase->getHeight(); int bpp = m_textureStartCase->getBytesPerPixel(); yam2d::Ref<yam2d::StreamTexture> newTexture = new yam2d::StreamTexture(); newTexture->setData(m_textureStartCase->getData(), width, height, bpp); m_texturePathFound = newTexture; m_searchCompleted = false; } if (!m_searchCompleted) { // Find start and end int startX, startY, endX, endY; startX = startY = endX = endY = -1; for (int y = 0; y < m_textureStartCase->getHeight(); ++y) { for (int x = 0; x < m_textureStartCase->getWidth(); ++x) { unsigned char* p = m_textureStartCase->getPixel(x, y); if (isRed(p)) { // Red pixel startX = x; startY = y; } else if (isGreen(p)) { // Green pixel } else if (isBlue(p)) { // Blue pixel endX = x; endY = y; } } } // Update path find!! Set m_searchCompleted to true, when path found, so the texture data is updated. if (startX >= 0 && startY >= 0 && endX >= 0 && endY >= 0) { yam2d::ElapsedTimer timer; timer.reset(); m_searchCompleted = doPathfinding(startX, startY, endX, endY); m_searchTimer += timer.getTime(); // Update new data to the GPU m_texturePathFound->updateData(); } else { assert(0); } if (m_searchCompleted) { char buf[100]; sprintf_s(buf, "Path find done. Time spent %.3f seconds", m_searchTimer); yam2d::esLogMessage("%s\n",buf); m_text->setText(buf); } } // if (!m_searchCompleted) // Clear sprite before add new dynamic sprites. m_batch->clear(); // Add sprites. m_batch->addSprite(m_textureStartCase, m_spriteStartCase, yam2d::vec2(-256.0f - 20.0f, 0.0f), 0.0f, yam2d::vec2(512.0f, 512.0f)); m_batch->addSprite(m_texturePathFound, m_spritePathFound, yam2d::vec2(256.0f + 20.0f, 0.0f), 0.0f, yam2d::vec2(512.0f, 512.0f)); // Add text to position -400,300 m_batch->addText(m_fontTexture, m_text, yam2d::vec2(0, ((float)ctx->height) / 2.0f - 20.0f), 0.0f); return true; }