/* ============================================================================= * fixAfterInsertion * ============================================================================= */ static void fixAfterInsertion (rbtree_t* s, node_t* x) { STF(x, c, RED); while (x != NULL && x != LDNODE(s, root)) { node_t* xp = LDNODE(x, p); if (LDF(xp, c) != RED) { break; } /* TODO: cache g = ppx = PARENT_OF(PARENT_OF(x)) */ if (PARENT_OF(x) == LEFT_OF(PARENT_OF(PARENT_OF(x)))) { node_t* y = RIGHT_OF(PARENT_OF(PARENT_OF(x))); if (COLOR_OF(y) == RED) { SET_COLOR(PARENT_OF(x), BLACK); SET_COLOR(y, BLACK); SET_COLOR(PARENT_OF(PARENT_OF(x)), RED); x = PARENT_OF(PARENT_OF(x)); } else { if (x == RIGHT_OF(PARENT_OF(x))) { x = PARENT_OF(x); ROTATE_LEFT(s, x); } SET_COLOR(PARENT_OF(x), BLACK); SET_COLOR(PARENT_OF(PARENT_OF(x)), RED); if (PARENT_OF(PARENT_OF(x)) != NULL) { ROTATE_RIGHT(s, PARENT_OF(PARENT_OF(x))); } } } else { node_t* y = LEFT_OF(PARENT_OF(PARENT_OF(x))); if (COLOR_OF(y) == RED) { SET_COLOR(PARENT_OF(x), BLACK); SET_COLOR(y, BLACK); SET_COLOR(PARENT_OF(PARENT_OF(x)), RED); x = PARENT_OF(PARENT_OF(x)); } else { if (x == LEFT_OF(PARENT_OF(x))) { x = PARENT_OF(x); ROTATE_RIGHT(s, x); } SET_COLOR(PARENT_OF(x), BLACK); SET_COLOR(PARENT_OF(PARENT_OF(x)), RED); if (PARENT_OF(PARENT_OF(x)) != NULL) { ROTATE_LEFT(s, PARENT_OF(PARENT_OF(x))); } } } } node_t* ro = LDNODE(s, root); if (LDF(ro, c) != BLACK) { STF(ro, c, BLACK); } }
void trickle_down (int index, struct pqueue *queue) { void *tmp; int which; /* Save current node as tmp node. */ tmp = queue->array[index]; /* Continue until the node have at least one (left) child. */ while (HAVE_CHILD (index, queue)) { /* If right child exists, and if the right child is more proper to be moved upper. */ if (RIGHT_OF (index) < queue->size && (*queue->cmp) (queue->array[LEFT_OF (index)], queue->array[RIGHT_OF (index)]) > 0) which = RIGHT_OF (index); else which = LEFT_OF (index); /* If the tmp node should be upper than the child, break. */ if ((*queue->cmp) (queue->array[which], tmp) > 0) break; /* Actually trickle down the tmp node. */ queue->array[index] = queue->array[which]; if (queue->update != NULL) (*queue->update) (queue->array[index], index); index = which; } /* Restore the tmp node to appropriate place. */ queue->array[index] = tmp; if (queue->update != NULL) (*queue->update) (tmp, index); }
///King Mask Generation Function // - @Returns a U64 of King's Full attack at position loc // uint64_t chssEng::kgMsk(uint_fast8_t loc) { uint64_t mask = 0; const uint_fast8_t kgShifts[4] = { 1,7,8,9 }; //Left mask |= IN_RANGE(loc - kgShifts[0]) ? (uint64_t{ 1 } << (loc - kgShifts[0])) : (0); //Right mask |= IN_RANGE(loc + kgShifts[0]) ? (uint64_t{ 1 } << (loc + kgShifts[0])) : (0); //Lower Left mask |= (IN_RANGE(loc - kgShifts[1]) && LEFT_OF(loc, loc - kgShifts[1])) ? (uint64_t{ 1 } << (loc - kgShifts[1])) : (0); //Upper Right mask |= (IN_RANGE(loc + kgShifts[1]) && RIGHT_OF(loc, loc + kgShifts[1])) ? (uint64_t{ 1 } << (loc + kgShifts[1])) : (0); //Down mask |= IN_RANGE(loc - kgShifts[2]) ? (uint64_t{ 1 } << (loc - kgShifts[2])) : (0); //Up mask |= IN_RANGE(loc + kgShifts[2]) ? (uint64_t{ 1 } << (loc + kgShifts[2])) : (0); //Lower Right mask |= (IN_RANGE(loc - kgShifts[3]) && RIGHT_OF(loc, loc - kgShifts[3])) ? (uint64_t{ 1 } << (loc - kgShifts[3])) : (0); //Upper Left mask |= (IN_RANGE(loc + kgShifts[3]) && LEFT_OF(loc, loc + kgShifts[3])) ? (uint64_t{ 1 } << (loc + kgShifts[3])) : (0); return mask; }
/* ============================================================================= * fixAfterDeletion * ============================================================================= */ static void fixAfterDeletion (rbtree_t* s, node_t* x) { while (x != LDNODE(s,root) && COLOR_OF(x) == BLACK) { if (x == LEFT_OF(PARENT_OF(x))) { node_t* sib = RIGHT_OF(PARENT_OF(x)); if (COLOR_OF(sib) == RED) { SET_COLOR(sib, BLACK); SET_COLOR(PARENT_OF(x), RED); ROTATE_LEFT(s, PARENT_OF(x)); sib = RIGHT_OF(PARENT_OF(x)); } if (COLOR_OF(LEFT_OF(sib)) == BLACK && COLOR_OF(RIGHT_OF(sib)) == BLACK) { SET_COLOR(sib, RED); x = PARENT_OF(x); } else { if (COLOR_OF(RIGHT_OF(sib)) == BLACK) { SET_COLOR(LEFT_OF(sib), BLACK); SET_COLOR(sib, RED); ROTATE_RIGHT(s, sib); sib = RIGHT_OF(PARENT_OF(x)); } SET_COLOR(sib, COLOR_OF(PARENT_OF(x))); SET_COLOR(PARENT_OF(x), BLACK); SET_COLOR(RIGHT_OF(sib), BLACK); ROTATE_LEFT(s, PARENT_OF(x)); /* TODO: consider break ... */ x = LDNODE(s,root); } } else { /* symmetric */ node_t* sib = LEFT_OF(PARENT_OF(x)); if (COLOR_OF(sib) == RED) { SET_COLOR(sib, BLACK); SET_COLOR(PARENT_OF(x), RED); ROTATE_RIGHT(s, PARENT_OF(x)); sib = LEFT_OF(PARENT_OF(x)); } if (COLOR_OF(RIGHT_OF(sib)) == BLACK && COLOR_OF(LEFT_OF(sib)) == BLACK) { SET_COLOR(sib, RED); x = PARENT_OF(x); } else { if (COLOR_OF(LEFT_OF(sib)) == BLACK) { SET_COLOR(RIGHT_OF(sib), BLACK); SET_COLOR(sib, RED); ROTATE_LEFT(s, sib); sib = LEFT_OF(PARENT_OF(x)); } SET_COLOR(sib, COLOR_OF(PARENT_OF(x))); SET_COLOR(PARENT_OF(x), BLACK); SET_COLOR(LEFT_OF(sib), BLACK); ROTATE_RIGHT(s, PARENT_OF(x)); /* TODO: consider break ... */ x = LDNODE(s, root); } } } if (x != NULL && LDF(x,c) != BLACK) { STF(x, c, BLACK); } }