Annotation of parser3/src/include/pa_stack.h, revision 1.24.12.1

1.7       paf         1: /** @file
1.8       paf         2:        Parser: stack class decl.
                      3: 
1.24      paf         4:        Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.13      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_STACK_H
                      9: #define PA_STACK_H
1.15      paf        10: 
1.24.12.1! paf        11: static const char * const IDENT_STACK_H="$Date: 2005/08/09 08:14:50 $";
1.1       paf        12: 
1.9       parser     13: #include "pa_config_includes.h"
1.1       paf        14: #include "pa_array.h"
                     15: 
1.7       paf        16: /// simple stack based on Array
1.18      paf        17: template<typename T> class Stack: public Array<T> {
1.1       paf        18: public:
                     19: 
1.18      paf        20:        Stack(): ftop(0) {}
1.1       paf        21: 
1.18      paf        22:        void push(T item) {
1.23      paf        23:                if(ftop<this->count()) // cell is already allocated?
1.24.12.1! paf        24:                        this->put(ftop, item); // use it
1.1       paf        25:                else
                     26:                        *this+=item; // append it
1.2       paf        27:                ftop++;
1.1       paf        28:        }
1.18      paf        29: 
                     30:        T pop() {
1.23      paf        31:                return this->get(--ftop);
1.1       paf        32:        }
                     33: 
1.18      paf        34:        bool is_empty() { return ftop==0; }
1.20      paf        35:        size_t top_index() { return ftop; }
                     36:        void set_top_index(size_t atop) { ftop=atop; }
                     37:        T top_value() { 
1.18      paf        38:                assert(!is_empty());
1.23      paf        39:                return this->get(ftop-1); 
1.18      paf        40:        }
1.2       paf        41: 
1.18      paf        42:        /// call this prior to collecting garbage [in unused part of stack there may be pointers(unused)]
                     43:        void wipe_unused() {
1.23      paf        44:                if(size_t above_top_size=this->fused-ftop)
                     45:                        memset(&this->felements[ftop], 0, above_top_size*sizeof(T));
1.18      paf        46:        }
1.1       paf        47: 
1.18      paf        48: protected:
1.1       paf        49: 
1.18      paf        50:        // deepest used index+1
                     51:        size_t ftop;
1.1       paf        52: };
                     53: 
                     54: #endif

E-mail: