--- parser3/src/include/pa_stack.h 2002/03/18 15:29:46 1.14 +++ parser3/src/include/pa_stack.h 2005/03/16 14:37:51 1.23 @@ -1,45 +1,54 @@ /** @file Parser: stack class decl. - Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - - $Id: pa_stack.h,v 1.14 2002/03/18 15:29:46 paf Exp $ */ #ifndef PA_STACK_H #define PA_STACK_H +static const char * const IDENT_STACK_H="$Date: 2005/03/16 14:37:51 $"; + #include "pa_config_includes.h" #include "pa_array.h" /// simple stack based on Array -class Stack : public Array { +template class Stack: public Array { public: - Stack(Pool& apool) : Array(apool), ftop(0) { - } + Stack(): ftop(0) {} - void push(Item *item) { - if(ftopcount()) // cell is already allocated? put(ftop, item); // use it else *this+=item; // append it ftop++; } - Item *pop() { - return get(--ftop); + + T pop() { + return this->get(--ftop); } - int top_index() { return ftop-1; } - void top_index(int top_index) { ftop=top_index+1; } - Item *top_value() { return get(top_index()); } + bool is_empty() { return ftop==0; } + size_t top_index() { return ftop; } + void set_top_index(size_t atop) { ftop=atop; } + T top_value() { + assert(!is_empty()); + return this->get(ftop-1); + } -private: + /// call this prior to collecting garbage [in unused part of stack there may be pointers(unused)] + void wipe_unused() { + if(size_t above_top_size=this->fused-ftop) + memset(&this->felements[ftop], 0, above_top_size*sizeof(T)); + } - // deepest used index - int ftop; +protected: + // deepest used index+1 + size_t ftop; }; #endif