--- parser3/src/include/pa_array.h 2006/04/09 13:38:47 1.68 +++ parser3/src/include/pa_array.h 2024/09/07 16:30:26 1.89 @@ -1,14 +1,14 @@ /** @file Parser: Array & Array_iterator classes decls. - Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #ifndef PA_ARRAY_H #define PA_ARRAY_H -static const char * const IDENT_ARRAY_Y="$Date: 2006/04/09 13:38:47 $"; +#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.89 2024/09/07 16:30:26 moko Exp $" // includes @@ -40,6 +40,8 @@ protected: size_t fused; public: + typedef Array_iterator Iterator; + struct Action_options { size_t offset; size_t limit; //< ARRAY_OPTION_LIMIT_ALL means 'all'. zero limit means 'nothing' @@ -56,12 +58,11 @@ public: bool adjust(size_t count) { if(!count || !limit) return false; - size_t row=offset; - if(row>=count) + if(offset>=count) return false; // max(limit) size_t m=reverse? - offset + offset+1 :count-offset; if(!m) return false; @@ -77,19 +78,26 @@ public: typedef T element_type; - Array(size_t initial=3): - fallocated(initial>3?initial:3), + inline Array(size_t initial=0): + fallocated(initial), fused(0) { - felements=static_cast(malloc(fallocated*sizeof(T))); + felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; + } + +#ifdef USE_DESTRUCTORS + inline ~Array(){ + if(felements) + pa_free(felements); } +#endif /// how many items are in Array - size_t count() const { return fused; } + inline size_t count() const { return fused; } /// append to array - Array& operator += (T src) { + inline Array& operator+=(T src) { if(is_full()) - expand(+2); + expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests felements[fused++]=src; @@ -99,63 +107,73 @@ 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, //< negative limit means 'all'. zero limit means 'nothing' - bool reverse=false) { + size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< negative limit means 'all'. zero limit means 'nothing' size_t src_count=src.count(); // skip tivials if(!src_count || !limit || offset>=src_count) return *this; // max(limit) - size_t m=reverse? - 1+offset - :src_count-offset; - if(!m) - return *this; + size_t m=src_count-offset; // fix limit if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) limit=m; - ssize_t delta=reverse? - (ssize_t)limit - :limit-(fallocated-fused); + ssize_t delta=limit-(fallocated-fused); if(delta>0) expand(delta); T* from=&src.felements[offset]; T* to=&felements[fused]; - if(reverse) { // reverse - for(T* from_end=from-limit; from>from_end; --from) - *to++=*from; - - } else { // forward - for(T* from_end=from+limit; from0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests + + memmove(felements+index+1, felements+index, (fused-index) * sizeof(T)); + + felements[index]=element; + fused++; + } + + /// remove index-element + inline void remove(size_t index) { + assert(index void for_each(void (*callback)(T, I), I info) const { @@ -189,16 +207,39 @@ public: return T(0); } + inline T* ptr(size_t index){ + return felements + index; + } + + void fit(size_t index, T element){ + if(index >= fallocated){ + size_t new_allocated=fallocated>0 ? fallocated : 3; + while(new_allocated <= index){ + new_allocated+=2 + new_allocated/32; + } + expand(new_allocated - fallocated); + } + felements[index]=element; + if(index >= fused){ + fused=index+1; + } + } + protected: bool is_full() { return fused == fallocated; } + void expand(size_t delta) { - size_t new_allocated=fallocated+delta; - felements = (T *)realloc(felements, new_allocated*sizeof(T)); - memset(&felements[fallocated], 0, delta*sizeof(T)); - fallocated=new_allocated; + if(fallocated){ + size_t new_allocated=fallocated+delta; + felements=(T *)pa_realloc(felements, new_allocated*sizeof(T)); + fallocated=new_allocated; + } else { + fallocated=delta; + felements=(T *)pa_malloc(fallocated*sizeof(T)); + } } private: //disabled @@ -211,7 +252,7 @@ private: //disabled /** Array iterator, usage: @code // Array a; - for(Array_iterator i(a); i.has_next(); ) { + for(Array_iterator i(a); i; ) { T& element=i.next(); ... } @@ -231,14 +272,24 @@ public: } /// there are still elements - bool has_next() { + operator bool () { return fcurrent