int main() { struct Stack *s = createStack(); int i, ele; printf("\ttop : %d \n",s->top); printf("\t capacity: %d\n", s->capacity); for (i=0; i<6; i++) { push(s, (i+1)*100); } printf(" \tStsack Elements\n\n"); printStack(s); reverseStack(s); printf(" \tStsack Elements after reverse : \n\n"); printStack(s); for (i=0; i<6; i++) { ele= pop(s); printf("\nFD stack:%d\n\n",ele); } printf(" \tStsack Elements\n\n"); printStack(s); return 0; }
void Misc::reverseStack(stack<int> &s) { if (s.size()<=1) return; int x = s.top(); s.pop(); reverseStack(s); pushBottom(s, x); }
void doubleEndSearch(State& from,State& to,stack<State>& result) { cleanMap(bfsMap); cleanMap(dfsMap); cleanStateQueue(); pthread_t tid1,tid2; stack<State> stackForBFS; stack<State> stackForDFS; Para bfsPara = {to,stackForBFS}; Para dfsPara = {from,stackForDFS}; pthread_create(&tid1,NULL,Thread1,&bfsPara); pthread_create(&tid2,NULL,Thread2,&dfsPara); pthread_join(tid1,NULL); pthread_join(tid2,NULL); while(!stackForBFS.empty()){ result.push(stackForBFS.top()); stackForBFS.pop(); } reverseStack(stackForDFS); while(!stackForDFS.empty()){ result.push(stackForDFS.top()); stackForDFS.pop(); } cleanMap(bfsMap); cleanMap(dfsMap); cleanStateQueue(); }
void reverseStack(struct Stack *s) { if(isEmpty(s)) return; int data = pop(s); reverseStack(s); insertAtBottom(s, data); }
void ShowSentence( a_state *s, a_sym *sym, a_pro *pro, a_state *to_state ) /************************************************************************/ { traceback *list; traceback *parse_stack; traceback *token_stack; a_parent *parent; for( parent = s->parents; parent; parent = parent->next ) { if( to_state != NULL ) { /* S/R conflict */ printf( "Sample sentence(s) for shift to state %u:\n", to_state->sidx ); } else { /* R/R conflict */ printf( "Sample sentence(s) for reduce of rule %u:\n", pro->pidx ); } list = getStatePrefix( s, parent->state ); parse_stack = makeReversedCopy( list ); token_stack = NULL; if( to_state != NULL ) { runUntilShift( &parse_stack, sym, &token_stack ); } else { performReduce( &parse_stack, pro ); runUntilShift( &parse_stack, sym, &token_stack ); } fputs( " ", stdout ); printAndFreeStack( list ); fputs( ".", stdout ); if( parse_stack == NULL ) { printf( "%s\n Will never shift token '%s' in this context\n", sym->name, sym->name ); } else { putchar( '\n' ); runUntilShift( &parse_stack, eofsym, &token_stack ); token_stack = reverseStack( token_stack ); printAndFreeStack( token_stack ); putchar( '\n' ); } // dump all of the contexts if we have verbose state output if( ! showflag ) break; putchar( '\n' ); } }
void cff_parseOutline(uint8_t *data, uint32_t len, cff_Index gsubr, cff_Index lsubr, cff_Stack *stack, void *outline, cff_IOutlineBuilder methods, const otfcc_Options *options) { uint16_t gsubr_bias = compute_subr_bias(gsubr.count); uint16_t lsubr_bias = compute_subr_bias(lsubr.count); uint8_t *start = data; uint32_t advance, i, cnt_bezier; cff_Value val; void (*setWidth)(void *context, double width) = methods.setWidth; void (*newContour)(void *context) = methods.newContour; void (*lineTo)(void *context, double x1, double y1) = methods.lineTo; void (*curveTo)(void *context, double x1, double y1, double x2, double y2, double x3, double y3) = methods.curveTo; void (*setHint)(void *context, bool isVertical, double position, double width) = methods.setHint; void (*setMask)(void *context, bool isContourMask, bool *mask) = methods.setMask; double (*getrand)(void *context) = methods.getrand; if (!setWidth) setWidth = callback_nopSetWidth; if (!newContour) newContour = callback_nopNewContour; if (!lineTo) lineTo = callback_nopLineTo; if (!curveTo) curveTo = callback_nopCurveTo; if (!setHint) setHint = callback_nopsetHint; if (!setMask) setMask = callback_nopsetMask; if (!getrand) getrand = callback_nopgetrand; while (start < data + len) { advance = cff_decodeCS2Token(start, &val); switch (val.t) { case CS2_OPERATOR: switch (val.i) { case op_hstem: case op_vstem: case op_hstemhm: case op_vstemhm: if (stack->index % 2) setWidth(outline, stack->stack[0].d); stack->stem += stack->index >> 1; double hintBase = 0; for (uint16_t j = stack->index % 2; j < stack->index; j += 2) { double pos = stack->stack[j].d; double width = stack->stack[j + 1].d; setHint(outline, (val.i == op_vstem || val.i == op_vstemhm), pos + hintBase, width); hintBase += pos + width; } stack->index = 0; break; case op_hintmask: case op_cntrmask: { if (stack->index % 2) setWidth(outline, stack->stack[0].d); bool isVertical = stack->stem > 0; stack->stem += stack->index >> 1; double hintBase = 0; for (uint16_t j = stack->index % 2; j < stack->index; j += 2) { double pos = stack->stack[j].d; double width = stack->stack[j + 1].d; setHint(outline, isVertical, pos + hintBase, width); hintBase += pos + width; } uint32_t maskLength = (stack->stem + 7) >> 3; bool *mask; NEW(mask, stack->stem + 7); for (uint32_t byte = 0; byte < maskLength; byte++) { uint8_t maskByte = start[advance + byte]; mask[(byte << 3) + 0] = maskByte >> 7 & 1; mask[(byte << 3) + 1] = maskByte >> 6 & 1; mask[(byte << 3) + 2] = maskByte >> 5 & 1; mask[(byte << 3) + 3] = maskByte >> 4 & 1; mask[(byte << 3) + 4] = maskByte >> 3 & 1; mask[(byte << 3) + 5] = maskByte >> 2 & 1; mask[(byte << 3) + 6] = maskByte >> 1 & 1; mask[(byte << 3) + 7] = maskByte >> 0 & 1; } setMask(outline, (val.i == op_cntrmask), mask); advance += maskLength; stack->index = 0; break; } case op_vmoveto: { CHECK_STACK_TOP(op_vmoveto, 1); if (stack->index > 1) setWidth(outline, stack->stack[stack->index - 2].d); newContour(outline); lineTo(outline, 0.0, stack->stack[stack->index - 1].d); stack->index = 0; break; } case op_rmoveto: { CHECK_STACK_TOP(op_rmoveto, 2); if (stack->index > 2) setWidth(outline, stack->stack[stack->index - 3].d); newContour(outline); lineTo(outline, stack->stack[stack->index - 2].d, stack->stack[stack->index - 1].d); stack->index = 0; break; } case op_hmoveto: { CHECK_STACK_TOP(op_hmoveto, 1); if (stack->index > 1) setWidth(outline, stack->stack[stack->index - 2].d); newContour(outline); lineTo(outline, stack->stack[stack->index - 1].d, 0.0); stack->index = 0; break; } case op_endchar: { if (stack->index > 0) setWidth(outline, stack->stack[stack->index - 1].d); break; } case op_rlineto: { for (i = 0; i < stack->index; i += 2) lineTo(outline, stack->stack[i].d, stack->stack[i + 1].d); stack->index = 0; break; } case op_vlineto: { if (stack->index % 2 == 1) { lineTo(outline, 0.0, stack->stack[0].d); for (i = 1; i < stack->index; i += 2) { lineTo(outline, stack->stack[i].d, 0.0); lineTo(outline, 0.0, stack->stack[i + 1].d); } } else { for (i = 0; i < stack->index; i += 2) { lineTo(outline, 0.0, stack->stack[i].d); lineTo(outline, stack->stack[i + 1].d, 0.0); } } stack->index = 0; break; } case op_hlineto: { if (stack->index % 2 == 1) { lineTo(outline, stack->stack[0].d, 0.0); for (i = 1; i < stack->index; i += 2) { lineTo(outline, 0.0, stack->stack[i].d); lineTo(outline, stack->stack[i + 1].d, 0.0); } } else { for (i = 0; i < stack->index; i += 2) { lineTo(outline, stack->stack[i].d, 0.0); lineTo(outline, 0.0, stack->stack[i + 1].d); } } stack->index = 0; break; } case op_rrcurveto: { for (i = 0; i < stack->index; i += 6) curveTo(outline, stack->stack[i].d, stack->stack[i + 1].d, stack->stack[i + 2].d, stack->stack[i + 3].d, stack->stack[i + 4].d, stack->stack[i + 5].d); stack->index = 0; break; } case op_rcurveline: { for (i = 0; i < stack->index - 2; i += 6) curveTo(outline, stack->stack[i].d, stack->stack[i + 1].d, stack->stack[i + 2].d, stack->stack[i + 3].d, stack->stack[i + 4].d, stack->stack[i + 5].d); lineTo(outline, stack->stack[stack->index - 2].d, stack->stack[stack->index - 1].d); stack->index = 0; break; } case op_rlinecurve: { for (i = 0; i < stack->index - 6; i += 2) lineTo(outline, stack->stack[i].d, stack->stack[i + 1].d); curveTo(outline, stack->stack[stack->index - 6].d, stack->stack[stack->index - 5].d, stack->stack[stack->index - 4].d, stack->stack[stack->index - 3].d, stack->stack[stack->index - 2].d, stack->stack[stack->index - 1].d); stack->index = 0; break; } case op_vvcurveto: { if (stack->index % 4 == 1) { curveTo(outline, stack->stack[0].d, stack->stack[1].d, stack->stack[2].d, stack->stack[3].d, 0.0, stack->stack[4].d); for (i = 5; i < stack->index; i += 4) curveTo(outline, 0.0, stack->stack[i].d, stack->stack[i + 1].d, stack->stack[i + 2].d, 0.0, stack->stack[i + 3].d); } else { for (i = 0; i < stack->index; i += 4) curveTo(outline, 0.0, stack->stack[i].d, stack->stack[i + 1].d, stack->stack[i + 2].d, 0.0, stack->stack[i + 3].d); } stack->index = 0; break; } case op_hhcurveto: { if (stack->index % 4 == 1) { curveTo(outline, stack->stack[1].d, stack->stack[0].d, stack->stack[2].d, stack->stack[3].d, stack->stack[4].d, 0.0); for (i = 5; i < stack->index; i += 4) curveTo(outline, stack->stack[i].d, 0.0, stack->stack[i + 1].d, stack->stack[i + 2].d, stack->stack[i + 3].d, 0.0); } else { for (i = 0; i < stack->index; i += 4) curveTo(outline, stack->stack[i].d, 0.0, stack->stack[i + 1].d, stack->stack[i + 2].d, stack->stack[i + 3].d, 0.0); } stack->index = 0; break; } case op_vhcurveto: { if (stack->index % 4 == 1) cnt_bezier = (stack->index - 5) / 4; else cnt_bezier = (stack->index / 4); for (i = 0; i < 4 * cnt_bezier; i += 4) { if ((i / 4) % 2 == 0) curveTo(outline, 0.0, stack->stack[i].d, stack->stack[i + 1].d, stack->stack[i + 2].d, stack->stack[i + 3].d, 0.0); else curveTo(outline, stack->stack[i].d, 0.0, stack->stack[i + 1].d, stack->stack[i + 2].d, 0.0, stack->stack[i + 3].d); } if (stack->index % 8 == 5) { curveTo(outline, 0.0, stack->stack[stack->index - 5].d, stack->stack[stack->index - 4].d, stack->stack[stack->index - 3].d, stack->stack[stack->index - 2].d, stack->stack[stack->index - 1].d); } if (stack->index % 8 == 1) { curveTo(outline, stack->stack[stack->index - 5].d, 0.0, stack->stack[stack->index - 4].d, stack->stack[stack->index - 3].d, stack->stack[stack->index - 1].d, stack->stack[stack->index - 2].d); } stack->index = 0; break; } case op_hvcurveto: { if (stack->index % 4 == 1) cnt_bezier = (stack->index - 5) / 4; else cnt_bezier = (stack->index / 4); for (i = 0; i < 4 * cnt_bezier; i += 4) { if ((i / 4) % 2 == 0) curveTo(outline, stack->stack[i].d, 0.0, stack->stack[i + 1].d, stack->stack[i + 2].d, 0.0, stack->stack[i + 3].d); else curveTo(outline, 0.0, stack->stack[i].d, stack->stack[i + 1].d, stack->stack[i + 2].d, stack->stack[i + 3].d, 0.0); } if (stack->index % 8 == 5) { curveTo(outline, stack->stack[stack->index - 5].d, 0.0, stack->stack[stack->index - 4].d, stack->stack[stack->index - 3].d, stack->stack[stack->index - 1].d, stack->stack[stack->index - 2].d); } if (stack->index % 8 == 1) { curveTo(outline, 0.0, stack->stack[stack->index - 5].d, stack->stack[stack->index - 4].d, stack->stack[stack->index - 3].d, stack->stack[stack->index - 2].d, stack->stack[stack->index - 1].d); } stack->index = 0; break; } case op_hflex: { CHECK_STACK_TOP(op_hflex, 7); curveTo(outline, stack->stack[0].d, 0.0, stack->stack[1].d, stack->stack[2].d, stack->stack[3].d, 0.0); curveTo(outline, stack->stack[4].d, 0.0, stack->stack[5].d, -stack->stack[2].d, stack->stack[6].d, 0.0); stack->index = 0; break; } case op_flex: { CHECK_STACK_TOP(op_flex, 12); curveTo(outline, stack->stack[0].d, stack->stack[1].d, stack->stack[2].d, stack->stack[3].d, stack->stack[4].d, stack->stack[5].d); curveTo(outline, stack->stack[6].d, stack->stack[7].d, stack->stack[8].d, stack->stack[9].d, stack->stack[10].d, stack->stack[11].d); stack->index = 0; break; } case op_hflex1: { CHECK_STACK_TOP(op_hflex1, 9); curveTo(outline, stack->stack[0].d, stack->stack[1].d, stack->stack[2].d, stack->stack[3].d, stack->stack[4].d, 0.0); curveTo(outline, stack->stack[5].d, 0.0, stack->stack[6].d, stack->stack[7].d, stack->stack[8].d, -(stack->stack[1].d + stack->stack[3].d + stack->stack[7].d)); stack->index = 0; break; } case op_flex1: { CHECK_STACK_TOP(op_flex1, 11); double dx = stack->stack[0].d + stack->stack[2].d + stack->stack[4].d + stack->stack[6].d + stack->stack[8].d; double dy = stack->stack[1].d + stack->stack[3].d + stack->stack[5].d + stack->stack[7].d + stack->stack[9].d; if (fabs(dx) > fabs(dy)) { dx = stack->stack[10].d; dy = -dy; } else { dx = -dx; dy = stack->stack[10].d; } curveTo(outline, stack->stack[0].d, stack->stack[1].d, stack->stack[2].d, stack->stack[3].d, stack->stack[4].d, stack->stack[5].d); curveTo(outline, stack->stack[6].d, stack->stack[7].d, stack->stack[8].d, stack->stack[9].d, dx, dy); stack->index = 0; break; } case op_and: { CHECK_STACK_TOP(op_and, 2); double num1 = stack->stack[stack->index - 1].d; double num2 = stack->stack[stack->index - 2].d; stack->stack[stack->index - 2].d = (num1 && num2) ? 1.0 : 0.0; stack->index -= 1; break; } case op_or: { CHECK_STACK_TOP(op_or, 2); double num1 = stack->stack[stack->index - 1].d; double num2 = stack->stack[stack->index - 2].d; stack->stack[stack->index - 2].d = (num1 || num2) ? 1.0 : 0.0; stack->index -= 1; break; } case op_not: { CHECK_STACK_TOP(op_not, 1); double num = stack->stack[stack->index - 1].d; stack->stack[stack->index - 1].d = num ? 0.0 : 1.0; break; } case op_abs: { CHECK_STACK_TOP(op_abs, 1); double num = stack->stack[stack->index - 1].d; stack->stack[stack->index - 1].d = (num < 0.0) ? -num : num; break; } case op_add: { CHECK_STACK_TOP(op_add, 2); double num1 = stack->stack[stack->index - 1].d; double num2 = stack->stack[stack->index - 2].d; stack->stack[stack->index - 2].d = num1 + num2; stack->index -= 1; break; } case op_sub: { CHECK_STACK_TOP(op_sub, 2); double num1 = stack->stack[stack->index - 2].d; double num2 = stack->stack[stack->index - 1].d; stack->stack[stack->index - 2].d = num1 - num2; stack->index -= 1; break; } case op_div: { CHECK_STACK_TOP(op_div, 2); double num1 = stack->stack[stack->index - 2].d; double num2 = stack->stack[stack->index - 1].d; stack->stack[stack->index - 2].d = num1 / num2; stack->index -= 1; break; } case op_neg: { CHECK_STACK_TOP(op_neg, 1); double num = stack->stack[stack->index - 1].d; stack->stack[stack->index - 1].d = -num; break; } case op_eq: { CHECK_STACK_TOP(op_eq, 2); double num1 = stack->stack[stack->index - 1].d; double num2 = stack->stack[stack->index - 2].d; stack->stack[stack->index - 2].d = (num1 == num2) ? 1.0 : 0.0; stack->index -= 1; break; } case op_drop: { CHECK_STACK_TOP(op_drop, 1); stack->index -= 1; break; } case op_put: { CHECK_STACK_TOP(op_put, 2); double val = stack->stack[stack->index - 2].d; int32_t i = (int32_t)stack->stack[stack->index - 1].d; stack->transient[i % type2_transient_array].d = val; stack->index -= 2; break; } case op_get: { CHECK_STACK_TOP(op_get, 1); int32_t i = (int32_t)stack->stack[stack->index - 1].d; stack->stack[stack->index - 1].d = stack->transient[i % type2_transient_array].d; break; } case op_ifelse: { CHECK_STACK_TOP(op_ifelse, 4); double v2 = stack->stack[stack->index - 1].d; double v1 = stack->stack[stack->index - 2].d; double s2 = stack->stack[stack->index - 3].d; double s1 = stack->stack[stack->index - 4].d; stack->stack[stack->index - 4].d = (v1 <= v2) ? s1 : s2; stack->index -= 3; break; } case op_random: { // Chosen from a fair dice // TODO: use a real randomizer stack->stack[stack->index].t = cff_DOUBLE; stack->stack[stack->index].d = getrand(outline); stack->index += 1; break; } case op_mul: { CHECK_STACK_TOP(op_mul, 2); double num1 = stack->stack[stack->index - 1].d; double num2 = stack->stack[stack->index - 2].d; stack->stack[stack->index - 2].d = num1 * num2; stack->index -= 1; break; } case op_sqrt: { CHECK_STACK_TOP(op_sqrt, 1); double num = stack->stack[stack->index - 1].d; stack->stack[stack->index - 1].d = sqrt(num); break; } case op_dup: { CHECK_STACK_TOP(op_dup, 1); stack->stack[stack->index] = stack->stack[stack->index - 1]; stack->index += 1; break; } case op_exch: { CHECK_STACK_TOP(op_exch, 2); double num1 = stack->stack[stack->index - 1].d; double num2 = stack->stack[stack->index - 2].d; stack->stack[stack->index - 1].d = num2; stack->stack[stack->index - 2].d = num1; break; } case op_index: { CHECK_STACK_TOP(op_index, 2); uint8_t n = stack->index - 1; uint8_t j = n - 1 - (uint8_t)(stack->stack[n].d) % n; stack->stack[n] = stack->stack[j]; break; } case op_roll: { CHECK_STACK_TOP(op_roll, 2); int32_t j = stack->stack[stack->index - 1].d; uint32_t n = stack->stack[stack->index - 2].d; CHECK_STACK_TOP(op_roll, 2 + n); j = -j % n; if (j < 0) j += n; if (!j) break; uint8_t last = stack->index - 3; uint8_t first = stack->index - 2 - n; reverseStack(stack, first, last); reverseStack(stack, last - j + 1, last); reverseStack(stack, first, last - j); stack->index -= 2; break; } case op_return: return; case op_callsubr: { CHECK_STACK_TOP(op_callsubr, 1); uint32_t subr = (uint32_t)stack->stack[--(stack->index)].d; cff_parseOutline(lsubr.data + lsubr.offset[lsubr_bias + subr] - 1, lsubr.offset[lsubr_bias + subr + 1] - lsubr.offset[lsubr_bias + subr], gsubr, lsubr, stack, outline, methods, options); break; } case op_callgsubr: { CHECK_STACK_TOP(op_callgsubr, 1); uint32_t subr = (uint32_t)stack->stack[--(stack->index)].d; cff_parseOutline(gsubr.data + gsubr.offset[gsubr_bias + subr] - 1, gsubr.offset[gsubr_bias + subr + 1] - gsubr.offset[gsubr_bias + subr], gsubr, lsubr, stack, outline, methods, options); break; } } break; case CS2_OPERAND: case CS2_FRACTION: stack->stack[(stack->index)++] = val; break; } start += advance; } }