--- parser3/src/include/pa_array.h 2003/01/31 12:34:29 1.57.2.16 +++ parser3/src/include/pa_array.h 2003/03/20 13:09:53 1.57.2.27.2.4 @@ -8,26 +8,24 @@ #ifndef PA_ARRAY_H #define PA_ARRAY_H -static const char* IDENT_ARRAY_Y="$Date: 2003/01/31 12:34:29 $"; +static const char* IDENT_ARRAY_H="$Date: 2003/03/20 13:09:53 $"; -#include "pa_pool.h" +// includes + +#include "pa_memory.h" #include "pa_exception.h" -template class Array_iterator; +// forwards -/** - Simple Array. +template class Array_iterator; -*/ +/// Simple Array template class Array: public PA_Object { friend class Array_iterator; protected: - // default expand delta size - int fdelta; - /// elements[growing size] here T *felements; @@ -40,24 +38,11 @@ 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=new T[fallocated]; - } - override ~Array() { - T *last=felements+fused; - for(T *current=felements; current(malloc(fallocated*sizeof(T))); } /// how many items are in Array @@ -65,7 +50,7 @@ public: /// append to array Array& operator += (T src) { if(is_full()) - expand(fdelta); + expand(2); felements[fused++]=src; @@ -74,11 +59,11 @@ public: /// append other Array portion to this one. starting from offset Array& append(const Array& src, int offset=0, int limit=0) { - if(!(offset>=0 && offset0) - expand(needed); - - memcpy(&felements[fused+=limit], &src.felements[offset], limit*sizeof(T)); + int delta=limit-(fallocated-fused); + if(delta>0) + expand(delta); + + T* from=&src.felements[offset]; + T* to=&felements[fused]; + T* from_end=from+limit; + while(from=0 && index=0 && index CharPtr; -class Pool: public Array { +/** 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: - char *malloc(size_t size) { - CharPtr result=CharPtr((char *)Array::malloc(size)); - *this += result; - return result.get(); + + Array_iterator(const Array& aarray): farray(aarray) { + fcurrent=farray.felements; + flast=farray.felements+farray.count(); + } + + /// there are still elements + bool has_next() { + return fcurrent class Array_iterator { +/** Nonconst array iterator, usage: + @code + // Array a; + Array_iterator i(a); + while(i.has_next()) { + T& element=i.next(); + ... + } + @endcode +*/ +template class Array_modifing_iterator { Array& farray; T *fcurrent; @@ -188,7 +201,7 @@ template class Array_iterato public: - Array_iterator(Array& aarray): farray(aarray) { + Array_modifing_iterator(Array& aarray): farray(aarray) { fcurrent=farray.felements; flast=farray.felements+farray.count(); } @@ -199,7 +212,7 @@ public: } /// quickly extracts next Array element - T next() { + T& next() { return *(fcurrent++); }