--- parser3/src/include/pa_array.h 2024/11/04 03:53:25 1.100 +++ parser3/src/include/pa_array.h 2024/12/23 16:59:17 1.102 @@ -8,7 +8,7 @@ #ifndef PA_ARRAY_H #define PA_ARRAY_H -#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.100 2024/11/04 03:53:25 moko Exp $" +#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.102 2024/12/23 16:59:17 moko Exp $" // includes @@ -19,6 +19,7 @@ // forwards template class Array_iterator; +template class Array_robust_iterator; template class Array_reverse_iterator; // defines @@ -29,6 +30,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 +46,7 @@ protected: public: typedef Array_iterator Iterator; + typedef Array_robust_iterator RobustIterator; typedef Array_reverse_iterator ReverseIterator; struct Action_options { @@ -166,7 +169,7 @@ public: inline void clear() { if(fsize) - memset(felements, 0, fsize * sizeof(T)); + memset((void *)felements, 0, fsize * sizeof(T)); fsize=0; } @@ -291,7 +294,6 @@ template class Array_iterato T *flast; public: - Array_iterator(const Array& aarray): farray(aarray) { fcurrent=farray.felements; flast=farray.felements + farray.fsize; @@ -317,41 +319,65 @@ public: return fcurrent - farray.felements; } - inline char *key(){ + // returns the current index string value of the iterator + inline char *key() { return pa_uitoa(index()); } - }; -template class Array_reverse_iterator { +// Slower array iterator for arrays that can be modified during iteration +template class Array_robust_iterator { const Array& farray; - T *fcurrent; + size_t findex; public: + Array_robust_iterator(const Array& aarray) : farray(aarray), findex(0) {} - Array_reverse_iterator(const Array& aarray): farray(aarray) { - fcurrent=farray.felements + farray.fsize; + inline operator bool() { + return findex < farray.fsize; } - /// there are still elements + inline void next() { + findex++; + } + + inline T value() { + return farray.felements[findex]; + } + + inline size_t index() { + return findex; + } + + inline char* key() { + return pa_uitoa(findex); + } +}; + +// Robust as used for arrays that can be modified during iteration +template class Array_reverse_iterator { + + const Array& farray; + size_t findex; + +public: + Array_reverse_iterator(const Array& aarray): farray(aarray), findex(aarray.fsize) {} + 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; + return findex; } - inline char *key(){ + inline char *key() { return pa_uitoa(index()); } - }; #endif