--- parser3/src/include/pa_array.h 2003/07/24 11:31:21 1.59 +++ parser3/src/include/pa_array.h 2017/02/07 22:00:31 1.85 @@ -1,14 +1,14 @@ /** @file Parser: Array & Array_iterator classes decls. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_ARRAY_H #define PA_ARRAY_H -static const char* IDENT_ARRAY_Y="$Date: 2003/07/24 11:31:21 $"; +#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.85 2017/02/07 22:00:31 moko Exp $" // includes @@ -56,12 +56,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 +76,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 +105,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; from=0 && index=0 && index=0 && index0? 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 { @@ -164,6 +180,21 @@ public: callback(*current, info); } + /// iterate over all elements + template void for_each(bool (*callback)(T, I), I info) const { + T *last=felements+fused; + for(T *current=felements; current void for_each_ref(void (*callback)(T&, I), I info) { + T *last=felements+fused; + for(T *current=felements; current T first_that(bool (*callback)(T, I), I info) const { T *last=felements+fused; @@ -174,16 +205,24 @@ public: return T(0); } + inline T* ptr(size_t index){ + return felements + index; + } + 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 @@ -221,44 +260,9 @@ public: } /// quickly extracts next Array element - const T next() { - return *(fcurrent++); - } - -}; -/* -/** Nonconst array iterator, usage: - @code - // Array a; - for(Array_iterator i(a); i.has_next(); ) { - T& element=i.next(); - ... - } - @endcode -* / -template class Array_modifing_iterator { - - Array& farray; - T *fcurrent; - T *flast; - -public: - - Array_modifing_iterator(Array& aarray): farray(aarray) { - fcurrent=farray.felements; - flast=farray.felements+farray.count(); - } - - /// there are still elements - bool has_next() { - return fcurrent