int list_if(tree<int> &T, list<int> &L, bool (*pred)(int)) { L.clear(); if (T.begin()!=T.end()) list_if(T,T.begin(),L,pred); }
void print_subtree_bracketed(const tree<T>& t, typename tree<T>::iterator iRoot, std::ostream& str) { if(t.begin() == t.end()) return; if (t.number_of_children(iRoot) == 0) { str << *iRoot; } else { // parent str << *iRoot; str << "("; // child1, ..., childn int siblingCount = t.number_of_siblings(t.begin(iRoot)); int siblingNum; typename tree<T>::sibling_iterator iChildren; for (iChildren = t.begin(iRoot), siblingNum = 0; iChildren != t.end(iRoot); ++iChildren, ++siblingNum) { // recursively print child print_subtree_bracketed(t,iChildren,str); // comma after every child except the last one if (siblingNum != siblingCount - 1 ) { str << ", "; } } str << ")"; } }
void writeSiblingsXML(const tree<AstNode>& t, const tree<AstNode>::iterator iRoot, ostream& stream) { if(t.empty()) return; if (iRoot->getType() == "root") { tree<AstNode>::sibling_iterator iChildren = t.begin(iRoot); stream << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" << endl; writeSiblingsXML(t,iChildren,stream); } else if (t.number_of_children(iRoot) == 0) { string type = iRoot->getType(); stream << "<php:" << type << '>'; if (iRoot->getValue().length() > 0) stream << htmlentities(iRoot->getValue()); stream << "</php:" << type << '>' << endl; } else { string type = iRoot->getType(); string xmlns=""; if (type == "start") xmlns = " xmlns:php=\"http://php.net/csl\""; stream << "<php:" << type << xmlns << '>' << endl; int siblingNum; tree<AstNode>::sibling_iterator iChildren; for (iChildren = t.begin(iRoot), siblingNum = 0; iChildren != t.end(iRoot); ++iChildren) { writeSiblingsXML(t,iChildren,stream); } stream << "</php:" << type << '>' << endl; } }
void print_tree_bracketed(const tree<T>& t, std::ostream& str) { int headCount = t.number_of_siblings(t.begin()); int headNum = 0; for(typename tree<T>::sibling_iterator iRoots = t.begin(); iRoots != t.end(); ++iRoots) { print_subtree_bracketed(t,iRoots,str); if (headNum <= headCount - 1) { str << std::endl; } } }
void printTree(tree<T> const &intree, std::ostream& str=std::cout) { int nhead(0); auto now(intree.begin()); while(intree.is_valid(now)){ str << " " << *now << boost::format(" <-- [Head](%3d)") % nhead++ << std::endl; for(typename tree<T>::iterator sib = intree.begin(now);sib != intree.end(now);++sib){ for(int i = 0;i < intree.depth(sib);++i) str << "---"; str << "> "; str << *sib << std::endl; } // End sib now = intree.next_at_same_depth(now); } // End while }
tree_node_<FILE_ITEM>* CFileTree::findNodeWithPathFromNode(std::string path, tree_node_<FILE_ITEM>* node) { tree<FILE_ITEM>::sibling_iterator sib = filesTree.begin(node); tree<FILE_ITEM>::sibling_iterator end = filesTree.end(node); bool currentLevel = true; std::string currentPath = _first_dirname(path); size_t position = currentPath.size(); std::string followingPath = _following_path(path); currentLevel = followingPath.empty(); while (sib != end) { // printf("sib->path '%s' lv %d curpath '%s' follow '%s'\n", sib->path, currentLevel, currentPath.c_str(), followingPath.c_str()); if (strcmp(sib->path, currentPath.c_str()) == 0) { if (currentLevel) { return sib.node; } else { return findNodeWithPathFromNode(followingPath, sib.node); } } ++sib; } return NULL; }
/*! * \fn static bool GenerateSubTree(const tree<HTML::Node> &tDom, const string &tagname, tree<HTML::Node> &tSubDom); * \brief 生成子树 * \param [in]DOM原树 * \param [in]子树根节点的标签名 * \param [out]DOM子树 * \return bool * \date 2011-06-01 * \author nanjunxiao */ bool Pretreat::GenerateSubTree(const tree<HTML::Node> &tDom, const std::string &tagname, tree<HTML::Node> &tSubDom)//目前只是为body使用 { tree<HTML::Node>::iterator tIter = tDom.begin(); tree<HTML::Node>::sibling_iterator tFromIter,tToIter; string sTagName; for (; tIter != tDom.end(); ++tIter) { /*if (tIter->tagName() == tagname) break;*/ if (tIter->isTag() ) { sTagName = tIter->tagName(); transform(sTagName.begin(), sTagName.end(), sTagName.begin(), ::tolower); if (sTagName == tagname) break; } } if (tIter == tDom.end() ) { return false; } tFromIter = tIter; tToIter = tDom.next_sibling(tFromIter); tDom.subtree(tSubDom, tFromIter, tToIter); return true; }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*> void graph2tree(map<int, list<int> > &G, tree<int> &T) { // Buscar la raiz. Para eso recorremos los padres (claves // de G) y buscamos cual no esta en la lista de hijos set<int> fathers, sons, tmp; map<int, list<int> >::iterator q = G.begin(); while (q!=G.end()) { fathers.insert(q->first); tmp.clear(); set_union(q->second.begin(),q->second.end(), sons.begin(),sons.end(), inserter(tmp,tmp.end())); swap(tmp,sons); q++; } // Saca de `fathers' los hijos, el unico que deberia // quedar es la raiz tmp.clear(); set_difference(fathers.begin(),fathers.end(), sons.begin(),sons.end(), inserter(tmp,tmp.end())); assert(tmp.size()==1); int root = *tmp.begin(); // printf("root of tree is %d\n",root); graph2tree(G,root,T,T.begin()); }
tree_node_<FILE_ITEM>* CFileTree::findNodeWithPathFromNode(std::string path, tree_node_<FILE_ITEM>* node) { tree<FILE_ITEM>::sibling_iterator sib = filesTree.begin(node); tree<FILE_ITEM>::sibling_iterator end = filesTree.end(node); bool currentLevel = true; std::string currentPath = path.substr(0, path.find('\\')); size_t position = path.find('\\'); std::string followingPath(""); if (position != std::string::npos) { followingPath = path.substr(path.find('\\') + 1); currentLevel = false; } while (sib != end) { if (strcmp(sib->path, currentPath.c_str()) == 0) { if (currentLevel) { return sib.node; } else { return findNodeWithPathFromNode(followingPath, sib.node); } } ++sib; } return NULL; }
void RenameClass::operator()(tree<AstNode>& tr, MapClasses* classes, MapVariables* vars, MapFunctions *func) { // for every names in the class, generate a new *unique* name map<string, string> classNames; for (MapClasses::iterator iter = classes->begin(); iter != classes->end(); ++iter) { string newName = generateName(); classNames.insert(make_pair(iter->first, newName)); } map<string, string>::iterator cter; tree<AstNode>::iterator parent; for (tree<AstNode>::iterator iter=tr.begin(); iter!=tr.end(); ++iter) { parent = tr.parent(iter); if (iter->getType() == "text" && parent->getType() == "T_STRING" && (tr.parent(parent)->getType() == "unticked_class_declaration_statement" || tr.parent(parent)->getType() == "function_call" // constructor || tr.parent(parent)->getType() == "class_name_reference" || tr.parent(parent)->getType() == "fully_qualified_class_name" ) && ((cter=classNames.find(iter->getValue())) != classNames.end())) { // rename the currenet node iter->setValue(cter->second); } } }
FILE_ITEM CFileTree::AddItem(char *absolutePath, unsigned char* handle) { FILE_ITEM item; item.handle = handle; item.bCached = false; if (filesTree.empty()) { item.path = new char[strlen(absolutePath) + 1]; strcpy_s(item.path, (strlen(absolutePath) + 1), absolutePath); item.nPathLen = strlen(item.path); filesTree.set_head(item); topNode = filesTree.begin(); } else { std::string sPath(absolutePath); tree_node_<FILE_ITEM>* parentNode = findParentNodeFromRootForPath(absolutePath); std::string splittedPath = sPath.substr(sPath.find_last_of('\\') + 1); item.path = new char[splittedPath.length() + 1]; strcpy_s(item.path, (splittedPath.length() + 1), splittedPath.c_str()); if (parentNode) { filesTree.append_child(tree<FILE_ITEM>::iterator_base(parentNode), item); } else { //printf("Parent node found for %s", absolutePath); } } DisplayTree(topNode.node, 0); return item; }
void WeaveNet(SQcont<T> const &incont, tree<T> &outnet) { outnet.clear(); auto top(outnet.begin()); auto now(outnet.insert(top, *incont.cbegin())); for(auto c = incont.cbegin()+1;c != incont.cend();++c) now = outnet.append_child(now, *c); }
/** Clean the possible patterns annotations: $enter_new_statement ... */ void clean_pattern(tree<AstNode>& tr) { for (tree<AstNode>::iterator iter=tr.begin(); iter!=tr.end(); ++iter) { if (iter->getType() == "text" && iter->getValue() == "$enter_the_new_statement") { iter = rewind(iter, "statement", tr); tr.erase(iter); break; } } }
bool detectAfterStmt(const tree<AstNode>& tr) { unsigned short ret = 0; for (tree<AstNode>::iterator iter=tr.begin(); iter!=tr.end(); ++iter) { if (iter->getType() == "text" && iter->getValue() == "$__END_OBF_HERE") { return true; } } return false; }
FILE_ITEM CFileTree::AddItem(char *absolutePath, unsigned char* handle) { FILE_ITEM item; item.handle = handle; item.bCached = false; // If the tree is empty just add the new path as node on the top level. if (filesTree.empty()) { item.path = new char[strlen(absolutePath) + 1]; strcpy_s(item.path, (strlen(absolutePath) + 1), absolutePath); item.nPathLen = strlen(item.path); filesTree.set_head(item); topNode = filesTree.begin(); } else { // Check if the requested path belongs to an already registered parent node. std::string sPath(absolutePath); tree_node_<FILE_ITEM>* parentNode = findParentNodeFromRootForPath(absolutePath); std::string splittedPath = _basename_932(sPath); //printf("spl %s %s\n", splittedPath.c_str(), absolutePath); item.path = new char[splittedPath.length() + 1]; strcpy_s(item.path, (splittedPath.length() + 1), splittedPath.c_str()); // If a parent was found use th parent. if (parentNode) { //printf("parent %s\n", parentNode->data.path); filesTree.append_child(tree<FILE_ITEM>::iterator_base(parentNode), item); } else { // Node wasn't found - most likely a new root - add it to the top level. //printf("No parent node found for %s. Adding new sibbling.", absolutePath); item.path = new char[strlen(absolutePath) + 1]; strcpy_s(item.path, (strlen(absolutePath) + 1), absolutePath); item.nPathLen = strlen(item.path); filesTree.insert(tree<FILE_ITEM>::iterator_base(topNode), item); topNode = filesTree.begin(); } } DisplayTree(topNode.node, 0); return item; }
void mergeNodes(tree<T> &intree) { // First, determine which top node has the shallowest depth int minimal_d(1000); for(typename tree<T>::leaf_iterator c = intree.begin_leaf();c != intree.end_leaf();++c){ int this_depth(intree.depth(c)); if(this_depth < minimal_d) minimal_d = this_depth; } // End c if(minimal_d == -1) return; typedef std::map<T, typename tree<T>::iterator> Info; auto now(intree.begin()); //const int max_depth(intree.max_depth()-1); const int max_depth(minimal_d); for(int depth = 0;depth < max_depth;++depth){ typename tree<T>::fixed_depth_iterator itr_begin(intree.begin_fixed(intree.begin(), depth)); Info siblings; while(intree.is_valid(itr_begin)){ int counts(siblings.count(*itr_begin)); typename tree<T>::fixed_depth_iterator itr_next(intree.next_at_same_depth(itr_begin)); // If current node and the next node are same, combine them if(!counts){ typename tree<T>::iterator current(itr_begin); siblings.insert(typename Info::value_type(*itr_begin, current)); } // End if else{ typename tree<T>::iterator current(itr_begin); typename tree<T>::iterator next(siblings[*itr_begin]); intree.merge(intree.begin(next), intree.end(next), intree.begin(current), intree.end(current)); #ifdef _DEBUG_TREE std::cout << "C: " << *current << " == N: " << *next << std::endl; #endif typename tree<T>::iterator c_back(itr_begin); const int mydepth(intree.depth(c_back)); c_back -= mydepth; intree.erase(c_back); // Erase the redundunt nodes } // End else itr_begin = itr_next; } // End while } // End depth }
void WeaveNets(SQcont<SQcont<T> > &incont, tree<T> &outnet) { outnet.clear(); auto top(outnet.begin()); for(auto c = incont.begin();c != incont.end();++c){ if(c->size()){ auto now(outnet.insert(top, c->at(0))); for(size_t num = 1;num < c->size();++num) now = outnet.append_child(now, c->at(num)); } // End if } // End c }
void show_tree ( tree<Symbol> T ) { for ( tree<Symbol> :: iterator it_of_tree = T.begin(); it_of_tree != T.end(); it_of_tree ++ ) { drawRed( it_of_tree -> name); cout<<endl; cout << " P:(" << it_of_tree -> position[0] << ", "<< it_of_tree -> position[1] << ", " << it_of_tree -> position[2] << ") "<<endl; cout << " S:(" << it_of_tree -> scale[0] << ", " << it_of_tree -> scale[1] << ", " << it_of_tree -> scale[0] << ") "<<endl; cout << " active: " << it_of_tree -> active <<endl<< " drawable: " << it_of_tree -> drawable<<endl; cout << endl; } }
tree_node_<FILE_ITEM>* CFileTree::findNodeFromRootWithPath(char *path) { // No topNode - bail out. if (topNode.node == NULL){ return NULL; } std::string sPath(path); std::string nPath(topNode->path); // topNode path and requested path are the same? Use the node. if (sPath == nPath) { return topNode.node; } // printf("Did not find node for path : %s\n", path); // If the topNode path is part of the requested path this is a subpath. // Use the node. if (sPath.find(nPath) != std::string::npos) { // printf("Found %s is part of %s \n", sPath.c_str(), topNode->path); std::string splittedString = sPath.substr(strlen(topNode->path) + 1); return findNodeWithPathFromNode(splittedString, topNode.node); } else { // If the current topNode isn't related to the requested path // iterate over all _top_ level elements in the tree to look for // a matching item and register it as current top node. // printf("NOT found %s is NOT part of %s \n", sPath.c_str(), topNode->path); tree<FILE_ITEM>::sibling_iterator it; for (it = filesTree.begin(); it != filesTree.end(); it++) { std::string itPath(it.node->data.path); // Current item path matches the requested path - use the item as topNode. if (sPath == itPath) { // printf("Found parent node %s \n", it.node->data.path); topNode = it; return it.node; } else if (sPath.find(itPath) != std::string::npos) { // If the item path is part of the requested path this is a subpath. // Use the the item as topNode and continue analyzing. // printf("Found root node %s \n", it.node->data.path); topNode = it; std::string splittedString = sPath.substr(itPath.length() + 1); return findNodeWithPathFromNode(splittedString, it.node); } } } // Nothing found return NULL. return NULL; }
/** Copy for duplicating a branch */ void copy_branch(tree<AstNode>::iterator where, tree<AstNode>::iterator from, tree<AstNode>& tr_where) { if (tr_where.number_of_children(from) == 0) { // simply add the node to it tr_where.append_child(where, *from); } else { where = tr_where.append_child(where, *from); tree<AstNode>::sibling_iterator cter; for (cter = tr_where.begin(from); cter != tr_where.end(from); ++cter) { copy_branch(where, cter, tr_where); } } }
void DisplayTree(tree_node_<FILE_ITEM>* node, int level) { if (CFileTree::debug) { printf("\n\n\n<<<<<<<<<<<<<<<<<<<<<DISPLAY tree \n\n\n"); tree<FILE_ITEM>::sibling_iterator sib2 = filesTree.begin(node); tree<FILE_ITEM>::sibling_iterator end2 = filesTree.end(node); while (sib2 != end2) { for (int i = 0; i < level; i++) { printf(" "); } if (tree<FILE_ITEM>::number_of_children(sib2) > 1) { DisplayTree(sib2.node, (level + 1)); } ++sib2; } printf("\n\n\n<<<<<<<<<<<<<<<<<<<<<End tree \n\n\n"); } }
void RenameVariable::operator()(tree<AstNode>& tr, MapClasses* classes, MapVariables* vars, MapFunctions *func) { // for every names in the class, generate a new *unique* name map<string, string> varNames; for (MapVariables::iterator iter = vars->begin(); iter != vars->end(); ++iter) { string newName = "$" + generateName(); varNames.insert(make_pair(iter->first, newName)); } map<string, string>::iterator cter; for (tree<AstNode>::iterator iter=tr.begin(); iter!=tr.end(); ++iter) { if (iter->getType() == "text" && ((cter=varNames.find(iter->getValue())) != varNames.end())) { // rename the currenet node iter->setValue(cter->second); } } }
/*! * \fn static void CleanUpFormTag(tree<HTML::Node> &m_TagTree); * \brief 剪枝,去除scrip等无用节点 * \param [in]DOM树 * \return void * \date 2011-06-01 * \author nanjunxiao */ void Pretreat::CleanUpFormTag(tree<HTML::Node> &m_TagTree) { set<string> m_sCleanFormSet; m_sCleanFormSet.insert("input"); m_sCleanFormSet.insert("noembed"); m_sCleanFormSet.insert("noscript"); m_sCleanFormSet.insert("textarea"); m_sCleanFormSet.insert("marquee"); m_sCleanFormSet.insert("object"); m_sCleanFormSet.insert("select"); m_sCleanFormSet.insert("iframe"); m_sCleanFormSet.insert("style"); m_sCleanFormSet.insert("script"); tree<HTML::Node>::iterator itTagTreeBegin = m_TagTree.begin(); tree<HTML::Node>::iterator itTagTreeEnd = m_TagTree.end(); while ( itTagTreeBegin != itTagTreeEnd ) { //遍历标签树,标签节点且节点名称在m_sCleanFormSet中则把标签节点在标签树上删除 if ( itTagTreeBegin->isTag() ) { string sTagName = itTagTreeBegin->tagName(); if ( m_sCleanFormSet.find(sTagName) != m_sCleanFormSet.end() ) { tree<HTML::Node>::sibling_iterator sibTmp = itTagTreeBegin; sibTmp++; m_TagTree.erase( itTagTreeBegin ); itTagTreeBegin = sibTmp; } else { itTagTreeBegin++ ; } }//end if else { itTagTreeBegin++; } }//end while }
void CFileTree::RenameItem(char *absolutePathFrom, char *absolutePathTo) { tree_node_<FILE_ITEM>* node = findNodeFromRootWithPath(absolutePathFrom); tree_node_<FILE_ITEM>* parentNode = findParentNodeFromRootForPath(absolutePathTo); if (parentNode != NULL && node != NULL) { if (filesTree.number_of_children(parentNode) < 1) { FILE_ITEM emptyItem; emptyItem.nPathLen = 0; emptyItem.path = const_cast<char*>(""); filesTree.append_child(tree<FILE_ITEM>::iterator_base(parentNode), emptyItem); } tree<FILE_ITEM>::iterator firstChild = filesTree.begin(parentNode); filesTree.move_after(firstChild, tree<FILE_ITEM>::iterator(node)); std::string sPath(absolutePathTo); std::string splittedPath = sPath.substr(sPath.find_last_of('\\') + 1); node->data.path = new char[splittedPath.length() + 1]; strcpy_s(node->data.path, (splittedPath.length() + 1), splittedPath.c_str()); } DisplayTree(topNode.node, 0); }
/** * This is the method for retrieving data from a file. The whole * tree will be written to the new file immediately after being * called. Interpreting the string is left up to the implementing * code. */ void TextPlainRetriever::getData(const string &location, tree<Node> &tr){ //cout << "TextPlainRetriever::getData(" << location << ",tree)" << endl; // REMOVE // check that the argument is not an empty string if(location.size()<=0) throw invalid_argument("cannot parse empty string"); // check that the argument is an integer int line_num=string_util::str_to_int(location); // set stream to the line before skip_to_line(infile,current_line,line_num); // read the line and print it to the console string text=read_line(infile); // create an empty node Node node("empty","empty"); // put the data in the node vector<int> dims; dims.push_back(text.size()); update_node_from_string(node,text,dims,Node::CHAR); tr.insert(tr.begin(),node); }
/** Apply some modification to the current tree. This is used to load the external definition and change some names */ void applyModification(tree<AstNode>& tr) { map<string, string> randNames; string randName = ""; string randFunc = ""; for (tree<AstNode>::iterator iter=tr.begin(); iter!=tr.end(); ++iter) { string valueType = iter->getValue(); if (utils::start_with(valueType,"$rand_name") || utils::start_with(valueType,"rand_func_name") || utils::start_with(valueType,"rand_class_name")) { string genValue = generateName(); if (valueType[0] == '$') genValue = "$" + genValue; if (randNames.find(valueType) == randNames.end()) randNames.insert(make_pair(valueType, genValue)); iter->setValue(randNames[valueType]); } else if (utils::start_with(valueType,"$param_")) { string name = valueType; utils::replace(name, "param_",""); iter->setValue(name); } } }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*> bool es_parcialmente_ordenado2(tree<int> &T, bool (*comp)(int,int)) { if (T.begin()==T.end()) return true; return es_parcialmente_ordenado2(T,T.begin(),comp); }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*>---:---<*>---: void tree2list(tree &A,list<elem_t> &L,elem_t BP,elem_t EP) { tree2list(A,A.begin(),L,BP,EP); }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*>---:---<*>---: iterator_t list2tree(tree &A,const list<elem_t> &L,elem_t BP,elem_t EP) { list<elem_t>::const_iterator p = L.begin(); return list2tree(A,A.begin(),L,p,BP,EP); }
void make_random_tree(tree<int> &T,int M,int siblings) { make_random_tree(T,T.begin(),M,1,siblings); }