// set up FM for iteration void initIterFM ( WordFM* fm ) { assert(fm); stackClear(fm); if (fm->root) stackPush(fm, fm->root, 1); }
// set up FM for iteration void VG_(initIterFM) ( WordFM* fm ) { tl_assert(fm); stackClear(fm); if (fm->root) stackPush(fm, fm->root, 1); }
// Destructor, frees up all memory held by remaining nodes. void VG_(OSet_Destroy)(AvlTree* t, OSetNodeDestroy_t destroyNode) { AvlNode* n; Int i, sz = 0; vg_assert(t); stackClear(t); if (t->root) stackPush(t, t->root, 1); // Free all the AvlNodes. This is a post-order traversal, because we // must free all children of a node before the node itself. while (stackPop(t, &n, &i)) { switch (i) { case 1: stackPush(t, n, 2); if (n->left) stackPush(t, n->left, 1); break; case 2: stackPush(t, n, 3); if (n->right) stackPush(t, n->right, 1); break; case 3: if (destroyNode) destroyNode(n); t->free(n); sz++; break; } } vg_assert(sz == t->nElems); // Free the AvlTree itself. t->free(t); }
void VG_(OSet_ResetIter)(AvlTree* t) { vg_assert(t); stackClear(t); if (t->root) stackPush(t, t->root, 1); }
// Destructor, frees up all memory held by remaining nodes. void VG_(OSetGen_Destroy)(AvlTree* t) { AvlNode* n = NULL; Int i = 0; Word sz = 0; vg_assert(t); stackClear(t); if (t->root) stackPush(t, t->root, 1); /* Free all the AvlNodes. This is a post-order traversal, because we */ /* must free all children of a node before the node itself. */ while (stackPop(t, &n, &i)) { switch (i) { case 1: stackPush(t, n, 2); if (n->left) stackPush(t, n->left, 1); break; case 2: stackPush(t, n, 3); if (n->right) stackPush(t, n->right, 1); break; case 3: t->free(n); sz++; break; } } vg_assert(sz == t->nElems); /* Free the AvlTree itself. */ t->free(t); }
// The underscores avoid GCC complaints about overshadowing global names. AvlTree* VG_(OSetGen_Create)(OffT _keyOff, OSetCmp_t _cmp, OSetAlloc_t _alloc, HChar* _cc, OSetFree_t _free) { AvlTree* t; // Check the padding is right and the AvlNode is the expected size. vg_assert(sizeof(AvlNode) == 3*sizeof(void*)); // Sanity check args vg_assert(_alloc); vg_assert(_free); if (!_cmp) vg_assert(0 == _keyOff); // If no cmp, offset must be zero t = _alloc(_cc, sizeof(AvlTree)); t->keyOff = _keyOff; t->cmp = _cmp; t->alloc = _alloc; t->cc = _cc; t->free = _free; t->nElems = 0; t->root = NULL; stackClear(t); return t; }
// set up 'oset' for iteration so that the first key subsequently // produced VG_(OSetGen_Next) is the smallest key in the map // >= start_at. Naturally ">=" is defined by the comparison // function supplied to VG_(OSetGen_Create). void VG_(OSetGen_ResetIterAt)(AvlTree* oset, void* k) { Int i; AvlNode *n, *t; Word cmpresS; /* signed */ UWord cmpresU; /* unsigned */ vg_assert(oset); stackClear(oset); if (!oset->root) return; n = NULL; // We need to do regular search and fill in the stack. t = oset->root; while (True) { if (t == NULL) return; if (oset->cmp) { cmpresS = (Word)slow_cmp(oset, k, t); } else { /* this is believed to be correct, but really needs testing before the assertion is removed. */ vg_assert(0); cmpresS = fast_cmp(k, t); } /* Switch the sense of the comparison, since the comparison order of args (k vs t) above is opposite to that of the corresponding code in hg_wordfm.c. */ if (cmpresS < 0) { cmpresS = 1; } else if (cmpresS > 0) { cmpresS = -1; } if (cmpresS == 0) { // We found the exact key -- we are done. // The iteration should start with this node. stackPush(oset, t, 2); // The stack now looks like {2, 2, ... ,2, 2} return; } cmpresU = (UWord)cmpresS; cmpresU >>=/*unsigned*/ (8 * sizeof(cmpresU) - 1); vg_assert(cmpresU == 0 || cmpresU == 1); if (!cmpresU) { // Push this node only if we go to the left child. stackPush(oset, t, 2); } t = cmpresU==0 ? t->left : t->right; } if (stackPop(oset, &n, &i)) { // If we've pushed something to stack and did not find the exact key, // we must fix the top element of stack. vg_assert(i == 2); stackPush(oset, n, 3); // the stack looks like {2, 2, ..., 2, 3} } }
// set up FM for iteration so that the first key subsequently produced // by VG_(nextIterFM) is the smallest key in the map >= start_at. // Naturally ">=" is defined by the comparison function supplied to // VG_(newFM), as documented above. void VG_(initIterAtFM) ( WordFM* fm, UWord start_at ) { Int i; AvlNode *n, *t; Word cmpresS; /* signed */ UWord cmpresU; /* unsigned */ tl_assert(fm); stackClear(fm); if (!fm->root) return; n = NULL; // We need to do regular search and fill in the stack. t = fm->root; while (True) { if (t == NULL) return; cmpresS = fm->kCmp ? /*boxed*/ fm->kCmp( t->key, start_at ) : /*unboxed*/ cmp_unsigned_Words( t->key, start_at ); if (cmpresS == 0) { // We found the exact key -- we are done. // The iteration should start with this node. stackPush(fm, t, 2); // The stack now looks like {2, 2, ... ,2, 2} return; } cmpresU = (UWord)cmpresS; cmpresU >>=/*unsigned*/ (8 * sizeof(cmpresU) - 1); if (!cmpresU) { // Push this node only if we go to the left child. stackPush(fm, t, 2); } t = t->child[cmpresU]; } if (stackPop(fm, &n, &i)) { // If we've pushed something to stack and did not find the exact key, // we must fix the top element of stack. tl_assert(i == 2); stackPush(fm, n, 3); // the stack looks like {2, 2, ..., 2, 3} } }
void destroyStack(Stack *S){ stackClear(S); free(S); }
void FI_StackClearAll(void) { if(!finaleStackInited) Con_Error("FI_StackClearAll: Not initialized yet!"); stackClear(false); }
void FI_StackClear(void) { if(!finaleStackInited) Con_Error("FI_StackClear: Not initialized yet!"); stackClear(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); }