Ptr getMaxTree(const Arr<T>& data) { if (data.empty()) return nullptr; Arr<Ptr> node(data.size(), nullptr); for(int i = 0; i < data.size(); ++i) node[i] = std::make_shared<TreeNode>(data[i]); HashMap MaxMap; std::stack<Ptr> s; for (int i = 0; i < data.size(); ++i) { auto cur = node[i]; while (!s.empty() && s.top()->value < data[i]) { popStackSetMap(s, MaxMap, cur); } s.push(node[i]); } while (!s.empty()) { popStackSetMap(s, MaxMap, nullptr); } Ptr head = nullptr; for (int i = 0; i < node.size(); ++i) { auto cur = node[i]; auto res = MaxMap.find(cur); //结构如下 node left right //node是当前访问节点 left是左边离他最近比node大的点,right是右边。。 //nullptr代表没有或者到达边界 if (!res->second.first && !res->second.second) { head = res->first; } else if (!res->second.first) { if (!res->second.second->left) { res->second.second->left = res->first; } else { res->second.second->right = res->first; } } else if (!res->second.second) { if (!res->second.first->left) { res->second.first->left = res->first; } else { res->second.first->right = res->first; } } else { auto tmp = res->second.first->value < res->second.second->value ? res->second.first : res->second.second; if (!tmp->left) { tmp->left = res->first; } else { tmp->right = res->first; } } } return head; }
template<typename T> inline static void toVal(Val& mV, T&& mX) { Arr result; result.reserve(sizeof...(TArgs)); // TODO: fwd macro? tplFor([&result](auto&& mI){ result.emplace_back(fwd<decltype(mI)>(mI)); }, fwd<T>(mX)); mV.setArr(std::move(result)); }
inline Val parseArr() { Arr arr; // Skip '[' ++idx; // Empty array if(isC(']')) goto end; // Reserve some memory arr.reserve(10); while(true) { // Get value arr.emplace_back(parseVal()); // Check for another value if(isC(',')) { ++idx; continue; } // Check for end of the array if(isC(']')) break; throwError("Invalid array", std::string{"Expected either `,` or `]`, got `"} + getC() + "`"); } end: // Skip ']' ++idx; return Val{arr}; }
void append_transport_source(int c, double p, double v, const Arr& s, TransportSource& src) { src.cell .push_back(c); src.pressure .push_back(p); src.flux .push_back(v); src.saturation.insert(src.saturation.end(), s.begin(), s.end()); ++src.nsrc; }
static Arr<Variable*> resolveBindings(const vector<Variable*>& targets) { if (targets.empty()) return {}; if (targets[0]->kind != Variable::KindFunction) return { targets[0] }; Arr<Variable*> result; for (Variable* var: targets) if (var->kind == Variable::KindFunction) result.push(var); return result; }
static void typeCall(Output& output, Ast::Call* n, TypeConstraints* constraints) { type(output, n->expr, constraints); for (auto& a: n->args) type(output, a, constraints); if (UNION_CASE(Ident, ne, n->expr)) { if (constraints && ne->targets.size > 1) { vector<Ty*> args; for (auto& a: n->args) args.push_back(astType(a)); constraints->rewrites += reduceCandidates(ne->targets, args); } } // This is important for vararg functions and generates nicer errors for argument count/type mismatch if (UNION_CASE(Function, fnty, astType(n->expr))) { if (isArgumentCountValid(fnty, n->args.size)) { for (size_t i = 0; i < fnty->args.size; ++i) typeMustEqual(n->args[i], fnty->args[i], constraints, output); } else if (!constraints) output.error(n->location, "Expected %d arguments but given %d", int(fnty->args.size), int(n->args.size)); n->type = fnty->ret; } else { Arr<Ty*> args; for (auto& a: n->args) args.push(astType(a)); Ty* ret = UNION_NEW(Ty, Unknown, {}); typeMustEqual(n->expr, UNION_NEW(Ty, Function, { args, ret }), constraints, output); n->type = ret; } }
std::pair<int, int> getMinMax(const Arr& data) { int maxNum = INT_MIN; int minNum = INT_MAX; if (data.size() & 0x1) maxNum = minNum = data.back(); for (int i = 0; i + 1< data.size(); i+=2) { if (data[i] < data[i + 1]) { minNum = data[i] > minNum ? minNum : data[i]; maxNum = data[i+1] < maxNum ? maxNum : data[i+1]; } else { minNum = data[i + 1] > minNum ? minNum : data[i + 1]; maxNum = data[i] < maxNum ? maxNum : data[i]; } } return { minNum, maxNum }; }
//************************************************ Arr Arr::transpose(){ //---------------------------------------------// // Transpose a row ordered array, arr, // Inputs: // arr: 1d array in column ordered format // nc: number of columns // nr: number of rows // Returns: // transpose of arr //---------------------------------------------// Arr RtnArray; RtnArray.Init(N,M); for (int ii = 0; ii < M; ii++){ for (int jj = 0; jj <N; jj++){ RtnArray.push(element(jj,ii),ii,jj); } } return RtnArray; }
static void typeLiteralTuple(Output& output, Ast::LiteralTuple* n, TypeConstraints* constraints) { if (!n->type) { Arr<Ty*> fields; for (size_t i = 0; i < n->fields.size; ++i) fields.push(UNION_NEW(Ty, Unknown, {})); n->type = UNION_NEW(Ty, Tuple, { fields }); } UNION_CASE(Tuple, tt, n->type); assert(tt); for (size_t i = 0; i < n->fields.size; ++i) { type(output, n->fields[i], constraints); typeMustEqual(n->fields[i], tt->fields[i], constraints, output); } }
void RobotTimer::saveReport(const std::string &filename,const BadRobot &badRobot) const { cout << "**** SAVING PERFORMANCES ****" << endl; cout << "status = " << badRobot.getType() << " (" << badRobot.what() << ")" << endl; cout << "filename = " << filename << endl; Tab dict; dict["definition"] = badRobot.getRobotDef().getDict(); dict["status"] = badRobot.getType(); dict["status_string"] = badRobot.what(); Arr posx,posy; Arr spex,spey; Arr bodyangle; Arr engineangle; Arr time; for (Records::const_iterator irecord=records.begin(); irecord!=records.end(); irecord++) { posx.append(irecord->position.x); posy.append(irecord->position.y); spex.append(irecord->speed.x); spey.append(irecord->speed.y); bodyangle.append(irecord->bodyangle); engineangle.append(irecord->engineangle); time.append(irecord->time); } dict["posx"] = posx; dict["posy"] = posy; dict["spex"] = spex; dict["spey"] = spey; dict["bodyangle"] = bodyangle; dict["engineangle"] = engineangle; dict["time"] = time; DumpValToFile(dict,filename); }
//************************************************ Arr Arr::cholesky(){ //---------------------------------------------// // Solve cholesky of a square array // Inputs: // S: input array // D: output array. Cholesky of input array. //---------------------------------------------// Arr D; D.Init(M,N); int d = M; D.M = M;D.N=N; for(int k=0;k<d;++k){ double sum=0.; for(int p=0;p<k;++p)sum+=D.val[k*d+p]*D.val[k*d+p]; D.val[k*d+k]=sqrt(val[k*d+k]-sum); for(int i=k+1;i<d;++i){ double sum=0.; for(int p=0;p<k;++p)sum+=D.val[i*d+p]*D.val[k*d+p]; D.val[i*d+k]=(val[i*d+k]-sum)/D.val[k*d+k]; } } return D; }
Arr* Parser::arr(){ if (checkToken(Token::SQUAREBRACKETOPEN)) { nextToken(); Arr* myArr = new Arr(); if (checkToken(Token::INTEGER)) { if (error) { return myArr; } myArr->addNode(new IntegerN(token)); nextToken(); } else { syntaxError(); } if (!checkToken(Token::SQUAREBRACKETCLOSE)) { syntaxError(); } nextToken(); return myArr; } else { // Epsilon return new ArrEpsilon(); } }
void SpParHelper::FetchMatrix(SpMat<IT,NT,DER> & MRecv, const vector<IT> & essentials, vector<MPI_Win> & arrwin, int ownind) { MRecv.Create(essentials); // allocate memory for arrays Arr<IT,NT> arrinfo = MRecv.GetArrays(); assert( (arrwin.size() == arrinfo.totalsize())); // C-binding for MPI::Get // int MPI_Get(void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank, MPI_Aint target_disp, // int target_count, MPI_Datatype target_datatype, MPI_Win win) IT essk = 0; for(int i=0; i< arrinfo.indarrs.size(); ++i) // get index arrays { //arrwin[essk].Lock(MPI::LOCK_SHARED, ownind, 0); MPI_Get( arrinfo.indarrs[i].addr, arrinfo.indarrs[i].count, MPIType<IT>(), ownind, 0, arrinfo.indarrs[i].count, MPIType<IT>(), arrwin[essk++]); } for(int i=0; i< arrinfo.numarrs.size(); ++i) // get numerical arrays { //arrwin[essk].Lock(MPI::LOCK_SHARED, ownind, 0); MPI_Get(arrinfo.numarrs[i].addr, arrinfo.numarrs[i].count, MPIType<NT>(), ownind, 0, arrinfo.numarrs[i].count, MPIType<NT>(), arrwin[essk++]); } }
Arr concatinate(Arr& obj1, Arr& obj2,int dim){ Arr ArrRtn; int M,N; //If the concatinate rows if (dim == 1){ M = obj1.M; N = obj1.N+obj2.N; }else{//concatinate columns M = obj1.M+obj2.M; N = obj1.N; } ArrRtn.Init(0.0,M,N); //fill using first array for (int ii = 0; ii < obj1.N; ii++){ for (int jj = 0; jj < obj1.M; jj++){ ArrRtn.push(obj1.element(ii,jj),ii,jj); } } //---------------------------------------------// // fill using second array //---------------------------------------------// //concatinate rows if (dim == 1){ for (int ii = obj1.N; ii < N; ii++){ for (int jj = 0; jj < M; jj++){ ArrRtn.push(obj2.element(ii-obj1.N,jj),ii,jj); } } }else{//concatinate columns for (int ii = 0; ii < N; ii++){ for (int jj = obj1.M; jj < M; jj++){ ArrRtn.push(obj2.element(ii,jj-obj1.M),ii,jj); } } } return ArrRtn; }
template<typename T> inline static void toVal(Val& mV, T&& mX) { Arr result; result.reserve(TS); for(auto i(0u); i < TS; ++i) result.emplace_back(moveIfRValue<decltype(mX)>(mX[i])); mV.setArr(std::move(result)); }
template<typename T> inline static void toVal(Val& mV, T&& mX) { Arr result; result.reserve(mX.size()); for(const auto& v : mX) result.emplace_back(moveIfRValue<decltype(mX)>(v)); mV.setArr(std::move(result)); }
template<typename T> inline static void toVal(Val& mV, T&& mX) { Arr result; result.reserve(sizeof...(TArgs)); TplCnvHelper::fromTpl<0, TArgs...>(result, fwd<T>(mX)); mV.setArr(std::move(result)); }