--- parser3/src/include/pa_array.h 2024/09/21 23:51:04 1.95 +++ parser3/src/include/pa_array.h 2026/01/06 13:07:58 1.106 @@ -1,24 +1,26 @@ /** @file Parser: Array & Array_iterator classes decls. - Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) Authors: Konstantin Morshnev , Alexandr Petrosian */ #ifndef PA_ARRAY_H #define PA_ARRAY_H -#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.95 2024/09/21 23:51:04 moko Exp $" +#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.106 2026/01/06 13:07:58 moko Exp $" // includes #include "pa_memory.h" #include "pa_types.h" +#include "pa_int.h" #include "pa_exception.h" // forwards template class Array_iterator; +template class Array_robust_iterator; template class Array_reverse_iterator; // defines @@ -29,6 +31,7 @@ template class Array_reverse template class Array: public PA_Object { friend class Array_iterator; + friend class Array_robust_iterator; friend class Array_reverse_iterator; protected: @@ -44,6 +47,7 @@ protected: public: typedef Array_iterator Iterator; + typedef Array_robust_iterator RobustIterator; typedef Array_reverse_iterator ReverseIterator; struct Action_options { @@ -108,14 +112,11 @@ public: } /// append other Array portion to this one. starting from offset - Array& append(const Array& src, - size_t offset=0, - size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< zero limit means 'nothing' - + void append(const Array& src, size_t offset=0, size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< zero limit means 'nothing' size_t src_count=src.count(); // skip tivials if(!src_count || !limit || offset>=src_count) - return *this; + return; // max(limit) size_t m=src_count-offset; // fix limit @@ -123,13 +124,8 @@ public: limit=m; fit(fsize-1+limit); - - T* from=&src.felements[offset]; - T* to=&felements[fsize]; - for(T* from_end=from+limit; from0 ? fallocated+fallocated/4+2 : 3); // 3 is PAF default, confirmed by tests + resize(fallocated>0 ? fallocated+fallocated/2+2 : 3); // 3 is PAF default, confirmed by tests } inline void fit(size_t index){ if(index >= fallocated) - resize(max(this->fallocated + this->fallocated/4, index+1)); + resize(max(fallocated+fallocated/4, index+1)); } void resize(size_t asize) { if(fallocated){ felements=(T *)pa_realloc(felements, asize*sizeof(T)); +#ifdef PA_DEBUG_DISABLE_GC + // non-gc realloc doesn't zero; manually zero expanded region + if(asize > fallocated) + memset((void *)(felements+fallocated), 0, (asize-fallocated) * sizeof(T)); +#endif fallocated=asize; } else { fallocated=asize; @@ -246,43 +247,6 @@ private: //disabled }; -/// Commonly used, templated to work with any integer type - -template char* pa_itoa(T n, T base=10){ - char buf[MAX_NUMBER + 1]; - char* pos=buf + MAX_NUMBER; - *pos='\0'; - - bool negative=n < 0; - if (n < 0){ - n=-n; - } - - do { - *(--pos)=(n % base) + '0'; - n/=base; - } while (n > 0); - - if (negative) { - *(--pos) = '-'; - } - return pa_strdup(pos, buf + MAX_NUMBER - pos); -} - -template char* pa_uitoa(T n, T base=10){ - char buf[MAX_NUMBER + 1]; - char* pos=buf + MAX_NUMBER; - *pos='\0'; - - do { - *(--pos)=(n % base) + '0'; - n/=base; - } while (n > 0); - - return pa_strdup(pos, buf + MAX_NUMBER - pos); -} - - /** Array iterator, usage: @code // Array a; @@ -299,7 +263,6 @@ template class Array_iterato T *flast; public: - Array_iterator(const Array& aarray): farray(aarray) { fcurrent=farray.felements; flast=farray.felements + farray.fsize; @@ -324,42 +287,53 @@ public: inline size_t index() { return fcurrent - farray.felements; } +}; + +// Slower array iterator for arrays that can be modified during iteration +template class Array_robust_iterator { + + const Array& farray; + size_t findex; - inline char *key(){ - return pa_uitoa(index()); +public: + Array_robust_iterator(const Array& aarray) : farray(aarray), findex(0) {} + + inline operator bool() { + return findex < farray.fsize; + } + + inline void next() { + findex++; + } + + inline T value() { + return farray.felements[findex]; } + inline size_t index() { + return findex; + } }; +// Robust as used for arrays that can be modified during iteration template class Array_reverse_iterator { const Array& farray; - T *fcurrent; + size_t findex; public: + Array_reverse_iterator(const Array& aarray): farray(aarray), findex(aarray.fsize) {} - Array_reverse_iterator(const Array& aarray): farray(aarray) { - fcurrent=farray.felements + farray.fsize; - } - - /// there are still elements inline operator bool () { - return fcurrent > farray.felements; + return (findex > 0) && (findex <= farray.fsize); } - /// returns the current element and advances the iterator inline T prev() { - return *(--fcurrent); + return farray.felements[--findex]; } - // returns the current index of the iterator inline size_t index() { - return fcurrent - farray.felements; - } - - inline char *key(){ - return pa_uitoa(index()); + return findex; } - }; #endif