int main(int argc, const char *argv[]) { int arr[] = {1, 2, 3, 4, 5, 6}; reverse(arr, ilen(arr)); for (int i = 0; i < ilen(arr); i++) { printf("%i\n", arr[i]); } return 0; }
ReadableStore* ReadableStore::openStore(PathRef segDir, fstring fname) { size_t sufpos = fname.size(); while (sufpos > 0 && fname[sufpos-1] != '.') --sufpos; auto suffix = fname.substr(sufpos); size_t idx = s_storeFactory.find_i(suffix); if (idx < s_storeFactory.end_i()) { const auto& factory = s_storeFactory.val(idx); ReadableStore* store = factory(); assert(NULL != store); if (NULL == store) { THROW_STD(runtime_error, "store factory should not return NULL store"); } auto fpath = segDir / fname.str(); store->load(fpath); return store; } else { THROW_STD(invalid_argument , "store type '%.*s' of '%s' is not registered" , suffix.ilen(), suffix.data() , (segDir / fname.str()).string().c_str() ); return NULL; // avoid compiler warning } }
PlanIter_t fn_subsequence::codegen( CompilerCB* /*cb*/, static_context* aSctx, const QueryLoc& aLoc, std::vector<PlanIter_t>& aArgs, expr& aAnn) const { fo_expr& subseqExpr = static_cast<fo_expr&>(aAnn); const expr* inputExpr = subseqExpr.get_arg(0); const expr* posExpr = subseqExpr.get_arg(1); const expr* lenExpr = (subseqExpr.num_args() > 2 ? subseqExpr.get_arg(2) : NULL); if (inputExpr->get_expr_kind() == relpath_expr_kind && posExpr->get_expr_kind() == const_expr_kind && lenExpr != NULL && lenExpr->get_expr_kind() == const_expr_kind) { xs_double dpos = static_cast<const const_expr*>(posExpr)-> get_val()->getDoubleValue().round(); xs_integer ipos(dpos.getNumber()); xs_double dlen = static_cast<const const_expr*>(lenExpr)-> get_val()->getDoubleValue().round(); xs_integer ilen(dlen.getNumber()); xs_long pos; xs_long len; try { pos = to_xs_long(ipos); len = to_xs_long(ilen); } catch (std::range_error const&) { goto done; } const relpath_expr* pathExpr = static_cast<const relpath_expr*>(inputExpr); csize numSteps = pathExpr->numSteps(); if (pos > 0 && len == 1 && numSteps == 2) { AxisIteratorHelper* input = dynamic_cast<AxisIteratorHelper*>(aArgs[0].getp()); assert(input != NULL); if (input->setTargetPos(pos-1)) return aArgs[0]; } } done: return new FnSubsequenceIterator(aSctx, aLoc, aArgs); }
static void print_double(void (*putc)(char), double n, int precision) { const int floor = (int) n; print_int(putc, floor, 10); putc('.'); int mult = 1; for (int i = 0; i < precision; ++i) { mult *= 10; } const int fract = mult * (n - floor); for (int i = ilen(fract, 10); i < precision - (n ? 1 : 0); ++i) { putc('0'); } print_int(putc, fract, 10); }
void fleece_size_array_part (insp_ctrl *ctrl, const Table *t, size_t *count, int *pure) { int lg; int ttlg; /* 2^lg */ int i = 1; /* count to traverse all array keys */ for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ int lim = ttlg; if (lim > t->sizearray) { lim = t->sizearray; /* adjust upper limit */ if (i > lim) break; /* no more elements to count */ } /* count elements in range (2^(lg-1), 2^lg] */ for (; i <= lim; i++) { TValue * v = &t->array[i-1]; if(!ttisnil(v)) { if((*count)++ != i) add(ctrl, ilen(i)); ctrl->total_len += 1; // : FLEECE_SIZE(ctrl, v); } } } }
coords_t track_to_maximum(const BasisSet & basis, const arma::mat & P, const coords_t r0, size_t & nd, size_t & ng) { // Track density to maximum. coords_t r(r0); size_t iiter=0; // Amount of density and gradient evaluations size_t ndens=0; size_t ngrad=0; // Nuclear coordinates arma::mat nuccoord=basis.get_nuclear_coords(); // Initial step size to use const double steplen=0.1; double dr(steplen); // Maximum amount of steps to take in line search const size_t nline=5; // Density and gradient double d; arma::vec g; while(true) { // Iteration number iiter++; // Compute density and gradient compute_density_gradient(P,basis,r,d,g); double gnorm=arma::norm(g,2); fflush(stdout); ndens++; ngrad++; // Normalize gradient and perform line search coords_t gn; gn.x=g(0)/gnorm; gn.y=g(1)/gnorm; gn.z=g(2)/gnorm; std::vector<double> len, dens; #ifdef BADERDEBUG printf("Gradient norm %e. Normalized gradient at % f % f % f is % f % f % f\n",gnorm,r.x,r.y,r.z,gn.x,gn.y,gn.z); #endif // Determine step size to use by finding out minimal distance to nuclei arma::rowvec rv(3); rv(0)=r.x; rv(1)=r.y; rv(2)=r.z; // and the closest nucleus double mindist=arma::norm(rv-nuccoord.row(0),2); arma::rowvec closenuc=nuccoord.row(0); for(size_t i=1;i<nuccoord.n_rows;i++) { double t=arma::norm(rv-nuccoord.row(i),2); if(t<mindist) { mindist=t; closenuc=nuccoord.row(i); } } // printf("Minimal distance to nucleus is %e.\n",mindist); // Starting point len.push_back(0.0); dens.push_back(d); #ifdef BADERDEBUG printf("Step length %e: % f % f % f, density %e, difference %e\n",len[0],r.x,r.y,r.z,dens[0],0.0); #endif // Trace until density does not increase any more. do { // Increase step size len.push_back(len.size()*dr); // New point coords_t pt=r+gn*len[len.size()-1]; // and density dens.push_back(compute_density(P,basis,pt)); ndens++; #ifdef BADERDEBUG printf("Step length %e: % f % f % f, density %e, difference %e\n",len[len.size()-1],pt.x,pt.y,pt.z,dens[dens.size()-1],dens[dens.size()-1]-dens[0]); #endif } while(dens[dens.size()-1]>dens[dens.size()-2] && dens.size()<nline); // Optimal line length double optlen=0.0; if(dens[dens.size()-1]>=dens[dens.size()-2]) // Maximum allowed optlen=len[len.size()-1]; else { // Interpolate arma::vec ilen(3), idens(3); if(dens.size()==2) { ilen(0)=len[len.size()-2]; ilen(2)=len[len.size()-1]; ilen(1)=(ilen(0)+ilen(2))/2.0; idens(0)=dens[dens.size()-2]; idens(2)=dens[dens.size()-1]; idens(1)=compute_density(P,basis,r+gn*ilen(1)); ndens++; } else { ilen(0)=len[len.size()-3]; ilen(1)=len[len.size()-2]; ilen(2)=len[len.size()-1]; idens(0)=dens[dens.size()-3]; idens(1)=dens[dens.size()-2]; idens(2)=dens[dens.size()-1]; } #ifdef BADERDEBUG arma::trans(ilen).print("Step lengths"); arma::trans(idens).print("Densities"); #endif // Fit polynomial arma::vec p=fit_polynomial(ilen,idens); // and solve for the roots of its derivative arma::vec roots=solve_roots(derivative_coefficients(p)); // The optimal step length is for(size_t i=0;i<roots.n_elem;i++) if(roots(i)>=ilen(0) && roots(i)<=ilen(2)) { optlen=roots(i); break; } } #ifdef BADERDEBUG printf("Optimal step length is %e.\n",optlen); #endif if(std::min(optlen,mindist)<=CONVTHR) { // Converged at nucleus. r.x=closenuc(0); r.y=closenuc(1); r.z=closenuc(2); } if(optlen==0.0) { if(dr>=CONVTHR) { // Reduce step length. dr/=10.0; continue; } else { // Converged break; } } else if(optlen<=CONVTHR) // Converged break; // Update point r=r+gn*optlen; } nd+=ndens; ng+=ngrad; #ifdef BADERDEBUG printf("Point % .3f % .3f %.3f tracked to maximum at % .3f % .3f % .3f with %s density evaluations.\n",r0.x,r0.y,r0.z,r.x,r.y,r.z,space_number(ndens).c_str()); #endif return r; }