void State::removeTrees(const NodeSet& a_nodeSet) { for (NodeSetConstIter iter = a_nodeSet.begin(); iter != a_nodeSet.end(); iter++) { removeTree(*iter); } }
void ConfigFileWriter::writeRadiosFile(std::string filename, NodeSet& ns) { std::fstream out; out.open(filename.c_str(), std::fstream::out | std::fstream::trunc); NodeSet::iterator it = ns.begin(); while (it != ns.end()) { EntityStruct* ent = (*it)->entity; int len = strlen((char*)ent->marking.markingData); if (len == 0 || len > 11) { it++; continue; } RadioStruct* radio = (*it)->radio; if (radio) { out << (*it)->NodeId << ", "; out << ent->marking << ", "; out << radio->radioIndex << ", "; out << radio->relativePosition << ", "; out << radio->radioSystemType ; out << std::endl; } it++; } out.close(); }
void State::functionGreaterEqual(const string& a_name, int a_value) { NodeSet* parentSet = getVariableNodes(a_name); if (parentSet == NULL) { error("State::functionGreaterEqual - variable " + a_name + " doesn't exist" , false); return; } NodeSet removedRoots; for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++) { if ((*iter) == NULL) { error("State::functionGreaterEqual - iterator for variable " + a_name + " is NULL", false); continue; } if ((*iter)->getMaxValue() < a_value) { debug("State::functionGreaterEqual - Adding " + a_name + "'s root to removedRoots set"); removedRoots.insert((*iter)->getRoot()); continue; } if ((*iter)->getMinValue() < a_value) { (*iter)->setMinValue(a_value); } } debug("State::functionGreaterEqual - removing trees"); removeTrees(removedRoots); }
void step(DhtRunner& dht, std::atomic_uint& done, std::shared_ptr<NodeSet> all_nodes, dht::InfoHash cur_h, unsigned cur_depth) { std::cout << "step at " << cur_h << ", depth " << cur_depth << std::endl; done++; dht.get(cur_h, [all_nodes](const std::vector<std::shared_ptr<Value>>& /*values*/) { return true; }, [&,all_nodes,cur_h,cur_depth](bool, const std::vector<std::shared_ptr<Node>>& nodes) { all_nodes->insert(nodes.begin(), nodes.end()); NodeSet sbuck {nodes.begin(), nodes.end()}; if (not sbuck.empty()) { unsigned bdepth = sbuck.size()==1 ? 0u : InfoHash::commonBits((*sbuck.begin())->id, (*std::prev(sbuck.end()))->id); unsigned target_depth = std::min(159u, bdepth+3u); std::cout << cur_h << " : " << nodes.size() << " nodes; target is " << target_depth << " bits deep (cur " << cur_depth << ")" << std::endl; for (unsigned b = cur_depth ; b < target_depth; b++) { auto new_h = cur_h; new_h.setBit(b, 1); step(dht, done, all_nodes, new_h, b+1); } } done--; std::cout << done.load() << " operations left, " << all_nodes->size() << " nodes found." << std::endl; cv.notify_one(); }); }
void ActionChoiceWindow::createButtons( ActionNode* actions, const CEGUI::Point& center, float radius, float angle, float angleWidth) { PushButton* button = NULL; if (actions->isLeaf()) { button = createButton(actions->getAction()->getName(), center); } else { if (actions->getGroup() != NULL) { button = createButton(actions->getGroup()->getName(), center); } const NodeSet children = actions->getChildren(); float angleStep = angleWidth / (float)children.size(); float ang = children.size()>1 ? angle - angleWidth : angle - 180; for (NodeSet::const_iterator iter = children.begin(); iter != children.end(); iter++) { CEGUI::Point centerChild = getPositionOnCircle(center, radius, ang); createButtons(*iter, centerChild, radius, ang, 60); ang += angleStep; } } actions->setButton(button); if (button != NULL) mWindow->addChildWindow(button); }
void ActionChoiceWindow::ActionNode::getAllNodes(ActionNode* treeRoot, NodeSet& nodes) { nodes.insert(treeRoot); const NodeSet children = treeRoot->getChildren(); for (NodeSet::const_iterator iter = children.begin(); iter != children.end(); iter++) getAllNodes(*iter, nodes); }
explicit RemovePlaceholdersVisitor(AstNode* nodep) { iterate(nodep); for (NodeSet::const_iterator it = m_removeSet.begin(); it != m_removeSet.end(); ++it) { AstNode* np = *it; np->unlinkFrBack(); // Without next np->deleteTree(); VL_DANGLING(np); } }
void writeHostnameSection(std::ostream& out, NodeSet& ns) { NodeSet::iterator it = ns.begin(); while (it != ns.end()) { out << "[" << (*it)->NodeId << "] HOSTNAME\t\"" << (*it)->entity->marking.markingData << "\"" << std::endl; it++; } out << std::endl; }
/** * Prepares constraints in order to apply VPSC vertically to remove ALL overlap. */ int generateYConstraints(const int n, Rectangle** rs, Variable** vars, Constraint** &cs) { events=new Event*[2*n]; int ctr=0,i,m; for(i=0;i<n;i++) { vars[i]->desiredPosition=rs[i]->getCentreY(); Node *v = new Node(vars[i],rs[i],rs[i]->getCentreY()); events[ctr++]=new Event(Open,v,rs[i]->getMinX()); events[ctr++]=new Event(Close,v,rs[i]->getMaxX()); } qsort((Event*)events, (size_t)2*n, sizeof(Event*), compare_events ); NodeSet scanline; vector<Constraint*> constraints; for(i=0;i<2*n;i++) { Event *e=events[i]; Node *v=e->v; if(e->type==Open) { scanline.insert(v); NodeSet::iterator i=scanline.find(v); if(i--!=scanline.begin()) { Node *u=*i; v->firstAbove=u; u->firstBelow=v; } i=scanline.find(v); if(++i!=scanline.end()) { Node *u=*i; v->firstBelow=u; u->firstAbove=v; } } else { // Close event Node *l=v->firstAbove, *r=v->firstBelow; if(l!=NULL) { double sep = (v->r->height()+l->r->height())/2.0; constraints.push_back(new Constraint(l->v,v->v,sep)); l->firstBelow=v->firstBelow; } if(r!=NULL) { double sep = (v->r->height()+r->r->height())/2.0; constraints.push_back(new Constraint(v->v,r->v,sep)); r->firstAbove=v->firstAbove; } scanline.erase(v); delete v; } delete e; } delete [] events; cs=new Constraint*[m=constraints.size()]; for(i=0;i<m;i++) cs[i]=constraints[i]; return m; }
void part_1() { // topological sort with an ordered set NodeSet roots; Graph children, parents; get_graphs("input.txt", children, parents, roots); stringstream res; while (!roots.empty()) { char n = *roots.begin(); roots.erase(roots.begin()); res << n; for (char m : children[n]) { parents[m].erase(n); if (parents[m].empty()) { roots.insert(m); } } children.erase(n); } cout << res.str() << endl; }
/** Get parsed fields. * Get fields stored below the given node. * @param node root node where to start searching * @return vector of field representations. */ std::vector<InterfaceField> InterfaceParser::getFields(xmlpp::Node *node) { vector<InterfaceField> result; NodeSet set = node->find("field"); for (NodeSet::iterator i = set.begin(); i != set.end(); ++i) { InterfaceField f(&enum_constants); const Element * el = dynamic_cast<const Element *>(*i); if ( el ) { // valid element const Element::AttributeList& attrs = el->get_attributes(); for(Element::AttributeList::const_iterator iter = attrs.begin(); iter != attrs.end(); ++iter) { const Attribute* attr = *iter; //std::cout << " Attribute " << attr->get_name() << " = " << attr->get_value() << std::endl; f.setAttribute(attr->get_name(), attr->get_value()); } } else { throw InterfaceGeneratorInvalidContentException("constant is not an element"); } // Get field comment NodeSet nameset = (*i)->find("text()"); if ( nameset.size() == 0 ) { throw InterfaceGeneratorInvalidContentException("no comment for field %s", f.getName().c_str()); } const TextNode *comment_node = dynamic_cast<const TextNode *>(nameset[0]); if ( ! comment_node ) { throw InterfaceGeneratorInvalidContentException("comment node not text node for constant"); } f.setComment(comment_node->get_content()); //std::cout << "Field name: " << field_name << std::endl; try { f.valid(); result.push_back(f); } catch ( fawkes::Exception &e ) { e.print_trace(); } } for (vector<InterfaceField>::iterator i = result.begin(); i != result.end(); ++i) { for (vector<InterfaceField>::iterator j = i + 1; j != result.end(); ++j) { if ( (*i).getName() == (*j).getName() ) { throw InterfaceGeneratorAmbiguousNameException((*i).getName().c_str(), "field"); } } } return result; }
void sort(const DOMNode& node, NodeSet& nodes, ExecutionContext<string_type, string_adaptor>& context) const { if(!sort_) { if(!nodes.forward()) nodes.to_document_order(); return; } sort_->set_context(node, context); std::stable_sort(nodes.begin(), nodes.end(), SortP(*sort_)); } // sort
void SomeFunction(DominatorMap &dominators, NodeSet &newDominators, ControlFlowNode *n, _Func func) { newDominators.insert(n); if (!func(n).empty()) { NodeSet result; bool first = true; for (ControlFlowNode *pred : func(n)) { if (first) { result = dominators[pred]; first = false; } else { NodeSet resultTemp; NodeSet &temp = dominators[pred]; set_intersection(result.begin(), result.end(), temp.begin(), temp.end(), inserter(resultTemp, resultTemp.begin()), std::less<ControlFlowNode*>() ); swap(resultTemp, result); } } for (ControlFlowNode *node : result) { newDominators.insert(node); } } // Now look at all the predecessors p of n. // Add in the intersection of all those dominators for them. }
void State::join(const State& a_otherState) { NodePairSet commonRoots; NodeSet myUniqueRoots; NodeSet otherUniqueRoots; debug("State::join - joining state: " + string(*this)); debug("State::join - with state: " + string(a_otherState)); divideTrees(a_otherState, commonRoots, myUniqueRoots, otherUniqueRoots); m_rootSet.clear(); for (NodePairSetIter iter = commonRoots.begin(); iter != commonRoots.end(); iter++) { TreeNode* myTree = iter->first; TreeNode* otherTree = iter->second; myTree->join(otherTree); m_rootSet.insert(myTree); } for (NodeSetIter iter = myUniqueRoots.begin(); iter != myUniqueRoots.end(); iter++) { m_rootSet.insert(*iter); } for (NodeSetIter iter = otherUniqueRoots.begin(); iter != otherUniqueRoots.end(); iter++) { TreeNode* node = new TreeNode(*(*iter)); m_rootSet.insert(node); } buildVariableMap(); debug("State::join - joined state: " + string(*this)); }
void GraphicsContext::removeCamera(osg::Camera* camera) { Cameras::iterator itr = std::find(_cameras.begin(), _cameras.end(), camera); if (itr != _cameras.end()) { // find a set of nodes attached the camera that we are removing that isn't // shared by any other cameras on this GraphicsContext typedef std::set<Node*> NodeSet; NodeSet nodes; for(unsigned int i=0; i<camera->getNumChildren(); ++i) { nodes.insert(camera->getChild(i)); } for(Cameras::iterator citr = _cameras.begin(); citr != _cameras.end(); ++citr) { if (citr != itr) { osg::Camera* otherCamera = *citr; for(unsigned int i=0; i<otherCamera->getNumChildren(); ++i) { NodeSet::iterator nitr = nodes.find(otherCamera->getChild(i)); if (nitr != nodes.end()) nodes.erase(nitr); } } } // now release the GLobjects associated with these non shared nodes for(NodeSet::iterator nitr = nodes.begin(); nitr != nodes.end(); ++nitr) { const_cast<osg::Node*>(*nitr)->releaseGLObjects(_state.get()); } // release the context of the any RenderingCache that the Camera has. if (camera->getRenderingCache()) { camera->getRenderingCache()->releaseGLObjects(_state.get()); } _cameras.erase(itr); } }
NodeSet Liveness::getAllReachedUses(RegisterRef RefRR, NodeAddr<DefNode*> DefA, const RegisterAggr &DefRRs) { NodeSet Uses; // If the original register is already covered by all the intervening // defs, no more uses can be reached. if (DefRRs.hasCoverOf(RefRR)) return Uses; // Add all directly reached uses. // If the def is dead, it does not provide a value for any use. bool IsDead = DefA.Addr->getFlags() & NodeAttrs::Dead; NodeId U = !IsDead ? DefA.Addr->getReachedUse() : 0; while (U != 0) { auto UA = DFG.addr<UseNode*>(U); if (!(UA.Addr->getFlags() & NodeAttrs::Undef)) { RegisterRef UR = UA.Addr->getRegRef(DFG); if (PRI.alias(RefRR, UR) && !DefRRs.hasCoverOf(UR)) Uses.insert(U); } U = UA.Addr->getSibling(); } // Traverse all reached defs. This time dead defs cannot be ignored. for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) { auto DA = DFG.addr<DefNode*>(D); NextD = DA.Addr->getSibling(); RegisterRef DR = DA.Addr->getRegRef(DFG); // If this def is already covered, it cannot reach anything new. // Similarly, skip it if it is not aliased to the interesting register. if (DefRRs.hasCoverOf(DR) || !PRI.alias(RefRR, DR)) continue; NodeSet T; if (DFG.IsPreservingDef(DA)) { // If it is a preserving def, do not update the set of intervening defs. T = getAllReachedUses(RefRR, DA, DefRRs); } else { RegisterAggr NewDefRRs = DefRRs; NewDefRRs.insert(DR); T = getAllReachedUses(RefRR, DA, NewDefRRs); } Uses.insert(T.begin(), T.end()); } return Uses; }
void emptyTree(NodeSet roots) { TreeNode *tempNode = 0, *parentNode = 0; NodeSetIter setIter; NodeList nodeList; NodeListIter listIter; for(setIter=roots.begin(); setIter!=roots.end(); ++setIter) { tempNode=0; parentNode=0; if(*setIter!=0) { nodeList.push_front(*setIter); while (nodeList.size()!=0) { listIter=nodeList.begin(); tempNode=(*listIter); nodeList.pop_front(); if (tempNode->right==0 && tempNode->left==0) { parentNode=tempNode->parent; if (parentNode->right->ID==tempNode->ID) parentNode->right = 0; else parentNode->left=0; delete tempNode; tempNode=0; } else { if(tempNode->right!=0) nodeList.push_front(tempNode->right); if(tempNode->left!=0) nodeList.push_front(tempNode->left); } } } nodeList.clear(); } }
void writeNodeIconSection(std::ostream& out, NodeSet& ns, Config& config) { iconNames.clear(); NodeSet::iterator it = ns.begin(); while (it != ns.end()) { EntityTypeStruct type = (*it)->entity->entityType; const std::string& icon = (*it)->getIconName(); if (!icon.empty()) { std::string filename = config.getFileName(icon); out << "[" << (*it)->NodeId << "] NODE-ICON"; out << "\t\ticons" << config.dirSep << filename << std::endl; iconNames.insert(icon); } it++; } out << std::endl; }
void ConfigFileWriter::writeNetworksFile(std::string filename, NodeSet& ns) { std::fstream out; out.open(filename.c_str(), std::fstream::out | std::fstream::trunc); NodeSet::iterator it = ns.begin(); out << "N1;3000000000;"; while (it != ns.end()) { out << (*it)->NodeId; it++; if (it != ns.end()) { out << ", "; } } out << ";255.255.255.255"; out.close(); }
NodeSet Liveness::getAllReachedUses(RegisterRef RefRR, NodeAddr<DefNode*> DefA, const RegisterSet &DefRRs) { NodeSet Uses; // If the original register is already covered by all the intervening // defs, no more uses can be reached. if (RAI.covers(DefRRs, RefRR, DFG)) return Uses; // Add all directly reached uses. NodeId U = DefA.Addr->getReachedUse(); while (U != 0) { auto UA = DFG.addr<UseNode*>(U); if (!(UA.Addr->getFlags() & NodeAttrs::Undef)) { auto UR = UA.Addr->getRegRef(); if (RAI.alias(RefRR, UR, DFG) && !RAI.covers(DefRRs, UR, DFG)) Uses.insert(U); } U = UA.Addr->getSibling(); } // Traverse all reached defs. for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) { auto DA = DFG.addr<DefNode*>(D); NextD = DA.Addr->getSibling(); auto DR = DA.Addr->getRegRef(); // If this def is already covered, it cannot reach anything new. // Similarly, skip it if it is not aliased to the interesting register. if (RAI.covers(DefRRs, DR, DFG) || !RAI.alias(RefRR, DR, DFG)) continue; NodeSet T; if (DFG.IsPreservingDef(DA)) { // If it is a preserving def, do not update the set of intervening defs. T = getAllReachedUses(RefRR, DA, DefRRs); } else { RegisterSet NewDefRRs = DefRRs; NewDefRRs.insert(DR); T = getAllReachedUses(RefRR, DA, NewDefRRs); } Uses.insert(T.begin(), T.end()); } return Uses; }
void State::functionSetRight(const string& a_parent, const string& a_child) { TreeNode* childNode = getUniqueRoot(a_child); if (childNode == NULL) { error("State::functionSetRight - Can't set a node as a child if it already has a parent", false); return; } NodeSet* parentSet = getVariableNodes(a_parent); if (parentSet == NULL) { error("State::functionSetRight - variable " + a_parent + " doesn't exist", false); return; } for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++) { if ((*iter) == NULL) { error("State::functionSetRight - iterator for variable " + a_parent + " is NULL", false); continue; } TreeNode* rightChild = (*iter)->getRightChild(); if (rightChild == NULL) { TreeNode* node = new TreeNode(*childNode); node->setParent(*iter); (*iter)->setRightChild(node); } else { rightChild->join(childNode); } } m_rootSet.erase(childNode); delete childNode; buildVariableMap(); }
void SchedViz::Visit_Deployment( const Semantics::Deployment &deployment ) { // Determine the hyperperiod // TODO: quick fix -- this needs to be changed if we have multiple hyperperiods BusSet busSet = deployment.CommMedium_children(); BusSet::iterator busIter = busSet.begin(); this->_hyperperiodSec = busIter->hyperperiodsecs(); // Get the set of children Nodes NodeSet nodeSet = deployment.Node_children(); NodeSet::iterator nodeIter = nodeSet.begin(); // For each node ... for ( ; nodeIter != nodeSet.end(); nodeIter++ ) { // Visit the node this->Visit_Node( *nodeIter ); } // For each bus ... for ( ; busIter != busSet.end(); busIter++ ) { // Visit each bus this->Visit_Bus( *busIter ); } }
void State::functionSetValue(const string& a_name, int a_value) { NodeSet* parentSet = getVariableNodes(a_name); if (parentSet == NULL) { stringstream ss; error("State::functionSetValue - variable " + a_name + " doesn't exist", false); return; } for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++) { if ((*iter) == NULL) { error("State::functionSetValue - iterator for variable " + a_name + " is NULL", false); continue; } (*iter)->setMaxValue(a_value); (*iter)->setMinValue(a_value); } }
void State::divideTrees(const State& a_other, NodePairSet& a_commonRoots, NodeSet& a_myUniqueRoots, NodeSet& a_otherUniqueRoots) { NodeSet otherRootSet = a_other.getRootSet(); // Copy the entire set a_commonRoots.clear(); a_myUniqueRoots.clear(); a_otherUniqueRoots.clear(); for (NodeSetIter iter = m_rootSet.begin(); iter != m_rootSet.end(); iter++) { bool isUnique = true; string rootName; if (!((*iter)->getUniqueName(rootName))) { error("State::divideTrees - Multiple roots (in this state) with the same name " + rootName + " exist! " + string(*this), true); } for (NodeSetIter otherIter = otherRootSet.begin(); otherIter != otherRootSet.end(); otherIter++) { string otherRootName; if (!((*otherIter)->getUniqueName(otherRootName))) { error("State::divideTrees - Multiple roots (in joined state) with the same name " + otherRootName + " exist! " + string(a_other), true); } if (rootName == otherRootName) { a_commonRoots.insert(make_pair((*iter), (*otherIter))); otherRootSet.erase(otherIter); isUnique = false; break; } } if (isUnique) { a_myUniqueRoots.insert(*iter); } } a_otherUniqueRoots = otherRootSet; }
void State::functionIncrement(const string& a_name, int a_value) { NodeSet* parentSet = getVariableNodes(a_name); if (parentSet == NULL) { verbose("State::functionIncrement - variable " + a_name + " doesn't exist"); return; } for (NodeSetConstIter iter = parentSet->begin(); iter != parentSet->end(); iter++) { if ((*iter) == NULL) { error("State::functionIncrement - iterator for variable " + a_name + " is NULL", false); continue; } int maxValue = (*iter)->getMaxValue(); int minValue = (*iter)->getMinValue(); (*iter)->setMaxValue(maxValue + a_value); (*iter)->setMinValue(minValue + a_value); } }
/* * DeleteCFG() * Remove one CFG. */ void DeleteCFG(GraphNode *Root) { NodeVec VisitStack; NodeSet Visited; VisitStack.push_back(Root); while(VisitStack.size()) { GraphNode *Parent = VisitStack.back(); VisitStack.pop_back(); if (Visited.count(Parent)) continue; Visited.insert(Parent); NodeVec &Child = Parent->getSuccessor(); for (int i = 0, e = Child.size(); i < e; i++) VisitStack.push_back(Child[i]); } for (NodeSet::iterator I = Visited.begin(), E = Visited.end(); I != E; I++) delete *I; }
void ConfigFileWriter::writeNodesFile(std::string filename, NodeSet& ns) { std::fstream out; out.open(filename.c_str(), std::fstream::out | std::fstream::trunc); out.precision(10); NodeSet::iterator it = ns.begin(); while (it != ns.end()) { EntityStruct* ent = (*it)->entity; double lat = ent->worldLocation.lat; double lon = ent->worldLocation.lon; if (fabs(lat) > 90.0 || fabs(lon) > 180.0) { lat = 0.0; lon = 0.0; } RadioStruct* radio = (*it)->radio; if (radio) { double lat, lon, alt; lat = radio->absoluteNodePosInLatLonAlt.lat; lon = radio->absoluteNodePosInLatLonAlt.lon; alt = radio->absoluteNodePosInLatLonAlt.alt; out << (*it)->NodeId << " 0 "; out << "(" << lat << ", " << lon << ", " << alt << ") "; out << ent->orientation.azimuth << " "; out << ent->orientation.elevation; out << std::endl; } it++; } out.close(); }
ActionChoiceWindow::NodeSet ActionChoiceWindow::ActionNode::getAllNodesNotBelow( ActionNode* treeRoot, ActionChoiceWindow::ActionNode* targetNode) { NodeSet allNodes; getAllNodes(treeRoot, allNodes); NodeSet nodes; for (NodeSet::iterator iter = allNodes.begin(); iter != allNodes.end(); iter++) { bool leaveOut = false; if ((*iter)->getParent() == treeRoot || *iter == targetNode || (*iter)->getButton() == NULL) { leaveOut = true; continue; } ActionNode* node = *iter; while(node->getParent() != NULL) { node = node->getParent(); if (node == targetNode) { leaveOut = true; continue; } } if (!leaveOut) nodes.insert(*iter); } return nodes; }
void generateConstraints(vector<Node*>& nodes, vector<Edge*>& edges,vector<SimpleConstraint*>& cs,Dim dim) { unsigned nevents=2*nodes.size()+2*edges.size(); events=new Event*[nevents]; unsigned ctr=0; if(dim==HORIZONTAL) { //cout << "Scanning top to bottom..." << endl; for(unsigned i=0;i<nodes.size();i++) { Node *v=nodes[i]; v->scanpos=v->x; events[ctr++]=new Event(Open,v,v->ymin+0.01); events[ctr++]=new Event(Close,v,v->ymax-0.01); } for(unsigned i=0;i<edges.size();i++) { Edge *e=edges[i]; events[ctr++]=new Event(Open,e,e->ymin-1); events[ctr++]=new Event(Close,e,e->ymax+1); } } else { //cout << "Scanning left to right..." << endl; for(unsigned i=0;i<nodes.size();i++) { Node *v=nodes[i]; v->scanpos=v->y; events[ctr++]=new Event(Open,v,v->xmin+0.01); events[ctr++]=new Event(Close,v,v->xmax-0.01); } for(unsigned i=0;i<edges.size();i++) { Edge *e=edges[i]; events[ctr++]=new Event(Open,e,e->xmin-1); events[ctr++]=new Event(Close,e,e->xmax+1); } } qsort((Event*)events, (size_t)nevents, sizeof(Event*), compare_events ); NodeSet openNodes; vector<Edge*> openEdges; for(unsigned i=0;i<nevents;i++) { Event *e=events[i]; Node *v=e->v; if(v!=NULL) { v->open = true; //printf("NEvent@%f,nid=%d,(%f,%f),w=%f,h=%f,openn=%d,opene=%d\n",e->pos,v->id,v->x,v->y,v->width,v->height,(int)openNodes.size(),(int)openEdges.size()); Node *l=NULL, *r=NULL; if(!openNodes.empty()) { // it points to the first node to the right of v NodeSet::iterator it=openNodes.lower_bound(v); // step left to find the first node to the left of v if(it--!=openNodes.begin()) { l=*it; //printf("l=%d\n",l->id); } it=openNodes.upper_bound(v); if(it!=openNodes.end()) { r=*it; } } vector<Node*> L; sortNeighbours(v,l,r,e->pos,openEdges,L,nodes,dim); //printf("L=["); for(unsigned i=0;i<L.size();i++) { //printf("%d ",L[i]->id); } //printf("]\n"); // Case A: create constraints between adjacent edges skipping edges joined // to l,v or r. Node* lastNode=NULL; for(vector<Node*>::iterator i=L.begin();i!=L.end();i++) { if((*i)->dummy) { // node is on an edge Edge *edge=(*i)->edge; if(!edge->isEnd(v->id) &&((l!=NULL&&!edge->isEnd(l->id))||l==NULL) &&((r!=NULL&&!edge->isEnd(r->id))||r==NULL)) { if(lastNode!=NULL) { //printf(" Rule A: Constraint: v%d +g <= v%d\n",lastNode->id,(*i)->id); cs.push_back(createConstraint(lastNode,*i,dim)); } lastNode=*i; } } else { // is an actual node lastNode=NULL; } } // Case B: create constraints for all the edges connected to the right of // their own end, also in the scan line vector<Node*> skipList; lastNode=NULL; for(vector<Node*>::iterator i=L.begin();i!=L.end();i++) { if((*i)->dummy) { // node is on an edge if(lastNode!=NULL) { if((*i)->edge->isEnd(lastNode->id)) { skipList.push_back(*i); } else { for(vector<Node*>::iterator j=skipList.begin(); j!=skipList.end();j++) { //printf(" Rule B: Constraint: v%d +g <= v%d\n",(*j)->id,(*i)->id); cs.push_back(createConstraint(*j,*i,dim)); } skipList.clear(); } } } else { // is an actual node skipList.clear(); skipList.push_back(*i); lastNode=*i; } } skipList.clear(); // Case C: reverse of B lastNode=NULL; for(vector<Node*>::reverse_iterator i=L.rbegin();i!=L.rend();i++) { if((*i)->dummy) { // node is on an edge if(lastNode!=NULL) { if((*i)->edge->isEnd(lastNode->id)) { skipList.push_back(*i); } else { for(vector<Node*>::iterator j=skipList.begin(); j!=skipList.end();j++) { //printf(" Rule C: Constraint: v%d +g <= v%d\n",(*i)->id,(*j)->id); cs.push_back(createConstraint(*i,*j,dim)); } skipList.clear(); } } } else { // is an actual node skipList.clear(); skipList.push_back(*i); lastNode=*i; } } if(e->type==Close) { if(l!=NULL) cs.push_back(createConstraint(l,v,dim)); if(r!=NULL) cs.push_back(createConstraint(v,r,dim)); } } if(e->type==Open) { if(v!=NULL) { openNodes.insert(v); } else { //printf("EdgeOpen@%f,eid=%d,(u,v)=(%d,%d)\n", e->pos,e->e->id,e->e->startNode,e->e->endNode); e->e->openInd=openEdges.size(); openEdges.push_back(e->e); } } else { // Close if(v!=NULL) { openNodes.erase(v); v->open=false; } else { //printf("EdgeClose@%f,eid=%d,(u,v)=(%d,%d)\n", e->pos,e->e->id,e->e->startNode,e->e->endNode); unsigned i=e->e->openInd; openEdges[i]=openEdges[openEdges.size()-1]; openEdges[i]->openInd=i; openEdges.resize(openEdges.size()-1); } } delete e; } delete [] events; }
/** * Prepares constraints in order to apply VPSC horizontally. Assumes variables have already been created. * useNeighbourLists determines whether or not a heuristic is used to deciding whether to resolve * all overlap in the x pass, or leave some overlaps for the y pass. */ int generateXConstraints(const int n, Rectangle** rs, Variable** vars, Constraint** &cs, const bool useNeighbourLists) { events=new Event*[2*n]; int i,m,ctr=0; for(i=0;i<n;i++) { vars[i]->desiredPosition=rs[i]->getCentreX(); Node *v = new Node(vars[i],rs[i],rs[i]->getCentreX()); events[ctr++]=new Event(Open,v,rs[i]->getMinY()); events[ctr++]=new Event(Close,v,rs[i]->getMaxY()); } qsort((Event*)events, (size_t)2*n, sizeof(Event*), compare_events ); NodeSet scanline; vector<Constraint*> constraints; for(i=0;i<2*n;i++) { Event *e=events[i]; Node *v=e->v; if(e->type==Open) { scanline.insert(v); if(useNeighbourLists) { v->setNeighbours( getLeftNeighbours(scanline,v), getRightNeighbours(scanline,v) ); } else { NodeSet::iterator it=scanline.find(v); if(it--!=scanline.begin()) { Node *u=*it; v->firstAbove=u; u->firstBelow=v; } it=scanline.find(v); if(++it!=scanline.end()) { Node *u=*it; v->firstBelow=u; u->firstAbove=v; } } } else { // Close event int r; if(useNeighbourLists) { for(NodeSet::iterator i=v->leftNeighbours->begin(); i!=v->leftNeighbours->end();i++ ) { Node *u=*i; double sep = (v->r->width()+u->r->width())/2.0; constraints.push_back(new Constraint(u->v,v->v,sep)); r=u->rightNeighbours->erase(v); } for(NodeSet::iterator i=v->rightNeighbours->begin(); i!=v->rightNeighbours->end();i++ ) { Node *u=*i; double sep = (v->r->width()+u->r->width())/2.0; constraints.push_back(new Constraint(v->v,u->v,sep)); r=u->leftNeighbours->erase(v); } } else { Node *l=v->firstAbove, *r=v->firstBelow; if(l!=NULL) { double sep = (v->r->width()+l->r->width())/2.0; constraints.push_back(new Constraint(l->v,v->v,sep)); l->firstBelow=v->firstBelow; } if(r!=NULL) { double sep = (v->r->width()+r->r->width())/2.0; constraints.push_back(new Constraint(v->v,r->v,sep)); r->firstAbove=v->firstAbove; } } r=scanline.erase(v); delete v; } delete e; } delete [] events; cs=new Constraint*[m=constraints.size()]; for(i=0;i<m;i++) cs[i]=constraints[i]; return m; }