void Operations::getTree(char s[], int i) { i++; operation = s[i]; i++; Operations *tmp; if (s[i] == '(') { tmp = new Operations(); tmp->getTree(s, i); left = tmp; i++; } else { int num = s[i] - '0'; left = new Numbers(num); } i++; if (s[i] == '(') { tmp = new Operations(); tmp->getTree(s, i); right = tmp; i++; } else { int num = s[i] - '0'; right = new Numbers(num); } i++; }
void NodeOperationBuilder::sort_operations() { Operations sorted; sorted.reserve(m_operations.size()); Tags visited; for (Operations::const_iterator it = m_operations.begin(); it != m_operations.end(); ++it) sort_operations_recursive(sorted, visited, *it); m_operations = sorted; }
StatusWith<OplogApplier::Operations> OplogApplier::getNextApplierBatch( OperationContext* opCtx, const BatchLimits& batchLimits) { if (batchLimits.ops == 0) { return Status(ErrorCodes::InvalidOptions, "Batch size must be greater than 0."); } std::uint32_t totalBytes = 0; Operations ops; BSONObj op; while (_oplogBuffer->peek(opCtx, &op)) { auto entry = OplogEntry(op); // Check for oplog version change. If it is absent, its value is one. if (entry.getVersion() != OplogEntry::kOplogVersion) { std::string message = str::stream() << "expected oplog version " << OplogEntry::kOplogVersion << " but found version " << entry.getVersion() << " in oplog entry: " << redact(entry.toBSON()); severe() << message; return {ErrorCodes::BadValue, message}; } // Commands must be processed one at a time. The only exception to this is applyOps because // applyOps oplog entries are effectively containers for CRUD operations. Therefore, it is // safe to batch applyOps commands with CRUD operations when reading from the oplog buffer. if (entry.isCommand() && (entry.getCommandType() != OplogEntry::CommandType::kApplyOps || entry.shouldPrepare())) { if (ops.empty()) { // Apply commands one-at-a-time. ops.push_back(std::move(entry)); BSONObj opToPopAndDiscard; invariant(_oplogBuffer->tryPop(opCtx, &opToPopAndDiscard)); dassert(ops.back() == OplogEntry(opToPopAndDiscard)); } // Otherwise, apply what we have so far and come back for the command. return std::move(ops); } // Apply replication batch limits. if (ops.size() >= batchLimits.ops) { return std::move(ops); } // Never return an empty batch if there are operations left. if ((totalBytes + entry.getRawObjSizeBytes() >= batchLimits.bytes) && (ops.size() > 0)) { return std::move(ops); } // Add op to buffer. totalBytes += entry.getRawObjSizeBytes(); ops.push_back(std::move(entry)); BSONObj opToPopAndDiscard; invariant(_oplogBuffer->tryPop(opCtx, &opToPopAndDiscard)); dassert(ops.back() == OplogEntry(opToPopAndDiscard)); } return std::move(ops); }
int main() { Operations op; cout << "-0 = " << op.negate(0) << endl; cout << "-1 = " << op.negate(1) << endl; cout << "-(-1) = " << op.negate(-1) << endl; int a = 0; int b = 0; cout << a << " - " << b << " = " << op.subtract(a,b) << endl; a = 0; b = -3; cout << a << " - " << b << " = " << op.subtract(a,b) << endl; a = 0; b = 3; cout << a << " - " << b << " = " << op.subtract(a,b) << endl; a = 4; b = 4; cout << a << " - " << b << " = " << op.subtract(a,b) << endl; a = 4; b = 2; cout << a << " - " << b << " = " << op.subtract(a,b) << endl; a = 4; b = 6; cout << a << " - " << b << " = " << op.subtract(a,b) << endl; a = 0; b = 0; cout << a << " * " << b << " = " << op.multiply(a,b) << endl; a = -2; b = 0; cout << a << " * " << b << " = " << op.multiply(a,b) << endl; a = -2; b = 4; cout << a << " * " << b << " = " << op.multiply(a,b) << endl; a = -2; b = -4; cout << a << " * " << b << " = " << op.multiply(a,b) << endl; a = 2; b = -3; cout << a << " * " << b << " = " << op.multiply(a,b) << endl; a = 2; b = 3; cout << a << " * " << b << " = " << op.multiply(a,b) << endl; a = 0; b = 0; cout << a << " / " << b << " = " << op.divide(a,b) << endl; a = -2; b = 0; cout << a << " / " << b << " = " << op.divide(a,b) << endl; a = -19; b = 4; cout << a << " / " << b << " = " << op.divide(a,b) << endl; a = -19; b = -4; cout << a << " / " << b << " = " << op.divide(a,b) << endl; a = 19; b = -5; cout << a << " / " << b << " = " << op.divide(a,b) << endl; a = 3; b = 5; cout << a << " / " << b << " = " << op.divide(a,b) << endl; a = 13; b = 5; cout << a << " / " << b << " = " << op.divide(a,b) << endl; return 0; }
Number* Shunting:: evaluate(string input) { Operations* o = new Operations(); vector<string> tokens = parseTokens(input); vector<string> converted; stack<Number*> nums; for(int i = 1; i < (int)tokens.size(); i++) { if(tokens[i]== "-" && (isOperator(tokens[i-1]) || isParenthesis(tokens[i-1]))) { tokens[i + 1].insert(0, "-"); tokens.erase(tokens.begin() + i); } } if(convertInput(tokens, tokens.size(), converted)) { for ( int i = 0; i < (int) converted.size(); i++ ) { if ( !isOperator(converted[i]) ) { try { nums.push(toNumber(converted[i], new Rational(1))); } catch(int e) { throw invalid_argument("Invalid expression input, please adhere to input standards."); } } else { Number* result; Number* n2 = nums.top(); nums.pop(); if (!nums.empty() ) { Number* n1 = nums.top(); nums.pop(); if(converted[i] == "+") { result = o->add(n1, n2); } else if(converted[i] == "-") result = o->subtract(n1, n2); else if(converted[i] == "*") result = o->multiply(n1, n2); else if(converted[i] == "/") result = o->divide(n1, n2); else if(converted[i] == "^") result = o->exponentiate(n1, n2); else throw invalid_argument("Invalid expression input, please adhere to input standards."); } else { if ( converted[i] == "-" ) result = o->multiply(n2, new Rational(-1)); else result = n2; } nums.push(result); } } } else { cout << "Mismatched parenthesis. Assuming answer = 0" << endl; Number* result = new Rational(0); nums.push(result); } return nums.top(); }
// Converts the input string to a number so we can check if it's rational or irrational. Number* Shunting:: toNumber(string str, Number* ansOld){ Number* ans; Operations * o = new Operations(); bool noAns = true; if(str.at(0) == 'a') { ans = ansOld; noAns = false; } if((str.at(0) == 'l') || (str.at(0) == 'e') || (str.at(0) == 'p') || (str.at(0) == 's')) { ans = new Irrational(str); noAns = false; } else { for(int i = 0; i < (int)str.size() && noAns; i++) { if(str.at(i) == '.') { ans = o->toRational(str); noAns = false; break; } else if(str.at(i) == 'r') { ans = new Irrational(str); noAns = false; break; } else if(str.at(i) == '/') { char *a=new char[str.size()+1]; a[str.substr(i-1).size()]=0; memcpy(a,str.c_str(),str.substr(i-1).size()); char *b=new char[str.size()+1]; b[str.substr(i+1).size()]=0; memcpy(b,str.c_str(),str.substr(i+1).size()); ans = new Rational(atoi(a), atoi(b)); noAns = false; delete[] a; delete[] b; break; } } } if(noAns) { char *c=new char[str.size()+1]; c[str.size()]=0; memcpy(c,str.c_str(),str.size()); ans = new Rational(atoi(c)); delete[] c; } return ans; }
int _tmain(int argc, _TCHAR* argv[]) { //Создание масивов int n = 21, m = 4; double ys, x1s, x2s, yd, x1d, x2d, ryx1, ryx2, R2, J; double **X = new double *[n]; for (int i = 0; i < n; i++){ X[i] = new double[m]; } double **Xt = new double *[m]; for (int i = 0; i < m; i++){ Xt[i] = new double[n]; } double *a = new double[m - 1]; double *SE = new double[m - 1]; ///////////////// cout <<endl<< "Исходные данные" << endl; //Открытие файла ifstream file("C:\\Users\\Nikita_Desktop\\Documents\\data.txt", ios_base::in); for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ file >> X[i][j]; cout << X[i][j] << " "; } cout << endl; } file.close(); //////////////// //Выполнение операций для исходных данных Operations action; action.tmatrix(X, Xt, n, m); ys = action.matozh(Xt[1], n); x1s = action.matozh(Xt[2], n); x2s = action.matozh(Xt[3], n); cout << endl << "Матожидание у=" << ys << endl << "Матожидание x1=" << x1s << endl << "Матожидание x2=" << x2s << endl; yd = action.disper(Xt[1], ys, n); x1d = action.disper(Xt[2], x1s, n); x2d = action.disper(Xt[3], x2s, n); cout << endl << "Дисперсия у=" << yd << endl << "Дисперсия x1=" << x1d << endl << "Дисперсия x2=" << x2d << endl; ryx1 = action.corel(Xt[1], Xt[2], ys, x1s, n, yd, x1d); ryx2 = action.corel(Xt[1], Xt[3], ys, x2s, n, yd, x2d); cout << endl << "Кореляция у и x1=" << ryx1 << endl << "Кореляция y и x2=" << ryx2 << endl << endl; action.LS(X, Xt, n, m, a); for (int i = 0; i < m - 1; i++) cout << "a" << i << "=" << a[i] << endl; R2 = action.kDeterm(yd, a, Xt, n); cout << endl << "R2=" << R2 << endl; J = action.sumSqrErr(a, Xt, n); cout << "J=" << J << endl; action.tStat(a, X, Xt, n, m, J, SE); for (int i = 0; i < m - 1; i++) cout << "SE" << i << "=" << SE[i] << endl; ////////////////////// //Выполнение операций для логарифмированых данных cout <<endl<< "Логарифмированые данные" << endl; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ if (j!=0) X[i][j] = log(X[i][j]); cout << X[i][j] << " "; }cout << endl; } Operations actionLog; actionLog.tmatrix(X, Xt, n, m); ys = actionLog.matozh(Xt[1], n); x1s = actionLog.matozh(Xt[2], n); x2s = actionLog.matozh(Xt[3], n); cout << endl << "Матожидание у=" << ys << endl << "Матожидание x1=" << x1s << endl << "Матожидание x2=" << x2s << endl; yd = actionLog.disper(Xt[1], ys, n); x1d = actionLog.disper(Xt[2], x1s, n); x2d = actionLog.disper(Xt[3], x2s, n); cout << endl << "Дисперсия у=" << yd << endl << "Дисперсия x1=" << x1d << endl << "Дисперсия x2=" << x2d << endl; ryx1 = actionLog.corel(Xt[1], Xt[2], ys, x1s, n, yd, x1d); ryx2 = actionLog.corel(Xt[1], Xt[3], ys, x2s, n, yd, x2d); cout << endl << "Кореляция у и x1=" << ryx1 << endl << "Кореляция y и x2=" << ryx2 << endl << endl; actionLog.LS(X, Xt, n, m, a); for (int i = 0; i < m - 1; i++) cout << "a" << i << "=" << a[i] << endl; R2 = actionLog.kDeterm(yd, a, Xt, n); cout << endl << "R2=" << R2 << endl; J = actionLog.sumSqrErr(a, Xt, n); cout << "J=" << J << endl; actionLog.tStat(a, X, Xt, n, m, J, SE); for (int i = 0; i < m - 1; i++) cout << "SE" << i << "=" << SE[i] << endl; ///////////////////////////////////////////////// //Удаление массивов for (int i = 0; i <n; i++) delete[]X[i]; delete[]X; for (int i = 0; i <m; i++) delete[]Xt[i]; delete[]Xt; delete[]a; delete[]SE; /////////////////// getchar(); return 0; }
inline void enrich_assign(Operations& operations, Turns& turns) { typedef typename boost::range_value<Turns>::type turn_type; typedef typename turn_type::turn_operation_type op_type; typedef typename boost::range_iterator<Operations>::type iterator_type; if (operations.size() > 0) { // Assign travel-to-vertex/ip index for each turning point. // Iterator "next" is circular geometry::ever_circling_range_iterator<Operations const> next(operations); ++next; for (iterator_type it = boost::begin(operations); it != boost::end(operations); ++it) { turn_type& turn = turns[it->turn_index]; op_type& op = turn.operations[it->operation_index]; // Normal behaviour: next should point at next turn: if (it->turn_index == next->turn_index) { ++next; } // Cluster behaviour: next should point after cluster, unless // their seg_ids are not the same while (turn.cluster_id != -1 && it->turn_index != next->turn_index && turn.cluster_id == turns[next->turn_index].cluster_id && op.seg_id == turns[next->turn_index].operations[next->operation_index].seg_id) { ++next; } turn_type const& next_turn = turns[next->turn_index]; op_type const& next_op = next_turn.operations[next->operation_index]; op.enriched.travels_to_ip_index = static_cast<signed_size_type>(next->turn_index); op.enriched.travels_to_vertex_index = next->subject->seg_id.segment_index; if (op.seg_id.segment_index == next_op.seg_id.segment_index && op.fraction < next_op.fraction) { // Next turn is located further on same segment // assign next_ip_index // (this is one not circular therefore fraction is considered) op.enriched.next_ip_index = static_cast<signed_size_type>(next->turn_index); } } } // DEBUG #ifdef BOOST_GEOMETRY_DEBUG_ENRICH { for (iterator_type it = boost::begin(operations); it != boost::end(operations); ++it) { op_type& op = turns[it->turn_index] .operations[it->operation_index]; std::cout << it->turn_index << " cl=" << turns[it->turn_index].cluster_id << " meth=" << method_char(turns[it->turn_index].method) << " seg=" << op.seg_id << " dst=" << op.fraction // needs define << " op=" << operation_char(turns[it->turn_index].operations[0].operation) << operation_char(turns[it->turn_index].operations[1].operation) << " (" << operation_char(op.operation) << ")" << " nxt=" << op.enriched.next_ip_index << " / " << op.enriched.travels_to_ip_index << " [vx " << op.enriched.travels_to_vertex_index << "]" << std::endl; ; } } #endif // END DEBUG }