/*! * \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 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 << ")"; } }
// ----------------------------------------------------------------- // Hace que el valor de un nod interior sea el resultado de aplicar // la funcion asociativa a los hijos, es decir // `*n = afun(init_val,s_1,s_2,...,s_m)', // donde `s_j' son los hijos de `n' template<class T> void reduce_up (tree<T> & Q, typename tree<T>::iterator n, T (*afun) (T,T), T init_val) { typename tree<T>::iterator c ; T val ; c = n.lchild(); if (c == Q.end()) return; val = init_val; while (c != Q.end()) { reduce_up (Q, c, afun, init_val); val = afun (val,*c); c++; } *n = val; }
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; } }
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; }
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 apply (tree<T> & Q, typename tree<T>::iterator n, T (*f) (T)) { typename tree<T>::iterator c ; *n = f (*n); c = n.lchild (); while (c != Q.end()) apply (Q, c++, f); }
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 tree2list(tree &A,iterator_t n, list<elem_t> &L,elem_t BP,elem_t EP) { if (n == A.end()) return; iterator_t c = n.lchild(); if (c == A.end()) { L.insert(L.end(),A.retrieve(n)); } else { L.insert(L.end(),BP); L.insert(L.end(),A.retrieve(n)); while (c != A.end()) { tree2list(A,c,L,BP,EP); c = c.right(); } L.insert(L.end(),EP); } }
bool all (tree<T> &Q,typename tree<T>::iterator n, bool (*pred_fun)(T j)) { if (!pred_fun(*n)) return false; typename tree<T>::iterator c = n.lchild(); while (c!=Q.end()) if (!pred_fun(*c++)) return false; return true; }
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); } } }
bool any (tree <T> & Q, typename tree<T>::iterator n, bool (*pred_fun) (T)) { typename tree<T>::iterator c ; if ( pred_fun (*n) ) return true; c = n.lchild (); while (c != Q.end()) if (pred_fun (*c++)) return true; return false; }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*> int count_if(tree<int> &T,tree<int>::iterator n, bool (*pred)(int)) { int count = pred(*n); tree<int>::iterator c = n.lchild(); while (c!=T.end()) count += count_if(T,c++,pred); return count; }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*> void list_if(tree<int> &T, tree<int>::iterator n, list<int> &L, bool (*pred)(int)) { if (pred(*n)) L.push_back(*n); tree<int>::iterator c = n.lchild(); while (c!=T.end()) list_if(T,c++,L,pred); }
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; }
/** 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; } } }
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 }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*> bool es_parcialmente_ordenado2(tree<int> &T, tree<int>::iterator q, bool (*comp)(int,int)) { tree<int>::iterator c; // Verifica que los valores nodales de los hijos sean mayores // o iguales que el de los padres c = q.lchild(); while (c!=T.end()) if (comp(*c++,*q)) return false; // Verifica que los subarboles de los hijos sean // (recursivamente) parc. ord. c = q.lchild(); while (c!=T.end()) if (!es_parcialmente_ordenado2(T,c++,comp)) return false; return true; }
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; } } }
int height_if (tree<T> & Q,typename tree<T>::iterator n, bool (* pred_fun) (T) ) { if ( !pred_fun (*n)) return -1; typename tree<T>::iterator c ; c = n.lchild(); int hcmax = -1; while (c != Q.end()) { int hc = height_if (Q, c++, pred_fun); if (hc > hcmax) hcmax = hc; } return hcmax + 1; }
typename tree<T>::iterator remove_if (tree <T> & Q, typename tree<T>::iterator n, bool (*pred_fun) (T) ) { typename tree<T>::iterator c ; if (pred_fun (*n) ) n = Q.erase(n); else { c = n.lchild(); while (c != Q.end()) c = remove_if (Q, c, pred_fun); n++; } // end if return n; }
U reduce (tree <T> & Q, typename tree<T>::iterator n, U (*assoc_fun) (U,U), U init_val, U (*f) (T) ) { U val = init_val; U u ; typename tree<T>::iterator c ; c = n.lchild (); while (c != Q.end()) { u = reduce (Q, c++, assoc_fun, init_val, f); val = assoc_fun (u,val); } // end while return assoc_fun ( f (*n), val ); }
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<int>::iterator graph2tree(map<int, list<int> > &G, int root, tree<int> &T, tree<int>::iterator n) { assert(n==T.end()); n = T.insert(n,root); list<int> &sons = G[root]; list<int>::iterator q = sons.begin(); tree<int>::iterator p = n.lchild(); while (q != sons.end()) { p = graph2tree(G,*q++,T,p); p++; } return n; }
typename tree<T>::iterator copy_if (tree<T> &Q,typename tree<T>::iterator nq, tree<T> &R,typename tree<T>::iterator nr, bool (*pred_fun)(T)) { if (pred_fun(*nr)) { nq = Q.insert(nq,*nr); typename tree<T>::iterator cr = nr.lchild(), cq = nq.lchild(); while (cr != R.end()) cq = copy_if(Q,cq,R,cr++,pred_fun); nq++; } return nq; }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*> bool es_parcialmente_ordenado(tree<int> &T, tree<int>::iterator q, bool (*comp)(int,int)) { tree<int>::iterator c; // Verifica que los valores nodales de los hijos sean mayores // o iguales que el de los padres y que los subarboles sean PO. c = q.lchild(); while (c!=T.end()) { if (comp(*c,*q) || !es_parcialmente_ordenado(T,c,comp)) return false; c++; } return true; }
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 }
/** 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); } } }
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; }
// ----------------------------------------------------------------- // Ordena los hijos del arbol usando la funcion de comparacion // `comp'. `comp(x,y)' debe retornar 1 si `x<y', -1 si `x>y' // y 0 si `x==y'. template<class T> void sort(tree<T> &Q,typename tree<T>::iterator n, int (*comp)(T,T)) { typedef tree<T> tree_t; typedef typename tree<T>::iterator node_t; typedef list< tree<T> > list_t; typedef typename list_t::iterator pos_t; list_t L; T tmin ; node_t cmin ; pos_t p = L.begin(); while (true) { node_t c = n.lchild(); if (c == Q.end()) break; cmin = c; tmin = *c; c++; while (c!=Q.end()) { if (comp(*c,tmin)==1) { cmin = c; tmin = *c; } c++; } p = L.insert(p,tree_t()); p->splice(p->begin(),cmin); p++; } p = L.begin(); node_t c = n.lchild(); while (p != L.end()) { c = Q.splice (c,p->begin()); sort (Q,c,comp); p = L.erase (p); c++; } }