--- parser3/src/include/pa_array.h 2003/03/19 13:16:56 1.57.2.27.2.2 +++ parser3/src/include/pa_array.h 2003/04/02 11:35:20 1.57.2.27.2.14 @@ -8,13 +8,15 @@ #ifndef PA_ARRAY_H #define PA_ARRAY_H -static const char* IDENT_ARRAY_H="$Date: 2003/03/19 13:16:56 $"; +static const char* IDENT_ARRAY_H="$Date: 2003/04/02 11:35:20 $"; // includes #include "pa_memory.h" #include "pa_exception.h" +// externals + // forwards template class Array_iterator; @@ -26,17 +28,14 @@ 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; @@ -47,21 +46,13 @@ public: { 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 - - free(felements); - } /// 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; @@ -72,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 } @@ -85,7 +76,7 @@ public: limit=m; } - int delta=limit-(fallocated-fused); + ssize_t delta=limit-(fallocated-fused); if(delta>0) expand(delta); @@ -100,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 { @@ -147,14 +132,16 @@ 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; } }; @@ -162,8 +149,7 @@ private: //disabled /** Array iterator, usage: @code // Array a; - Array_iterator i(a); - while(i.has_next()) { + for(Array_iterator i(a); i.has_next(); ) { T& element=i.next(); ... } @@ -188,22 +174,21 @@ public: } /// quickly extracts next Array element - const T& next() { + const T next() { return *(fcurrent++); } }; - +/* /** Nonconst array iterator, usage: @code // Array a; - Array_iterator i(a); - while(i.has_next()) { + for(Array_iterator i(a); i.has_next(); ) { T& element=i.next(); ... } @endcode -*/ +* / template class Array_modifing_iterator { Array& farray; @@ -228,5 +213,5 @@ public: } }; - +*/ #endif