void CipherText::compute_node(element_t& v, Node* node){//v amounts to s if(node->getType() == LEAF){ Leaf* leaf = (Leaf*)node; leaf->compute(&v, this->pub, this->p); // printf("leaf: %d, %d, computed\n", leaf->getK(), leaf->getNum()); } else if (node->getType() == INTERNAL_NODE){ InternalNode* internalNode = (InternalNode*)node; int num = internalNode->getNum(); int k = internalNode->getK(); Node** sons = internalNode->getSons();//?? // printf("internal Node: %d, %d\n", k, num); element_t* ys = (element_t*)malloc(sizeof(element_t) * (num + 1)); element_init_Zr(ys[0], *(this->p)); element_set(ys[0], v); //set ys[0] to v computePoints(ys, k, num); //compute other num point, int i = 1; for (i = 1; i <= num; i++){ compute_node(ys[i], sons[i - 1]); } } }
virtual void processNode(InternalNode &V) { V.setLeftSubstMatrix(Q.instantiate(V.getLeftDistance())); V.setRightSubstMatrix(Q.instantiate(V.getRightDistance())); int id=V.getID(); if(id>largestID) largestID=id; ++numNodes; }
void InternalNode::borrowRight() { InternalNode * rightSib = (InternalNode *) getRightSibling(); insert(rightSib->children[0]); //add min value from sibling to borrower rightSib->removeDriver(0); //remove min value from sibling if(parent) parent->resetMinimum(rightSib); } //InternalNode::borrowRight()
void InternalNode::borrowLeft() { InternalNode * leftSib = (InternalNode *) getLeftSibling(); int max = leftSib->getMaximum(); insert(leftSib->children[count-1]); //add max value from sibling to front of borrower leftSib->removeDriver(getPosition(max)); //remove the max value from sibling if(parent) parent->resetMinimum(this); } //InternalNode::borrowLeft()
Policy::Policy(unsigned char* buf, pairing_t* p) { //get nodeNum int pos = 0; int numberOfNode = Utility::str2int(buf); pos += 4; // printf("node num is %d\n", numberOfNode); this->nodeNum = numberOfNode; //reconstruct access tree form bytes Node** nodes = (Node**)malloc(sizeof(Node*) * this->nodeNum); //store all nodes int index = 0; Node* node = NULL; int nodeSize = 0; for(index = 0; index < numberOfNode; index++){ printf("index: %d\n", index); //get father position int father = Utility::str2int(buf + pos); pos += 4; printf("father is %d\t", father); //get node size to indentify node type nodeSize = Utility::str2int(buf + pos); pos += 4; //printf("node size is %d\t", nodeSize); if(nodeSize == -1){ nodeSize = INTERNAL_NODE_SIZE; node = new InternalNode(buf + pos); printf("internal node\t"); InternalNode* t = (InternalNode*)node; printf("%d, %d\t", t->getK(), t->getNum()); } else if(nodeSize == -2){ nodeSize = LEAF_SIZE; node = new Leaf(buf + pos, p); // printf("leaf\t"); } else { node = new ExLeaf((char*)(buf + pos), nodeSize); // printf("ex leaf\t"); } pos += nodeSize; nodes[index] = node; if(father == -1){ this->root = node; } else { this->addSon(nodes[father],node); } // printf("add son to node %d\n", father); } // printf("reconstruct node ok\n"); free(nodes); }
bool DendrogramTest::run() { context("triangle.pairs"); Graph *graph = new Graph("data/triangle.pairs"); Dendrogram *dendro = new Dendrogram(graph); testcase(dendro != NULL, "Initialized Dendrogram is NULL!"); testcase(dendro->getRoot() != NULL, "Dendrogram has NULL root"); testcase(((InternalNode *)(dendro->getRoot()))->getLeft() != NULL, "Dendrogram root has NULL left child"); testcase(((InternalNode *)(dendro->getRoot()))->getRight() != NULL, "Dendrogram root has NULL right child"); for (NodeList::iterator iterator=dendro->nodes.begin(); iterator!=dendro->nodes.end(); iterator++) { DendrogramNode *node = *iterator; if (node != dendro->getRoot()) { test_not_equal(node->parent,NULL,"Non-root node has NULL parent"); } if (node->type == NODE_INTERNAL) { InternalNode *internal = (InternalNode *)node; if (internal->getLeft() != NULL) { test_equal(internal, internal->getLeft()->parent, "Left child of X does not have X as parent"); } if (internal->getRight() != NULL) { test_equal(internal, internal->getRight()->parent, "Right child of X does not have X as parent"); } } } delete dendro; delete graph; context("triangle.weights"); graph = new Graph("data/triangle.weights"); testcase(graph->isValid(), "failed to load valid graph from data/triangle.weights"); dendro = new Dendrogram(graph); testcase(dendro != NULL, "Initialized Dendrogram is NULL!"); testcase(dendro->getRoot() != NULL, "Dendrogram has NULL root"); for (int i=0; i<100; i++) { dendro->sample(); } testcase(dendro->likelihood() < 0.0f, "Invalid likelihood for post-sampled dendrogram"); graph->addNode(99); graph->addEdge(99,1,0.9); std::set<Node> nodes_ab, nodes_z; nodes_ab.insert(1); nodes_ab.insert(2); nodes_z.insert(99); testcase(graph->linksBetween(nodes_ab,nodes_z) == 0.9, "Incorrect link weight between [A,B] , [Z]"); dendro->addLeaf(99,1); testcase(graph->linksBetween(nodes_ab,nodes_z) == 0.9, "Incorrect link weight between [A,B] , [Z]"); return this->didPass(); }
void BTree::insert(const int value) { BTreeNode *ptr = root->insert(value); if(ptr) // root split { InternalNode *IPtr = new InternalNode(internalSize, leafSize, NULL, NULL, NULL); IPtr->insert(root, ptr); root = IPtr; } // if root split } // BTree::insert()
void BTree::remove(int value) { // To be written by students root = root->remove(value); InternalNode* ptr = dynamic_cast<InternalNode*>(root); while(root->getCount() <= 1 && ptr != NULL) { if(ptr != NULL) root = ptr->getChildren()[0]; ptr = dynamic_cast<InternalNode*>(root); } } // BTree::remove()
virtual void processNode(InternalNode &u) { int id=u.getID(); PhylogenyNode *left=u.getLeft(), *right=u.getRight(); NthOrdSubstMatrix &leftPt=*u.getLeftSubstMatrix(); NthOrdSubstMatrix &rightPt=*u.getRightSubstMatrix(); Array2D<double>::RowIn2DArray<double> row=L[id]; for(Symbol a=0 ; a<numAlpha ; ++a) { row[a]= processInternalChild(a,left,leftPt,&u)+ processInternalChild(a,right,rightPt,&u); } }
void BTree::insert(const int value) { //cout << "Root insert\n"; BTreeNode *ptr = root->insert(value); if(ptr) //split { //cout << "[Root] Split\n"; InternalNode *nroot = new InternalNode (internalSize, leafSize, NULL, NULL, NULL); nroot->insert(root,ptr); root = nroot; } } // BTree::insert()
InternalNode* InternalNode::split(BTreeNode* nodeCausingSplit) { int i; //cout << "SPLIT : " << nodeCausingSplit << " : " << nodeCausingSplit->getMinimum() << endl; InternalNode* newRight = new InternalNode(internalSize, leafSize, this->parent, this, this->rightSibling); /*(internalSize + 1) / 2 gives index of last value guaranteed to move to newRight ((internalSize + 1) / 2) - 1 gives the value that could go depending on if the new value or the old value stored at that position in keys[] is larger */ for (i = internalSize - 1; i >= (internalSize + 1) / 2; i--) { newRight->insert(children[i]); (children[i])->setParent(newRight); } if (nodeCausingSplit->getMinimum() > keys[((internalSize + 1) / 2) - 1] ) // given value is greater than middle position { nodeCausingSplit->setParent(newRight); newRight->insert(nodeCausingSplit); (children[i + 1])->setLeftSibling(nodeCausingSplit); nodeCausingSplit->setRightSibling(children[i + 1]); nodeCausingSplit->setLeftSibling(children[((internalSize + 1) / 2) - 1]); (children[((internalSize + 1) / 2) - 1])->setRightSibling(nodeCausingSplit); } else { newRight->insert( children[((internalSize + 1) / 2) - 1] ); (children[((internalSize + 1) / 2) - 1])->setParent(newRight); (children[i + 1])->setLeftSibling(children[((internalSize + 1) / 2) - 1]); (children[((internalSize + 1) / 2) - 1])->setRightSibling(children[i + 1]); (children[((internalSize + 1) / 2) - 1])->setLeftSibling(nodeCausingSplit); nodeCausingSplit->setRightSibling(children[((internalSize + 1) / 2) - 1]); children[((internalSize + 1) / 2) - 1] = nodeCausingSplit; keys[((internalSize + 1) / 2) - 1] = nodeCausingSplit->getMinimum(); } /*if (internalSize % 2 == 0)*/ count = ((internalSize + 1) / 2); /*else count = ((internalSize + 1) / 2) + 1;*/ if (rightSibling) rightSibling->setLeftSibling(newRight); rightSibling = newRight; return newRight; } // LeafNode::split()
void BTree::remove(int value) { BTreeNode *ptr = root-> remove(value); if(ptr==root) { InternalNode *IPtr = static_cast < InternalNode * > (ptr); ptr = IPtr->firstChild(); ptr->setParent(NULL); root = ptr; } // To be written by students } // BTree::remove()
virtual void processNode(InternalNode &u) { int id=u.getID(); int left=u.getLeft()->getID(), right=u.getRight()->getID(); NthOrdSubstMatrix &leftPt=*u.getLeftSubstMatrix(); NthOrdSubstMatrix &rightPt=*u.getRightSubstMatrix(); Array2D<double>::RowIn2DArray<double> row=L[id]; for(Symbol a=0 ; a<numAlpha ; ++a) { if(gapSymbols.isMember(a)) continue; row[a]= processInternalChild(a,left,leftPt,0,0)+ processInternalChild(a,right,rightPt,0,0); } }
virtual void processNode(InternalNode &u) { int id=u.getID(); Array2D<double>::RowIn2DArray<double> row=L[id]; Taxon *taxon=static_cast<Taxon*>(u.getDecoration()); SubstitutionMatrix &leftPt=getMatrix(*taxon,LEFT); SubstitutionMatrix &rightPt=getMatrix(*taxon,RIGHT); int left=u.getLeft()->getID(), right=u.getRight()->getID(); for(Symbol a=0 ; a<numAlpha ; ++a) if(a!=gap) row[a]= processInternalChild(a,left,leftPt)+ processInternalChild(a,right,rightPt); }
/*分裂内部结点,此种情况发生在原先结点的keyNum=MAX_KEY下*/ void InternalNode::splitNode(FatherNode* parentNode, int childIndex) { InternalNode* newNode = new InternalNode(); //分裂后的新结点 newNode->setKeyNum(MIN_KEY); int i; for (i = 0; i < MIN_KEY; ++i) { newNode->setKeyValue(i, m_KeyValues[i + MIN_CHILD]); } //向新结点中拷贝键值 for (i = 0; i < MIN_CHILD; ++i) { newNode->setChild(i, m_Childs[i + MIN_CHILD]); } //向新结点中拷贝指针 setKeyNum(MIN_KEY); //更新原先结点中的键值个数 ((InternalNode*)parentNode)->insert(childIndex, childIndex + 1, m_KeyValues[MIN_KEY], newNode); //将新结点插入树中 }
virtual void processNode(InternalNode &u) { int id=u.getID(); Array2D<double>::RowIn2DArray<double> row=L[id]; int left=u.getLeft()->getID(), right=u.getRight()->getID(); NthOrdSubstMatrix &leftPt=*u.getLeftSubstMatrix(); NthOrdSubstMatrix &rightPt=*u.getRightSubstMatrix(); Sequence nmer; for(int i=0 ; i<numNmers ; ++i) { nmer.fromInt(i,numCols,alphabetMap); row[i]= processInternalChild(nmer,left,leftPt)+ processInternalChild(nmer,right,rightPt); } }
/*! \brief Goes into a given direction. \a direction can be - \c FORWARD: Forward/to the right. Goes to the next child node of the current node. - \c BACKWARDS: Forward/to the right. Goes to the previous child node of the current node. - \c UP: Up one level (in root direction). Goes to the parent node of the current node. The current node is the child node, it is pointed to afterward. - \c DOWN: Down one level (in leaf direction). Goes to the child node of the current node the iterator currently points at. Points afterwards to the 0th child node of the new current node (unless it is a leaf node). \c FORWARD and \c BACKWARDS do not change the current node! \param direction \c FORWARD, \c BACKWARDS, \c UP or \c DOWN \return \c B_OK, if everything went fine */ status_t TreeIterator::GoTo(uint32 direction) { status_t error = InitCheck(); if (error == B_OK) { switch (direction) { case FORWARD: { if (fCurrentNode->IsInternal() && fIndex < fCurrentNode->CountItems()) { fIndex++; } else { error = B_ENTRY_NOT_FOUND; } break; } case BACKWARDS: { if (fCurrentNode->IsInternal() && fIndex > 0) fIndex--; else error = B_ENTRY_NOT_FOUND; break; } case UP: { error = _PopTopNode(); break; } case DOWN: { if (fCurrentNode->IsInternal()) { InternalNode *internal = fCurrentNode->ToInternalNode(); const DiskChild *child = internal->ChildAt(fIndex); if (child) { Node *node = NULL; error = fTree->GetNode(child->GetBlockNumber(), &node); if (error == B_OK) error = _PushCurrentNode(node, 0); } else error = B_ENTRY_NOT_FOUND; } else error = B_ENTRY_NOT_FOUND; break; } } } return error; }
void InternalNode::mergeLeft() { InternalNode * leftSib = (InternalNode *) getLeftSibling(); while(count != 0) { leftSib->insert(children[count-1]); //minimize shifting removeDriver(count-1); //delete shifted value } InternalNode * rightSib = (InternalNode *) getRightSibling(); leftSib->rightSibling = rightSib; if(rightSib != NULL) rightSib->leftSibling = leftSib; if(parent) parent->resetMinimum(this); } //InternalNode::mergeLeft()
void BTree::insert(const int value) { BTreeNode *split = root->insert(value); if (split) // LeafNode needs split { InternalNode *parent = new InternalNode(internalSize, leafSize, NULL, NULL, NULL); root->setParent(parent); // set parents to root split->setParent(parent); parent->insert(root); // should be on the left parent->insert(split); // should be on the right root = parent; } // if split needed } // BTree::insert()
InternalNode* InternalNode::split(int value){ InternalNode* newNode = new InternalNode(internalSize, leafSize, parent, this, NULL); sort(); BTreeNode* oldRightNode = getRightSibling(); if(oldRightNode){ oldRightNode->setLeftSibling(newNode); newNode->setRightSibling(oldRightNode); newNode->setLeftSibling(this); } //cout << keys[0] << " " << keys[1] << " " << keys[2] << endl; //cout << children[0]->getMinimum() << " " << children[1]->getMinimum() << endl; for(int i = ((internalSize+1) / 2); i < internalSize; i++){ //cout << "Internal: Inserting " << keys[i] << " into new Node" << endl; newNode->insert(keys[i]); } count = count - newNode->getCount(); if(keys[0] > value){ newNode->insert(keys[0]); keys[0] = value; } else{ newNode->insert(value); } setRightSibling(newNode); return newNode; }
virtual void processNode(InternalNode &u) { int id=u.getID(); NthOrdSubstMatrix &leftPt=*u.getLeftSubstMatrix(); NthOrdSubstMatrix &rightPt=*u.getRightSubstMatrix(); int left=u.getLeft()->getID(), right=u.getRight()->getID(); Array2D<double>::RowIn2DArray<double> row=L[id]; Symbol a=A.getIthTrack(id)[column]; if(gapSymbols.isMember(a)) for(a=0 ; a<numAlpha ; ++a) { if(gapSymbols.isMember(a)) continue; row[a]= processInternalChild(a,id,left,leftPt)+ processInternalChild(a,id,right,rightPt); } else { for(Symbol b=0 ; b<numAlpha ; ++b) row[b]=NEGATIVE_INFINITY; double l=processInternalChild(a,id,left,leftPt); double r=processInternalChild(a,id,right,rightPt); row[a]=l+r; } }
BTreeNode* InternalNode::tryBorrowing() { if(!this->isHalfFull()) { bool has_borrowed = false; InternalNode *left_node = (InternalNode *) this->leftSibling; InternalNode *right_node = (InternalNode *) this->rightSibling; if(left_node != NULL) { if(left_node->canBorrow()) { left_node->moveChildRight(); has_borrowed = true; } } if(right_node != NULL && !has_borrowed) { if(right_node->canBorrow()) { right_node->moveChildLeft(); has_borrowed = true; } } if(!has_borrowed) { if(this->getLeftSibling() != NULL) this->merge(1); else if(this->getRightSibling() != NULL) this->merge(0); } // Drive the trickle down the tree for(int i = 0; i < count; i++) { InternalNode* child = dynamic_cast<InternalNode *> (this->children[i]); if(child != NULL) child->tryBorrowing(); } } return this; }
InternalNode* InternalNode::insert(int value) { // students must write this updateMinimums(); sort(); //updateMinimums(); //cout << "Starting InternalNode insert: " << value << endl; //search keys for position to insert, count - 1 because elements start at 0 int searchKey = count-1; for(int i = count-1; keys[searchKey] > value && i > 0; i--){ //cout << keys[searchKey] << " > " << value << endl; searchKey--; } BTreeNode* leafSplit = children[searchKey]->insert(value); updateMinimums(); sort(); //updateMinimums(); //for(int i = 6; i >= 0; i--) // cout << "Keys[" << i << "]: " << keys[i] << endl; if(!leafSplit){ //cout << "no leaf split" << endl; return NULL; } if(leafSplit && count < internalSize){ //add here //cout << "InternalNode Insert Here" << endl; //cout << "InternalNode leafSplit has min: " << leafSplit->getMinimum() << endl; keys[count] = leafSplit->getMinimum(); children[count] = leafSplit; count++; updateMinimums(); sort(); //updateMinimums(); return NULL; } else if(leftSibling && leftSibling->getCount() < internalSize){ //addToLeft(internalSplit) //cout << "NEED TO SHARE TO LEFT" << endl; // cout << "Adding to left internal" << endl; ((InternalNode*)leftSibling)->insert(children[0]); ((InternalNode*)leftSibling)->updateMinimums(); ((InternalNode*)leftSibling)->sort(); //((InternalNode*)leftSibling)->updateMinimums(); count--; for(int i = 0; i < count; i++) { children[i] = children[i + 1]; keys[i] = keys[i + 1]; } children[count] = leafSplit; keys[count] = leafSplit->getMinimum(); count++; updateMinimums(); sort(); //updateMinimums(); //cout << "leafSplit min: " << leafSplit->getMinimum() << endl; leafSplit->setParent(this); if(parent) parent->findMin(this); return NULL; } else if(rightSibling && rightSibling->getCount() < internalSize){ //addtoRight if(keys[count-1] > leafSplit->getMinimum()){ //cout << "Internal Passing to right: " << keys[count-1] << endl; ((InternalNode*) rightSibling)->insert(children[count-1]); ((InternalNode*) rightSibling)->updateMinimums(); ((InternalNode*) rightSibling)->sort(); children[count-1] = leafSplit; updateMinimums(); sort(); if(((InternalNode*) rightSibling)->parent){ (((InternalNode*) rightSibling)->parent)->updateMinimums(); (((InternalNode*) rightSibling)->parent)->sort(); } } return NULL; } else if(leafSplit){ //cout << "Internal Split" << endl; InternalNode* newRight = new InternalNode(internalSize, leafSize, parent, this, rightSibling); BTreeNode* oldRight = getRightSibling(); if(rightSibling){ oldRight->setLeftSibling(newRight); newRight->setRightSibling(oldRight); newRight->setLeftSibling(this); } rightSibling = newRight; int rightCount; for(int i = (internalSize+1) / 2; i < internalSize; i++){ //cout << "i: " << i << endl; //cout << "rightCount: " << newRight->getCount() << endl; //cout << "passing " << keys[i] << endl; //cout << "value: " << value << endl; rightCount = newRight->getCount(); newRight->children[rightCount] = children[i]; newRight->keys[rightCount] = keys[i]; newRight->count++; children[i]->setParent(newRight); count--; } //pass the leafSplit if(leafSplit->getMinimum() > keys[((internalSize+1)/2)-1]){ //cout << leafSplit->getMinimum() << " > " << keys[(((internalSize+1)/2)-1)] << endl; //cout << "Passing: " << leafSplit->getMinimum() << endl; newRight->children[newRight->count] = leafSplit; newRight->count++; leafSplit->setParent(newRight); } //pass the value directly left of keys internalsize+1/2-1... else{ //cout << "Passing: " << keys[(((internalSize+1)/2)-1)] << endl; newRight->children[newRight->count] = children[(((internalSize+1)/2)-1)]; newRight->keys[newRight->count] = keys[(((internalSize+1)/2)-1)]; count--; newRight->count++; children[(((internalSize+1)/2)-1)] = leafSplit; keys[(((internalSize+1)/2)-1)] = leafSplit->getMinimum(); count++; } updateMinimums(); sort(); //sort(); //updateMinimums(); //newRight->sort(); //newRight->updateMinimums(); //cout << "newRight ->count : " << newRight->count << endl; //cout << "newRight keys 0 and 1: " << newRight->keys[0] << " " << newRight->keys[1] << endl; newRight->updateMinimums(); newRight->sort(); //cout << "newRight keys 0 and 1: " << newRight->keys[0] << " " << newRight->keys[1] << endl; //newRight->updateMinimums(); return newRight; //split return NULL; //need to split here } return NULL; // to avoid warnings for now. } // InternalNode::insert()
void processNode(InternalNode &v) { Taxon &taxon=*static_cast<Taxon*>(v.getDecoration()); //cout<<"extending "<<taxon.getName()<<" "<<taxon.getSeqLen()<<endl; taxon.getSeq()=Sequence(gapSymbol,taxon.getSeqLen()); }
void processNode(InternalNode &v) { Taxon &taxon=*static_cast<Taxon*>(v.getDecoration()); cout<<" LLL "<<taxon.getName()<<"="<<taxon.getSeqLen()<<endl; }