/* set[0].way_head, way_tail */ void init_plru(){ cache_blk_t *ptr; int i; /* must check if I might exchange head for tail !!!!!! */ for(i=0,ptr=cp->way_head;ptr!=NULL;ptr=ptr->way_next,i++) cacheLeafs[i]=ptr; //now the leafs contains the ways of the associative cache assert(i==16); //if it's not some error happened for(i=0;i<TREENODES;i++) //is it correct or do I have to add 1's also? prluTree[i]=isRightChild(i); }
void promote(cache_blk_t *p){ /* direct the prlu bits away */ int x; int parent; x=getIndex(p); x+=16; #ifdef DEBUG printf("Promoting index %d with value %d\n",x,p->value); #endif while(!isRoot(x)){ parent=getParent(x); #ifdef DEBUG printf("father of %d is %d\n", x,parent); #endif prluTree[parent]=!isRightChild(x); //if it is a right child make parents x=parent; //prlu bit to zero } }
void RbTreeUtil::insertAt(RbTreeAnchor *tree, RbTreeNode *parentNode, bool leftChildFlag, RbTreeNode *newNode) { BSLS_ASSERT(parentNode); BSLS_ASSERT(newNode); BSLS_ASSERT(tree); newNode->setLeftChild(0); newNode->setRightChild(0); // Insert the node as a leaf in the tree, and assign its parent // pointer. newNode->makeRed(); newNode->setParent(parentNode); if (leftChildFlag) { parentNode->setLeftChild(newNode); if (parentNode == tree->firstNode()) { tree->setFirstNode(newNode); } } else { parentNode->setRightChild(newNode); } // Fix the tree coloring (if necessary). // Implementation Note: The following is adapted with few changes from // "Introduction to Algorithms" [Cormen, Leiserson, Rivest] , except that // explicit conditions are required to treat NULL values as Black, and // consequently to avoid setting a color (BLACK) on a (already BLACK) null // node. RbTreeNode *node = newNode; while (node != tree->rootNode() && node->parent()->isRed()) { if (isLeftChild(node->parent())) { RbTreeNode *uncle = node->parent()->parent()->rightChild(); // Test if 'uncle' is BLACK (0 is considered BLACK) if (uncle && uncle->isRed()) { // Case 1: grandParent[node] -> (X:B) // / \. // parent[node]- > (Y:R) (uncle:R) // / // (node:R) node->parent()->parent()->makeRed(); node->parent()->makeBlack(); uncle->makeBlack(); node = node->parent()->parent(); } else { if (isRightChild(node)) { // Case 2: grandParent[node] -> (X:B) // / \. // parent[node]- > (Y:R) (uncle:B) // \. // (node:R) // // Perform a left rotation on parent[node] to reduce to // case 3. node = node->parent(); rotateLeft(node); } // Case 3: grandParent[node] -> (X:B) // / \. // parent[node]- > (Y:R) (uncle:B) // / // (node:R) // // Recolor the parent black, the grand parent-red, then rotate // the grand-parent right, so that its the new root of the // sub-tree. node->parent()->makeBlack(); node->parent()->parent()->makeRed(); rotateRight(node->parent()->parent()); } } else { // The following mirrors the cases above, but with right and left // exchanged. RbTreeNode *uncle = node->parent()->parent()->leftChild(); if (uncle && uncle->isRed()) { node->parent()->parent()->makeRed(); node->parent()->makeBlack(); uncle->makeBlack(); node = node->parent()->parent(); } else { if (isLeftChild(node)) { node = node->parent(); rotateRight(node); } node->parent()->makeBlack(); node->parent()->parent()->makeRed(); rotateLeft(node->parent()->parent()); } } } BSLS_ASSERT(tree->sentinel() == tree->rootNode()->parent()); tree->rootNode()->makeBlack(); tree->incrementNumNodes(); }