bool Matcher::add_occurrence(off_t pos, off_t tpos, size_t len) { QueryTerm* mexp = _match_iter.current(); LOG(spam, "Match: %s(%ld)", mexp->term(), tpos); // Add new occurrence to sequence of all occurrences key_occ_ptr k = new key_occ(mexp->term(), pos, tpos, len); if (!k) return false; _occ.push_back(k); if (!(_need_complete_cnt > 0)) { size_t nodeno; // From the head of the sequences, remove any candidates that are // "too old", eg. that is not complete within the winsize window // and also trig further processing of complete matches: for (nodeno = 0; nodeno < _nontermcnt; nodeno++) { match_sequence& ws = _wrk_set[nodeno]; for (match_sequence::iterator it = ws.begin(); it != ws.end();) { MatchCandidate* m = (*it); if ((k->startpos() - m->startpos()) < static_cast<int>(_winsize)) break; it = ws.erase(it); // This moves the iterator forward if (m->partial_ok()) update_match(m); else DerefCandidate(m); } } } // Then add a new candidate starting at the currently found keyword // for each subexpression that matches this keyword for (; mexp != NULL; mexp = _match_iter.next()) { QueryNode* pexp = mexp->_parent; assert(pexp); MatchCandidate* nm = NewCandidate(pexp); if (!nm || nm->elems() < 0) { LOG(error, "Matcher could not allocate memory for candidate - bailing out"); if (nm) DerefCandidate(nm); return false; } match_sequence& cs = _wrk_set[pexp->_node_idx]; if (cs.size() >= _max_match_candidates) { DerefCandidate(nm); LOG(debug, "The max number of match candidates (%zu) in the work set for query node idx '%u' has been reached. " "No more candidates are added", _max_match_candidates, pexp->_node_idx); } else { cs.push_back(nm); } update_wrk_set(cs, k, mexp); } return true; }
void Matcher::update_match(MatchCandidate* m) { QueryNode* nexp = m->match()->_parent; if (!nexp) { // root node of query _matches.insert(m); // Tag all terms m->set_valid(); } else { // Add the parent candidate MatchCandidate* nm = NewCandidate(nexp); match_sequence& cs = _wrk_set[nexp->_node_idx]; cs.push_back(nm); // Update the parent candidate work set update_wrk_set(_wrk_set[nexp->_node_idx], m, m->match()); // This candidate was removed from it's wrk set but // the ref is not forwarded to the matches list since it is an // intermediate node.. DerefCandidate(m); } }
void main() { printf("e or Enter- end of work\nEnter E and press Enter. Then enter an expression using '(',')','+','-','*','/','cos','sin','tg','ctg','sqrt','!','^','%'\n" ); printf("Please enter a Fractional point of Number\n"); char c = 0; int Eps = 0; c= get; while (c != '\n' && c != '\r' && c>='0' && c<='9') {//читаем выражение Eps *= 10; Eps += c -'0'; c = get; } Eps = !Eps? 1:Eps; while (printf("\nFractional point of number is %i\nPress Enter to exit!\nYour expression is:\n",Eps),c = get,c!='e' && c!='\n') try { NQList<char> InputLine; while (c != '\n' && c != '\r') {//читаем выражение InputLine.addLast(c); c = get; } bool GoodExpression = true; NQList<char>& t = InputLine;//копия для удобства NQList<FSM_Calc*> Tokens;//лексемы int i = 0;//начальная позиция while (i < t.Count()) { if (t[i] == '(' && i+1<t.Count() && t[i+1] == '-') {//выражение с минусом вначале char *temp =new char[2]; temp[1] = 0; temp[0] = '('; Tokens.addLast(new FSM_Calc(temp,Eps)); delete[] temp; //накапливается скобка NQList<char> NewCandidate(t[++i]); i++;//а затем и число while (i < t.Count() && ((t[i] <='9' && t[i] >= '0')||(t[i] == '.'))) { NewCandidate.addLast(t[i]); i++; } char *Temp = new char[NewCandidate.Count()+1]; for (int j = 0; j<NewCandidate.Count(); j++) Temp[j] = NewCandidate[j]; Temp[NewCandidate.Count()] = 0; Tokens.addLast(new FSM_Calc(Temp,Eps)); Tokens[Tokens.Count()-1]->AddErrSize(Eps); delete[] Temp; }else if(t[i] == 'c' || t[i] == 't' || t[i] == 's') {//максимальная длина этого выражения 4 символа NQList<char> NewCandidate(t[i]); i++; while (i<t.Count() && (t[i]>='a'&&t[i]<='z')) { NewCandidate.addLast(t[i]); i++; } char *Temp = new char[NewCandidate.Count() + 1]; for (int j=0; j< NewCandidate.Count(); j++) { Temp[j] = NewCandidate[j]; } Temp[NewCandidate.Count()] = 0; Tokens.addLast(new FSM_Calc(Temp,Eps)); delete[] Temp; /*char *NewCandidate = new char[5]; int j; for ( j = 0; j<4 && i+j < t.Count() &&((i+j == 0)|| (t[i+j -1] != '(' || j == 0)); j++) NewCandidate[j] = t[i+j]; NewCandidate[j] = 0; Tokens.addLast(new FSM_Calc(NewCandidate)); delete[] NewCandidate; i += j;*/ } else if(t[i] == '(' || t[i] == '!' || t[i] == ')' || t[i] == '+' || t[i] == '-' || t[i] == '*' || t[i] == '/' || t[i] == '^' || t[i] == '%') {//закрытие вынесем в отдельный блок char *temp = new char[2]; temp[0] = t[i]; temp[1] = 0; Tokens.addLast(new FSM_Calc(temp,Eps)); delete[] temp; i++; } else if (t[i] <= '9' && t[i] >= '0' || t[i] =='.') {//просто число. NQList<char> NewCandidate(t[i]); i++; while (i < t.Count() && ((t[i] <='9' && t[i] >= '0')||(t[i] == '.'))) { NewCandidate.addLast(t[i]); i++; } char *Temp = new char[NewCandidate.Count()+1]; for (int j = 0; j<NewCandidate.Count(); j++) Temp[j] = NewCandidate[j]; Temp[NewCandidate.Count()] = 0; Tokens.addLast(new FSM_Calc(Temp,Eps)); Tokens[Tokens.Count()-1]->AddErrSize(Eps); delete[] Temp; } else {//если не попали, известим об ошибке и закончим работу printf("Error in expression %c",t[i]); i = t.Count(); GoodExpression = false; // return; } }//накопление прошло. if (GoodExpression && isExpressionCurrent(Tokens,Eps)) {//невообразимый поток сознания printf("\nAll is ok\n"); printf("Input string is\n"); for(int i = 0; i<Tokens.Count(); i++) { printf("%s ",Tokens[i]->getData(Eps)); } printf("\n POLIZE \n"); PSum POL(Eps); for(int i = 0; i<Tokens.Count();i++) POL.NextToken(*Tokens[i]); POL.EndTokens(); POL.print(); printf("\n End POLIZE \n Summary \n"); POL.Calculate(); printf("Result is: "); POL.print(); }else printf("\nThere are some bugs in your expression\n"); //Tokens[Tokens.Count()]; printf("\n"); } catch (NQLException ex) { printf("%s",ex.Message()); printf("\n"); } }