--- parser3/src/include/pa_array.h 2003/02/26 10:17:30 1.57.2.26 +++ parser3/src/include/pa_array.h 2003/03/27 10:04:06 1.57.2.27.2.8 @@ -8,7 +8,7 @@ #ifndef PA_ARRAY_H #define PA_ARRAY_H -static const char* IDENT_ARRAY_H="$Date: 2003/02/26 10:17:30 $"; +static const char* IDENT_ARRAY_H="$Date: 2003/03/27 10:04:06 $"; // includes @@ -26,50 +26,31 @@ template class Array: public protected: - // default expand delta size - int fdelta; - /// elements[growing size] here T *felements; // allocated size - int fallocated; + size_t fallocated; // array size - int fused; + size_t fused; public: typedef T element_type; - Array(int initial=3, int delta=1): - fallocated(initial?initial:3), - fdelta(delta), + Array(int initial=3): + fallocated(initial>3?initial:3), fused(0) { - if(fallocated<=0 || fdelta<1) - throw Exception(0, - Exception::undefined_source, - "Array::Array(%d, %d) too small", initial, delta); - - // we can't use new(0) here[like we did in Hash] because we need to use realloc - // on MSVC new returns NOT pointer learned from pa_malloc, so we can't do new+realloc - felements=static_cast(calloc(fallocated*sizeof(T))); - } - override ~Array() { - // see comment in ctor - T *last=felements+fused; - for(T *current=felements; current~T(); // manually invoking destructors - - free(felements); + felements=static_cast(malloc(fallocated*sizeof(T))); } /// how many items are in Array - int count() const { return fused; } + size_t count() const { return fused; } /// append to array Array& operator += (T src) { if(is_full()) - expand(fdelta); + expand(2); felements[fused++]=src; @@ -80,7 +61,7 @@ public: Array& append(const Array& src, int offset=0, int limit=0) { if(offset<0) { throw Exception(0, - Exception::undefined_source, + 0, "Array::append(offset=%d) out <0", offset); //return Nothing; // never } @@ -107,29 +88,37 @@ public: return *this; } - /// get index-element - T& get(int index) const { - if(!(index>=0 && index=0 && index=0 && index a; + Array_iterator i(a); + while(i.has_next()) { + T& element=i.next(); + ... + } + @endcode +*/ template class Array_iterator { + const Array& farray; + T *fcurrent; + T *flast; + +public: + + Array_iterator(const Array& aarray): farray(aarray) { + fcurrent=farray.felements; + flast=farray.felements+farray.count(); + } + + /// there are still elements + bool has_next() { + return fcurrent a; + Array_iterator i(a); + while(i.has_next()) { + T& element=i.next(); + ... + } + @endcode +*/ +template class Array_modifing_iterator { + Array& farray; T *fcurrent; T *flast; public: - Array_iterator(Array& aarray): farray(aarray) { + Array_modifing_iterator(Array& aarray): farray(aarray) { fcurrent=farray.felements; flast=farray.felements+farray.count(); } @@ -187,7 +222,7 @@ public: } /// quickly extracts next Array element - T& next() { + T next() { return *(fcurrent++); }