--- parser3/src/include/pa_array.h 2009/04/17 23:21:12 1.73 +++ parser3/src/include/pa_array.h 2023/09/26 20:49:06 1.87 @@ -1,14 +1,14 @@ /** @file Parser: Array & Array_iterator classes decls. - Copyright (c) 2001-2009 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: 2009/04/17 23:21:12 $"; +#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.87 2023/09/26 20:49:06 moko Exp $" // includes @@ -80,19 +80,22 @@ public: fallocated(initial), fused(0) { - felements=fallocated?static_cast(malloc(fallocated*sizeof(T))):0; + felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; } +#ifdef USE_DESTRUCTORS inline ~Array(){ - free(felements); + if(felements) + pa_free(felements); } +#endif /// how many items are in Array inline size_t count() const { return fused; } /// append to array inline Array& operator+=(T src) { if(is_full()) - expand(fallocated>0?2:3); // 3 is PAF default, confirmed by tests + expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests felements[fused++]=src; @@ -102,40 +105,26 @@ 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 { T *last=felements+fused; @@ -192,6 +205,10 @@ public: return T(0); } + inline T* ptr(size_t index){ + return felements + index; + } + protected: bool is_full() { @@ -200,12 +217,11 @@ protected: void expand(size_t delta) { if(fallocated){ size_t new_allocated=fallocated+delta; - felements = (T *)realloc(felements, new_allocated*sizeof(T)); - memset(&felements[fallocated], 0, delta*sizeof(T)); + felements=(T *)pa_realloc(felements, new_allocated*sizeof(T)); fallocated=new_allocated; } else { fallocated=delta; - felements=static_cast(malloc(fallocated*sizeof(T))); + felements=(T *)pa_malloc(fallocated*sizeof(T)); } }