Annotation of parser3/src/include/pa_stack.h, revision 1.35
1.7 paf 1: /** @file
1.8 paf 2: Parser: stack class decl.
3:
1.35 ! moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.32 moko 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.35 ! moko 11: #define IDENT_PA_STACK_H "$Id: pa_stack.h,v 1.34 2024/11/04 03:53:25 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
1.33 moko 24: this->felements[this->fsize++]=item;
1.1 paf 25: }
1.18 paf 26:
1.26 misha 27: inline T pop() {
1.33 moko 28: return this->felements[--this->fsize];
1.1 paf 29: }
30:
1.33 moko 31: inline bool is_empty() { return this->fsize==0; }
32: inline size_t top_index() { return this->fsize; }
33: inline void set_top_index(size_t atop) { this->fsize=atop; }
1.26 misha 34: inline T top_value() {
1.18 paf 35: assert(!is_empty());
1.33 moko 36: return this->felements[this->fsize-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.33 moko 41: if(size_t above_top_size=this->fallocated-this->fsize)
42: memset((void *)&this->felements[this->fsize], 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: