void test_lower_bound() { stackinit(); push(3); assert(pop() == 3); assert(pop() == 0); printf("+"); }
/* * Two simple tests: * - order preserved * - unable to pop more than pushed */ void test_order_preserved() { stackinit(); push(2); push(4); assert(pop() == 4); assert(pop() == 2); printf("+"); }
void test_upper_bound() { int i; stackinit(); for (i = 0; i < MAX; i++) push(i); push(++i); assert(pop() != i); printf("+"); }
// The KLU factorization might have some information about cliques in Y22 only, // but we want to consider the whole system, so this function // performs a new DFS on the compressed non-zero pattern // This function could behave differently than before, // since the compression process removes numerical zero elements unsigned int KLUSystem::FindIslands(unsigned int *idClique) { Factor (); int *clique = (int *) cs_malloc (m_nBus, sizeof (int)); int *Ap = Y22->p; int *Ai = Y22->i; unsigned j; // DFS down the columns int cnt = 0; for (j = 0; j < m_nBus; j++) clique[j] = cnt; // use to mark the nodes with clique # stackinit (m_nBus); for (j = 0; j < m_nBus; j++) { if (clique[j] == 0) { // have not visited this column yet ++cnt; mark_dfs (j, cnt, Ap, Ai, clique); } } for (j = 0; j < m_nBus; j++) idClique[j] = clique[j]; // if there is more than one clique, stuff the row numbers (+1) into island lists // unsigned i, p; /* used to return allocated lists of nodes in each island if (cnt > 1) { *paaidBus = new unsigned * [cnt]; unsigned **aaidBus = *paaidBus; for (i = 0; i < cnt; i++) { p = 0; for (j = 0; j < m_nBus; j++) { // count nodes in this clique if (clique[j] == i+1) ++p; } aaidBus[i] = new unsigned [p + 1]; aaidBus[i][0] = p; p = 1; for (j = 0; j < m_nBus; j++) { if (clique[j] == i+1) { aaidBus[i][p++] = j + 1; } } } } else { paaidBus = NULL; } */ cs_free (clique); return cnt; }
int main(){ char c; for(stackinit(); scanf("%1s", &c) != EOF;){ if(c == ')') printf("%lc", (char) pop()); if(c == '+') push((int) c); if(c == '*') push((int) c); while(c >= '0' && c <= '9'){ printf("%1c", c); scanf("%1c", &c); } if(c != '(') printf(" "); } printf("\n"); return 0; }
int main(){ char c; int x; for(stackinit(); scanf("%1s", &c) != EOF;){ x = 0; if(c == '+') x = pop()+pop(); if(c == '*') x = pop()*pop(); while(c >= '0' && c <= '9'){ x = 10*x + (c-'0'); scanf("%1c", &c); } push(x); // printf("%d\n", x); } printf("%d\n", x); return 0; }
// entry point into program int main(int argc, char *argv[]) { int x; program *pgm; FILE *fp; signal(SIGFPE, fpe_handler); signal(SIGINT, brk_handler); gprog = SafeMalloc(sizeof(program)); storageinit(gprog); envinfo.argc = argc; for (x=0; x<argc; x++) envinfo.argv[x] = argv[x]; if ((fp = fopen("LOGIN", "rb")) == NULL) printf("ERROR: Could not open login script\n"); else { fclose(fp); pgm = SafeMalloc(sizeof(program)); envinit(); loadall(pgm, "LOGIN"); runprog(pgm, 1); GC_free(pgm); } printf("\x1B[11m"); rl_inhibit_completion = 1; printf("\n\x1B[10mREADY\n"); setjmp(mark); do { stackinit(); gprog->nextprog = 0; gprog->prevprog = 0; input = readline(">"); prog = input; if (*input) { add_history(input); encode_rpn(); } } while (1); return 0; }
/* pre-improvement initialization code */ void improveinit(struct tetcomplex *mesh, struct proxipool *vertexpool, struct arraypoolstack *tetstack, struct behavior *behave, struct inputs *in, int argc, char **argv, starreal bestmeans[NUMMEANTHRESHOLDS]) { int consistent; starreal minqualbefore; starreal meanqualbefore[NUMMEANTHRESHOLDS]; starreal worstin; starreal worstinitqual; int i; for (i=0; i<NUMMEANTHRESHOLDS; i++) { bestmeans[i] = 0.0; } /* assure that the mesh is consistent at the outset */ consistent = mytetcomplexconsistency(mesh); assert(consistent); stats.startnumverts = countverts(vertexpool); stats.startnumtets = counttets(mesh); /* upon request, just spit out mesh and quit */ if (improvebehave.outputandquit) { printf("Outputting mesh and quitting.\n"); outputqualmesh(behave, in, vertexpool, mesh, argc, argv, 0, SMOOTHPASS, 0, improvebehave.qualmeasure); starexit(1); } if (improvebehave.verbosity > 0) { printf("Improving mesh.\n"); } /* this array pool stores information on vertices */ arraypoolinit(&vertexinfo, sizeof(struct vertextype), LOG2TETSPERSTACKBLOCK, 0); /* stack of tets to be improved */ stackinit(tetstack, sizeof(struct improvetet)); /* this stack stores a journal of improvement steps */ journal = &journalstack; stackinit(journal, sizeof(struct journalentry)); /* compute bounding box for anisotropy */ setboundingbox(mesh); if (improvebehave.verbosity > 1) { /* print improvement configuration */ printimproveoptions(&improvebehave); /* print initial mesh statistics before improvement */ printf("Mesh quality before improvement:\n"); improvestatistics(behave, mesh, false); if (improvebehave.anisotropic) { printf("Mesh quality before improvement (ISOTROPIC SPACE):\n"); improvestatistics(behave, mesh, true); } } /* print out the worst input angle */ worstin = worstinputangle(mesh); if (improvebehave.verbosity > 2) { printf("The worst input angle is %g radians (%g degrees).\n", worstin, worstin * (180.0 / PI)); } /* classify degrees of freedom of all vertices */ if (improvebehave.verbosity > 2) { printf("Performing vertex classification.\n"); } classifyvertices(mesh); /* compute surface quadric information */ if (improvebehave.verbosity > 2) { printf("Computing intial surface quadrics\n"); } collectquadrics(mesh); if (improvebehave.verbosity > 2) { checkquadrics(mesh); } /* make sure that all the tets are right-side out before we start */ worstinitqual = worstquality(mesh); if (worstinitqual <= 0.0) { textcolor(BRIGHT, RED, BLACK); printf("***** ALERT Input mesh has non-positive worst quality of %g, dying *****\n", worstinitqual); textcolor(RESET, WHITE, BLACK); } if (improvebehave.dynimprove) { assert(worstinitqual > 0.0); } /* build stack for initial quality evaluation */ fillstackqual(mesh, tetstack, improvebehave.qualmeasure, HUGEFLOAT, meanqualbefore, &minqualbefore); meanimprove(bestmeans, meanqualbefore, SMOOTHPASS); /* set initial minimum and thresholded mean qualities */ for (i=0; i<NUMMEANTHRESHOLDS; i++) { stats.startmeanquals[i] = meanqualbefore[i]; } stats.startworstqual = minqualbefore; setbeginqual(behave, mesh); }
int main(void) { gcinit(); stackinit(); while(1) { char buffer[80]; ssize_t bytes_read = raw_input("calc> ", buffer, sizeof(buffer)); if(bytes_read == EOF) { return 0; } else { Buffer p; buffer_init(&p, buffer, bytes_read); size_t i = 0; while(p.pos != p.end) { if(IS_DIGIT(*p.pos)) { int num = *p.pos - '0'; stackpush(newLong(num)); printf("PUSH %d\n", num); } else if(IS_OPERATOR(*p.pos)) { if(pstack.stacksize < 2) { fprintf(stderr, "operator '%c' takes 2 args\n", *p.pos); goto finally; } else { Object* op2 = stackpop(); Object* op1 = stackpop(); if(*p.pos == '+') { printf("POP %ld\n", O_LVAL(op2)); printf("POP %ld\n", O_LVAL(op1)); long result; result = O_LVAL(op1) + O_LVAL(op2); Object *retval = newLong(result); printf("ADD\n"); stackpush(retval); printf("PUSH %ld\n", result); } else if(*p.pos == '-') { long result; result = O_LVAL(op1) - O_LVAL(op2); Object *retval = newLong(result); stackpush(retval); } } } else if (IS_WHITE(*p.pos)) { goto out; } else { fprintf(stderr, "Invalid token %c\n", *p.pos); goto finally; } out: p.pos++; i++; } if(pstack.stacksize == 1) { Object *top = stackpop(); objectEcho(top); } else { printf("To many values: %zu", pstack.stacksize); } finally: gcterm(); gcinit(); stackinit(); printf("\n"); } } return 0; }
void base_quicksort7(unsigned int a[], int N) { int l, r; int i; int m; int il, ir; /* names follow pl, pm, and pn from bently/mcilroy. used ir instead of in */ stackinit(N); describe_predictor(&global_predictor[0], "i"); describe_predictor(&global_predictor[1], "j"); describe_predictor(&global_predictor[2], "partition end"); describe_predictor(&global_predictor[3], "insertion"); describe_predictor(&global_predictor[4], "median"); /* describe_predictor(&global_predictor[4], "median of 7 ab"); */ describe_predictor(&global_predictor[5], "median of 7 bc"); describe_predictor(&global_predictor[6], "median of 7 ac"); describe_predictor(&global_predictor[7], "median of 7 cb"); describe_predictor(&global_predictor[8], "median of 7 ca"); describe_predictor(&global_predictor[9], "median of 7 ab2"); describe_predictor(&global_predictor[10], "median of 7 bc2"); describe_predictor(&global_predictor[11], "median of 7 ac2"); describe_predictor(&global_predictor[12], "median of 7 cb2"); describe_predictor(&global_predictor[13], "median of 7 ca2"); describe_predictor(&global_predictor[14], "median of 3 cmp1"); describe_predictor(&global_predictor[15], "median of 3 cmp2"); describe_predictor(&global_predictor[16], "median of 3 cmp3"); r = N-1; l = 0; while(1) { int n = r - l; int n6 = n/6; int n3 = n/3; if (r - l <= THRESHHOLD) { if (stackempty()) break; l = pop(); r = pop(); continue; } /* pseudo - Median of 7 partitioning*/ m = (l+r)/2; if (n > 40) { il = med3(a, l, l + n6, l + n3); /* the 2 is for seperate branch predictors, as it's inlined */ ir = med3_2(a, r - n3, r - n6, r); exch(a[l], a[il]); exch(a[r], a[ir]); exch(a[m], a[r-1]); } pred_compexch(a[l], a[r-1], 14); pred_compexch(a[l], a[r], 15); pred_compexch(a[r-1], a[r], 16); i = partition(a,l+1,r-1); /* here is the bug */ /* then key is being copied more times than necessary. the reason for this is that it is not being removed when it is taken as the key */ /* instead, it is being put in place more than once */ /* example: i == 1, j == 10; key = a[1]; key < pivot, so key is swapped with a[2], the key is now in a[1] and a[2]. uh oh */ if (i-l > r-i) { push(i-1,l); l = i+1; } else { push(r,i+1); r = i-1; } } stackclear(); /* the +1 isnt immediately obvious. its because THRESHHOLD is the difference between l and r up above */ if (2*THRESHHOLD > N) insertion_sentinel(a,N); else insertion_sentinel(a,2*THRESHHOLD); insertion(a, N); /* add the predictors up */ add_predictor(&global_predictor[4], &global_predictor[5]); add_predictor(&global_predictor[4], &global_predictor[6]); add_predictor(&global_predictor[4], &global_predictor[7]); add_predictor(&global_predictor[4], &global_predictor[8]); add_predictor(&global_predictor[4], &global_predictor[9]); add_predictor(&global_predictor[4], &global_predictor[10]); add_predictor(&global_predictor[4], &global_predictor[11]); add_predictor(&global_predictor[4], &global_predictor[12]); add_predictor(&global_predictor[4], &global_predictor[13]); add_predictor(&global_predictor[4], &global_predictor[14]); add_predictor(&global_predictor[4], &global_predictor[15]); add_predictor(&global_predictor[4], &global_predictor[16]); init_predictor(&global_predictor[5]); init_predictor(&global_predictor[6]); init_predictor(&global_predictor[7]); init_predictor(&global_predictor[8]); init_predictor(&global_predictor[9]); init_predictor(&global_predictor[10]); init_predictor(&global_predictor[11]); init_predictor(&global_predictor[12]); init_predictor(&global_predictor[13]); init_predictor(&global_predictor[14]); init_predictor(&global_predictor[15]); init_predictor(&global_predictor[16]); }