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

1.7       paf         1: /** @file
1.8       paf         2:        Parser: stack class decl.
                      3: 
1.32    ! moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
        !             5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_STACK_H
                      9: #define PA_STACK_H
1.15      paf        10: 
1.32    ! moko       11: #define IDENT_PA_STACK_H "$Id: pa_stack.h,v 1.31 2020/12/15 17:10:32 moko Exp $"
1.1       paf        12: 
                     13: #include "pa_array.h"
                     14: 
1.7       paf        15: /// simple stack based on Array
1.18      paf        16: template<typename T> class Stack: public Array<T> {
1.1       paf        17: public:
                     18: 
1.26      misha      19:        Stack(size_t initial=4) : Array<T>(initial){}
1.1       paf        20: 
1.26      misha      21:        inline void push(T item) {
                     22:                if(this->is_full())
                     23:                        expand(this->fallocated); // free is not called, so expanding a lot to decrease memory waste
                     24:                this->felements[this->fused++]=item;
1.1       paf        25:        }
1.18      paf        26: 
1.26      misha      27:        inline T pop() {
                     28:                return this->felements[--this->fused];
1.1       paf        29:        }
                     30: 
1.26      misha      31:        inline bool is_empty() { return this->fused==0; }
                     32:        inline size_t top_index() { return this->fused; }
                     33:        inline void set_top_index(size_t atop) { this->fused=atop; }
                     34:        inline T top_value() {
1.18      paf        35:                assert(!is_empty());
1.26      misha      36:                return this->felements[this->fused-1];
1.18      paf        37:        }
1.2       paf        38: 
1.18      paf        39:        /// call this prior to collecting garbage [in unused part of stack there may be pointers(unused)]
                     40:        void wipe_unused() {
1.26      misha      41:                if(size_t above_top_size=this->fallocated-this->fused)
1.30      moko       42:                        memset((void *)&this->felements[this->fused], 0, above_top_size*sizeof(T));
1.18      paf        43:        }
1.1       paf        44: 
1.18      paf        45: protected:
1.1       paf        46: 
1.26      misha      47:        void expand(size_t delta) {
                     48:                size_t new_allocated=this->fallocated+delta;
                     49:                // we can't use realloc as MethodParams references allocated stack
                     50:                T* new_elements = (T *)pa_malloc(new_allocated*sizeof(T));
                     51:                memcpy(new_elements, this->felements, this->fallocated*sizeof(T));
                     52:                this->felements=new_elements;
                     53:                this->fallocated=new_allocated;
                     54:        }
1.1       paf        55: };
                     56: 
                     57: #endif

E-mail: