--- parser3/src/include/pa_array.h 2003/02/26 10:17:30 1.57.2.26 +++ parser3/src/include/pa_array.h 2003/04/02 08:35:43 1.57.2.27.2.13 @@ -8,13 +8,15 @@ #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/04/02 08:35:43 $"; // includes #include "pa_memory.h" #include "pa_exception.h" +// externals + // forwards template class Array_iterator; @@ -26,50 +28,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 +63,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 } @@ -108,30 +91,24 @@ public: } /// get index-element - T& get(int index) const { - if(!(index>=0 && index=0 && index=0 && index=0 && index=0 && index void for_each(void (*callback)(T, I), I info) const { @@ -155,28 +132,72 @@ protected: bool is_full() { return fused == fallocated; } - void expand(int delta) { - felements = (T *)realloc(felements, (fallocated+delta)*sizeof(T)); + 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+=delta; + fallocated=new_allocated; } private: //disabled + Array(const Array&) {} Array& operator = (const Array&) { return *this; } }; -/// handy array iterator +/** Array iterator, usage: + @code + // Array a; + for(Array_iterator i(a); 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; + 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_iterator(Array& aarray): farray(aarray) { + Array_modifing_iterator(Array& aarray): farray(aarray) { fcurrent=farray.felements; flast=farray.felements+farray.count(); } @@ -192,5 +213,5 @@ public: } }; - +*/ #endif