void read(){ n = gi; int now = 0,maxlen = 0; com = -1; for(int i = 1;i<=n;++i){ cnt[i] = 0; bool mark = true; cin>>str+now+1; from[i] = now+1; for(int j = now+1;isstr(str[j]);++j){ str[++now] = str[j]; if(str[j] == '*') mark = false; else ++cnt[i]; } if(mark) com = i; maxlen = max(maxlen,now); to[i] = now; } if(nowlen < maxlen) for(int i = nowlen+1;i<=maxlen;++i) power[i] = power[i-1]*p; for(int i = 1;i<=now;++i) sum[i] = sum[i-1]*p+str[i]; }
prolog_term intern_rec(CTXTdeclc prolog_term term) { int areaindex, reclen, i, j; CPtr hc_term; Cell dterm[255]; Cell arg; // printf("intern_rec\n"); // create term-record with all fields dereffed in dterm XSB_Deref(term); if (isinternstr(term)) {printf("old\n"); return term;} if (isconstr(term)) { areaindex = get_arity(get_str_psc(term)); reclen = areaindex + 1; cell(dterm) = (Cell)get_str_psc(term); // copy psc ptr j=1; } else if (islist(term)) { areaindex = LIST_INDEX; reclen = 2; j=0; } else return 0; for (i=j; i<reclen; i++) { arg = get_str_arg(term,i); // works for lists and strs XSB_Deref(arg); if (isref(arg) || (isstr(arg) && !isinternstr(arg)) || isattv(arg)) { return 0; } cell(dterm+i) = arg; } hc_term = insert_interned_rec(reclen, areaindex, dterm); if (islist(term)) return makelist(hc_term); else return makecs(hc_term); }
static char *get_str_val(Cell *vp, char **fmt) /* get string val of a Cell */ { char s[100]; /* BUG: unchecked */ double dtemp; if ((vp->tval & (NUM | STR)) == 0) funnyvar(vp, "read value of"); if (isfld(vp) && donefld == 0) fldbld(); else if (isrec(vp) && donerec == 0) recbld(); if (isstr(vp) == 0) { if (freeable(vp)) xfree(vp->sval); if (modf(vp->fval, &dtemp) == 0) /* it's integral */ sprintf(s, "%.30g", vp->fval); else sprintf(s, *fmt, vp->fval); vp->sval = tostring(s); vp->tval &= ~DONTFREE; vp->tval |= STR; } dprintf( ("getsval %p: %s = \"%s (%p)\", t=%o\n", (void*)vp, NN(vp->nval), vp->sval, vp->sval, vp->tval) ); return(vp->sval); }
/* Evaluate the expressions inside the file pointed by ep */ static exp_t * prim_load(exp_t *args) { chkargs("load", args, 1); if (!isstr(car(args))) everr("load: should be a string", car(args)); load(str(car(args)), NINTER); return NULL; }
//------------------------------------------------------------------------------ int Filter::parameters(int numParams, const char *const* paramStrings) { if (numParams == 0 || paramStrings == NULL) return(0); const char* param = snl_fei::getParam("outputLevel",numParams,paramStrings); if ( param != NULL){ std::string str(&(param[11])); FEI_ISTRINGSTREAM isstr(str); isstr >> outputLevel_; }
char *prefixify(node package, char *name){ char buf[500]; if (package==NULL) return name; assertpos(issym(package),package); assertpos(isstr(package->body.symbol.name),package); assertpos(tostring(package) != NULL, package->body.symbol.name); buf[0]=0; setprefix(buf,package); strcat(buf,name); return strperm(buf); }
bool isPalindrome(char* s) { if(NULL == s) return true; if('\0' == s) return true; if(strlen(s) < 2) return true; char* pa = s; char* pb = s; char* l = s; while(*pb != '\0') pb++; pb--; char* n = pb; while(pa < pb) { while(!isstr(*pa) && pa<=n) pa++; while(!isstr(*pb) && pb>=l) pb--; if(((*pa != *pb) && (abs(*pa-*pb) != 'a'-'A')) && (isstr(*pa)) && (isstr(*pb))) return false; else { pa++; pb--; } } return true; }
Cell *copycell(Cell *x) /* make a copy of a cell in a temp */ { Cell *y; y = gettemp(); y->csub = CCOPY; /* prevents freeing until call is over */ y->nval = x->nval; /* BUG? */ if (isstr(x)) y->sval = tostring(x->sval); y->fval = x->fval; y->tval = x->tval & ~(CON|FLD|REC|DONTFREE); /* copy is not constant or field */ /* is DONTFREE right? */ return y; }
void printstringlist(){ node p; unsigned int h; pput("String Table\n"); for (h=0; h<numberof(hash_buckets); h++) { for (p = hash_buckets[h]; p != NULL; p = CDR(p)) { node str = CAR(p); assertpos(isstr(str),str); pprint(str); pput(" : "); pprint(str->body.unique_string.symbol_list); pput("\n"); } } pput("\n"); }
Datum ctos(Datum d) { /* convert char to string */ if (ischar(d)) { /* only if Datum is char */ char *s; s = (char *) getheap(2);/* put the string on heap */ *s = d.u.i; s[1] = '\0'; d.u.s = s; d.type = STRING; } if (!isstr(d)) error("string needed"); return d; }
static void unique_f(U *p) { if (isstr(p)) { if (p1 == symbol(NIL)) p1 = p; else if (p != p1) p2 = p; return; } while (iscons(p)) { unique_f(car(p)); if (p2 != symbol(NIL)) return; p = cdr(p); } }
//----------------------------------------------------------- // Utility functions //----------------------------------------------------------- void LQCDA::readArray(AsciiReader& reader, Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic>& output) { try { std::string line; reader.readLine(line); std::istringstream isstr (line); int i, j; isstr >> i >> j; output.resize(i, j); for(int _i=0; _i<i; ++_i) for(int _j=0; _j<j; ++_j) { reader.read(output(_i,_j)); } } catch (const IOException& e) { } }
node nextfun(node fun){ node sym, name, symbol_list, nextsym; if (fun->tag == position_tag) { sym = fun->body.position.contents; } else { sym = fun; } if (sym->tag != symbol_tag) return NULL; name = sym->body.symbol.name; assert(isstr(name)); symbol_list = name->body.unique_string.symbol_list; while (car(symbol_list) != sym) { symbol_list = cdr(symbol_list); } symbol_list = cdr(symbol_list); if (symbol_list == NULL) return NULL; nextsym = car(symbol_list); return nextsym; }
dsap_tai_init() { FILE *fp; char *cp; char buf[BUFSIZ]; if(!isstr(tailfile)) /* it's compiled in */ return(OK); if( (fp = fopen(cp = isodefile(tailfile, 0), "r")) == (FILE *)NULL) { LLOG (log_dsap,LLOG_FATAL,("can't open tailor file '%s'", cp)); fatal (-1, "Cannot open tailor file"); } while(fgets(buf, sizeof(buf), fp) != NULLCP) if ( (*buf != '#') && (*buf != '\n') ) /* not a comment or blank */ if (tai_string (buf) == NOTOK) LLOG (log_dsap,LLOG_EXCEPTIONS,("tai_string failed %s",buf)); (void) fclose(fp); return OK; }
Epetra_Vector* read_vector_mm(const std::string& mm_file, const Epetra_Comm& comm) { int my_proc = comm.MyPID(); long long num_global_rows = 0; std::ifstream* infile = NULL; if (my_proc == 0) { infile = new std::ifstream(mm_file.c_str()); if (infile == NULL || !*infile) { throw std::runtime_error("Failed to open file "+mm_file); } std::ifstream& in = *infile; //first skip over the file header, which has //lines beginning with '%'. std::string line; do { getline(in, line); } while(line[0] == '%'); //now get the matrix dimensions. int numrows, numcols; std::istringstream isstr(line); isstr >> numrows >> numcols; //make sure we successfully read the ints from that line. if (isstr.fail()) { throw std::runtime_error("Failed to parse matrix-market header."); } num_global_rows = numrows; }
node newsymbol(node p, node ptype, scope v, int flags){ node name; node s; struct POS *ppos = NULL; assertpos(istype(ptype),ptype); if (issym(p)) { /* defined previously so the Cname can be set by the translator */ return p; } if (isstr(p)) { name = p; p = NULL; } else if (isstrpos(p)) { ppos = &p->body.position.pos; name = p->body.position.contents; assertpos(name->tag == unique_string_tag,name); } else { errorpos(p,"defining a nonsymbol"); return NULL; } s = newnode(SYMBOL,symbol_tag); /* newnode clears the memory */ assert(s->body.symbol.flags == 0); s->body.symbol.name = name; s->body.symbol.type = ptype; s->body.symbol.pos = ppos==NULL ? empty_pos : *ppos; if (!inside_defun(v) && !(flags & tmp_F)) flags |= global_F; s->body.symbol.flags = flags & ~intern_F; if (v!=NULL && v->previous != NULL) { scope w = v->previous; assert(w->current_package==NULL || issym(w->current_package)); s->body.symbol.package = w->current_package; } if (flags & intern_F) internsymbol(s,v); return s; }
int cmp_expr(U *p1, U *p2) { int n; if (p1 == p2) return 0; if (p1 == symbol(NIL)) return -1; if (p2 == symbol(NIL)) return 1; if (isnum(p1) && isnum(p2)) return sign(compare_numbers(p1, p2)); if (isnum(p1)) return -1; if (isnum(p2)) return 1; if (isstr(p1) && isstr(p2)) return sign(strcmp(p1->u.str, p2->u.str)); if (isstr(p1)) return -1; if (isstr(p2)) return 1; if (issymbol(p1) && issymbol(p2)) return sign(strcmp(get_printname(p1), get_printname(p2))); if (issymbol(p1)) return -1; if (issymbol(p2)) return 1; if (istensor(p1) && istensor(p2)) return compare_tensors(p1, p2); if (istensor(p1)) return -1; if (istensor(p2)) return 1; while (iscons(p1) && iscons(p2)) { n = cmp_expr(car(p1), car(p2)); if (n != 0) return n; p1 = cdr(p1); p2 = cdr(p2); } if (iscons(p2)) return -1; if (iscons(p1)) return 1; return 0; }
void fldbld(void) /* create fields from current record */ { /* this relies on having fields[] the same length as $0 */ /* the fields are all stored in this one array with \0's */ char *r, *fr, sep; Cell *p; int i, j, n; if (donefld) return; if (!isstr(fldtab[0])) getsval(fldtab[0]); r = fldtab[0]->sval; n = strlen(r); if (n > fieldssize) { xfree(fields); if ((fields = (char *) malloc(n+1)) == NULL) FATAL("out of space for fields in fldbld %d", n); fieldssize = n; } fr = fields; i = 0; /* number of fields accumulated here */ if (strlen(inputFS) > 1) { /* it's a regular expression */ i = refldbld(r, inputFS); } else if ((sep = *inputFS) == ' ') { /* default whitespace */ for (i = 0; ; ) { while (*r == ' ' || *r == '\t' || *r == '\n') r++; if (*r == 0) break; i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); fldtab[i]->sval = fr; fldtab[i]->tval = FLD | STR | DONTFREE; do *fr++ = *r++; while (*r != ' ' && *r != '\t' && *r != '\n' && *r != '\0'); *fr++ = 0; } *fr = 0; } else if ((sep = *inputFS) == 0) { /* new: FS="" => 1 char/field */ for (i = 0; *r != 0; r++) { char buf[2]; i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); buf[0] = *r; buf[1] = 0; fldtab[i]->sval = tostring(buf); fldtab[i]->tval = FLD | STR; } *fr = 0; } else if (*r != 0) { /* if 0, it's a null field */ for (;;) { i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); fldtab[i]->sval = fr; fldtab[i]->tval = FLD | STR | DONTFREE; while (*r != sep && *r != '\n' && *r != '\0') /* \n is always a separator */ *fr++ = *r++; *fr++ = 0; if (*r++ == 0) break; } *fr = 0; } if (i > nfields) FATAL("record `%.30s...' has too many fields; can't happen", r); cleanfld(i+1, lastfld); /* clean out junk from previous record */ lastfld = i; donefld = 1; for (j = 1; j <= lastfld; j++) { p = fldtab[j]; if(is_number(p->sval)) { p->fval = atof(p->sval); p->tval |= NUM; } } setfval(nfloc, (Awkfloat) lastfld); if (dbg) { for (j = 0; j <= lastfld; j++) { p = fldtab[j]; printf("field %d (%s): |%s|\n", j, p->nval, p->sval); } } }
static Out apply(std::string const& val) { std::istringstream isstr(val); Out out; isstr >> out; return out; }
Cell *assign(Node **a, int n) /* a[0] = a[1], a[0] += a[1], etc. */ { /* this is subtle; don't muck with it. */ Cell *x, *y; Awkfloat xf, yf; double v; y = execute(a[1]); x = execute(a[0]); if (n == ASSIGN) { /* ordinary assignment */ if (x == y && !(x->tval & (FLD|REC))) /* self-assignment: */ ; /* leave alone unless it's a field */ else if ((y->tval & (STR|NUM)) == (STR|NUM)) { setsval(x, getsval(y)); x->fval = getfval(y); x->tval |= NUM; } else if (isstr(y)) setsval(x, getsval(y)); else if (isnum(y)) setfval(x, getfval(y)); else funnyvar(y, "read value of"); tempfree(y); return(x); } xf = getfval(x); yf = getfval(y); switch (n) { case ADDEQ: xf += yf; break; case SUBEQ: xf -= yf; break; case MULTEQ: xf *= yf; break; case DIVEQ: if (yf == 0) FATAL("division by zero in /="); xf /= yf; break; case MODEQ: if (yf == 0) FATAL("division by zero in %%="); modf(xf/yf, &v); xf = xf - yf * v; break; case POWEQ: if (yf >= 0 && modf(yf, &v) == 0.0) /* pos integer exponent */ xf = ipow(xf, (int) yf); else xf = errcheck(pow(xf, yf), "pow"); break; default: FATAL("illegal assignment operator %d", n); break; } tempfree(y); setfval(x, xf); return(x); }
/* Test if the expression is self-evaluating */ int isself(exp_t *ep) { return ep == NULL || isnum(ep) || isstr(ep) || isbool(ep) || ischar(ep); }
void run(char *s) { int i, n; /*if (strncmp(s, "selftest", 8) == 0) { selftest(); return; }*/ if (setjmp(stop_return)) return; init(); while (1) { n = scan(s); p1 = pop(); check_stack(); if (n == 0) break; // if debug mode then print the source text if (equaln(get_binding(symbol(TRACE)), 1)) { for (i = 0; i < n; i++) if (s[i] != '\r') printchar(s[i]); if (s[n - 1] != '\n') // n is not zero, see above printchar('\n'); } s += n; push(p1); top_level_eval(); p2 = pop(); check_stack(); if (p2 == symbol(NIL)) continue; // print string w/o quotes if (isstr(p2)) { printstr(p2->u.str); printstr("\n"); continue; } if (equaln(get_binding(symbol(TTY)), 1) || test_flag) // tty mode? printline(p2); else { //#ifdef LINUX display(p2); /*#else push(p2); cmdisplay(); #endif*/ } } }
Epetra_CrsMatrix* read_matrix_mm(const std::string& mm_file, const Epetra_Comm& comm) { int my_proc = comm.MyPID(); long long num_global_rows = 0; int nnz_per_row = 0; std::ifstream* infile = NULL; infile = new std::ifstream(mm_file.c_str()); if (infile == NULL || !*infile) { throw std::runtime_error("Failed to open file "+mm_file); } std::ifstream& in = *infile; //first skip over the file header, which has //lines beginning with '%'. std::string line; do { getline(in, line); } while(line[0] == '%'); //now get the matrix dimensions. int numrows, numcols, nnz; std::istringstream isstr(line); isstr >> numrows >> numcols >> nnz; //make sure we successfully read the three ints from that line. if (isstr.fail()) { throw std::runtime_error("Failed to parse matrix-market header."); } if (my_proc == 0) { num_global_rows = numrows; nnz_per_row = nnz/numrows; } comm.Broadcast(&num_global_rows, 1, 0); comm.Broadcast(&nnz_per_row, 1, 0); const int indexBase = 0; Epetra_Map rowmap(num_global_rows, indexBase, comm); Epetra_CrsMatrix* A = new Epetra_CrsMatrix(Copy, rowmap, nnz_per_row); Teuchos::Array<long long> col; Teuchos::Array<double> coef; int irow=0, icol=0; int g_row=-1, last_row=-1; double val=0; while(!in.eof()) { getline(in, line); std::istringstream isstr(line); isstr >> irow >> icol >> val; if (isstr.fail()) continue; if (!rowmap.MyGID(irow-1)) continue; g_row = irow-1; if (g_row != last_row) { if (col.size() > 0) { A->InsertGlobalValues(last_row, col.size(), &coef[0], &col[0] ); col.clear(); coef.clear(); } last_row = g_row; } col.push_back(icol-1); coef.push_back(val); } if (col.size() > 0) { A->InsertGlobalValues(g_row, col.size(), &coef[0], &col[0]); } A->FillComplete(); return A; }