--- parser3/src/include/pa_array.h 2003/02/21 10:55:43 1.57.2.25 +++ parser3/src/include/pa_array.h 2003/03/19 13:16:56 1.57.2.27.2.2 @@ -8,7 +8,7 @@ #ifndef PA_ARRAY_H #define PA_ARRAY_H -static const char* IDENT_ARRAY_H="$Date: 2003/02/21 10:55:43 $"; +static const char* IDENT_ARRAY_H="$Date: 2003/03/19 13:16:56 $"; // includes @@ -41,19 +41,14 @@ protected: 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); - - felements=static_cast(calloc(fallocated*sizeof(T))); + felements=static_cast(malloc(fallocated*sizeof(T))); } override ~Array() { + // see comment in ctor T *last=felements+fused; for(T *current=felements; current~T(); // manually invoking destructors @@ -164,16 +159,60 @@ private: //disabled }; -/// handy array iterator +/** Array iterator, usage: + @code + // Array 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(); }