--- parser3/src/include/pa_array.h 2003/11/25 12:58:53 1.63 +++ parser3/src/include/pa_array.h 2009/04/29 03:26:38 1.76 @@ -1,14 +1,14 @@ /** @file Parser: Array & Array_iterator classes decls. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_ARRAY_H #define PA_ARRAY_H -static const char * const IDENT_ARRAY_Y="$Date: 2003/11/25 12:58:53 $"; +static const char * const IDENT_ARRAY_Y="$Date: 2009/04/29 03:26:38 $"; // 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?static_cast(malloc(fallocated*sizeof(T))):0; } +#ifdef USE_DESTRUCTORS + inline ~Array(){ + if(felements) + 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:3); // 3 is PAF default, confirmed by tests felements[fused++]=src; @@ -138,24 +144,24 @@ public: } /// get index-element - T get(size_t index) const { + inline T get(size_t index) const { assert(index void for_each(void (*callback)(T, I), I info) const { @@ -165,6 +171,14 @@ public: } /// 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(malloc(fallocated*sizeof(T))); + } } private: //disabled @@ -228,7 +251,7 @@ public: } /// quickly extracts next Array element - const T next() { + T next() { return *(fcurrent++); }