//add a leaf to the tree. heap must stay complete at all times. void addLeaf(heap *h,data *d) { //We already have code from binary trees that we can use to //fill the heap completely, this should closely mirror that code //If the heap is empty if(h->root == NULL) { //Create a new leaf to put in the heap leaf* new_leaf = createLeaf(d); //Set the root to be the new leaf. //Note that left, right and parent are all already appropriately set for the root node h->root = new_leaf; //This is definitely a heap, as it contains only one node. dequePushFront(h->q, new_leaf); } //If the heap is not empty else { //Then our node will get linked to the first node in the deque (previously this was a queue) leaf* parent = dequeFront(h->q); //If the parent does not have a left child, then this new node becomes the left child if(parent->left == NULL) { //Create a new leaf, and link it in appropriately leaf* new_leaf = createLeaf(d); new_leaf->parent = parent; parent->left = new_leaf; //Enqueue the leaf dequePushBack(h->q, new_leaf); //Now we may have violated the max-heap property, we can fix this by using percolate up percolateUp(new_leaf); } //Otherwise the parent node should only have space in the right child else { //Just to be sure that the left child was non-null assert(parent->left != NULL && parent->right == NULL); //Create a new leaf, and link it in appropriately leaf* new_leaf = createLeaf(d); new_leaf->parent = parent; parent->right = new_leaf; //Enqueue the leaf dequePushBack(h->q, new_leaf); //Now the front of the deque does not have any more space, so remove it dequePopFront(h->q); //Now we may have violated the max-heap property, we can fix this by using percolate up percolateUp(new_leaf); } } return; }
void addLeaf(Sprig* thisSprig, int key, int caliper){ Leaf* thisLeaf = thisSprig->firstLeaf; if (thisLeaf == NULL) { thisLeaf = (Leaf*)malloc(sizeof(Leaf)); createLeaf(thisLeaf, key, NULL, NULL, thisSprig); thisSprig->firstLeaf = thisLeaf; return; } int count = caliper; while(thisLeaf->key < key) { if (thisLeaf->nextLeaf != NULL) { thisLeaf = thisLeaf->nextLeaf; } else if (count == 2) { //reached end of sprig, keys still smaller. if (thisLeaf->childSprig == NULL) { //split and promote, because no far right sprig available splitPromote(thisLeaf, thisSprig, key, caliper); } //add this key to far right sprig because it exists addLeaf(thisLeaf->childSprig, key, caliper); } else { //there is room in this sprig for a new leaf Leaf* newLeaf = (Leaf*)malloc(sizeof(Leaf)); createLeaf(newLeaf, key, thisLeaf, NULL, thisSprig); thisLeaf->nextLeaf = newLeaf; return; } count--; } //ran into a key too big //try to fit in same sprig if (numLeaves(thisSprig) < caliper - 1) { int tempKey = thisLeaf->key; thisLeaf->key = key; Leaf* newLeaf = (Leaf*)malloc(sizeof(Leaf)); createLeaf(newLeaf, tempKey, thisLeaf, NULL, thisSprig); if (thisLeaf->nextLeaf != NULL) { thisLeaf->nextLeaf->prevLeaf = newLeaf; newLeaf->nextLeaf = thisLeaf->nextLeaf; } thisLeaf->nextLeaf = newLeaf; } //sprig is too full, else { splitPromote(thisLeaf, thisSprig, key, caliper); } return; }
Q3BSPNode *Q3BSPRep::createNode( int n ){ q3_node *q3node=(q3_node*)header.dir[3].lump+n; q3_plane *q3plane=(q3_plane*)header.dir[2].lump+q3node->plane; Q3BSPNode *node=new Q3BSPNode; Vector mins( q3node->mins[0],q3node->mins[1],q3node->mins[2] ); Vector maxs( q3node->maxs[0],q3node->maxs[1],q3node->maxs[2] ); node->box=Box( tf(mins) ); node->box.update( tf(maxs) ); node->plane.n=tf(q3plane->normal); node->plane.d=-q3plane->distance; for( int k=0;k<2;++k ){ if( q3node->children[k]>=0 ){ node->nodes[k]=createNode( q3node->children[k] ); node->leafs[k]=0; }else{ node->leafs[k]=createLeaf( -q3node->children[k]-1 ); node->nodes[k]=0; } } return node; }
void BVH4BuilderTopLevel::recurseSAH(size_t depth, BuildRecord& task, const size_t mode, const size_t threadID, const size_t numThreads) { /* return leaf node */ assert(task.end-task.begin > 0); if (unlikely(task.end-task.begin == 1)) { *(NodeRef*)task.parentNode = refs[task.begin].node; return; } /* create leaf node */ if (unlikely(task.depth >= BVH4::maxBuildDepth)) { createLeaf(task,threadID,numThreads); return; } /*! initialize task list */ BuildRecord childTasks[4]; childTasks[0] = task; size_t numChildren = 1; /*! split until node is full */ do { /*! find best child to split */ float bestArea = inf; ssize_t bestChild = -1; for (size_t i=0; i<numChildren; i++) { float A = childTasks[i].sceneArea(); size_t items = childTasks[i].items(); if (items > 1 && A <= bestArea) { bestChild = i; bestArea = A; } } if (bestChild == -1) break; /*! split best child into left and right child */ __align(64) BuildRecord left, right; split(childTasks[bestChild],left,right,mode,threadID,numThreads); /* add new children left and right */ left.depth = right.depth = task.depth+1; childTasks[bestChild] = childTasks[numChildren-1]; childTasks[numChildren-1] = left; childTasks[numChildren+0] = right; numChildren++; } while (numChildren < 4); /* recurse */ BVH4::Node* node = bvh->allocNode(threadID); for (ssize_t i=numChildren-1; i>=0; i--) { childTasks[i].parentNode = (size_t)&node->child(i); recurse(depth+1,childTasks[i],mode,threadID,numThreads); node->set(i,childTasks[i].bounds.geometry); } *(NodeRef*)task.parentNode = bvh->encodeNode(node); }
void BVHNBuilder<N>::BVHNBuilderV::build(BVH* bvh, BuildProgressMonitor& progress_in, PrimRef* prims, const PrimInfo& pinfo, const size_t blockSize, const size_t minLeafSize, const size_t maxLeafSize, const float travCost, const float intCost) { //bvh->alloc.init_estimate(pinfo.size()*sizeof(PrimRef)); auto progressFunc = [&] (size_t dn) { progress_in(dn); }; auto createLeafFunc = [&] (const BVHBuilderBinnedSAH::BuildRecord& current, Allocator* alloc) -> size_t { return createLeaf(current,alloc); }; NodeRef root; BVHBuilderBinnedSAH::build_reduce<NodeRef> (root,typename BVH::CreateAlloc(bvh),size_t(0),typename BVH::CreateNode(bvh),rotate<N>,createLeafFunc,progressFunc, prims,pinfo,N,BVH::maxBuildDepthLeaf,blockSize,minLeafSize,maxLeafSize,travCost,intCost); bvh->set(root,pinfo.geomBounds,pinfo.size()); #if ROTATE_TREE if (N == 4) { for (int i=0; i<ROTATE_TREE; i++) BVHNRotate<N>::rotate(bvh->root); bvh->clearBarrier(bvh->root); } #endif bvh->layoutLargeNodes(pinfo.size()*0.005f); }
void BVHBuilderSpatial<N>::BVHBuilderV::build(BVH* bvh, BuildProgressMonitor& progress_in, PrimRefList& prims, const PrimInfo& pinfo, const size_t blockSize, const size_t minLeafSize, const size_t maxLeafSize, const float travCost, const float intCost) { //bvh->alloc.init_estimate(pinfo.size()*sizeof(PrimRef)); auto progressFunc = [&] (size_t dn) { progress_in(dn); }; auto splitPrimitiveFunc = [&] (const PrimRef& prim, int dim, float pos, PrimRef& left_o, PrimRef& right_o) -> void { splitPrimitive(prim,dim,pos,left_o,right_o); }; auto createLeafFunc = [&] (BVHBuilderBinnedSpatialSAH::BuildRecord& current, Allocator* alloc) -> size_t { return createLeaf(current,alloc); }; typename BVH::NodeRef root; BVHBuilderBinnedSpatialSAH::build_reduce<typename BVH::NodeRef> (root,typename BVH::CreateAlloc(bvh),size_t(0),typename BVH::CreateNode(bvh),rotate<N>, createLeafFunc,splitPrimitiveFunc,progressFunc, prims,pinfo,BVH::N,BVH::maxBuildDepthLeaf,blockSize,minLeafSize,maxLeafSize,travCost,intCost); bvh->set(root,pinfo.geomBounds,pinfo.size()); #if ROTATE_TREE for (int i=0; i<ROTATE_TREE; i++) BVHRotate::rotate(bvh->root); bvh->clearBarrier(bvh->root); #endif bvh->layoutLargeNodes(pinfo.size()*0.005f); }
void BVHNBuilderQuantized<N>::BVHNBuilderV::build(BVH* bvh, BuildProgressMonitor& progress_in, PrimRef* prims, const PrimInfo& pinfo, const size_t blockSize, const size_t minLeafSize, const size_t maxLeafSize, const float travCost, const float intCost) { //bvh->alloc.init_estimate(pinfo.size()*sizeof(PrimRef)); auto progressFunc = [&] (size_t dn) { progress_in(dn); }; auto createLeafFunc = [&] (const BVHBuilderBinnedSAH::BuildRecord& current, Allocator* alloc) -> size_t { return createLeaf(current,alloc); }; #if ENABLE_32BIT_OFFSETS_FOR_QUANTIZED_NODES == 1 typename BVH::QuantizedNode *first = (typename BVH::QuantizedNode*)bvh->alloc.malloc(sizeof(typename BVH::QuantizedNode), bvh->byteNodeAlignment); NodeRef &root = *(NodeRef*)first; // as the builder assigns current.parent = (size_t*)&root assert(((size_t)first & 0x7) == 0); #else NodeRef root = 0; #endif BVHBuilderBinnedSAH::build_reduce<NodeRef> (root,typename BVH::CreateAlloc(bvh),size_t(0),typename BVH::CreateQuantizedNode(bvh),dummy<N>,createLeafFunc,progressFunc, prims,pinfo,N,BVH::maxBuildDepthLeaf,blockSize,minLeafSize,maxLeafSize,travCost,intCost); #if ENABLE_32BIT_OFFSETS_FOR_QUANTIZED_NODES == 1 NodeRef new_root = ((size_t)first + first->childOffset(0)) | BVH::tyQuantizedNode; #else NodeRef new_root = (size_t)root | BVH::tyQuantizedNode; // todo: COPY LAYOUT FOR LARGE NODES !!! //bvh->layoutLargeNodes(pinfo.size()*0.005f); #endif assert(new_root.isQuantizedNode()); bvh->set(new_root,pinfo.geomBounds,pinfo.size()); }
void Trees::addLeafp(int data, Node* ptr) { //Check if the current tree is empty if(root == NULL) { root = createLeaf(data); } //Check the left side of the tree (less than n) else if(data < ptr->data) { //If there is something there (recursive) if(ptr->left != NULL) { addLeafp(data, ptr->left); } //If nothing is there else { ptr->left = createLeaf(data); } } //Check the right side of the tree (greater than n) else if(data > ptr->data) { //If there is something there (recursive) if(ptr->right != NULL) { addLeafp(data, ptr->right); } //If nothing is there else { ptr->right = createLeaf(data); } } //If data is equal to same as original data else { cout << "The data " << data << " is already in the current tree" << endl; } }
void RootedForest<T, U>::registerLeaf(long ottId) { if (ottId == rootID) { return; } auto f = ottIdToNodeMap.find(ottId); if (f != ottIdToNodeMap.end()) { return; } createLeaf(nullptr, ottId, nullptr); }
std::shared_ptr<ComPWA::FunctionTree> FormFactorDecorator::createFunctionTree(const ParameterList &DataSample, unsigned int pos, const std::string &suffix) const { // size_t sampleSize = DataSample.mDoubleValue(pos)->values().size(); size_t sampleSize = DataSample.mDoubleValue(0)->values().size(); std::string NodeName = "BreitWignerWithProductionFormFactor(" + Name + ")" + suffix; auto tr = std::make_shared<FunctionTree>(NodeName, MComplex("", sampleSize), std::make_shared<MultAll>(ParType::MCOMPLEX)); std::string ffNodeName = "ProductionFormFactor(" + Name + ")" + suffix; auto ffTree = std::make_shared<FunctionTree>(ffNodeName, MDouble("", sampleSize), std::make_shared<FormFactorStrategy>()); // add L and FFType as double value leaf, since there is no int leaf ffTree->createLeaf("OrbitalAngularMomentum", (double ) L, ffNodeName); ffTree->createLeaf("MesonRadius", MesonRadius, ffNodeName); ffTree->createLeaf("FormFactorType", (double) FFType, ffNodeName); ffTree->createLeaf("MassA", Daughter1Mass, ffNodeName); ffTree->createLeaf("MassB", Daughter2Mass, ffNodeName); ffTree->createLeaf("Data_mSq[" + std::to_string(pos) + "]", DataSample.mDoubleValue(pos), ffNodeName); ffTree->parameter(); tr->insertTree(ffTree, NodeName); std::shared_ptr<ComPWA::FunctionTree> breitWignerTree = UndecoratedBreitWigner->createFunctionTree(DataSample, pos, suffix); breitWignerTree->parameter(); tr->insertTree(breitWignerTree, NodeName); if (!tr->sanityCheck()) throw std::runtime_error( "FormFactorDecorator::createFunctionTree() | " "Tree didn't pass sanity check!"); return tr; };
/* Codes_SRS_MULTITREE_99_053:[ MultiTree_AddChild shall add a new node with the name childName to the multi tree node identified by treeHandle] */ MULTITREE_RESULT MultiTree_AddChild(MULTITREE_HANDLE treeHandle, const char* childName, MULTITREE_HANDLE* childHandle) { MULTITREE_RESULT result; /* Codes_SRS_MULTITREE_99_055:[ If any argument is NULL, MultiTree_AddChild shall return MULTITREE_INVALID_ARG.] */ if ((treeHandle == NULL) || (childName == NULL) || (childHandle == NULL)) { result = MULTITREE_INVALID_ARG; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); } else { MULTITREE_HANDLE_DATA* childNode; /* Codes_SRS_MULTITREE_99_060:[ The value associated with the new node shall be NULL.] */ CREATELEAF_RESULT res = createLeaf((MULTITREE_HANDLE_DATA*)treeHandle, childName, NULL, &childNode); switch (res) { default: { result = MULTITREE_ERROR; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); break; } case CREATELEAF_ALREADY_EXISTS: { /* Codes_SRS_MULTITREE_99_061:[ If a child node with the same name already exists, MultiTree_AddChild shall return MULTITREE_ALREADY_HAS_A_VALUE.] */ result = MULTITREE_ALREADY_HAS_A_VALUE; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); break; } case CREATELEAF_OK: { /* Codes_SRS_MULTITREE_99_062:[ The new node handle shall be returned in the childHandle argument.] */ *childHandle = childNode; /* Codes_SRS_MULTITREE_99_054:[ On success, MultiTree_AddChild shall return MULTITREE_OK.] */ result = MULTITREE_OK; break; } case CREATELEAF_EMPTY_NAME: { /* Tests_SRS_MULTITREE_99_066:[ If the childName argument is an empty string, MultiTree_AddChild shall return MULTITREE_EMPTY_CHILD_NAME.] */ result = MULTITREE_EMPTY_CHILD_NAME; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); break; } } } return result; }
void TwoThreeTree::insert(int x) { // insert x into the 2-3 tree if (root == NULL) { root = createLeaf(x); return; } TNode *p = locate(x); // duplication is allowed, but a note will be generated if (p->key == x) { cout << "Note: duplication of " << x << " is inserted.\n"; } TNode *q = createLeaf(x); // swap the old left most node with the new node if the new node // is smallest. Avoid the insertion pattern "X O O O" if (p->key > x) { swapNodes(p, q); } attach(p, q); }
std::shared_ptr<ComPWA::FunctionTree> MCIntegrationStrategy::createFunctionTree( std::shared_ptr<const ComPWA::Intensity> intensity, const std::string &suffix) const { const ParameterList &PhspDataSampleList = PhspSample->getParameterList(); double PhspWeightSum(PhspDataSampleList.mDoubleValue(0)->values().size()); std::shared_ptr<Value<std::vector<double>>> phspweights; try { phspweights = findMDoubleValue("Weight", PhspDataSampleList); PhspWeightSum = std::accumulate(phspweights->values().begin(), phspweights->values().end(), 0.0); } catch (const Exception &e) { } auto tr = std::make_shared<FunctionTree>( "Normalization", ComPWA::ValueFactory(ParType::DOUBLE), std::shared_ptr<Strategy>(new Inverse(ParType::DOUBLE))); tr->createNode("Integral", std::shared_ptr<Strategy>(new MultAll(ParType::DOUBLE)), "Normalization"); // normTree->createLeaf("PhspVolume", PhspVolume, "Integral"); tr->createLeaf("InverseSampleWeights", 1.0 / PhspWeightSum, "Integral"); tr->createNode("Sum", std::shared_ptr<Strategy>(new AddAll(ParType::DOUBLE)), "Integral"); tr->createNode("WeightedIntensities", std::shared_ptr<Strategy>(new MultAll(ParType::MDOUBLE)), "Sum"); if (phspweights) tr->createLeaf("EventWeight", phspweights, "WeightedIntensities"); tr->insertTree(intensity->createFunctionTree(PhspDataSampleList, suffix), "WeightedIntensities"); return tr; }
void BVHNBuilderMblur<N>::BVHNBuilderV::build(BVH* bvh, BuildProgressMonitor& progress_in, PrimRef* prims, const PrimInfo& pinfo, const size_t blockSize, const size_t minLeafSize, const size_t maxLeafSize, const float travCost, const float intCost) { //bvh->alloc.init_estimate(pinfo.size()*sizeof(PrimRef)); auto progressFunc = [&] (size_t dn) { progress_in(dn); }; auto createLeafFunc = [&] (const BVHBuilderBinnedSAH::BuildRecord& current, Allocator* alloc) -> std::pair<BBox3fa,BBox3fa> { return createLeaf(current,alloc); }; /* reduction function */ auto reduce = [] (NodeMB* node, const std::pair<BBox3fa,BBox3fa>* bounds, const size_t num) -> std::pair<BBox3fa,BBox3fa> { assert(num <= N); BBox3fa bounds0 = empty; BBox3fa bounds1 = empty; for (size_t i=0; i<num; i++) { const BBox3fa b0 = bounds[i].first; const BBox3fa b1 = bounds[i].second; node->set(i,b0,b1); bounds0 = merge(bounds0,b0); bounds1 = merge(bounds1,b1); } return std::pair<BBox3fa,BBox3fa>(bounds0,bounds1); }; auto identity = std::make_pair(BBox3fa(empty),BBox3fa(empty)); NodeRef root; BVHBuilderBinnedSAH::build_reduce<NodeRef> (root,typename BVH::CreateAlloc(bvh),identity,CreateNodeMB<N>(bvh),reduce,createLeafFunc,progressFunc, prims,pinfo,N,BVH::maxBuildDepthLeaf,blockSize,minLeafSize,maxLeafSize,travCost,intCost); bvh->set(root,pinfo.geomBounds,pinfo.size()); #if ROTATE_TREE if (N == 4) { for (int i=0; i<ROTATE_TREE; i++) BVHNRotate<N>::rotate(bvh->root); bvh->clearBarrier(bvh->root); } #endif //bvh->layoutLargeNodes(pinfo.size()*0.005f); // FIXME: implement for Mblur nodes and activate }
void addValue(int value, SetNode *&setNode) { if(setNode != NULL) { if (value < setNode->value) { addValue(value, setNode->leftChild); } if (value > setNode->value) { addValue(value, setNode->rightChild); } return; } SetNode * leaf = createLeaf(value); setNode = leaf; }
void BVH4BuilderTopLevel::createLeaf(BuildRecord& current, size_t threadIndex, size_t threadCount) { #if defined(DEBUG) if (current.depth > BVH4::maxBuildDepthLeaf) throw std::runtime_error("ERROR: depth limit reached"); #endif /* return empty node */ if (current.end-current.begin == 0) { *(NodeRef*)current.parentNode = BVH4::emptyNode; return; } /* return leaf node */ if (current.end-current.begin == 1) { *(NodeRef*)current.parentNode = refs[current.begin].node; return; } /* first split level */ BuildRecord record0, record1; split_fallback2(&refs[0],current,record0,record1); /* second split level */ BuildRecord children[4]; split_fallback2(&refs[0],record0,children[0],children[1]); split_fallback2(&refs[0],record1,children[2],children[3]); /* allocate next four nodes */ BVH4::Node* node = bvh->allocNode(threadIndex); *(NodeRef*)current.parentNode = bvh->encodeNode(node); /* recurse into each child */ for (size_t i=0; i<4; i++) { children[i].parentNode = (size_t)&node->child(i); children[i].depth = current.depth+1; createLeaf(children[i],threadIndex,threadCount); node->set(i,children[i].bounds.geometry); } BVH4::compact(node); // move empty nodes to the end }
void SuffixTree::addSymbol(int position) { Vertex * last = nullptr; while (!canMove(line[position])) { if (startPoint.currentShift != 0) { startPoint.vertex = createVertex(); startPoint.currentShift = 0; startPoint.currentChar = -1; } if (last != nullptr) { last->updateLink(startPoint.vertex); } last = startPoint.vertex; createLeaf(startPoint.vertex, position); getLink(); if (startPoint.currentShift == 0) { last->updateLink(startPoint.vertex); last = nullptr; } } }
void BVH4BuilderFast::createLeaf(BuildRecord& current, Allocator& nodeAlloc, Allocator& leafAlloc, size_t threadIndex, size_t threadCount) { #if defined(DEBUG) if (current.depth > BVH4::maxBuildDepthLeaf) throw std::runtime_error("ERROR: depth limit reached"); #endif /* create leaf for few primitives */ if (current.items() <= QBVH_BUILDER_LEAF_ITEM_THRESHOLD) { createSmallLeaf(this,current,leafAlloc,threadIndex); return; } /* first split level */ BuildRecord record0, record1; split_fallback(prims,current,record0,record1); /* second split level */ BuildRecord children[4]; split_fallback(prims,record0,children[0],children[1]); split_fallback(prims,record1,children[2],children[3]); /* allocate node */ Node* node = (Node*) nodeAlloc.malloc(sizeof(Node)); node->clear(); *(NodeRef*)current.parentNode = bvh->encodeNode(node); /* recurse into each child */ for (size_t i=0; i<4; i++) { node->set(i,children[i].bounds.geometry); children[i].parentNode = (size_t)&node->child(i); children[i].depth = current.depth+1; createLeaf(children[i],nodeAlloc,leafAlloc,threadIndex,threadCount); } BVH4::compact(node); // move empty nodes to the end }
void BVH4BuilderFast::recurseSAH(BuildRecord& current, Allocator& nodeAlloc, Allocator& leafAlloc, const size_t mode, const size_t threadID, const size_t numThreads) { __aligned(64) BuildRecord children[BVH4::N]; /* create leaf node */ if (current.depth >= BVH4::maxBuildDepth || current.isLeaf()) { assert(mode != BUILD_TOP_LEVEL); createLeaf(current,nodeAlloc,leafAlloc,threadID,numThreads); return; } /* fill all 4 children by always splitting the one with the largest surface area */ unsigned int numChildren = 1; children[0] = current; do { /* find best child with largest bounding box area */ int bestChild = -1; float bestArea = neg_inf; for (unsigned int i=0; i<numChildren; i++) { /* ignore leaves as they cannot get split */ if (children[i].isLeaf()) continue; /* remember child with largest area */ if (children[i].sceneArea() > bestArea) { bestArea = children[i].sceneArea(); bestChild = i; } } if (bestChild == -1) break; /*! split best child into left and right child */ __aligned(64) BuildRecord left, right; if (!split(children[bestChild],left,right,mode,threadID,numThreads)) continue; /* add new children left and right */ left.depth = right.depth = current.depth+1; children[bestChild] = children[numChildren-1]; children[numChildren-1] = left; children[numChildren+0] = right; numChildren++; } while (numChildren < BVH4::N); /* create leaf node if no split is possible */ if (numChildren == 1) { assert(mode != BUILD_TOP_LEVEL); createLeaf(current,nodeAlloc,leafAlloc,threadID,numThreads); return; } /* allocate node */ Node* node = (Node*) nodeAlloc.malloc(sizeof(Node)); node->clear(); *(NodeRef*)current.parentNode = bvh->encodeNode(node); /* recurse into each child */ for (unsigned int i=0; i<numChildren; i++) { node->set(i,children[i].bounds.geometry); children[i].parentNode = (size_t)&node->child(i); children[i].depth = current.depth+1; recurse(children[i],nodeAlloc,leafAlloc,mode,threadID,numThreads); } }
void dag(qcode_t *first, qcode_t *last){ qcode_t p = *first, ret = 0, q; nodelist nr; dnode node, x, y, z, cur_inst = 0, new_inst; ident_t nid; if(p->code == NOP){ q = p; ret = q; } while(p && p->code != RET && (p->code < JGT || p->code > JMP)){ if(p->code == MOV){ nr = findlist(p->op1); if(!nr){ node = createLeaf(p->op1); updatelist(p->op1, node); }else{ node = nr->addr; } updatelist(p->opr, node); } else if(p->code!= CMP && p->code!=FCMP && p->code >= ADD && p->code <=FNEG){ nr = findlist(p->op1); if(!nr){ node = createLeaf(p->op1); updatelist(p->op1, node); }else{ node = nr->addr; } x = node; y = 0; if(p->op2){ nr = findlist(p->op2); if(!nr){ node = createLeaf(p->op2); updatelist(p->op2, node); }else{ node = nr->addr; } y = node; } nr = dnodelist; while(nr){ z = nr->addr; if(z->op == p->code && z->x == x && z->y == y) break; nr = nr->next; } if(!nr){ z = createNode(DAG_OP, p->code, 0, x, y); }else{ z = nr->addr; } updatelist(p->opr, z); } else { if(p->op1){ nr = findlist(p->op1); if(!nr){ x = createLeaf(p->op1); updatelist(p->op1, x); }else{ x = nr->addr; } }else{ x = 0; } if(p->op2){ nr = findlist(p->op2); if(!nr){ y = createLeaf(p->op2); updatelist(p->op2, y); }else{ y = nr->addr; } }else{ y = 0; } new_inst = createNode(DAG_INST, p->code, cur_inst, x, y); cur_inst = new_inst; if(p->opr){ nid = createIdent(); nid->name = ccgenname(namenum++, '$'); nid->type = TEMP; nid->subtype = p->opr->subtype; nid->loc = LOC_LOCAL; insertTable(stab, nid); cur_inst->oprand = nid; cur_inst->vartype = nid->subtype; updatelist(p->opr, cur_inst); }else{ cur_inst->vartype = TVOID; } } p = p->next; } if(ret) ret->next = undag(); else ret = undag(); *first = ret; q = ret; while(q->next){ q = q->next; } if(p){ if(p->code == RET && p->opr){ nr = findlist(p->opr); p->opr = nr->addr->oprand; } *last = p; q->next = p; p->next = 0; }else{ *last = q; } deleteNodes(); clearnodelist(); }
void splitPromote(Leaf* thisLeaf, Sprig* thisSprig, int key, int caliper) { //new sprig Sprig* newRightSprig = (Sprig*)malloc(sizeof(Sprig)); if (thisSprig->parentSprig == NULL) { //need to make new sprig on top Sprig* newTopSprig = (Sprig*)malloc(sizeof(Sprig)); newRightSprig->parentSprig = newTopSprig; thisSprig->parentSprig = newTopSprig; } else { //need to incorporate with existing upper sprig newRightSprig->parentSprig = thisSprig->parentSprig; } Leaf* newLeaf = (Leaf*)malloc(sizeof(Leaf)); if (thisLeaf->nextLeaf == NULL) { //add leaf to end createLeaf(newLeaf, key, thisLeaf, NULL, newRightSprig); thisLeaf->nextLeaf = newLeaf; } else { createLeaf(newLeaf, key, thisLeaf->prevLeaf, thisLeaf, newRightSprig); thisLeaf->prevLeaf->nextLeaf = newLeaf; thisLeaf->prevLeaf = newLeaf; } //start at the rightmost Leaf while (thisLeaf->nextLeaf != NULL) { thisLeaf = thisLeaf->nextLeaf; } //then move back to split int count = caliper/2; while (count > 0) { //every leaf on this side of the split needs to reassociate thisLeaf->currSprig = newRightSprig; //newRightSprig needs to find its first leaf newRightSprig->firstLeaf = thisLeaf; thisLeaf = thisLeaf->prevLeaf; count--; } //now thisLeaf is the median thisLeaf->childSprig = newRightSprig; //separate from the right leaves if (thisLeaf->nextLeaf != NULL) { thisLeaf->nextLeaf->prevLeaf = NULL; thisLeaf->nextLeaf = NULL; } //bring this leaf up to the parentSprig addLeaf(thisSprig->parentSprig, thisLeaf->key, caliper); //find the leaf just before the one we just added up there Leaf* currLeaf = thisSprig->parentSprig->firstLeaf; while (currLeaf->key < thisLeaf->key) { currLeaf = currLeaf->nextLeaf; } //give the promoted leaf the right child sprig currLeaf->childSprig = newRightSprig; //currLeaf is now one step too far right... if (currLeaf->prevLeaf != NULL) { currLeaf->prevLeaf->childSprig = thisSprig; } else { currLeaf->currSprig->leftSprig = thisSprig; } //separate from the left leaf if (thisLeaf->prevLeaf != NULL) { thisLeaf->prevLeaf->nextLeaf = NULL; } //free up the memory of the left behind leaf free(thisLeaf); }
static void drawASTNode(ASTNode *Node) { PtrVector *Child = &(Node->Child); PtrVectorIterator I = beginPtrVector(Child), E = endPtrVector(Child); void *Value = Node->Value; switch (Node->Kind) { case IntLit: createLeaf(Node, "Int: %d", *((int*)Value)); break; case FloatLit: createLeaf(Node, "Float: %f", *((float*)Value)); break; case StringLit: createLeaf(Node, "String: %s", (char*)Value); break; case IdLval: createLeaf(Node, "Id: %s", (char*)Value); break; case IntTy: createLeaf(Node, "Type: int"); break; case FloatTy: createLeaf(Node, "Type: float"); break; case StringTy: createLeaf(Node, "Type: string"); break; case AnswerTy: createLeaf(Node, "Type: answer"); break; case ContTy: createLeaf(Node, "Type: cont"); break; case StrConsumerTy: createLeaf(Node, "Type: strConsumer"); break; case IdTy: createLeaf(Node, "TypeId: %s", (char*)Value); break; case NilExpr: createLeaf(Node, "Nil"); break; default: createNode(Node, Name[Node->Kind]); if (Value) { createLeaf(Value, "Id: %s", (char*)Value); createEdge(Node, Value); } for (; I != E; ++I) if (*I) { createEdge(Node, *I); drawASTNode(*I); } break; } }
Trie::Trie(char* word) : m_notFound(-1) { m_root = new TrieNonLeafNode(word[0]); // initialize the root createLeaf(word[0],word+1,m_root); // to avoid later tests; }
void Trie::insert(char *word) { TrieNonLeafNode *p = m_root; TrieLeafNode *lf; int offset, pos; char *hold = word; while (true) { // the end of word is reached if (*word == '\0') { if (p->m_endOfWord) mc2log<< "Duplicate entry1 " << hold << endl; else p->m_endOfWord = true; return; } pos = position(p,*word); if (pos == m_notFound) { createLeaf(*word,word+1,p); return; } else if (p->m_ptrs[pos] == NULL){ createLeaf(*word,word+1,p); return; } else if (pos != m_notFound && p->m_ptrs[pos]->m_leaf) { // hold this leaf lf = static_cast<TrieLeafNode*>(p->m_ptrs[pos]); if (strcmp(lf->m_word,word+1) == 0) { mc2log<< "Duplicate entry2 " << hold << endl; return; } offset = 0; // create as many non-leaves as the length of identical // prefix of word and the string in the leaf (for cell <b>'R'</b>, // leaf <b>'EP'</b>, and word <b>'REAR'</b>, two such nodes are created) do { pos = position(p,word[offset]); // word == <b>"ABC"</b>, leaf = <b>"ABCDEF"</b> => leaf = <b>"DEF"</b> if (int(strlen(word)) == offset+1) { p->m_ptrs[pos] = new TrieNonLeafNode(word[offset]); p->m_ptrs[pos]->m_endOfWord = true; createLeaf(lf->m_word[offset], lf->m_word + offset+1, p->m_ptrs[pos]); return; } // word == <b>"ABCDEF"</b>, leaf = <b>"ABC"</b> => leaf = <b>"DEF"</b> else if (int(strlen(lf->m_word)) == offset) { p->m_ptrs[pos] = new TrieNonLeafNode(word[offset+1]); p->m_ptrs[pos]->m_endOfWord = true; createLeaf(word[offset+1],word+offset+2,p->m_ptrs[pos]); return; } p->m_ptrs[pos] = new TrieNonLeafNode(word[offset+1]); p = p->m_ptrs[pos]; offset++; }while (word[offset] == lf->m_word[offset-1]); offset--; // word = <b>"ABCDEF"</b>, leaf = <b>"ABCPQR"</b> => // leaf(<b>'D'</b>) = <b>"EF"</b>, leaf(<b>'P'</b>) = <b>"QR"</b> // check whether there is a suffix left: // word = <b>"ABCD"</b>, leaf = <b>"ABCPQR"</b> => // leaf(<b>'D'</b>) = null, leaf(<b>'P'</b>) = <b>"QR"</b> char *s; if (int(strlen(word)) > offset+2) s = word+offset+2; else s = new char[1]; createLeaf(word[offset+1],s,p); // check whether there is a suffix left: // word = <b>"ABCDEF"</b>, leaf = <b>"ABCP"</b> => // leaf(<b>'D'</b>) = <b>"EF"</b>, leaf(<b>'P'</b>) = NULL if (int(strlen(lf->m_word)) > offset+1) s = lf->m_word+offset+1; else { s = new char[1]; s[0]='\0'; } createLeaf(lf->m_word[offset],s,p); delete [] lf->m_word; delete lf; return; } // if (pos != m_notFound && .. else { p = p->m_ptrs[pos]; word++; } } }
void CreateXMLTree::createNode(TreeObject * node) { switch ((TreeNode::Type)(node->getType())) { case TreeNode::PROG: wb->writeLine("<Prog>"); createNode(node->getChild(0)); createNode(node->getChild(1)); wb->writeLine("</Prog>"); break; case TreeNode::DECLS_DECL: wb->writeLine("<Decls-Decl>"); createNode(node->getChild(0)); createNode(node->getChild(1)); wb->writeLine("</Decls-Decl>"); break; case TreeNode::DECLS_e: wb->writeLine("<Decls-E />"); break; case TreeNode::DECL: wb->writeLine("<Decl>"); createNode(node->getChild(0)); createNode(node->getChild(1)); createLeaf(node->getChild(2)); wb->writeLine("</Decl>"); break; case TreeNode::ARRAY_Index: wb->writeLine("<Array-Index>"); createLeaf(node->getChild(0)); wb->writeLine("</Array-Index>"); break; case TreeNode::ARRAY_e: wb->writeLine("<Array-E />"); break; case TreeNode::TYPE_int: wb->writeLine("<Type-Int />"); break; case TreeNode::TYPE_float: wb->writeLine("<Type-Float />"); break; case TreeNode::STATEMENTS_STATEMENT: wb->writeLine("<Statements-Statement>"); createNode(node->getChild(0)); createNode(node->getChild(1)); wb->writeLine("</Statements-Statement>"); break; case TreeNode::STATEMENTS_e: wb->writeLine("<Statements-E />"); break; case TreeNode::STATEMENT_identifier: wb->writeLine("<Statements-Identifier>"); createLeaf(node->getChild(0)); createNode(node->getChild(1)); createNode(node->getChild(2)); wb->writeLine("</Statements-Identifier>"); break; case TreeNode::STATEMENT_write: wb->writeLine("<Statements-Write>"); createNode(node->getChild(0)); wb->writeLine("</Statements-Write>"); break; case TreeNode::STATEMENT_read: wb->writeLine("<Statements-Read>"); createLeaf(node->getChild(0)); createNode(node->getChild(1)); wb->writeLine("</Statements-Read>"); break; case TreeNode::STATEMENT_Brackets: wb->writeLine("<Statements-Brackets>"); createNode(node->getChild(0)); wb->writeLine("</Statements-Brackets>"); break; case TreeNode::STATEMENT_if: wb->writeLine("<Statements-If>"); createNode(node->getChild(0)); createNode(node->getChild(1)); createNode(node->getChild(2)); wb->writeLine("</Statements-If>"); break; case TreeNode::STATEMENT_while: wb->writeLine("<Statements-While>"); createNode(node->getChild(0)); createNode(node->getChild(1)); wb->writeLine("</Statements-While>"); break; case TreeNode::EXP: wb->writeLine("<Exp>"); createNode(node->getChild(0)); createNode(node->getChild(1)); wb->writeLine("</Exp>"); break; case TreeNode::EXP2_Brackets: wb->writeLine("<Exp2-Brackets>"); createNode(node->getChild(0)); wb->writeLine("</Exp2-Brackets>"); break; case TreeNode::EXP2_identifier: wb->writeLine("<Exp2-Identifier>"); createLeaf(node->getChild(0)); createNode(node->getChild(1)); wb->writeLine("</Exp2-Identifier>"); break; case TreeNode::EXP2_integer: wb->writeLine("<Exp2-Integer>"); createLeaf(node->getChild(0)); wb->writeLine("</Exp2-Integer>"); break; case TreeNode::EXP2_real: wb->writeLine("<Exp2-Real>"); createLeaf(node->getChild(0)); wb->writeLine("</Exp2-Real>"); break; case TreeNode::EXP2_Minus: wb->writeLine("<Exp2-Minus>"); createNode(node->getChild(0)); wb->writeLine("</Exp2-Minus>"); break; case TreeNode::EXP2_EM: wb->writeLine("<Exp2-EM>"); createNode(node->getChild(0)); wb->writeLine("</Exp2-EM>"); break; case TreeNode::EXP2_float: wb->writeLine("<Exp2-Float>"); createNode(node->getChild(0)); wb->writeLine("</Exp2-Float>"); break; case TreeNode::INDEX_Brackets: wb->writeLine("<Index-Brackets>"); createNode(node->getChild(0)); wb->writeLine("</Index-Brackets>"); break; case TreeNode::INDEX_e: wb->writeLine("<Index-E />"); break; case TreeNode::OP_EXP_OP: wb->writeLine("<Op-Exp-Op>"); createNode(node->getChild(0)); createNode(node->getChild(1)); wb->writeLine("</Op-Exp-Op>"); break; case TreeNode::OP_EXP_e: wb->writeLine("<Op-Exp-E />"); break; case TreeNode::OP_PLUS: wb->writeLine("<Op-Plus />"); break; case TreeNode::OP_MINUS: wb->writeLine("<Op-Minus />"); break; case TreeNode::OP_MULTIPLY: wb->writeLine("<Op-Multiply />"); break; case TreeNode::OP_DEVIDE: wb->writeLine("<Op-Devide />"); break; case TreeNode::OP_LT: wb->writeLine("<Op-LT />"); break; case TreeNode::OP_EQ: wb->writeLine("<Op-EQ />"); break; case TreeNode::OP_AND: wb->writeLine("<Op-AND />"); break; } }
MULTITREE_RESULT MultiTree_AddLeaf(MULTITREE_HANDLE treeHandle, const char* destinationPath, const void* value) { /*codes_SRS_MULTITREE_99_018:[ If the treeHandle parameter is NULL, MULTITREE_INVALID_ARG shall be returned.]*/ MULTITREE_RESULT result; if (treeHandle == NULL) { result = MULTITREE_INVALID_ARG; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); } /*Codes_SRS_MULTITREE_99_019:[ If parameter destinationPath is NULL, MULTITREE_INVALID_ARG shall be returned.]*/ else if (destinationPath == NULL) { result = MULTITREE_INVALID_ARG; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); } /*Codes_SRS_MULTITREE_99_020:[ If parameter value is NULL, MULTITREE_INVALID_ARG shall be returned.]*/ else if (value == NULL) { result = MULTITREE_INVALID_ARG; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); } /*Codes_SRS_MULTITREE_99_050:[ If destinationPath a string with zero characters, MULTITREE_INVALID_ARG shall be returned.]*/ else if (strlen(destinationPath) == 0) { result = MULTITREE_EMPTY_CHILD_NAME; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); } else { /*break the path into components*/ /*find the first child name*/ MULTITREE_HANDLE_DATA * node = (MULTITREE_HANDLE_DATA *)treeHandle; char * whereIsDelimiter; /*if first character is / then skip it*/ /*Codes_SRS_MULTITREE_99_014:[DestinationPath is a string in the following format: /child1/child12 or child1/child12] */ if (destinationPath[0] == '/') { destinationPath++; } /*if there's just a string, it needs to be created here*/ whereIsDelimiter = (char*)strchr(destinationPath, '/'); if (whereIsDelimiter == NULL) { /*Codes_SRS_MULTITREE_99_017:[ Subsequent names designate hierarchical children in the tree. The last child designates the child that will receive the value.]*/ CREATELEAF_RESULT res = createLeaf(node, destinationPath, (const char*)value, NULL); switch (res) { default: { /*Codes_SRS_MULTITREE_99_025:[The function shall return MULTITREE_ERROR to indicate any other error not specified here.]*/ result = MULTITREE_ERROR; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); break; } case CREATELEAF_ALREADY_EXISTS: { /*Codes_SRS_MULTITREE_99_021:[ If the node already has a value assigned to it, MULTITREE_ALREADY_HAS_A_VALUE shall be returned and the existing value shall not be changed.]*/ result = MULTITREE_ALREADY_HAS_A_VALUE; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); break; } case CREATELEAF_OK: { /*Codes_SRS_MULTITREE_99_034:[ The function returns MULTITREE_OK when data has been stored in the tree.]*/ result = MULTITREE_OK; break; } case CREATELEAF_EMPTY_NAME: { /*Codes_SRS_MULTITREE_99_024:[ if a child name is empty (such as in "/child1//child12"), MULTITREE_EMPTY_CHILD_NAME shall be returned.]*/ result = MULTITREE_EMPTY_CHILD_NAME; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); break; } } } else { /*if there's more or 1 delimiter in the path... */ /*Codes_SRS_MULTITREE_99_017:[ Subsequent names designate hierarchical children in the tree. The last child designates the child that will receive the value.]*/ char firstInnerNodeName[INNER_NODE_NAME_SIZE]; if (strncpy_s(firstInnerNodeName, INNER_NODE_NAME_SIZE, destinationPath, whereIsDelimiter - destinationPath) != 0) { /*Codes_SRS_MULTITREE_99_025:[ The function shall return MULTITREE_ERROR to indicate any other error not specified here.]*/ result = MULTITREE_ERROR; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); } else { MULTITREE_HANDLE_DATA *child = getChildByName(node, firstInnerNodeName); if (child == NULL) { /*Codes_SRS_MULTITREE_99_022:[ If a child along the path does not exist, it shall be created.] */ /*Codes_SRS_MULTITREE_99_023:[ The newly created children along the path shall have a NULL value by default.]*/ CREATELEAF_RESULT res = createLeaf(node, firstInnerNodeName, NULL, NULL); switch (res) { default: { result = MULTITREE_ERROR; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); break; } case(CREATELEAF_EMPTY_NAME): { /*Codes_SRS_MULTITREE_99_024:[ if a child name is empty (such as in "/child1//child12"), MULTITREE_EMPTY_CHILD_NAME shall be returned.]*/ result = MULTITREE_EMPTY_CHILD_NAME; LogError("(result = %s)", ENUM_TO_STRING(MULTITREE_RESULT, result)); break; } case(CREATELEAF_OK): { MULTITREE_HANDLE_DATA *createdChild = getChildByName(node, firstInnerNodeName); result = MultiTree_AddLeaf(createdChild, whereIsDelimiter, value); break; } }; } else { result = MultiTree_AddLeaf(child, whereIsDelimiter, value); } } } } return result; }