void cGraphMaster::internal_sort(NodeVec& tree) { sort(tree.begin(), tree.end()); for (NodeVec::iterator it = tree.begin(); it != tree.end(); ++it) { internal_sort(it->same_childs); internal_sort(it->diff_childs); } }
void cGraphMaster::addNode(AIMLentry& entry, NodeType curr_type, NodeVec& tree, unsigned long rec, bool insert_ordered) { list<string>& curr_list = entry.getList(curr_type); string head = curr_list.front(); curr_list.pop_front(); bool last_tok = curr_list.empty(); Node node; node.key = head; node.type = curr_type; for (NodeVec::iterator it = tree.begin(); it != tree.end(); ++it) { if (it->key == head) { if (last_tok) { if (curr_type == NODE_TOPIC) it->templ = entry.templ; else addNode(entry, nextNodeType(curr_type), it->diff_childs, rec+1); } else addNode(entry, curr_type, it->same_childs, rec+1); return; } } if (!last_tok) addNode(entry, curr_type, node.same_childs, rec+1); else { if (curr_type == NODE_TOPIC) { node.templ = entry.templ; gm_size++; } else addNode(entry, nextNodeType(curr_type), node.diff_childs, rec+1); } if (insert_ordered) tree.insert(lower_bound(tree.begin(), tree.end(), node), node); else tree.insert(tree.end(), node); }
void PseudoBooleanProcessor::learn(const NodeVec& assertions){ NodeVec::const_iterator ci, cend; ci = assertions.begin(); cend=assertions.end(); for(; ci != cend; ++ci ){ learn(*ci); } }
void GetkMax(NodeVec& rtv, int k) { int i ; for( i = 0 ; i < k ; ++i) rtv.push_back(i); make_heap(rtv.begin(), rtv.end(), compare()); while(i < MaxN) { if(compare()(i, rtv[0])) { pop_heap(rtv.begin(), rtv.end(), compare()); rtv.pop_back(); rtv.push_back(i); push_heap(rtv.begin(), rtv.end(), compare()); } ++i; } }
bool cGraphMaster::getMatch(InputIterator input, NodeType curr_type, const NodeVec& tree, MatcherStruct& ms, unsigned long rec) { // save head of input and advance iterator string input_front = *input; input++; //cout<<"tree.front().key="<<tree.front().key<<endl; _DBG_CODE(msg_dbg() << "[" << rec << "]getMatch(" << curr_type << ") HEAD: [" << input_front << "] last input?:" << boolalpha << input.isDone() << endl); /** FIRST WILDCARD: try to match the '_' wildcard **/ if (tree.front().key == "_") { _DBG_CODE(msg_dbg() << "[" << rec << "]" << "Looking in '_'" << endl); if (getMatchWildcard(input, curr_type, tree.front(), ms, rec, input_front)) { if (ms.log) ms.logMatch("_", curr_type); return true; } } /** KEYS: if wildcard didn't matched, look for specific matching keys against current input_front **/ _DBG_CODE(msg_dbg() << "[" << rec << "]" << "KEYS" << endl); Node tmp_node; tmp_node.key = input_front; NodeVec::const_iterator it = lower_bound(tree.begin(), tree.end(), tmp_node); // if there was a match if (it != tree.end() && it->key == input_front) { // if there are more tokens on input if (!input.isDone()) { // if I have something to match with, then look for a match if (!it->same_childs.empty()) { if (getMatch(input, curr_type, it->same_childs, ms, rec+1)) { if (ms.log) ms.logMatch(it->key, curr_type); return true; } } } // else, this is the last token else { // get the the next type of nodes to match if necessary list<string> match_list; if (curr_type != NODE_TOPIC && !it->diff_childs.empty()) ms.user.getMatchList(NodeType(curr_type+1), match_list); // if in previous situation check if there's a match deeper if (curr_type != NODE_TOPIC && !it->diff_childs.empty() && getMatch(match_list, nextNodeType(curr_type), it->diff_childs, ms, rec+1)) { if (ms.log) ms.logMatch(it->key, curr_type); return true; } // if I'm in the final type of nodes, it needs to be a leaf to have a match else if (curr_type == NODE_TOPIC && !it->templ.empty()) { ms.templ = it->templ; if (ms.log) ms.logMatch(it->key, curr_type); return true; } } } /** LAST WILDCARD: if above didn't matched repeat procedure for '_' **/ if (tree.back().key == "*") { _DBG_CODE(msg_dbg() << "[" << rec << "]" << "Looking in '*'" << endl); if (getMatchWildcard(input, curr_type, tree.back(), ms, rec, input_front)) { if (ms.log) ms.logMatch("*", curr_type); return true; } } return false; }
int main() { NodeVec kmax; kmax.reserve(600); int k = 500; GenData(); GetkMax(kmax, k); for_each(kmax.begin(), kmax.end(), print); //输出是按照堆的顺序输出,如果要有序的话,可以先sort_heap一下 cout<<endl; return 0; }
static void NoStateSort(NodeVecVec& nodeVecVec, std::vector<const Sprite*>& spriteVec) { // no sorting, just copy sprites from nodeVecVec to spriteVec NodeVecVec::iterator vecVecIter = nodeVecVec.begin(); NodeVecVec::iterator vecVecEnd = nodeVecVec.end(); for(; vecVecIter != vecVecEnd; ++vecVecIter) { NodeVec* v = (*vecVecIter); NodeVec::iterator vecIter = v->begin(); NodeVec::iterator vecEnd = v->end(); for(; vecIter != vecEnd; ++vecIter) { const Sprite* sprite = (*vecIter)->sprite; if (sprite != s_screenSprite) spriteVec.push_back(sprite); } } }
/* * Writes the network and random walk information to the given file. */ void write_output ( std::string filename, std::vector<CreditVec> updates ) { FILE * pfile = fopen ( filename.c_str(), "w" ); GraphSize id; // sort nodevec by id std::sort( nodevec.begin(), nodevec.end(), nodecomp() ); for ( auto& node : nodevec ) { id = node->id(); if ( id != -1 ) { fprintf( pfile, "%lu\t%lu", id, node->edgeCount() ); // output update values for node n for ( int i = 0; i < updates.size(); ++i ) { fprintf( pfile, "\t%.6lf", updates[i][id] ); } fprintf( pfile, "\n" ); } } fclose( pfile ); }
static void DumpNodeVecVec() { // nodeVecVec is topologically sorted. printf("nodeVecVec = [\n"); NodeVecVec::iterator vecVecIter = s_nodeVecVec.begin(); NodeVecVec::iterator vecVecEnd = s_nodeVecVec.end(); for(; vecVecIter != vecVecEnd; ++vecVecIter) { NodeVec* v = (*vecVecIter); assert(v); printf(" [ "); NodeVec::iterator vecIter = v->begin(); NodeVec::iterator vecEnd = v->end(); for(; vecIter != vecEnd; ++vecIter) { printf("%s ", (*vecIter)->sprite->GetName().c_str()); } printf("]\n"); } printf("]\n"); }
static void TextureStateSort(NodeVecVec& nodeVecVec, std::vector<const Sprite*>& spriteVec) { // use the nodeVecVec and state sort each sub vec by texture. NodeVecVec::iterator vecVecIter = nodeVecVec.begin(); NodeVecVec::iterator vecVecEnd = nodeVecVec.end(); for(; vecVecIter != vecVecEnd; ++vecVecIter) { // Each NodeVec can be sorted by texture. // iterate over each sprite and insert into s_texMap. NodeVec* v = (*vecVecIter); NodeVec::iterator vecIter = v->begin(); NodeVec::iterator vecEnd = v->end(); for(; vecIter != vecEnd; ++vecIter) { const Sprite* sprite = (*vecIter)->sprite; if (sprite != s_screenSprite) { TexMap::iterator mapIter = s_texMap.find(sprite->GetTexture()); assert(mapIter != s_texMap.end()); mapIter->second.push_back(sprite); } } // Now iterate over s_texMap and insert into spriteVec TexMap::iterator mapIter = s_texMap.begin(); TexMap::iterator mapEnd = s_texMap.end(); for (; mapIter != mapEnd; ++mapIter) { SpriteVec::iterator sVecIter = mapIter->second.begin(); SpriteVec::iterator sVecEnd = mapIter->second.end(); for (; sVecIter != sVecEnd; ++sVecIter) spriteVec.push_back(*sVecIter); } // clean s_texMap mapIter = s_texMap.begin(); for (; mapIter != mapEnd; ++mapIter) mapIter->second.clear(); } }