void addNeighbor(Node* node, const Node* neighbor) { addNeighborCapacityIfFull(node); node->neighbors[node->neighborCount++] = neighbor; //adding a neighbor is a mutual operation --> add node also to neighbor if (searchNeighbor(neighbor, node->name) == NOT_FOUND) addNeighbor(neighbor, node); }
void ParticleNeighborSearcher::doNeighborSearch(NeighborTable& table) { insertParticles(); unsigned int particleNum = mParticles->size(); table.clear(); table.resize(particleNum); //#pragma omp parallel for for(unsigned int id = 0; id < particleNum; ++id)// { searchNeighbor(id, table); } }
//process the input from system, e.g. X-AB void buildGraphFromInput(char* input, Graph* graph) { char name = input[0]; if (name == '\n') return; //catch empty case //check if node already exists if (searchNode(graph, name) == NOT_FOUND) { addNode(graph, name); } int nodeIndex = searchNode(graph, name); //add neighbors after '-' if (input[1] == '\n') return; //catch one letter case name = input[2]; for (int i = 2; name != '\n'; i++) { if (searchNode(graph, name) == NOT_FOUND) { addNode(graph, name); } int neighborIndex = searchNode(graph, name); //check if neighbor already exists if (searchNeighbor(&graph->nodes[nodeIndex], name) == NOT_FOUND) addNeighbor(&graph->nodes[nodeIndex], &graph->nodes[neighborIndex]); name = input[i + 1]; } }
// It returns repeating patterns of a sequence of numbers. (x,x,x...)^r PatChain<PatternUnit> PatternUtil::findPattern( vector<off_t> deltas ) { // pointer(iterator) to the lookahead window, both should move together // mlog(IDX_WARN, "Entering %s", __FUNCTION__); // mlog(IDX_WARN, "deltas: %s", printVector(deltas).c_str()); vector<off_t>::const_iterator p_lookahead_win; PatChain<PatternUnit> pattern_stack; p_lookahead_win = deltas.begin(); pattern_stack.clear(); while ( p_lookahead_win < deltas.end() ) { //lookahead window is not empty // mlog(IDX_WARN, "window position:%d", p_lookahead_win - deltas.begin()); Tuple cur_tuple = searchNeighbor( deltas, p_lookahead_win ); if ( cur_tuple.isRepeatingNeighbor() ) { if ( pattern_stack.isPopSafe( cur_tuple.length ) ) { // mlog(IDX_WARN, "SAFE" ); //safe //pop out elements without breaking existing patterns pattern_stack.popElem( cur_tuple.length ); vector<off_t>::const_iterator first, last; first = p_lookahead_win; last = p_lookahead_win + cur_tuple.length; PatternUnit pu; pu.seq.assign(first, last); pu.cnt = 2; pattern_stack.push( pu ); p_lookahead_win += cur_tuple.length; } else { //unsafe // mlog(IDX_WARN, "UNSAFE" ); PatternUnit pu = pattern_stack.top(); if ( cur_tuple.length % pu.seq.size() == 0 && cur_tuple.length <= pu.seq.size() * pu.cnt ) { // mlog(IDX_WARN, "-REPEATING LAST Pattern"); //the subseq in lookahead window repeats //the top pattern in stack. //initial remains the same. pu.cnt += cur_tuple.length / pu.seq.size() ; pattern_stack.popPattern(); pattern_stack.push(pu); p_lookahead_win += cur_tuple.length; } else { // mlog(IDX_WARN, "-Just Pust it in"); //cannot pop out cur_tuple.length elems without //totally breaking any pattern. //So we simply add one elem to the stack PatternUnit pu; pu.seq.push_back( *p_lookahead_win ); pu.cnt = 1; pattern_stack.push(pu); p_lookahead_win++; } } } else { //(0,0,x), nothing repeats PatternUnit pu; pu.seq.push_back(cur_tuple.next_symbol); pu.cnt = 1; pattern_stack.push(pu); p_lookahead_win++; } pattern_stack.chain.back().compressRepeats(); // mlog(IDX_WARN, "LOOP: %s", pattern_stack.show().c_str()); } // mlog(IDX_WARN, "Leaving %s:%s", __FUNCTION__, pattern_stack.show().c_str()); return pattern_stack; }
//find out pattern of a number sequence(deltas) with its //original sequence //if seq and orig have the same sizes. // the function returns pattern representing all orig numbers. //else if orig has one more than seq (including seq.size()==0) // the function returns pattern representing all orig numbers, // with the last orig num with seq.size()==0 //else // error SigStack<IdxSigUnit> IdxSignature::discoverSigPattern( vector<off_t> const &seq, vector<off_t> const &orig ) { // pointer(iterator) to the lookahead window, bot should move together vector<off_t>::const_iterator p_lookahead_win, p_lookahead_win_orig; SigStack<IdxSigUnit> pattern_stack; p_lookahead_win = seq.begin(); p_lookahead_win_orig = orig.begin(); pattern_stack.clear(); if (! (seq.size() == orig.size() || seq.size() + 1 == orig.size() ) ) { ostringstream oss; oss << "seq.size():" << seq.size() << " orig.size():" << orig.size() << endl; fprintf(stderr,"discoverSigPattern() needs to be used with " "seq.size==orig.size or seq.size+1==orig.size. \n %s", oss.str().c_str() ); exit(-1); } //cout << endl << "this is discoverPattern() :)" << endl; //TODO: //There's bug in handling the case of only one entry. //And whether 1,(3,4)^2 represnets five or four numbers. //Go back to handle this when protoc buffer is integrated. /* cout << "seq.size(): " << seq.size() << "orig.size():" << orig.size() << endl; assert(seq.size() == (orig.size()-1)); //for the case there is only one entry if ( seq.size() == 0 ) { IdxSigUnit pu; pu.init = *p_lookahead_win_orig; pu.cnt = 0; } */ //TODO: WHY p_lookahead_win != seq.end() is a dead loop???? while ( p_lookahead_win < seq.end() ) { //lookahead window is not empty Tuple cur_tuple = searchNeighbor( seq, p_lookahead_win ); //cur_tuple.show(); if ( cur_tuple.isRepeatingNeighbor() ) { if ( pattern_stack.isPopSafe( cur_tuple.length ) ) { //safe pattern_stack.popElem( cur_tuple.length ); vector<off_t>::const_iterator first, last; first = p_lookahead_win; last = p_lookahead_win + cur_tuple.length; IdxSigUnit pu; pu.seq.assign(first, last); pu.cnt = 2; pu.init = *(p_lookahead_win_orig - cur_tuple.length); pattern_stack.push( pu ); p_lookahead_win += cur_tuple.length; p_lookahead_win_orig += cur_tuple.length; } else { //unsafe IdxSigUnit pu = pattern_stack.top(); if ( pu.seq.size() == cur_tuple.length ) { //the subseq in lookahead window repeats //the top pattern in stack. //initial remains the same. pu.cnt++; pattern_stack.popPattern(); pattern_stack.push(pu); pu.init = *p_lookahead_win_orig; //should delete this. keep if only for //tmp debug. p_lookahead_win += cur_tuple.length; p_lookahead_win_orig += cur_tuple.length; } else { //cannot pop out cur_tuple.length elems without //totally breaking any pattern. //So we simply add one elem to the stack IdxSigUnit pu; pu.seq.push_back( *p_lookahead_win ); pu.init = *p_lookahead_win_orig; pu.cnt = 1; pattern_stack.push(pu); p_lookahead_win++; p_lookahead_win_orig++; } } } else { //(0,0,x) IdxSigUnit pu; pu.seq.push_back(cur_tuple.next_symbol); pu.init = *p_lookahead_win_orig; pu.cnt = 1; pattern_stack.push(pu); p_lookahead_win++; p_lookahead_win_orig++; } } if ( p_lookahead_win_orig < orig.end() ) { assert(p_lookahead_win_orig + 1 == orig.end()); IdxSigUnit pu; pu.init = *p_lookahead_win_orig; pu.cnt = 0; pattern_stack.push(pu); } SigStack<IdxSigUnit> pattern_stack_compressed; vector<IdxSigUnit>::iterator it; for ( it = pattern_stack.the_stack.begin(); it != pattern_stack.the_stack.end(); it++ ) { it->compressRepeats(); if (pattern_stack_compressed.the_stack.empty()) { //mlog(IDX_WARN, "Empty"); pattern_stack_compressed.the_stack.push_back(*it); } else { bool ret; ret = pattern_stack_compressed.the_stack.back().append(*it); if (ret == false) { pattern_stack_compressed.the_stack.push_back(*it); } } //ostringstream oss; //oss << pattern_stack_compressed.show(); ////mlog(IDX_WARN, "%s", oss.str().c_str()); } if ( pattern_stack.size() != orig.size() ) { ostringstream oss; oss<< "pattern_stack.size() != orig.size() in" << __FUNCTION__ << pattern_stack.size() << "," << orig.size() << endl; oss << "seq.size():" << seq.size() << endl; oss << pattern_stack.show() << endl; vector<off_t>::const_iterator it; for ( it = orig.begin(); it != orig.end(); it++ ) { oss << *it << ","; } oss << endl; //mlog(IDX_ERR, "%s", oss.str().c_str()); exit(-1); } return pattern_stack_compressed; }