/************************************************************************** v m C r e a t e ** Creates a virtual machine either from scratch (if pVM is NULL on entry) ** or by resizing and reinitializing an existing VM to the specified stack ** sizes. **************************************************************************/ FICL_VM *vmCreate(FICL_VM *pVM, unsigned nPStack, unsigned nRStack) { if (pVM == NULL) { pVM = (FICL_VM *)ficlMalloc(sizeof (FICL_VM)); assert (pVM); memset(pVM, 0, sizeof (FICL_VM)); } if (pVM->pStack) stackDelete(pVM->pStack); pVM->pStack = stackCreate(nPStack); if (pVM->rStack) stackDelete(pVM->rStack); pVM->rStack = stackCreate(nRStack); #if FICL_WANT_FLOAT if (pVM->fStack) stackDelete(pVM->fStack); pVM->fStack = stackCreate(nPStack); #endif pVM->textOut = ficlTextOut; vmReset(pVM); return pVM; }
// Starting with a valid triangulation, uses the Edge Flip algorithm to // refine the triangulation into a Constrained Delaunay Triangulation. void tessMeshRefineDelaunay( TESSmesh *mesh, TESSalloc *alloc ) { // At this point, we have a valid, but not optimal, triangulation. // We refine the triangulation using the Edge Flip algorithm // // 1) Find all internal edges // 2) Mark all dual edges // 3) insert all dual edges into a queue TESSface *f; EdgeStack stack; TESShalfEdge *e; int maxFaces = 0, maxIter = 0, iter = 0; stackInit(&stack, alloc); for( f = mesh->fHead.next; f != &mesh->fHead; f = f->next ) { if ( f->inside) { e = f->anEdge; do { e->mark = EdgeIsInternal(e); // Mark internal edges if (e->mark && !e->Sym->mark) stackPush(&stack, e); // Insert into queue e = e->Lnext; } while (e != f->anEdge); maxFaces++; } } // The algorithm should converge on O(n^2), since the predicate is not robust, // we'll save guard against infinite loop. maxIter = maxFaces * maxFaces; // Pop stack until we find a reversed edge // Flip the reversed edge, and insert any of the four opposite edges // which are internal and not already in the stack (!marked) while (!stackEmpty(&stack) && iter < maxIter) { e = stackPop(&stack); e->mark = e->Sym->mark = 0; if (!tesedgeIsLocallyDelaunay(e)) { TESShalfEdge *edges[4]; int i; tessMeshFlipEdge(mesh, e); // for each opposite edge edges[0] = e->Lnext; edges[1] = e->Lprev; edges[2] = e->Sym->Lnext; edges[3] = e->Sym->Lprev; for (i = 0; i < 4; i++) { if (!edges[i]->mark && EdgeIsInternal(edges[i])) { edges[i]->mark = edges[i]->Sym->mark = 1; stackPush(&stack, edges[i]); } } } iter++; } stackDelete(&stack); }
/* Starting with a valid triangulation, uses the Edge Flip algorithm to refine the triangulation into a Constrained Delaunay Triangulation. */ int tessMeshRefineDelaunay( TESSmesh *mesh, TESSalloc *alloc ) { /* At this point, we have a valid, but not optimal, triangulation. We refine the triangulation using the Edge Flip algorithm */ /* 1) Find all internal edges 2) Mark all dual edges 3) insert all dual edges into a queue */ TESSface *f; EdgeStack stack; TESShalfEdge *e; TESShalfEdge *edges[4]; stackInit(&stack, alloc); for( f = mesh->fHead.next; f != &mesh->fHead; f = f->next ) { if ( f->inside) { e = f->anEdge; do { e->mark = EdgeIsInternal(e); /* Mark internal edges */ if (e->mark && !e->Sym->mark) stackPush(&stack, e); /* Insert into queue */ e = e->Lnext; } while (e != f->anEdge); } } // Pop stack until we find a reversed edge // Flip the reversed edge, and insert any of the four opposite edges // which are internal and not already in the stack (!marked) while (!stackEmpty(&stack)) { e = stackPop(&stack); e->mark = e->Sym->mark = 0; if (!tesedgeIsLocallyDelaunay(e)) { int i; tessMeshFlipEdge(mesh, e); // for each opposite edge edges[0] = e->Lnext; edges[1] = e->Lprev; edges[2] = e->Sym->Lnext; edges[3] = e->Sym->Lprev; for (i=0;i<3;i++) { if (!edges[i]->mark && EdgeIsInternal(edges[i])) { edges[i]->mark = edges[i]->Sym->mark = 1; stackPush(&stack, edges[i]); } } } } stackDelete(&stack); return 1; }
/** * Hlavní program. */ int main(void) { tStack s; stackInit(&s); tiskniZasobnik(&s); prazdnyZasobnik(&s); stackPush(&s, 1); tiskniZasobnik(&s); prazdnyZasobnik(&s); stackPush(&s, 2); stackPush(&s, 3); stackPush(&s, 4); tiskniZasobnik(&s); prazdnyZasobnik(&s); int pom = 0; stackTop(&s, &pom); stackPop(&s); printf("Vyjmuli jsme: %d\n",pom); stackTop(&s, &pom); stackPop(&s); printf("Vyjmuli jsme: %d\n",pom); tiskniZasobnik(&s); prazdnyZasobnik(&s); stackDelete(&s); tiskniZasobnik(&s); prazdnyZasobnik(&s); return EXIT_SUCCESS; }
int main() { TTable table; tableInit(&table); printf("\nTabulka by mela byt prazdna: \n"); tablePrintOrder(table); printf("\n----------------------------\n"); tableInsertFunction(&table, strCreateString("func1")); functionInsertVar(table.lastAddedFunc, strCreateString("var1func1")); functionInsertVar(table.lastAddedFunc, strCreateString("var2func1")); functionInsertVar(table.lastAddedFunc, strCreateString("var3func1")); printf("\nJedna funkce: \n"); tablePrintOrder(table); printf("\n----------------------------\n"); tableInsertFunction(&table, strCreateString("func2")); functionInsertVar(table.lastAddedFunc, strCreateString("var1func2")); functionInsertVar(table.lastAddedFunc, strCreateString("var2func2")); functionInsertVar(table.lastAddedFunc, strCreateString("var3func2")); printf("\nDve funkce: \n"); tablePrintOrder(table); printf("\n----------------------------\n"); tableInsertFunction(&table, strCreateString("func3")); functionInsertVar(table.lastAddedFunc, strCreateString("var1func3")); functionInsertVar(table.lastAddedFunc, strCreateString("var2func3")); functionInsertVar(table.lastAddedFunc, strCreateString("var3func3")); printf("\nVsechny: \n"); tablePrintOrder(table); printf("\n----------------------------\n"); // test heldani { TFunction *fceSearch; printf("\nObsahuje tabulka funkci %s? \t", "func1"); fceSearch = tableSearchFunction(&table, strCreateString("func1")); if(fceSearch != NULL) { printf("ANO\n"); printf(" Obsahuje funkce promenou %s?\t", "var1func1"); if(functionSearchVar(fceSearch, strCreateString("var1func1")) != NULL) printf("ANO"); else printf("NE"); } else printf("NE\n"); printf("\nObsahuje tabulka funkci %s? \t", "funcX"); fceSearch = tableSearchFunction(&table, strCreateString("funcX")); if(fceSearch != NULL) { printf("ANO\n"); printf(" Obsahuje funkce promenou %s?\t", "var1func1"); if(functionSearchVar(fceSearch, strCreateString("var1func1")) != NULL) printf("ANO"); else printf("NE"); } else printf("NE\n"); printf("\n----------------------------\n"); } // test zásobníku: printf("TEST ZASOBNIKU:\n"); printf("----------------------------\n"); TStack s; stackInit(&s); prazdnyStack(s); TFunction *fce = tableSearchFunction(&table, strCreateString("func2")); TVar *id = functionSearchVar(fce, strCreateString("var1func2")); stackPush(&s, (void*)id); prazdnyStack(s); tiskniStack(&s); id = functionSearchVar(fce, strCreateString("var2func2")); stackPush(&s, (void*)id); tiskniStack(&s); id = functionSearchVar(fce, strCreateString("var3func2")); stackPush(&s, (void*)id); tiskniStack(&s); id = functionSearchVar(fce, strCreateString("var1func2")); stackPush(&s, (void*)id); tiskniStack(&s); TVar *data = (TVar*)stackTopPop(&s); if (data != NULL) printf("Vybráno ze zásobníku: %s\n",data->name); else printf("Ukazatel je nulový! \n"); prazdnyStack(s); data = (TVar*)stackTopPop(&s); if (data != NULL) printf("Vybráno ze zásobníku: %s\n",data->name); else printf("Ukazatel je nulový! \n"); prazdnyStack(s); data = (TVar*)stackTopPop(&s); if (data != NULL) printf("Vybráno ze zásobníku: %s\n",data->name); else printf("Ukazatel je nulový! \n"); prazdnyStack(s); tiskniStack(&s); stackDelete (&s); prazdnyStack(s); tiskniStack(&s); data = (TVar*)stackTopPop(&s); if (data != NULL) printf("Vybráno ze zásobníku: %s\n",data->name); else printf("Ukazatel je nulový! \n"); printf("----------------------------\n"); // test seznamu: printf("TEST SEZNAMU:\n"); printf("----------------------------\n"); TList L; listInit (&L); id = functionSearchVar(fce, strCreateString("var1func2")); TLItem *uk2 = listGetActive (&L); listSetActive(&L, uk2); aktivniList(L); listInsertLast(&L, id); tiskniList(&L); void *uk4 = listCopyLast(&L); printf("Zkopírováno: %s\n",((TVar*)uk4)->name); aktivniList(L); listFirst(&L); tiskniList(&L); listLast(&L); tiskniList(&L); id = functionSearchVar(fce, strCreateString("var2func2")); listPostInsert(&L, id); tiskniList(&L); id = functionSearchVar(fce, strCreateString("var3func2")); listInsertLast(&L, id); tiskniList(&L); id = functionSearchVar(fce, strCreateString("var1func2")); listInsertLast(&L, id); tiskniList(&L); listFirst(&L); listSucc(&L); TLItem *uk = listGetActive (&L); tiskniList(&L); listLast(&L); tiskniList(&L); listSetActive(&L, uk); tiskniList(&L); aktivniList(L); listDeleteFirst(&L); tiskniList(&L); void *uk3 = listCopyLast(&L); printf("Zkopírováno: %s\n",((TVar*)uk3)->name); listSucc(&L); tiskniList(&L); listActualize(&L, uk3); tiskniList(&L); listDispose (&L); printf("----------------------------\n"); // konec testu seznamu printf("\nSmazu: \n"); tableClear(&table); tablePrintOrder(table); printf("\n----------------------------\n"); }
void deleteHashTable(HashTable &myTable) { for (int i = 0; i < myTable.size; ++i) stackDelete(myTable.all[i]); delete[] myTable.all; }
bool decode(FILE* infile, FILE* outfile) { // get the header info unsigned int maxBits = getBits(NBITS_MAXBITS, infile); unsigned int window = getBits(NBITS_WINDOW, infile); char eFlag = getBits(NBITS_EFLAG, infile); stringTable* table = (eFlag == 1) ? stringTableNew(maxBits, true) : stringTableNew(maxBits, false); pruneInfo* pi = pruneInfoNew(maxBits); unsigned int oldCode = EMPTY_PREFIX; // the previous code read from infile unsigned int newCode; // the code just read from infile unsigned int code; // initially equal to newCode, but then set to its // prefixes to obtain the entire string of newCode unsigned char finalK = 0, k; // characters in newCode stack* kStack = stackNew(); // holds characters from newCode in order to // put them in the correct order by reversal unsigned char nbits = (eFlag) ? 2 : 9; // number of bits per code while((newCode = code = getBits(nbits, infile)) != STOP_CODE) { switch(code) { case EOF: { // getting EOF before the STOP_CODE is an error stringTableDelete(table); stackDelete(kStack); pruneInfoDelete(pi); return false; break; } case GROW_NBITS_CODE: { nbits++; if(nbits > maxBits) { stringTableDelete(table); stackDelete(kStack); pruneInfoDelete(pi); return false; } break; } case PRUNE_CODE: { if(window == 0) { stringTableDelete(table); stackDelete(kStack); pruneInfoDelete(pi); return false; } else { table = stringTablePrune(table, pi, window, &oldCode); oldCode = EMPTY_PREFIX; // update nbits for(nbits = 2; (1 << nbits) -1 < table->highestCode; nbits++); } break; } case ESCAPE_CODE: { if(eFlag == 0) { stringTableDelete(table); stackDelete(kStack); pruneInfoDelete(pi); return false; } unsigned char escapedChar = getBits(8, infile); fputc(escapedChar, outfile); if(oldCode != EMPTY_PREFIX) { stringTableAdd(table, oldCode, escapedChar, NULL); } unsigned int tempCode; stringTableAdd(table, EMPTY_PREFIX, escapedChar, &tempCode); pruneInfoSawCode(pi, tempCode); oldCode = EMPTY_PREFIX; // reset prefix to EMPTY break; } default: { pruneInfoSawCode(pi, newCode); if(!stringTableCodeSearch(table, code)) { stackPush(kStack, finalK); code = oldCode; } // push string for code onto kStack until we get to the code // with an empty prefix, which goes into finalK tableElt* elt = stringTableCodeSearch(table, code); while(elt && elt->prefix != EMPTY_PREFIX) { stackPush(kStack, elt->k); code = elt->prefix; elt = stringTableCodeSearch(table, code); } finalK = elt->k; // print the characters in correct order now that they've been // reversed by pushing them onto kStack fputc(finalK, outfile); while(stackPop(kStack, &k)) { fputc(k, outfile); } // add oldCode to the table, then update it to the current code if(oldCode != EMPTY_PREFIX) { stringTableAdd(table, oldCode, finalK, NULL); } oldCode = newCode; break; } } } stringTableDelete(table); stackDelete(kStack); pruneInfoDelete(pi); return true; }
int main() { Stack *tst; int opt, misc; char dummy[BUFSIZE]; struct etype *el, *rel; if (!(tst = stackCreate())) exit(-1); while(1) { printf("\n\n\nStack (<ctrl-C> to quit):\n"); printf("1 - stackClear()\n"); printf("2 - stackPush()\n"); printf("3 - stackPop()\n"); printf("4 - stackIsEmpty()\n"); printf("5 - stackLength()\n"); printf("\n"); printf("CHOICE: "); gets(dummy); sscanf(dummy,"%d",&opt); switch(opt) { case 1: if ((misc = stackClear(tst)) < 0) { fprintf(stderr,"stackClear error\n"); exit(-1); } else printf("\nStack of size %d cleared\n",misc); break; case 2: el = malloc(sizeof(struct etype)); printf("\nKEY(int) : "); gets(dummy); sscanf(dummy,"%d", &el->key); printf("STRING(char *) : "); gets(el->str); stackPush(tst,el); break; case 3: if (!(rel = (struct etype *)stackPop(tst))) fprintf(stderr,"\n>>> Stack is empty\n"); else printf("\nELEMENT : Key: %d\n Str: %s\n",rel->key, rel->str); break; case 4: printf("\nStack is %s\n",(listIsEmpty(tst) ? "empty" : "non-empty")); break; case 5: printf("\nSize of stack: %d\n",listLength(tst)); break; default: printf("\n>>> ILLEGAL SELECTION\n"); break; } printf("\nDone.\nHit <enter>...\n"); gets(dummy); } stackDelete(tst); }