/* $Id: stack.c,v 1.1 1996/10/10 10:01:26 roitzsch Exp $ (C)opyright 1996 by Konrad-Zuse-Center, Berlin All rights reserved. Part of the Kaskade distribution */ #include "stack.h" template Stack:: Stack(int l1, int top1, int h1) : Vector(l1,top1) { top=top1; this->h=h1; } template Stack:: Stack(int l1, int top1) : Vector(l1,top1) { top=top1; this->h=l1-1; } template Stack:: Stack(int top1) : Vector(1,top1) { top=top1; this->h=0;} template Stack:: Stack() : Vector(DefaultSize) { top=DefaultSize; this->h=0;} template void Stack:: push(const T a) { if (this->h==top) extend(); this->v[++this->h] = a; } template T Stack:: pop() { if (this->h < this->l) { cout << "\n*** pop tried from empty stack \n"; cout.flush(); abort(); return this->v[this->l]; // dummy to avoid compiler warnings } else return this->v[this->h--]; } template T Stack:: Top() const { if (this->h < this->l) { cout << "\n*** Class Stack (Top): empty stack \n"; cout.flush(); abort(); return this->v[this->l]; // dummy to avoid compiler warnings } else return this->v[this->h]; } template T Stack:: Prev() const { if (this->h <= this->l) { cout << "\n*** Class Stack (Prev): only one element on stack \n"; cout.flush(); abort(); return this->v[this->l]; // dummy to avoid compiler warnings } else return this->v[this->h-1]; } template void Stack:: extend() { int stackSize = top - this->l + 1; stackSize = int(1.5*stackSize); if (stackSize <= 2) stackSize = 4; top = this->l+stackSize-1; T* vnew = new T[stackSize]; if (!vnew) { cout << "\n*** stack extension failure: l=" <l<< " top=" <l; for (int i=this->l; i<=this->h; ++i) vnew[i] = this->v[i]; this->v = this->v + this->l; delete [] this->v; this->v = vnew; if (AnnounceExtensionFlag) cout << "\n* Stack extended to " << top << "\n"; } //------------------------------------------------------------------------- template void Stack:: resize(int newl, int newTop) { this->v += this->l; delete[] this->v; this->allocate (newl, newTop); top = newTop; this->h = newl-1; } template void Stack:: resize(int newTop) { resize (1,newTop); } template void Stack:: resize(int newl, int newTop, int newh) { resize(newl, newTop); this->h = newh; } //------------------------------------------------------------------------- template Stack:: Stack(Stack& /*vec*/) { this->notImplemented("Stack:: copy constructor! Check function arguments"); }